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

124
LINES

< > BotCompany Repo | #1008217 // aContinuousEnglishRecognition

JavaX fragment (include)

1  
import android.app.*;
2  
import android.content.*;
3  
import android.os.Bundle;
4  
import android.view.View;
5  
import android.view.View.OnClickListener;
6  
import android.speech.RecognitionListener;
7  
import android.speech.RecognizerIntent;
8  
import android.speech.SpeechRecognizer;
9  
import android.widget.Button;
10  
import android.widget.TextView;
11  
import android.util.Log;
12  
import android.media.*;
13  
14  
// onSpeech: voidfunc(L<S>)
15  
static void aContinuousEnglishRecognition(O onSpeech) {
16  
  new ContinuousEnglishRecognition(onSpeech).start();
17  
}
18  
19  
sclass ContinuousEnglishRecognition {
20  
  SpeechRecognizer sr;
21  
  final String TAG = "MyStt3Activity";
22  
  boolean loop = true;
23  
  S language = "en-US";
24  
  O onSpeech, onReady, onUnready;
25  
   
26  
  *(O *onSpeech) {}
27  
  *() {}
28  
  
29  
  void start() {
30  
    androidUI(r {
31  
      androidMuteAudio();
32  
      makeNewObject();
33  
    });
34  
  }
35  
  
36  
  void makeNewObject() {
37  
    sr = SpeechRecognizer.createSpeechRecognizer(androidContext());
38  
    sr.setRecognitionListener(new listener());        
39  
    go();
40  
  }
41  
  
42  
  void stop {
43  
    if (sr != null)
44  
      sr.destroy();
45  
    sr = null;
46  
  }
47  
48  
  class listener implements RecognitionListener {
49  
      public void onReadyForSpeech(Bundle params)
50  
      {
51  
               print("PLEASE SPEAK.");
52  
               pcallF(onReady);
53  
      }
54  
      public void onBeginningOfSpeech()
55  
      {
56  
               //print("LISTENING.");
57  
      }
58  
      public void onRmsChanged(float rmsdB)
59  
      {
60  
               //print("onRmsChanged");
61  
      }
62  
      public void onBufferReceived(byte[] buffer)
63  
      {
64  
               print("onBufferReceived");
65  
      }
66  
      public void onEndOfSpeech()
67  
      {
68  
               //print("RECOGNIZING.");
69  
      }
70  
      public void onError(int error)
71  
      {
72  
               print( "error " +  error); // screw the errors!
73  
               pcallF(onUnready);
74  
               //if (loop) go();
75  
               try {
76  
                 sr.destroy();
77  
               } catch (Throwable e) {
78  
                 print(e);
79  
               }
80  
               if (loop)
81  
                 makeNewObject();
82  
      }
83  
      public void onResults(Bundle results)                   
84  
      {
85  
               //print("onResults " + results);
86  
               ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
87  
               S bestMatch = data.get(0);
88  
               print("I heard: " + quote(bestMatch));
89  
               //print("I heard: " +structure(data));
90  
               
91  
               pcallF(onUnready);
92  
               
93  
               if (loop) {
94  
                 sr.stopListening();
95  
                 go();
96  
               }
97  
      }
98  
      public void onPartialResults(Bundle results)
99  
      {
100  
              // print("onPartialResults");
101  
           L<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
102  
               //S bestMatch = data.get(0);
103  
          //printStructure(data);
104  
        pcallF(onSpeech, data);
105  
      }
106  
      public void onEvent(int eventType, Bundle params)
107  
      {
108  
               print("onEvent " + eventType);
109  
      }
110  
   }
111  
   
112  
  void go() {
113  
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
114  
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
115  
    intent.putExtra(RecognizerIntent.
116  
EXTRA_PARTIAL_RESULTS, true);
117  
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
118  
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
119  
120  
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
121  
    sr.startListening(intent);
122  
    //print("started listening");
123  
  }
124  
}

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: 556 / 577
Version history: 9 change(s)
Referenced in: [show references]