Main Page | Packages | Class List | File List | Class Members

SpeechRecognizer.java

Go to the documentation of this file.
00001 /*************************************************************************
00002  *                                                                       *
00003  * Voce                                                                  *
00004  * Copyright (C) 2005                                                    *
00005  * Tyler Streeter  tylerstreeter@gmail.com                               *
00006  * All rights reserved.                                                  *
00007  * Web: voce.sourceforge.net                                             *
00008  *                                                                       *
00009  * This library is free software; you can redistribute it and/or         *
00010  * modify it under the terms of EITHER:                                  *
00011  *   (1) The GNU Lesser General Public License as published by the Free  *
00012  *       Software Foundation; either version 2.1 of the License, or (at  *
00013  *       your option) any later version. The text of the GNU Lesser      *
00014  *       General Public License is included with this library in the     *
00015  *       file license-LGPL.txt.                                          *
00016  *   (2) The BSD-style license that is included with this library in     *
00017  *       the file license-BSD.txt.                                       *
00018  *                                                                       *
00019  * This library is distributed in the hope that it will be useful,       *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00022  * license-LGPL.txt and license-BSD.txt for more details.                *
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                                 // This will create this componenent if it has not already 
00079                                 // been created.
00080                                 cm.lookup("jsgfGrammar");
00081 
00082                                 // Setup in the user-defined grammar.
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                 //Thread currThread = Thread.currentThread();
00119 
00120                 //while (mRecognitionThread == currThread)
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                                         // Only save non-empty strings.
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                         //Utils.log("debug", "Clearing queues, stopping microphone...");
00198                         mRecognizedStringQueue.clear();
00199                         mMicrophone.stopRecording();
00200                         mMicrophone.clear();
00201 
00202                         // The following line indirectly stops the recognition thread 
00203                         // from running.
00204                         mRecognitionThread = null;
00205 
00206                         // Wait for the thread to die before proceeding.
00207                         while (mRecognitionThreadRunning)
00208                         {
00209                                 Utils.log("debug", "Waiting for recognition thread to die...");
00210 
00211                                 try
00212                                 {
00213                                         // Have the main thread sleep for a bit...
00214                                         Thread.sleep(100);
00215                                 }
00216                                 catch (InterruptedException exception)
00217                                 {
00218                                 }
00219                         }
00220 
00221                         //Utils.log("debug", "...done clearing queues, stopping microphone");
00222                 }
00223         }
00224 
00226         public boolean isEnabled()
00227         {
00228                 return mMicrophone.isRecording();
00229         }
00230 
00232         public void destroy()
00233         {
00234                 // This function call will shut down everything, including the 
00235                 // recognition thread.
00236                 setEnabled(false);
00237 
00238                 // It should now be safe to deallocate the recognizer.
00239                 mRecognizer.deallocate();
00240         }
00241 }

Generated on Wed Aug 3 16:25:01 2005 for Voce Java API by  doxygen 1.4.1