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
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
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
00074 String configFilename = "voce.config.xml";
00075
00076
00077 Utils.log("", "Initializing recognizer. "
00078 + "This may take some time...");
00079 mRecognizer = new SpeechRecognizer(vocePath + "/"
00080 + configFilename, grammarPath, grammarName);
00081
00082
00083
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
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 }