Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

124
LINES

< > BotCompany Repo | #1008217 // aContinuousEnglishRecognition

JavaX fragment (include)

import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.media.*;

// onSpeech: voidfunc(L<S>)
static void aContinuousEnglishRecognition(O onSpeech) {
  new ContinuousEnglishRecognition(onSpeech).start();
}

sclass ContinuousEnglishRecognition {
  SpeechRecognizer sr;
  final String TAG = "MyStt3Activity";
  boolean loop = true;
  S language = "en-US";
  O onSpeech, onReady, onUnready;
   
  *(O *onSpeech) {}
  *() {}
  
  void start() {
    androidUI(r {
      androidMuteAudio();
      makeNewObject();
    });
  }
  
  void makeNewObject() {
    sr = SpeechRecognizer.createSpeechRecognizer(androidContext());
    sr.setRecognitionListener(new listener());        
    go();
  }
  
  void stop {
    if (sr != null)
      sr.destroy();
    sr = null;
  }

  class listener implements RecognitionListener {
      public void onReadyForSpeech(Bundle params)
      {
               print("PLEASE SPEAK.");
               pcallF(onReady);
      }
      public void onBeginningOfSpeech()
      {
               //print("LISTENING.");
      }
      public void onRmsChanged(float rmsdB)
      {
               //print("onRmsChanged");
      }
      public void onBufferReceived(byte[] buffer)
      {
               print("onBufferReceived");
      }
      public void onEndOfSpeech()
      {
               //print("RECOGNIZING.");
      }
      public void onError(int error)
      {
               print( "error " +  error); // screw the errors!
               pcallF(onUnready);
               //if (loop) go();
               try {
                 sr.destroy();
               } catch (Throwable e) {
                 print(e);
               }
               if (loop)
                 makeNewObject();
      }
      public void onResults(Bundle results)                   
      {
               //print("onResults " + results);
               ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
               S bestMatch = data.get(0);
               print("I heard: " + quote(bestMatch));
               //print("I heard: " +structure(data));
               
               pcallF(onUnready);
               
               if (loop) {
                 sr.stopListening();
                 go();
               }
      }
      public void onPartialResults(Bundle results)
      {
              // print("onPartialResults");
           L<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
               //S bestMatch = data.get(0);
          //printStructure(data);
        pcallF(onSpeech, data);
      }
      public void onEvent(int eventType, Bundle params)
      {
               print("onEvent " + eventType);
      }
   }
   
  void go() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.
EXTRA_PARTIAL_RESULTS, true);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
    sr.startListening(intent);
    //print("started listening");
  }
}

Author comment

Began life as a copy of #1001252

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008217
Snippet name: aContinuousEnglishRecognition
Eternal ID of this version: #1008217/10
Text MD5: 57620ccd2af9ce0eae3dee6309495b48
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-01 17:05:57
Source code size: 3684 bytes / 124 lines
Pitched / IR pitched: No / No
Views / Downloads: 555 / 576
Version history: 9 change(s)
Referenced in: [show references]