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

SpeechInterface.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 
00027 package voce;
00028 
00032 public class SpeechInterface
00033 {
00034         private static SpeechSynthesizer mSynthesizer = null;
00035         private static SpeechRecognizer mRecognizer = null;
00036 
00047         public static void init(String vocePath, boolean initSynthesis, 
00048                 boolean initRecognition, String grammarPath, String grammarName)
00049         {
00050                 Utils.setPrintDebug(false);
00051                 Utils.log("debug", "Beginning initialization");
00052 
00053                 if (!initSynthesis && !initRecognition)
00054                 {
00055                         Utils.log("warning", "Synthesizer and recognizer are both" 
00056                                 + "uninitialized.");
00057                 }
00058 
00059                 if (initSynthesis)
00060                 {
00061                         // Create a speech synthesizer and give it a name.
00062                         Utils.log("", "Initializing synthesizer");
00063                         mSynthesizer = new SpeechSynthesizer("Kevin16");
00064                 }
00065 
00066                 if (initRecognition)
00067                 {
00068                         if (grammarPath.equals(""))
00069                         {
00070                                 grammarPath = "./";
00071                         }
00072 
00073                         // Always use the same config file.
00074                         String configFilename = "voce.config.xml";
00075 
00076                         // Create the speech recognizer.
00077                         Utils.log("", "Initializing recognizer. " 
00078                                 + "This may take some time...");
00079                         mRecognizer = new SpeechRecognizer(vocePath + "/" 
00080                                 + configFilename, grammarPath, grammarName);
00081 
00082                         // Enable the recognizer; this will start the recognition 
00083                         // thread.
00084                         setRecognizerEnabled(true);
00085                 }
00086 
00087                 Utils.log("", "Initialization complete");
00088         }
00089 
00091         public static void destroy()
00092         {
00093                 Utils.log("debug", "Shutting down...");
00094                 
00095                 if (null != mSynthesizer)
00096                 {
00097                         mSynthesizer.destroy();
00098                 }
00099 
00100                 if (null != mRecognizer)
00101                 {
00102                         mRecognizer.destroy();
00103                 }
00104 
00105                 Utils.log("", "Shutdown complete");
00106         }
00107 
00109         public static void synthesize(String message)
00110         {
00111                 if (null == mSynthesizer)
00112                 {
00113                         Utils.log("warning", "synthesize called before " 
00114                                 + "synthesizer was initialized.  Request will be ignored.");
00115                         return;
00116                 }
00117 
00118                 //Utils.log("debug", "SpeechInterface.speak: Adding message to speech queue: " + message);
00119                 
00120                 mSynthesizer.synthesize(message);
00121         }
00122 
00125         public static void stopSynthesizing()
00126         {
00127                 if (null == mSynthesizer)
00128                 {
00129                         Utils.log("warning", "stopSynthesizing called before " 
00130                                 + "synthesizer was initialized.  Request will be ignored.");
00131                         return;
00132                 }
00133 
00134                 mSynthesizer.stopSynthesizing();
00135         }
00136 
00139         public static int getRecognizerQueueSize()
00140         {
00141                 if (null == mRecognizer)
00142                 {
00143                         Utils.log("warning", "getRecognizerQueueSize "
00144                                 + "called before recognizer was initialized.  Returning " 
00145                                 + "0.");
00146                         return 0;
00147                 }
00148 
00149                 return mRecognizer.getQueueSize();
00150         }
00151 
00154         public static String popRecognizedString()
00155         {
00156                 if (null == mRecognizer)
00157                 {
00158                         Utils.log("warning", "popRecognizedString "
00159                                 + "called before recognizer was initialized.  Returning " 
00160                                 + "an empty string.");
00161                         return "";
00162                 }
00163 
00164                 return mRecognizer.popString();
00165         }
00166 
00168         public static void setRecognizerEnabled(boolean e)
00169         {
00170                 if (null == mRecognizer)
00171                 {
00172                         Utils.log("warning", "setRecognizerEnabled "
00173                                 + "called before recognizer was initialized.  Request " 
00174                                 + "will be ignored.");
00175                         return;
00176                 }
00177 
00178                 mRecognizer.setEnabled(e);
00179         }
00180 
00182         public static boolean isRecognizerEnabled()
00183         {
00184                 if (null == mRecognizer)
00185                 {
00186                         Utils.log("warning", "isRecognizerEnabled "
00187                                 + "called before recognizer was initialized.  Returning " 
00188                                 + "false.");
00189                         return false;
00190                 }       
00191 
00192                 return mRecognizer.isEnabled();
00193         }
00194 }

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