00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 package voce;
00027
00028 import edu.cmu.sphinx.frontend.util.Microphone;
00029 import edu.cmu.sphinx.recognizer.Recognizer;
00030 import edu.cmu.sphinx.result.Result;
00031 import edu.cmu.sphinx.util.props.ConfigurationManager;
00032 import edu.cmu.sphinx.util.props.PropertyException;
00033
00034 import java.util.LinkedList;
00035 import java.io.File;
00036 import java.io.IOException;
00037 import java.net.URL;
00038
00042 public class SpeechRecognizer implements Runnable
00043 {
00045 private Recognizer mRecognizer = null;
00046
00048 private Microphone mMicrophone = null;
00049
00052 private volatile Thread mRecognitionThread = null;
00053
00055 private boolean mRecognitionThreadRunning = false;
00056
00058 private LinkedList<String> mRecognizedStringQueue;
00059
00065 public SpeechRecognizer(String configFilename, String grammarPath,
00066 String grammarName)
00067 {
00068 try
00069 {
00070 URL configURL = new File(configFilename).toURI().toURL();
00071 ConfigurationManager cm = new ConfigurationManager(configURL);
00072
00073 mRecognizer = (Recognizer) cm.lookup("recognizer");
00074 mMicrophone = (Microphone) cm.lookup("microphone");
00075
00076 if (!grammarName.equals(""))
00077 {
00078
00079
00080 cm.lookup("jsgfGrammar");
00081
00082
00083 cm.setProperty("jsgfGrammar", "grammarLocation", grammarPath);
00084 cm.setProperty("jsgfGrammar", "grammarName", grammarName);
00085 }
00086 else
00087 {
00088 Utils.log("", "No grammar file specified. Defaulting to "
00089 + "'digits.gram'");
00090 }
00091
00092 mRecognizer.allocate();
00093 mRecognizedStringQueue = new LinkedList<String>();
00094 }
00095 catch (IOException e)
00096 {
00097 Utils.log("ERROR", "Cannot load speech recognizer: ");
00098 e.printStackTrace();
00099 }
00100 catch (PropertyException e)
00101 {
00102 Utils.log("ERROR", "Cannot configure speech recognizer: ");
00103 e.printStackTrace();
00104 }
00105 catch (InstantiationException e)
00106 {
00107 Utils.log("ERROR", "Cannot create speech recognizer: ");
00108 e.printStackTrace();
00109 }
00110 }
00111
00114 public void run()
00115 {
00116 Utils.log("debug", "Recognition thread starting");
00117 mRecognitionThreadRunning = true;
00118
00119
00120
00121 while (null != mRecognitionThread)
00122 {
00123 if (!mMicrophone.isRecording())
00124 {
00125 Utils.log("warning", "Recognition thread is running, but "
00126 + "the microphone is disabled.");
00127 }
00128 else
00129 {
00130 Result result = mRecognizer.recognize();
00131
00132 if (result != null)
00133 {
00134 String s = result.getBestFinalResultNoFiller();
00135
00136
00137 if (!s.equals(""))
00138 {
00139 Utils.log("debug", "Finished recognizing");
00140 mRecognizedStringQueue.addLast(s);
00141 }
00142 }
00143 }
00144 }
00145
00146 Utils.log("debug", "Recognition thread finished");
00147 mRecognitionThreadRunning = false;
00148 }
00149
00152 public int getQueueSize()
00153 {
00154 return mRecognizedStringQueue.size();
00155 }
00156
00160 public String popString()
00161 {
00162 if (getQueueSize() > 0)
00163 {
00164 return mRecognizedStringQueue.removeFirst();
00165 }
00166 else
00167 {
00168 return "";
00169 }
00170 }
00171
00174 public void setEnabled(boolean e)
00175 {
00176 if (e)
00177 {
00178 boolean success = mMicrophone.startRecording();
00179
00180 if (!success)
00181 {
00182 Utils.log("warning", "Cannot initialize microphone. " +
00183 "Speech recognition disabled.");
00184 return;
00185 }
00186 else
00187 {
00188 if (null == mRecognitionThread)
00189 {
00190 mRecognitionThread = new Thread(this, "Recognition thread");
00191 mRecognitionThread.start();
00192 }
00193 }
00194 }
00195 else
00196 {
00197
00198 mRecognizedStringQueue.clear();
00199 mMicrophone.stopRecording();
00200 mMicrophone.clear();
00201
00202
00203
00204 mRecognitionThread = null;
00205
00206
00207 while (mRecognitionThreadRunning)
00208 {
00209 Utils.log("debug", "Waiting for recognition thread to die...");
00210
00211 try
00212 {
00213
00214 Thread.sleep(100);
00215 }
00216 catch (InterruptedException exception)
00217 {
00218 }
00219 }
00220
00221
00222 }
00223 }
00224
00226 public boolean isEnabled()
00227 {
00228 return mMicrophone.isRecording();
00229 }
00230
00232 public void destroy()
00233 {
00234
00235
00236 setEnabled(false);
00237
00238
00239 mRecognizer.deallocate();
00240 }
00241 }