The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * --------------------------------------------------------------------------- |
| 3 | * Recognizer.java |
| 4 | * |
| 5 | * Copyright 2007 Nuance Communciations, Inc. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the 'License'); you may not |
| 8 | * use this file except in compliance with the License. |
| 9 | * |
| 10 | * You may obtain a copy of the License at |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | * License for the specific language governing permissions and limitations under |
| 17 | * the License. |
| 18 | * |
| 19 | * --------------------------------------------------------------------------- |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | package android.speech.srec; |
| 24 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import java.io.File; |
| 26 | import java.io.InputStream; |
| 27 | import java.io.IOException; |
| 28 | import java.util.Locale; |
| 29 | |
| 30 | /** |
| 31 | * Simple, synchronous speech recognizer, using the Nuance SREC package. |
| 32 | * Usages proceeds as follows: |
| 33 | * |
| 34 | * <ul> |
| 35 | * <li>Create a <code>Recognizer</code>. |
| 36 | * <li>Create a <code>Recognizer.Grammar</code>. |
| 37 | * <li>Setup the <code>Recognizer.Grammar</code>. |
| 38 | * <li>Reset the <code>Recognizer.Grammar</code> slots, if needed. |
| 39 | * <li>Fill the <code>Recognizer.Grammar</code> slots, if needed. |
| 40 | * <li>Compile the <code>Recognizer.Grammar</code>, if needed. |
| 41 | * <li>Save the filled <code>Recognizer.Grammar</code>, if needed. |
| 42 | * <li>Start the <code>Recognizer</code>. |
| 43 | * <li>Loop over <code>advance</code> and <code>putAudio</code> until recognition complete. |
| 44 | * <li>Fetch and process results, or notify of failure. |
| 45 | * <li>Stop the <code>Recognizer</code>. |
| 46 | * <li>Destroy the <code>Recognizer</code>. |
| 47 | * </ul> |
| 48 | * |
| 49 | * <p>Below is example code</p> |
| 50 | * |
| 51 | * <pre class="prettyprint"> |
| 52 | * |
| 53 | * // create and start audio input |
| 54 | * InputStream audio = new MicrophoneInputStream(11025, 11025*5); |
| 55 | * // create a Recognizer |
| 56 | * String cdir = Recognizer.getConfigDir(null); |
| 57 | * Recognizer recognizer = new Recognizer(cdir + "/baseline11k.par"); |
| 58 | * // create and load a Grammar |
| 59 | * Recognizer.Grammar grammar = recognizer.new Grammar(cdir + "/grammars/VoiceDialer.g2g"); |
| 60 | * // setup the Grammar to work with the Recognizer |
| 61 | * grammar.setupRecognizer(); |
| 62 | * // fill the Grammar slots with names and save, if required |
| 63 | * grammar.resetAllSlots(); |
| 64 | * for (String name : names) grammar.addWordToSlot("@Names", name, null, 1, "V=1"); |
| 65 | * grammar.compile(); |
| 66 | * grammar.save(".../foo.g2g"); |
| 67 | * // start the Recognizer |
| 68 | * recognizer.start(); |
| 69 | * // loop over Recognizer events |
| 70 | * while (true) { |
| 71 | * switch (recognizer.advance()) { |
| 72 | * case Recognizer.EVENT_INCOMPLETE: |
| 73 | * case Recognizer.EVENT_STARTED: |
| 74 | * case Recognizer.EVENT_START_OF_VOICING: |
| 75 | * case Recognizer.EVENT_END_OF_VOICING: |
| 76 | * // let the Recognizer continue to run |
| 77 | * continue; |
| 78 | * case Recognizer.EVENT_RECOGNITION_RESULT: |
| 79 | * // success, so fetch results here! |
| 80 | * for (int i = 0; i < recognizer.getResultCount(); i++) { |
| 81 | * String result = recognizer.getResult(i, Recognizer.KEY_LITERAL); |
| 82 | * } |
| 83 | * break; |
| 84 | * case Recognizer.EVENT_NEED_MORE_AUDIO: |
| 85 | * // put more audio in the Recognizer |
| 86 | * recognizer.putAudio(audio); |
| 87 | * continue; |
| 88 | * default: |
| 89 | * notifyFailure(); |
| 90 | * break; |
| 91 | * } |
| 92 | * break; |
| 93 | * } |
| 94 | * // stop the Recognizer |
| 95 | * recognizer.stop(); |
| 96 | * // destroy the Recognizer |
| 97 | * recognizer.destroy(); |
| 98 | * // stop the audio device |
| 99 | * audio.close(); |
| 100 | * |
| 101 | * </pre> |
| 102 | */ |
| 103 | public final class Recognizer { |
| 104 | static { |
| 105 | System.loadLibrary("srec_jni"); |
| 106 | } |
| 107 | |
| 108 | private static String TAG = "Recognizer"; |
| 109 | |
| 110 | /** |
| 111 | * Result key corresponding to confidence score. |
| 112 | */ |
| 113 | public static final String KEY_CONFIDENCE = "conf"; |
| 114 | |
| 115 | /** |
| 116 | * Result key corresponding to literal text. |
| 117 | */ |
| 118 | public static final String KEY_LITERAL = "literal"; |
| 119 | |
| 120 | /** |
| 121 | * Result key corresponding to semantic meaning text. |
| 122 | */ |
| 123 | public static final String KEY_MEANING = "meaning"; |
| 124 | |
| 125 | // handle to SR_Vocabulary object |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 126 | private long mVocabulary = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
| 128 | // handle to SR_Recognizer object |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 129 | private long mRecognizer = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | |
| 131 | // Grammar currently associated with Recognizer via SR_GrammarSetupRecognizer |
| 132 | private Grammar mActiveGrammar = null; |
| 133 | |
| 134 | /** |
| 135 | * Get the pathname of the SREC configuration directory corresponding to the |
| 136 | * language indicated by the Locale. |
| 137 | * This directory contains dictionaries, speech models, |
| 138 | * configuration files, and other data needed by the Recognizer. |
| 139 | * @param locale <code>Locale</code> corresponding to the desired language, |
| 140 | * or null for default, currently <code>Locale.US</code>. |
| 141 | * @return Pathname of the configuration directory. |
| 142 | */ |
| 143 | public static String getConfigDir(Locale locale) { |
| 144 | if (locale == null) locale = Locale.US; |
| 145 | String dir = "/system/usr/srec/config/" + |
Elliott Hughes | cb64d43 | 2013-08-02 10:00:44 -0700 | [diff] [blame] | 146 | locale.toString().replace('_', '.').toLowerCase(Locale.ROOT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | if ((new File(dir)).isDirectory()) return dir; |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Create an instance of a SREC speech recognizer. |
| 153 | * |
| 154 | * @param configFile pathname of the baseline*.par configuration file, |
| 155 | * which in turn contains references to dictionaries, speech models, |
| 156 | * and other data needed to configure and operate the recognizer. |
| 157 | * A separate config file is needed for each audio sample rate. |
| 158 | * Two files, baseline11k.par and baseline8k.par, which correspond to |
| 159 | * 11025 and 8000 hz, are present in the directory indicated by |
| 160 | * {@link #getConfigDir}. |
| 161 | * @throws IOException |
| 162 | */ |
| 163 | public Recognizer(String configFile) throws IOException { |
| 164 | PMemInit(); |
| 165 | SR_SessionCreate(configFile); |
| 166 | mRecognizer = SR_RecognizerCreate(); |
| 167 | SR_RecognizerSetup(mRecognizer); |
| 168 | mVocabulary = SR_VocabularyLoad(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Represents a grammar loaded into the Recognizer. |
| 173 | */ |
| 174 | public class Grammar { |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 175 | private long mGrammar = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * Create a <code>Grammar</code> instance. |
| 179 | * @param g2gFileName pathname of g2g file. |
| 180 | */ |
| 181 | public Grammar(String g2gFileName) throws IOException { |
| 182 | mGrammar = SR_GrammarLoad(g2gFileName); |
| 183 | SR_GrammarSetupVocabulary(mGrammar, mVocabulary); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Reset all slots. |
| 188 | */ |
| 189 | public void resetAllSlots() { |
| 190 | SR_GrammarResetAllSlots(mGrammar); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Add a word to a slot. |
| 195 | * |
| 196 | * @param slot slot name. |
| 197 | * @param word word to insert. |
| 198 | * @param pron pronunciation, or null to derive from word. |
| 199 | * @param weight weight to give the word. One is normal, 50 is low. |
| 200 | * @param tag semantic meaning tag string. |
| 201 | */ |
| 202 | public void addWordToSlot(String slot, String word, String pron, int weight, String tag) { |
| 203 | SR_GrammarAddWordToSlot(mGrammar, slot, word, pron, weight, tag); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Compile all slots. |
| 208 | */ |
| 209 | public void compile() { |
| 210 | SR_GrammarCompile(mGrammar); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Setup <code>Grammar</code> with <code>Recognizer</code>. |
| 215 | */ |
| 216 | public void setupRecognizer() { |
| 217 | SR_GrammarSetupRecognizer(mGrammar, mRecognizer); |
| 218 | mActiveGrammar = this; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Save <code>Grammar</code> to g2g file. |
| 223 | * |
| 224 | * @param g2gFileName |
| 225 | * @throws IOException |
| 226 | */ |
| 227 | public void save(String g2gFileName) throws IOException { |
| 228 | SR_GrammarSave(mGrammar, g2gFileName); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Release resources associated with this <code>Grammar</code>. |
| 233 | */ |
| 234 | public void destroy() { |
| 235 | // TODO: need to do cleanup and disassociation with Recognizer |
| 236 | if (mGrammar != 0) { |
| 237 | SR_GrammarDestroy(mGrammar); |
| 238 | mGrammar = 0; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Clean up resources. |
| 244 | */ |
| 245 | protected void finalize() { |
| 246 | if (mGrammar != 0) { |
| 247 | destroy(); |
| 248 | throw new IllegalStateException("someone forgot to destroy Grammar"); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Start recognition |
| 255 | */ |
| 256 | public void start() { |
| 257 | // TODO: shouldn't be here? |
| 258 | SR_RecognizerActivateRule(mRecognizer, mActiveGrammar.mGrammar, "trash", 1); |
| 259 | SR_RecognizerStart(mRecognizer); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Process some audio and return the current status. |
| 264 | * @return recognition event, one of: |
| 265 | * <ul> |
| 266 | * <li><code>EVENT_INVALID</code> |
| 267 | * <li><code>EVENT_NO_MATCH</code> |
| 268 | * <li><code>EVENT_INCOMPLETE</code> |
| 269 | * <li><code>EVENT_STARTED</code> |
| 270 | * <li><code>EVENT_STOPPED</code> |
| 271 | * <li><code>EVENT_START_OF_VOICING</code> |
| 272 | * <li><code>EVENT_END_OF_VOICING</code> |
| 273 | * <li><code>EVENT_SPOKE_TOO_SOON</code> |
| 274 | * <li><code>EVENT_RECOGNITION_RESULT</code> |
| 275 | * <li><code>EVENT_START_OF_UTTERANCE_TIMEOUT</code> |
| 276 | * <li><code>EVENT_RECOGNITION_TIMEOUT</code> |
| 277 | * <li><code>EVENT_NEED_MORE_AUDIO</code> |
| 278 | * <li><code>EVENT_MAX_SPEECH</code> |
| 279 | * </ul> |
| 280 | */ |
| 281 | public int advance() { |
| 282 | return SR_RecognizerAdvance(mRecognizer); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Put audio samples into the <code>Recognizer</code>. |
| 287 | * @param buf holds the audio samples. |
| 288 | * @param offset offset of the first sample. |
| 289 | * @param length number of bytes containing samples. |
| 290 | * @param isLast indicates no more audio data, normally false. |
| 291 | * @return number of bytes accepted. |
| 292 | */ |
| 293 | public int putAudio(byte[] buf, int offset, int length, boolean isLast) { |
| 294 | return SR_RecognizerPutAudio(mRecognizer, buf, offset, length, isLast); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Read audio samples from an <code>InputStream</code> and put them in the |
| 299 | * <code>Recognizer</code>. |
| 300 | * @param audio <code>InputStream</code> containing PCM audio samples. |
| 301 | */ |
| 302 | public void putAudio(InputStream audio) throws IOException { |
| 303 | // make sure the audio buffer is allocated |
| 304 | if (mPutAudioBuffer == null) mPutAudioBuffer = new byte[512]; |
| 305 | // read some data |
| 306 | int nbytes = audio.read(mPutAudioBuffer); |
| 307 | // eof, so signal Recognizer |
| 308 | if (nbytes == -1) { |
| 309 | SR_RecognizerPutAudio(mRecognizer, mPutAudioBuffer, 0, 0, true); |
| 310 | } |
| 311 | // put it into the Recognizer |
| 312 | else if (nbytes != SR_RecognizerPutAudio(mRecognizer, mPutAudioBuffer, 0, nbytes, false)) { |
| 313 | throw new IOException("SR_RecognizerPutAudio failed nbytes=" + nbytes); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // audio buffer for putAudio(InputStream) |
| 318 | private byte[] mPutAudioBuffer = null; |
| 319 | |
| 320 | /** |
| 321 | * Get the number of recognition results. Must be called after |
| 322 | * <code>EVENT_RECOGNITION_RESULT</code> is returned by |
| 323 | * <code>advance</code>, but before <code>stop</code>. |
| 324 | * |
| 325 | * @return number of results in nbest list. |
| 326 | */ |
| 327 | public int getResultCount() { |
| 328 | return SR_RecognizerResultGetSize(mRecognizer); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Get a set of keys for the result. Must be called after |
| 333 | * <code>EVENT_RECOGNITION_RESULT</code> is returned by |
| 334 | * <code>advance</code>, but before <code>stop</code>. |
| 335 | * |
| 336 | * @param index index of result. |
| 337 | * @return array of keys. |
| 338 | */ |
| 339 | public String[] getResultKeys(int index) { |
| 340 | return SR_RecognizerResultGetKeyList(mRecognizer, index); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Get a result value. Must be called after |
| 345 | * <code>EVENT_RECOGNITION_RESULT</code> is returned by |
| 346 | * <code>advance</code>, but before <code>stop</code>. |
| 347 | * |
| 348 | * @param index index of the result. |
| 349 | * @param key key of the result. This is typically one of |
| 350 | * <code>KEY_CONFIDENCE</code>, <code>KEY_LITERAL</code>, or |
| 351 | * <code>KEY_MEANING</code>, but the user can also define their own keys |
| 352 | * in a grxml file, or in the <code>tag</code> slot of |
| 353 | * <code>Grammar.addWordToSlot</code>. |
| 354 | * @return the result. |
| 355 | */ |
| 356 | public String getResult(int index, String key) { |
| 357 | return SR_RecognizerResultGetValue(mRecognizer, index, key); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Stop the <code>Recognizer</code>. |
| 362 | */ |
| 363 | public void stop() { |
| 364 | SR_RecognizerStop(mRecognizer); |
| 365 | SR_RecognizerDeactivateRule(mRecognizer, mActiveGrammar.mGrammar, "trash"); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Reset the acoustic state vectorto it's default value. |
| 370 | * |
| 371 | * @hide |
| 372 | */ |
| 373 | public void resetAcousticState() { |
| 374 | SR_AcousticStateReset(mRecognizer); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Set the acoustic state vector. |
| 379 | * @param state String containing the acoustic state vector. |
| 380 | * |
| 381 | * @hide |
| 382 | */ |
| 383 | public void setAcousticState(String state) { |
| 384 | SR_AcousticStateSet(mRecognizer, state); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Get the acoustic state vector. |
| 389 | * @return String containing the acoustic state vector. |
| 390 | * |
| 391 | * @hide |
| 392 | */ |
| 393 | public String getAcousticState() { |
| 394 | return SR_AcousticStateGet(mRecognizer); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Clean up resources. |
| 399 | */ |
| 400 | public void destroy() { |
| 401 | try { |
| 402 | if (mVocabulary != 0) SR_VocabularyDestroy(mVocabulary); |
| 403 | } finally { |
| 404 | mVocabulary = 0; |
| 405 | try { |
| 406 | if (mRecognizer != 0) SR_RecognizerUnsetup(mRecognizer); |
| 407 | } finally { |
| 408 | try { |
| 409 | if (mRecognizer != 0) SR_RecognizerDestroy(mRecognizer); |
| 410 | } finally { |
| 411 | mRecognizer = 0; |
| 412 | try { |
| 413 | SR_SessionDestroy(); |
| 414 | } finally { |
| 415 | PMemShutdown(); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Clean up resources. |
| 424 | */ |
| 425 | protected void finalize() throws Throwable { |
| 426 | if (mVocabulary != 0 || mRecognizer != 0) { |
| 427 | destroy(); |
| 428 | throw new IllegalStateException("someone forgot to destroy Recognizer"); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* an example session captured, for reference |
| 433 | void doall() { |
| 434 | if (PMemInit ( ) |
| 435 | || lhs_audioinOpen ( WAVE_MAPPER, SREC_TEST_DEFAULT_AUDIO_FREQUENCY, &audio_in_handle ) |
| 436 | || srec_test_init_application_data ( &applicationData, argc, argv ) |
| 437 | || SR_SessionCreate ( "/system/usr/srec/config/en.us/baseline11k.par" ) |
| 438 | || SR_RecognizerCreate ( &applicationData.recognizer ) |
| 439 | || SR_RecognizerSetup ( applicationData.recognizer) |
| 440 | || ESR_SessionGetLCHAR ( L("cmdline.vocabulary"), filename, &flen ) |
| 441 | || SR_VocabularyLoad ( filename, &applicationData.vocabulary ) |
| 442 | || SR_VocabularyGetLanguage ( applicationData.vocabulary, &applicationData.locale ) |
| 443 | || (applicationData.nametag = NULL) |
| 444 | || SR_NametagsCreate ( &applicationData.nametags ) |
| 445 | || (LSTRCPY ( applicationData.grammars [0].grammar_path, "/system/usr/srec/config/en.us/grammars/VoiceDialer.g2g" ), 0) |
| 446 | || (LSTRCPY ( applicationData.grammars [0].grammarID, "BothTags" ), 0) |
| 447 | || (LSTRCPY ( applicationData.grammars [0].ruleName, "trash" ), 0) |
| 448 | || (applicationData.grammars [0].is_ve_grammar = ESR_FALSE, 0) |
| 449 | || SR_GrammarLoad (applicationData.grammars [0].grammar_path, &applicationData.grammars [applicationData.grammarCount].grammar ) |
| 450 | || SR_GrammarSetupVocabulary ( applicationData.grammars [0].grammar, applicationData.vocabulary ) |
| 451 | || SR_GrammarSetupRecognizer( applicationData.grammars [0].grammar, applicationData.recognizer ) |
| 452 | || SR_GrammarSetDispatchFunction ( applicationData.grammars [0].grammar, L("myDSMCallback"), NULL, myDSMCallback ) |
| 453 | || (applicationData.grammarCount++, 0) |
| 454 | || SR_RecognizerActivateRule ( applicationData.recognizer, applicationData.grammars [0].grammar, |
| 455 | applicationData.grammars [0].ruleName, 1 ) |
| 456 | || (applicationData.active_grammar_num = 0, 0) |
| 457 | || lhs_audioinStart ( audio_in_handle ) |
| 458 | || SR_RecognizerStart ( applicationData.recognizer ) |
| 459 | || strl ( applicationData.grammars [0].grammar, &applicationData, audio_in_handle, &recognition_count ) |
| 460 | || SR_RecognizerStop ( applicationData.recognizer ) |
| 461 | || lhs_audioinStop ( audio_in_handle ) |
| 462 | || SR_RecognizerDeactivateRule ( applicationData.recognizer, applicationData.grammars [0].grammar, applicationData.grammars [0].ruleName ) |
| 463 | || (applicationData.active_grammar_num = -1, 0) |
| 464 | || SR_GrammarDestroy ( applicationData.grammars [0].grammar ) |
| 465 | || (applicationData.grammarCount--, 0) |
| 466 | || SR_NametagsDestroy ( applicationData.nametags ) |
| 467 | || (applicationData.nametags = NULL, 0) |
| 468 | || SR_VocabularyDestroy ( applicationData.vocabulary ) |
| 469 | || (applicationData.vocabulary = NULL) |
| 470 | || SR_RecognizerUnsetup ( applicationData.recognizer) // releases acoustic models |
| 471 | || SR_RecognizerDestroy ( applicationData.recognizer ) |
| 472 | || (applicationData.recognizer = NULL) |
| 473 | || SR_SessionDestroy ( ) |
| 474 | || srec_test_shutdown_application_data ( &applicationData ) |
| 475 | || lhs_audioinClose ( &audio_in_handle ) |
| 476 | || PMemShutdown ( ) |
| 477 | } |
| 478 | */ |
| 479 | |
| 480 | |
| 481 | // |
| 482 | // PMem native methods |
| 483 | // |
| 484 | private static native void PMemInit(); |
| 485 | private static native void PMemShutdown(); |
| 486 | |
| 487 | |
| 488 | // |
| 489 | // SR_Session native methods |
| 490 | // |
| 491 | private static native void SR_SessionCreate(String filename); |
| 492 | private static native void SR_SessionDestroy(); |
| 493 | |
| 494 | |
| 495 | // |
| 496 | // SR_Recognizer native methods |
| 497 | // |
| 498 | |
| 499 | /** |
| 500 | * Reserved value. |
| 501 | */ |
| 502 | public final static int EVENT_INVALID = 0; |
| 503 | |
| 504 | /** |
| 505 | * <code>Recognizer</code> could not find a match for the utterance. |
| 506 | */ |
| 507 | public final static int EVENT_NO_MATCH = 1; |
| 508 | |
| 509 | /** |
| 510 | * <code>Recognizer</code> processed one frame of audio. |
| 511 | */ |
| 512 | public final static int EVENT_INCOMPLETE = 2; |
| 513 | |
| 514 | /** |
| 515 | * <code>Recognizer</code> has just been started. |
| 516 | */ |
| 517 | public final static int EVENT_STARTED = 3; |
| 518 | |
| 519 | /** |
| 520 | * <code>Recognizer</code> is stopped. |
| 521 | */ |
| 522 | public final static int EVENT_STOPPED = 4; |
| 523 | |
| 524 | /** |
| 525 | * Beginning of speech detected. |
| 526 | */ |
| 527 | public final static int EVENT_START_OF_VOICING = 5; |
| 528 | |
| 529 | /** |
| 530 | * End of speech detected. |
| 531 | */ |
| 532 | public final static int EVENT_END_OF_VOICING = 6; |
| 533 | |
| 534 | /** |
| 535 | * Beginning of utterance occured too soon. |
| 536 | */ |
| 537 | public final static int EVENT_SPOKE_TOO_SOON = 7; |
| 538 | |
| 539 | /** |
| 540 | * Recognition match detected. |
| 541 | */ |
| 542 | public final static int EVENT_RECOGNITION_RESULT = 8; |
| 543 | |
| 544 | /** |
| 545 | * Timeout occured before beginning of utterance. |
| 546 | */ |
| 547 | public final static int EVENT_START_OF_UTTERANCE_TIMEOUT = 9; |
| 548 | |
| 549 | /** |
| 550 | * Timeout occured before speech recognition could complete. |
| 551 | */ |
| 552 | public final static int EVENT_RECOGNITION_TIMEOUT = 10; |
| 553 | |
| 554 | /** |
| 555 | * Not enough samples to process one frame. |
| 556 | */ |
| 557 | public final static int EVENT_NEED_MORE_AUDIO = 11; |
| 558 | |
| 559 | /** |
| 560 | * More audio encountered than is allowed by 'swirec_max_speech_duration'. |
| 561 | */ |
| 562 | public final static int EVENT_MAX_SPEECH = 12; |
| 563 | |
| 564 | /** |
| 565 | * Produce a displayable string from an <code>advance</code> event. |
| 566 | * @param event |
| 567 | * @return String representing the event. |
| 568 | */ |
| 569 | public static String eventToString(int event) { |
| 570 | switch (event) { |
| 571 | case EVENT_INVALID: |
| 572 | return "EVENT_INVALID"; |
| 573 | case EVENT_NO_MATCH: |
| 574 | return "EVENT_NO_MATCH"; |
| 575 | case EVENT_INCOMPLETE: |
| 576 | return "EVENT_INCOMPLETE"; |
| 577 | case EVENT_STARTED: |
| 578 | return "EVENT_STARTED"; |
| 579 | case EVENT_STOPPED: |
| 580 | return "EVENT_STOPPED"; |
| 581 | case EVENT_START_OF_VOICING: |
| 582 | return "EVENT_START_OF_VOICING"; |
| 583 | case EVENT_END_OF_VOICING: |
| 584 | return "EVENT_END_OF_VOICING"; |
| 585 | case EVENT_SPOKE_TOO_SOON: |
| 586 | return "EVENT_SPOKE_TOO_SOON"; |
| 587 | case EVENT_RECOGNITION_RESULT: |
| 588 | return "EVENT_RECOGNITION_RESULT"; |
| 589 | case EVENT_START_OF_UTTERANCE_TIMEOUT: |
| 590 | return "EVENT_START_OF_UTTERANCE_TIMEOUT"; |
| 591 | case EVENT_RECOGNITION_TIMEOUT: |
| 592 | return "EVENT_RECOGNITION_TIMEOUT"; |
| 593 | case EVENT_NEED_MORE_AUDIO: |
| 594 | return "EVENT_NEED_MORE_AUDIO"; |
| 595 | case EVENT_MAX_SPEECH: |
| 596 | return "EVENT_MAX_SPEECH"; |
| 597 | } |
| 598 | return "EVENT_" + event; |
| 599 | } |
| 600 | |
| 601 | // |
| 602 | // SR_Recognizer methods |
| 603 | // |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 604 | private static native void SR_RecognizerStart(long recognizer); |
| 605 | private static native void SR_RecognizerStop(long recognizer); |
| 606 | private static native long SR_RecognizerCreate(); |
| 607 | private static native void SR_RecognizerDestroy(long recognizer); |
| 608 | private static native void SR_RecognizerSetup(long recognizer); |
| 609 | private static native void SR_RecognizerUnsetup(long recognizer); |
| 610 | private static native boolean SR_RecognizerIsSetup(long recognizer); |
| 611 | private static native String SR_RecognizerGetParameter(long recognizer, String key); |
| 612 | private static native int SR_RecognizerGetSize_tParameter(long recognizer, String key); |
| 613 | private static native boolean SR_RecognizerGetBoolParameter(long recognizer, String key); |
| 614 | private static native void SR_RecognizerSetParameter(long recognizer, String key, String value); |
| 615 | private static native void SR_RecognizerSetSize_tParameter(long recognizer, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | String key, int value); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 617 | private static native void SR_RecognizerSetBoolParameter(long recognizer, String key, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | boolean value); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 619 | private static native void SR_RecognizerSetupRule(long recognizer, long grammar, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | String ruleName); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 621 | private static native boolean SR_RecognizerHasSetupRules(long recognizer); |
| 622 | private static native void SR_RecognizerActivateRule(long recognizer, long grammar, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | String ruleName, int weight); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 624 | private static native void SR_RecognizerDeactivateRule(long recognizer, long grammar, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | String ruleName); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 626 | private static native void SR_RecognizerDeactivateAllRules(long recognizer); |
| 627 | private static native boolean SR_RecognizerIsActiveRule(long recognizer, long grammar, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | String ruleName); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 629 | private static native boolean SR_RecognizerCheckGrammarConsistency(long recognizer, |
| 630 | long grammar); |
| 631 | private static native int SR_RecognizerPutAudio(long recognizer, byte[] buffer, int offset, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | int length, boolean isLast); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 633 | private static native int SR_RecognizerAdvance(long recognizer); |
| 634 | // private static native void SR_RecognizerLoadUtterance(long recognizer, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | // const LCHAR* filename); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 636 | // private static native void SR_RecognizerLoadWaveFile(long recognizer, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | // const LCHAR* filename); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 638 | // private static native void SR_RecognizerSetLockFunction(long recognizer, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | // SR_RecognizerLockFunction function, void* data); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 640 | private static native boolean SR_RecognizerIsSignalClipping(long recognizer); |
| 641 | private static native boolean SR_RecognizerIsSignalDCOffset(long recognizer); |
| 642 | private static native boolean SR_RecognizerIsSignalNoisy(long recognizer); |
| 643 | private static native boolean SR_RecognizerIsSignalTooQuiet(long recognizer); |
| 644 | private static native boolean SR_RecognizerIsSignalTooFewSamples(long recognizer); |
| 645 | private static native boolean SR_RecognizerIsSignalTooManySamples(long recognizer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | // private static native void SR_Recognizer_Change_Sample_Rate (size_t new_sample_rate); |
| 647 | |
| 648 | |
| 649 | // |
| 650 | // SR_AcousticState native methods |
| 651 | // |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 652 | private static native void SR_AcousticStateReset(long recognizer); |
| 653 | private static native void SR_AcousticStateSet(long recognizer, String state); |
| 654 | private static native String SR_AcousticStateGet(long recognizer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | |
| 656 | |
| 657 | // |
| 658 | // SR_Grammar native methods |
| 659 | // |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 660 | private static native void SR_GrammarCompile(long grammar); |
| 661 | private static native void SR_GrammarAddWordToSlot(long grammar, String slot, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | String word, String pronunciation, int weight, String tag); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 663 | private static native void SR_GrammarResetAllSlots(long grammar); |
| 664 | // private static native void SR_GrammarAddNametagToSlot(long grammar, String slot, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | // const struct SR_Nametag_t* nametag, int weight, String tag); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 666 | private static native void SR_GrammarSetupVocabulary(long grammar, long vocabulary); |
| 667 | // private static native void SR_GrammarSetupModels(long grammar, SR_AcousticModels* models); |
| 668 | private static native void SR_GrammarSetupRecognizer(long grammar, long recognizer); |
| 669 | private static native void SR_GrammarUnsetupRecognizer(long grammar); |
| 670 | // private static native void SR_GrammarGetModels(long grammar,SR_AcousticModels** models); |
| 671 | private static native long SR_GrammarCreate(); |
| 672 | private static native void SR_GrammarDestroy(long grammar); |
| 673 | private static native long SR_GrammarLoad(String filename); |
| 674 | private static native void SR_GrammarSave(long grammar, String filename); |
| 675 | // private static native void SR_GrammarSetDispatchFunction(long grammar, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | // const LCHAR* name, void* userData, SR_GrammarDispatchFunction function); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 677 | // private static native void SR_GrammarSetParameter(long grammar, const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | // LCHAR* key, void* value); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 679 | // private static native void SR_GrammarSetSize_tParameter(long grammar, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | // const LCHAR* key, size_t value); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 681 | // private static native void SR_GrammarGetParameter(long grammar, const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | // LCHAR* key, void** value); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 683 | // private static native void SR_GrammarGetSize_tParameter(long grammar, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | // const LCHAR* key, size_t* value); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 685 | // private static native void SR_GrammarCheckParse(long grammar, const LCHAR* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | // transcription, SR_SemanticResult** result, size_t* resultCount); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 687 | private static native void SR_GrammarAllowOnly(long grammar, String transcription); |
| 688 | private static native void SR_GrammarAllowAll(long grammar); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | |
| 690 | |
| 691 | // |
| 692 | // SR_Vocabulary native methods |
| 693 | // |
| 694 | // private static native int SR_VocabularyCreate(); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 695 | private static native long SR_VocabularyLoad(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | // private static native void SR_VocabularySave(SR_Vocabulary* self, |
| 697 | // const LCHAR* filename); |
| 698 | // private static native void SR_VocabularyAddWord(SR_Vocabulary* self, |
| 699 | // const LCHAR* word); |
| 700 | // private static native void SR_VocabularyGetLanguage(SR_Vocabulary* self, |
| 701 | // ESR_Locale* locale); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 702 | private static native void SR_VocabularyDestroy(long vocabulary); |
| 703 | private static native String SR_VocabularyGetPronunciation(long vocabulary, String word); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | |
| 705 | |
| 706 | // |
| 707 | // SR_RecognizerResult native methods |
| 708 | // |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 709 | private static native byte[] SR_RecognizerResultGetWaveform(long recognizer); |
| 710 | private static native int SR_RecognizerResultGetSize(long recognizer); |
| 711 | private static native int SR_RecognizerResultGetKeyCount(long recognizer, int nbest); |
| 712 | private static native String[] SR_RecognizerResultGetKeyList(long recognizer, int nbest); |
| 713 | private static native String SR_RecognizerResultGetValue(long recognizer, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | int nbest, String key); |
Ashok Bhat | bdebe1a | 2014-01-06 11:35:32 +0000 | [diff] [blame] | 715 | // private static native void SR_RecognizerResultGetLocale(long recognizer, ESR_Locale* locale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | } |