set flag NoAWT. set flag Android. 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.*; static SpeechRecognizer sr; static final String TAG = "MyStt3Activity"; static S language = "de-DE"; static int extraResults = 1; static bool fastSynthesis = false; // use device voice (faster) static bool noSpeech = false; // disable speech output static bool manualMode = false; // Start recognition on click only sbool muteAlways, printErrors; static volatile bool listening; static new L history; static Q mechAppendQ; static Lock speechLock = lock(); svoid leoMain { aHideTitleBar(); mechAppendQ = startQ(); androidSay_keepEngine = true; if (muteAlways) androidMuteAudio(); S hello = null; /*if (args.length != 0 && args[0].equals("nogfx")) setOpt(getMainClass(), "showPicture", false);*/ try { history.add("*"); //hello = callStaticAnswerMethod("*", history); if (hello == null) hello = german() ? "hallo" : "hello"; } catch (Throwable e) { e.printStackTrace(); return; } //if (!noSpeech) say(hello); callOptMC('happy); androidUI(r { //androidMuteAudio(androidActivity()); if (!manualMode) newRecognizer(); }); thread "Long-Poll" { repeat with sleep 1 { // TODO: interrupt connection on cleanup S action = postPage("http://butter.botcompany.de:8080/1014866/raw", muricaCredentials()); if (nempty(action)) for (S s : lines(action)) say(s); } } aAddMenuItems("Switch to manual mode", "Switch to auto mode"); // init engine? if (german()) androidSayInGerman(""); else androidSayInEnglish(""); noMainDone(); } static void newRecognizer() { sr = SpeechRecognizer.createSpeechRecognizer(androidActivity()); sr.setRecognitionListener(new listener()); recog(); } static class listener implements RecognitionListener { public void onReadyForSpeech(Bundle params) { callOptMC('setBorderColor, 0xFF66FF66); callOptMC('oh); showText(german() ? "SAG WAS." : "TALK NOW."); } public void onBeginningOfSpeech() { //showText("User talks"); //callOptMC('oh); if (!manualMode && !muteAlways) androidMuteAudio(); // Mute now, so we don't hear the end-of-speech sound } public void onRmsChanged(float rmsdB) {} public void onBufferReceived(byte[] buffer) {} public void onEndOfSpeech() { ping(); showText("onEndOfSpeech"); callOptMC('setBorderColor, 0xFFFFFFFF); } public void onError(int error) { ping(); listening = false; if (printErrors) if (error == 6) // timeout print("speech timeout"); else print("error " + error); // screw the errors! try { sr.destroy(); } catch (Throwable e) { print(e); } if (!manualMode) newRecognizer(); else callOptMC('setBorderColor, 0xFFFFFFFF); callOpt(getMainClass(), "happy"); } public void onResults(Bundle results) { ping(); listening = false; //showText("onResults"); ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); fS s = data.get(0); mechAppendQ.add(r { appendToMechList_noUniq("Katze Speech Recognition Log With Date", "[" + localDateWithMilliseconds() + ", through phone] " + s) }); showText((german() ? "Ich habe geh\u00f6rt: " : "I heard: ") + quote(s)); // TODO: fix the java strings umlaut problem final boolean goodbye = match3("goodbye", s) || match3("bye", s) || match3("tsch\u00fcss", s) || match3("tsch\u00fcss ...", s); thread { // get answer history.add(s); if "stop listening" switchToManual(); if "start listening" { manualMode = false; androidMuteAudio(); } S answer; try { answer = goodbye ? "tsch\u00fcss" : callStaticAnswerMethod(s, history); } catch (Throwable e) { e.printStackTrace(); appendToFile(getProgramFile("errors.txt"), getTheStackTrace(e)); answer = "Fehler"; } if (answer != null) print(answer); androidUI(r { if (goodbye) { print("\nGOODBYE!"); sr.destroy(); callOpt(getMainClass(), "disappear"); } else { sr.stopListening(); listening = false; //newRecognizer(); // always make a new one - gives endless errors if (!manualMode) recog(); } }); } // end of thread } public void onPartialResults(Bundle partialResults) { print("onPartialResults"); } public void onEvent(int eventType, Bundle params) { print("onEvent" + eventType); } } static void recog() { if (sr == null) ret with newRecognizer(); callOptMC('setBorderColor, 0xFFFFFF66); // yellow (initializing speech recognizer) Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, extraResults); // customize recognition time settings callOpt(mc(), "recogTimeSettings", intent); sr.startListening(intent); listening = true; //print("started listening"); } static void say(S s) { ping(); lock speechLock; showText(">> " + s); if (manualMode && listening) { print("User is speaking, postponing speech output."); ret; } androidUI_wait(f stopListening); history.add(s); if (!noSpeech) { androidUnmuteAudio(); callOptMC('oh); if (fastSynthesis) { if (german()) androidSayInGerman(s); else androidSayInEnglish(s); } else { File mp3 = cereproc_silent(german() ? "Leopold" : "Jack", s); androidPlayMp3(androidActivity(), mp3); } callOptMC('happy); } if (muteAlways) androidMuteAudio(); } static void stopListening() { listening = false; if (sr != null) sr.stopListening(); } svoid startListening() { if (listening) ret; listening = true; recog(); } sbool german() { ret swic(language, "de"); } svoid switchToManual { stopListening(); manualMode = true; androidUnmuteAudio(); } svoid switchToAuto { manualMode = false; startListening(); androidUnmuteAudio(); } svoid onMenuCommand(S s) { print("menu cmd: " + s); if (eqicOneOf(s, "stop listening", "Switch to manual mode")) switchToManual(); if (eqicOneOf(s, "Switch to auto mode")) switchToAuto(); } svoid cleanMeUp_leo { if (sr != null) { sr.destroy(); sr = null; } }