Libraryless. Click here for Pure Java version (703L/6K/18K).
1 | !747 |
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 | main { |
17 | static Activity androidContext; |
18 | |
19 | static SpeechRecognizer sr; |
20 | static final String TAG = "MyStt3Activity"; |
21 | static boolean loop = true; |
22 | static S language = "de-DE"; |
23 | static int extraResults = 1; |
24 | |
25 | static S voice = "Leopold"; |
26 | |
27 | static new L<S> history; |
28 | |
29 | p { |
30 | say("hallo"); |
31 | |
32 | androidContext.runOnUiThread(runnable { |
33 | //androidMuteAudio(androidContext); |
34 | newRecognizer(); |
35 | }); |
36 | sleep(); |
37 | } |
38 | |
39 | static void newRecognizer() { |
40 | sr = SpeechRecognizer.createSpeechRecognizer(androidContext); |
41 | sr.setRecognitionListener(new listener()); |
42 | recog(); |
43 | } |
44 | |
45 | static class listener implements RecognitionListener { |
46 | public void onReadyForSpeech(Bundle params) { |
47 | print("SAG WAS."); |
48 | } |
49 | |
50 | public void onBeginningOfSpeech() {} |
51 | public void onRmsChanged(float rmsdB) {} |
52 | public void onBufferReceived(byte[] buffer) {} |
53 | public void onEndOfSpeech() {} |
54 | |
55 | public void onError(int error) { |
56 | //print( "error " + error); // screw the errors! |
57 | //if (loop) recog(); |
58 | try { |
59 | sr.destroy(); |
60 | } catch (Throwable e) { |
61 | print(e); |
62 | } |
63 | if (loop) |
64 | newRecognizer(); |
65 | } |
66 | |
67 | public void onResults(Bundle results) { |
68 | //print("onResults " + results); |
69 | ArrayList<S> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); |
70 | final S s = data.get(0); |
71 | print("Ich habe geh\u00f6rt: " + quote(s)); |
72 | //print("I heard: " +structure(data)); |
73 | |
74 | // TODO: fix the java strings umlaut problem |
75 | |
76 | final boolean goodbye = match3("goodbye", s) || match3("bye", s) || match3("tsch\u00fcss", s); |
77 | |
78 | thread { |
79 | // get answer |
80 | |
81 | history.add(s); |
82 | |
83 | S answer; |
84 | try { |
85 | answer = goodbye ? "tsch\u00fcss" : answer(s, history); |
86 | } catch (Throwable e) { |
87 | e.printStackTrace(); |
88 | answer = "Fehler"; |
89 | } |
90 | |
91 | if (answer != null) |
92 | say(answer); |
93 | |
94 | androidContext.runOnUiThread(runnable { |
95 | if (goodbye) { |
96 | print("\nGOODBYE!"); |
97 | sr.destroy(); |
98 | } else if (loop) { |
99 | sr.stopListening(); |
100 | recog(); |
101 | } |
102 | }); |
103 | } // end of thread |
104 | } |
105 | |
106 | public void onPartialResults(Bundle partialResults) {} |
107 | public void onEvent(int eventType, Bundle params) {} |
108 | } |
109 | |
110 | static void recog() { |
111 | Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
112 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); |
113 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); |
114 | intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); |
115 | |
116 | intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, extraResults); |
117 | sr.startListening(intent); |
118 | //print("started listening"); |
119 | } |
120 | |
121 | static synchronized S answer(S s, L<S> history) { |
122 | return s; |
123 | } |
124 | |
125 | static void say(S s) { |
126 | androidUnmuteAudio(androidContext); |
127 | File mp3 = new File(getProgramDir(), "bla.mp3"); |
128 | makeSpeechCereproc(s, voice, mp3); |
129 | androidPlayMp3(androidContext, mp3); |
130 | androidMuteAudio(androidContext); |
131 | } |
132 | |
133 | static void partialMute(Activity androidContext) { |
134 | AudioManager amanager = (AudioManager) androidContext.getSystemService(Context.AUDIO_SERVICE); |
135 | amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true); |
136 | amanager.setStreamMute(AudioManager.STREAM_ALARM, true); |
137 | //amanager.setStreamMute(AudioManager.STREAM_MUSIC, true); |
138 | amanager.setStreamMute(AudioManager.STREAM_RING, true); |
139 | amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true); |
140 | } |
141 | } |
Began life as a copy of #1001259
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, ufhdzwgrioxs, vouqrxazstgt
No comments. add comment
Snippet ID: | #1001276 |
Snippet name: | Unterhaltung |
Eternal ID of this version: | #1001276/1 |
Text MD5: | 3bae88c282ae0b0bb2255710f6ac34ae |
Transpilation MD5: | fea884f9516cfc9c341d1cdf16ed18e7 |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-10-06 13:22:38 |
Source code size: | 4239 bytes / 141 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 648 / 728 |
Referenced in: | [show references] |