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