top of page

Final Code: Dialogue File

  • Yavneeka Patel
  • Apr 29, 2015
  • 4 min read

/*

* Copyright 2013 Carnegie Mellon University.

* Portions Copyright 2004 Sun Microsystems, Inc.

* Portions Copyright 2004 Mitsubishi Electric Research Laboratories.

* All Rights Reserved. Use is subject to license terms.

*

* See the file "license.terms" for information on usage and

* redistribution of this file, and for a DISCLAIMER OF ALL

* WARRANTIES.

*

* Audio files come from http://www2.research.att.com/~ttsweb/tts/demo.php

*

* Message sending to arduino adapted from Yu Hin Hau's code & Audrey's code

* https://billwaa.wordpress.com/2012/05/18/engineering-arduino-to-java-communication-2/

*

* Simple example of Java and Arduino Communication

* Sends messages using the serial bus.

* If sent message is: 0 -> LED off, 1 -> LED on, 2 -> LED blink once.

*/

package edu.mhc.sphinx;

import java.util.Scanner;

import jssc.SerialPort;

import jssc.SerialPortException;

import java.io.File;

import java.io.IOException;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

import edu.cmu.sphinx.api.Configuration;

import edu.cmu.sphinx.api.LiveSpeechRecognizer;

public class DialogDemo {

/**** SETTINGS -- change to match your setup! *******/

// change the following to match the serial port you use for the Arduino

final static String SERIAL_PORT_ADDRESS = "/dev/tty.usbmodem1411";

// change to match the baud rate you're using from Arduino

final static int BAUD_RATE = 19200;

/**** END SETTINGS *********************************/

//Declare Special Symbol Used in Serial Data Stream from Arduino

final static String start_char = "@";

final static String end_char = "#";

final static String sep_char = ":";

private static SerialPort serialPort;

//---------------------------BEGIN CLASS VARIABLES---------------------------------//

//create an audio input stream instance

private static AudioInputStream audio;

//---------------------------------------------------------------------------------//

private static final String ACOUSTIC_MODEL =

"resource:/edu/cmu/sphinx/models/en-us/en-us";

private static final String DICTIONARY_PATH =

"resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict";

private static final String GRAMMAR_PATH =

"resource:/resource/edu/mhc/sphinx/";

//---------------------------END CLASS VARIABLES---------------------------------//

//---------------------------------MAIN FUNCTION---------------------------------//

public static void main(String[] args) throws Exception {

System.out.println ("STARTING PROGRAM");

//------------------BEGIN CONFIGURATION--------------//

//create a new configuration

Configuration configuration = new Configuration();

//set the acoustic model path

configuration.setAcousticModelPath(ACOUSTIC_MODEL);

//set the dictionary path

configuration.setDictionaryPath(DICTIONARY_PATH);

//set the grammar path

configuration.setGrammarPath(GRAMMAR_PATH);

//configure the jsfg to use grammar

configuration.setUseGrammar(true);

//set the grammar being used to be the dialog file

configuration.setGrammarName("dialog");

//------------------END CONFIGURATION--------------//

//add the configuration to the jsgf recognizer

LiveSpeechRecognizer jsgfRecognizer = new LiveSpeechRecognizer(configuration);

//start the speech recognition

jsgfRecognizer.startRecognition(true);

//Initialize Port

serialPort = new SerialPort( SERIAL_PORT_ADDRESS );

//Open Serial Port and set Parameters

serialPort.openPort();

serialPort.setParams( BAUD_RATE, 8, 1, 0);

try {

//wait 2 seconds to allow the port to open

Thread.sleep(2000);

//while the speech recognizer is listening

while (true) {

//print out say something

//this program takes in a predefined set of words and does something if that word is said

System.out.println("Say a command or exit the program");

//figure out what was said and store it in the utterance

String utterance = jsgfRecognizer.getResult().getHypothesis();

//print out what the jsgfRecognizer thought was said

System.out.println( "utterance: " + utterance );

//if the utterance was bye, quit the program

if (utterance.equals("bye")) {

break;

}

//otherwise

else {

//send the utterance to another function to deal with the speech

recognizeSpeech (utterance);

//wait 2 seconds before reading things again (this is to prevent the program from continually spitting out responses when confused)

Thread.sleep(2000);

}

}

}

catch ( Exception e )

{

e.printStackTrace();

}

//stop speech recognition

jsgfRecognizer.stopRecognition();

}

/**

* Take in user input as speech and provide the correct response

* @param utterance

* @throws IOException

* @throws LineUnavailableException

* @throws SerialPortException

*/

private static void recognizeSpeech (String utterance) throws IOException, LineUnavailableException, SerialPortException {

//if the person says hello

if (utterance.equals("hello")) {

//try to do the audio

try {

//get the hello.wav file

audio = AudioSystem.getAudioInputStream(new File("hello.wav"));

//get the clip from the wav file

javax.sound.sampled.Clip clip = AudioSystem.getClip();

//open the audio file

clip.open(audio);

//play the audio file

clip.start();

} catch (UnsupportedAudioFileException e) {

//print the error

e.printStackTrace();

}

}

//if the person says my name is

if (utterance.equals("my name is")) {

try {

//get the greet.wav file

audio = AudioSystem.getAudioInputStream(new File("greet.wav"));

//get the clip from the wav file

javax.sound.sampled.Clip clip = AudioSystem.getClip();

//open the audio file

clip.open(audio);

//play the audio file

clip.start();

} catch (UnsupportedAudioFileException e) {

//print the error

e.printStackTrace();

}

}

//if the person asks how you are doing

if (utterance.equals("how are")) {

try{

//get the feeling.wav file

audio = AudioSystem.getAudioInputStream(new File("feeling.wav"));

//get the clip from the wav file

javax.sound.sampled.Clip clip = AudioSystem.getClip();

//open the audio file

clip.open(audio);

//play the audio file

clip.start();

} catch (UnsupportedAudioFileException e) {

//print the error

e.printStackTrace();

}

}

//if the person asks where you are from

if (utterance.contains("where are")) {

try{

//get the location.wav

audio = AudioSystem.getAudioInputStream(new File("location.wav"));

//get the clip from the wav file

javax.sound.sampled.Clip clip = AudioSystem.getClip();

//open the audio file

clip.open(audio);

//play the audio file

clip.start();

} catch (UnsupportedAudioFileException e) {

//print the error

e.printStackTrace();

}

}

//if the person asks what your age is

if (utterance.contains("old")) {

try{

//get the age.wav file

audio = AudioSystem.getAudioInputStream(new File("age.wav"));

//get the clip from the wav file

javax.sound.sampled.Clip clip = AudioSystem.getClip();

//open the audio file

clip.open(audio);

//play the audio file

clip.start();

} catch (UnsupportedAudioFileException e) {

//print the error

e.printStackTrace();

}

}

//if the person says on

if (utterance.equals("on")) {

// convert to byte by casting

// and send the data across the port

serialPort.writeByte((byte)('1'));

System.out.println((byte)('1') + " <= sent!");

}

//if the person says off

if (utterance.equals("off")) {

// convert to byte by casting

// and send the data across the port

serialPort.writeByte((byte)'0');

System.out.println((byte)('0') + " <= sent!");

}

//if the person says blink

if (utterance.equals("blink")) {

// convert to byte by casting

// and send the data across the port

serialPort.writeByte((byte)'2');

System.out.println((byte)('2') + " <= sent!");

}

}

}


 
 
 

Comments


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
  • Facebook Classic
  • Twitter Classic
  • Google Classic
  • RSS Classic

© 2023 by TOKYO DESIGN. Proudly created with Wix.com

bottom of page