Skip to content

Commit

Permalink
Speech controller
Browse files Browse the repository at this point in the history
  • Loading branch information
GGancarczyk committed Dec 22, 2015
0 parents commit 08f883d
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="/Users/Grzegorz/Desktop/sphinx4/sphinx4-core-5prealpha-20151205.160447-3-javadoc.jar"/>
<classpathentry kind="lib" path="/Users/Grzegorz/Desktop/sphinx4/sphinx4-core-5prealpha-20151205.160447-3-sources.jar"/>
<classpathentry kind="lib" path="/Users/Grzegorz/Desktop/sphinx4/sphinx4-core-5prealpha-20151205.160447-3.jar"/>
<classpathentry kind="lib" path="/Users/Grzegorz/Desktop/sphinx4/sphinx4-data-5prealpha-20151205.160456-3-javadoc.jar"/>
<classpathentry kind="lib" path="/Users/Grzegorz/Desktop/sphinx4/sphinx4-data-5prealpha-20151205.160456-3-sources.jar"/>
<classpathentry kind="lib" path="/Users/Grzegorz/Desktop/sphinx4/sphinx4-data-5prealpha-20151205.160456-3.jar"/>
<classpathentry kind="lib" path="/Users/Grzegorz/Desktop/java-json-2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SpeechController</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
10 changes: 10 additions & 0 deletions grammar/grammar.gram
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#JSGF V1.0;

grammar grammar;

public <forward> = [move] forward | up [please];
public <backward> = [move] backward | back | down [please];
public <right> = [move] right [please];
public <left> = [move] left [please];
public <wait> = [please] wait;

82 changes: 82 additions & 0 deletions src/speechcontroller/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package speechcontroller;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Client implements Runnable {

JSONObject packet;
public static final String SERVER_IP = "192.168.0.2";
public static final int SERVER_PORT = 5678;

private String mServerMessage;

private boolean mRun = false;
private PrintWriter mBufferOut;
private BufferedReader mBufferIn;
Thread backgroundThread;

public void sendMessage(String message) {
if (mBufferOut != null && !mBufferOut.checkError()) {
mBufferOut.println(message);
mBufferOut.flush();
}
}

public void stopClient() {
mRun = false;

if (mBufferOut != null) {
mBufferOut.flush();
mBufferOut.close();
}

mBufferIn = null;
mBufferOut = null;
mServerMessage = null;
}

public Client(JSONObject packet) {
this.packet = packet;
}

@Override
public void run() {

mRun = true;

try {

InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

Socket socket = new Socket(serverAddr, SERVER_PORT);
try {

mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
sendMessage("!" + packet.toString().length() + "!" + packet.toString());

Thread.sleep(500);

} catch (Exception e) {

e.printStackTrace();

} finally {

socket.close();
}

} catch (Exception e) {

e.printStackTrace();

}
}
}

127 changes: 127 additions & 0 deletions src/speechcontroller/MainSpeechRecognition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package speechcontroller;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;

public class MainSpeechRecognition {

static int left_motor;
static int right_motor;
static JSONObject packet = new JSONObject();

//Path to acoustic mode
private static final String ACOUSTIC_MODEL =
"resource:/edu/cmu/sphinx/models/en-us/en-us";
//Path to dictionary
private static final String DICTIONARY_PATH =
"resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict";
//Language model
private static String LANGUAGE_MODEL =
"resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin";
private static String GRAMMAR_PATH =
"grammar/";

//To check if utterance matches orders
private static boolean utteranceRegex(String direction, String utterance) {

Pattern pattern = Pattern.compile("(move\\s|turn\\s)*+"+direction+"(\\splease)*");

Matcher matcher = pattern.matcher((CharSequence) utterance);

return matcher.matches() ? true : false;
}

private static void connection(int left_motor, int right_motor, JSONObject packet) {
try {
packet.put("left_motor", left_motor);
packet.put("right_motor", right_motor);
Client client = new Client(packet);
client.run();
new Thread(client).start();
} catch (JSONException e) {
e.printStackTrace();
}
}


private static void recognizeOrders(LiveSpeechRecognizer recognizer, String utterance) {

//Catch utterance until 'stop' or 'break' is said
while(true) {

utterance = recognizer.getResult().getHypothesis();

if(utterance.startsWith("stop") || utterance.equals("break")) break;

else if(utteranceRegex("forward", utterance)) {

left_motor = 250;
right_motor = 250;

connection(left_motor, right_motor, packet);

} else if(utteranceRegex("backward", utterance)) {
left_motor = -250;
right_motor = -250;

connection(left_motor, right_motor, packet);

} else if(utteranceRegex("left", utterance)) {

left_motor = -250;
right_motor = 250;

connection(left_motor, right_motor, packet);

} else if(utteranceRegex("right", utterance)) {

left_motor = 250;
right_motor = -250;

connection(left_motor, right_motor, packet);

} else if(utterance.startsWith("wait") || utterance.equals("please wait")) {
left_motor = 0;
right_motor = 0;

connection(left_motor, right_motor, packet);
}
}
}

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

Configuration configuration = new Configuration();

configuration
.setAcousticModelPath(ACOUSTIC_MODEL);
configuration
.setDictionaryPath(DICTIONARY_PATH);
configuration
.setLanguageModelPath(LANGUAGE_MODEL);
configuration
.setGrammarPath(GRAMMAR_PATH);

//To catch words only from grammar file
configuration.setUseGrammar(true);
configuration.setGrammarName("grammar");

LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);

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

recognizeOrders(recognizer, utterance);

recognizer.stopRecognition();
}

}


0 comments on commit 08f883d

Please sign in to comment.