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

350
LINES

< > BotCompany Repo | #1001279 // Android Cat Main (include, old)

JavaX fragment (include)

1  
set flag NoAWT. set flag Android. set flag AndroidOnly.
2  
3  
import android.app.*;
4  
import android.content.*;
5  
import android.os.Bundle;
6  
import android.view.View;
7  
import android.view.View.OnClickListener;
8  
import android.speech.RecognitionListener;
9  
import android.speech.RecognizerIntent;
10  
import android.speech.SpeechRecognizer;
11  
import android.widget.Button;
12  
import android.widget.TextView;
13  
import android.util.Log;
14  
import android.media.*;
15  
16  
static SpeechRecognizer sr;
17  
static final String TAG = "MyStt3Activity";
18  
static S language = "en-US";
19  
static int extraResults = 1;
20  
21  
static bool fastSynthesis = false; // use device voice (faster)
22  
static bool noSpeech = false; // disable speech output
23  
24  
static bool manualMode = false; // Start recognition on click only
25  
sbool muteAlways, printErrors;
26  
sbool listenAfterTalk = true; // it works now
27  
28  
static volatile bool listening, recognitionCancelled;
29  
30  
static new L<S> history;
31  
static Lock speechLock = lock();
32  
33  
svoid leoMain {
34  
  if (androidIsAdminMode()) {
35  
    print("Going admin.");
36  
    androidUnsetAdminMode();
37  
    removeMyselfAsInjection();
38  
    aShowStartScreen();
39  
    ret;
40  
  }
41  
    
42  
  aHideTitleBar();
43  
  
44  
  androidSay_keepEngine = true;
45  
  
46  
  if (muteAlways) androidMuteAudio();
47  
  
48  
  S hello = null;
49  
  
50  
  /*if (args.length != 0 && args[0].equals("nogfx"))
51  
    setOpt(getMainClass(), "showPicture", false);*/
52  
  
53  
  try {
54  
    history.add("*");
55  
    //hello = callStaticAnswerMethod("*", history);
56  
    if (hello == null) hello = german() ? "hallo" : "hello";
57  
  } catch (Throwable e) {
58  
    e.printStackTrace();
59  
    return;
60  
  }
61  
  
62  
  if (!androidIsAdminMode())
63  
    aClearConsole();
64  
65  
  listening = true; // so user can cancel early
66  
  //if (!noSpeech) say(hello);
67  
  justASec(); // show interface
68  
  callOptMC('happy);
69  
    
70  
  thread "Long-Poll" {
71  
    repeat with sleep 1 {
72  
      // TODO: interrupt connection on cleanup
73  
      // TODO: make it work for other users!
74  
      S action = postPageSilently("http://butter.botcompany.de:8080/1014866/raw", muricaCredentials());
75  
      if (nempty(action))
76  
        for (S s : lines(action)) say(s);
77  
    }
78  
  }
79  
80  
  // setLanguage(language);
81  
  
82  
  aAddMenuItems("Switch to manual mode", "Switch to auto mode");
83  
  
84  
  // init engine?
85  
  if (german()) androidSayInGerman(""); else androidSayInEnglish("");
86  
  
87  
  if (recognitionCancelled) recognitionCancelled = false;
88  
  else
89  
    androidUI(f newRecognizer);
90  
91  
  noMainDone();
92  
}
93  
94  
static void newRecognizer() {
95  
  //print("listening");
96  
  listening = true;
97  
  sr = SpeechRecognizer.createSpeechRecognizer(androidActivity());
98  
  sr.setRecognitionListener(new listener());        
99  
  recog();
100  
}
101  
102  
static class listener implements RecognitionListener {
103  
  public void onReadyForSpeech(Bundle params) {
104  
    if (recognitionCancelled) {
105  
      recognitionCancelled = false;
106  
      sr.stopListening();
107  
      ret;
108  
    }
109  
    callOptMC('setBorderAndStatus, 0xFF66FF66,
110  
      german() ? "JETZT SPRECHEN!" : "TALK NOW!");
111  
    callOptMC('oh);
112  
    //showText(german() ? "SAG WAS." : "TALK NOW.");
113  
  }
114  
  
115  
  public void onBeginningOfSpeech() {
116  
    //showText("User talks");
117  
    //callOptMC('oh);
118  
    if (!manualMode && !muteAlways)
119  
      androidMuteAudio(); // Mute now, so we don't hear the end-of-speech sound
120  
  }
121  
  
122  
  public void onRmsChanged(float rmsdB) {}
123  
  public void onBufferReceived(byte[] buffer) {}
124  
  
125  
  public void onEndOfSpeech() {
126  
    ping();
127  
    //showText("onEndOfSpeech");
128  
    callOptMC('setBorderAndStatus, aWhite(), baseStatus());
129  
  }
130  
  
131  
  public void onError(int error) {
132  
     ping();
133  
     listening = false;
134  
     if (printErrors)
135  
       if (error == 6) // timeout
136  
         print("speech timeout");
137  
       else
138  
         print("error " +  error); // screw the errors!
139  
     try {
140  
       sr.destroy();
141  
     } catch (Throwable e) {
142  
       print(e);
143  
     }
144  
     if (!manualMode)
145  
       newRecognizer();
146  
     else
147  
      callOptMC('setBorderAndStatus, aWhite(), baseStatus());
148  
     callOpt(getMainClass(), "happy");
149  
  }
150  
  
151  
  public void onResults(Bundle results) {
152  
    ping();
153  
    listening = false;
154  
    //showText("onResults");
155  
    ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
156  
    fS s = data.get(0);
157  
    onInput(s, false);
158  
  }
159  
160  
  public void onPartialResults(Bundle partialResults) {
161  
    print("onPartialResults");
162  
  }
163  
  
164  
  public void onEvent(int eventType, Bundle params) {
165  
    print("onEvent" + eventType);
166  
  }
167  
}
168  
 
169  
svoid onInput(S _s, final bool typed) {
170  
  fS s = trim(_s); if (empty(s)) ret;
171  
  thread "onInput" {
172  
    mechQ(r {
173  
      appendToMechList_noUniq("Katze Speech Recognition Log With Date", "[" + localDateWithMilliseconds() + ", " +
174  
        (typed ? "typed on phone" : "through phone") + "] " + s) });
175  
    aSetText(userTextView, lang("You: ", "Du: ") + s);
176  
    aPutViewBehindOtherView(userTextView, myTextView);
177  
  
178  
    showText(
179  
      (typed 
180  
        ? lang("You typed: ", "Du schrubst: ")
181  
        : lang("I heard: ", "Ich habe geh\u00f6rt: ")) + quote(s));
182  
  
183  
    // TODO: fix the java strings umlaut problem
184  
     
185  
    final boolean goodbye = match3("goodbye", s) || match3("bye", s) || match3("tsch\u00fcss", s) || match3("tsch\u00fcss ...", s);
186  
  
187  
    // get answer
188  
    
189  
    history.add(s);
190  
    
191  
    handleCommand(s);
192  
    
193  
    S answer;
194  
    try {
195  
      answer = goodbye ? "tsch\u00fcss" : callStaticAnswerMethod(s, history);
196  
    } catch (Throwable e) {
197  
      e.printStackTrace();
198  
      appendToFile(getProgramFile("errors.txt"), getTheStackTrace(e));
199  
      answer = "Fehler";
200  
    }
201  
    
202  
    if (answer != null)
203  
      print(answer);
204  
      
205  
    androidUI(r {
206  
      if (goodbye) {
207  
        print("\nGOODBYE!");
208  
        sr.destroy();
209  
        
210  
        callOpt(getMainClass(), "disappear");
211  
      } else {
212  
        sr.stopListening();
213  
        listening = false;
214  
        //newRecognizer(); // always make a new one - gives endless errors
215  
        if (!manualMode)
216  
          recog();
217  
      }
218  
    });
219  
  } // end of thread
220  
}
221  
222  
svoid recog() {
223  
  if (sr == null) ret with newRecognizer();
224  
  print("recog");
225  
  listening = true;
226  
  justASec();
227  
  Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
228  
  intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
229  
  intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
230  
  intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
231  
232  
  intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, extraResults); 
233  
  
234  
  // customize recognition time settings
235  
  callOpt(mc(), "recogTimeSettings", intent);
236  
  
237  
  sr.startListening(intent);
238  
  print("started listening");
239  
}
240  
241  
svoid say(fS s) {
242  
  ping();
243  
  lock speechLock;
244  
  showText(">> " + s);
245  
  aSetText(myTextView, lang("Me: ", "Ich: ") + s);
246  
  aPutViewBehindOtherView(myTextView, userTextView);
247  
  if (manualMode && listening) {
248  
    print("User is speaking, postponing speech output.");
249  
    ret;
250  
  }
251  
  androidUI_wait(f stopListening);
252  
  history.add(s);
253  
  mechQ(r {
254  
    appendToMechList_noUniq("Katze | Things the phone said", "[" + localDateWithMilliseconds() + "] " + s) });
255  
  if (!noSpeech) {
256  
    androidUnmuteAudio();
257  
    callOptMC('oh);
258  
    if (fastSynthesis) {
259  
      if (german()) androidSayInGerman(s); else androidSayInEnglish(s);
260  
    } else {
261  
      File mp3 = cereproc_silent(german() ? "Leopold" : "Jack", s);
262  
      androidPlayMp3(androidActivity(), mp3);
263  
    }
264  
    callOptMC('happy);
265  
    if (listenAfterTalk)
266  
      //androidUI_noWait(r startListening); // works, but freezes UI
267  
      androidUI_noWait(r newRecognizer);
268  
  }
269  
  if (muteAlways)
270  
    androidMuteAudio();
271  
}
272  
273  
static void stopListening() {
274  
  listening = false;
275  
  if (sr != null)
276  
    sr.stopListening();
277  
}
278  
279  
svoid startListening() {
280  
  if (listening) ret;
281  
  listening = true;
282  
  recog();
283  
}
284  
  
285  
sbool german() {
286  
  ret swic(language, "de");
287  
}
288  
289  
svoid switchToManual {
290  
  stopListening();
291  
  manualMode = true;
292  
  androidUnmuteAudio();
293  
}
294  
295  
svoid switchToAuto {
296  
  manualMode = false;
297  
  startListening();
298  
  androidUnmuteAudio();
299  
}
300  
301  
svoid onMenuCommand(S s) {
302  
  //print("menu cmd: " + s);
303  
  handleCommand(s);
304  
}
305  
306  
// spoken, typed or through menu
307  
svoid handleCommand(S s) {
308  
  if "stop listening|Switch to manual mode" switchToManual();
309  
  if "Switch to auto mode" switchToAuto();
310  
  if "german|deutsch" setLanguage("de-DE");
311  
  if "english|englisch" setLanguage("en-US");
312  
}
313  
314  
svoid cleanMeUp_leo {
315  
  if (sr != null) {
316  
    sr.destroy();
317  
    sr = null;
318  
  }
319  
}
320  
321  
sS baseStatus() {
322  
  ret !manualMode ? "" :
323  
    german() ? "KLICKEN ZUM SPRECHEN" : "CLICK ON ME IF YOU WANT TO TALK";
324  
}
325  
326  
sS lang(S en, S de) {
327  
  ret german() ? de : en;
328  
}
329  
330  
svoid setLanguage(S l) {
331  
  language = l;
332  
  setCurrentBotLanguage(l);
333  
  aSetText(lblInputView, inputViewLabel());
334  
  androidUI_noWait(r newRecognizer);
335  
}
336  
337  
svoid justASec {
338  
  callOptMC('setBorderAndStatus, 0xFFFF0000,
339  
    german() ? "BITTE WARTEN" : "JUST A SEC"); // (initializing speech recognizer)
340  
}
341  
342  
// Don't use - freezes UI
343  
svoid _cancelRecognition {
344  
  //print("Cancelling recognition " + listening + " " + (sr != null));
345  
  recognitionCancelled = true;
346  
  //stopListening();
347  
  //listening = false;
348  
  //if (sr != null) sr.cancel();
349  
  //callOptMC('setBorderAndStatus, aWhite(), baseStatus());
350  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001279
Snippet name: Android Cat Main (include, old)
Eternal ID of this version: #1001279/118
Text MD5: cfa19f07bc8a689fec04e968e3d6e2c0
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-05-21 14:01:15
Source code size: 9419 bytes / 350 lines
Pitched / IR pitched: No / No
Views / Downloads: 749 / 2480
Version history: 117 change(s)
Referenced in: [show references]