-
Notifications
You must be signed in to change notification settings - Fork 226
/
WordActivator.java
210 lines (187 loc) · 5.62 KB
/
WordActivator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright 2012 Greg Milette and Adam Stroud
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package root.gast.speech.activation;
import java.util.List;
import root.gast.speech.SpeechRecognitionUtil;
import root.gast.speech.text.WordList;
import root.gast.speech.text.match.SoundsLikeWordMatcher;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
/**
* Uses direct speech recognition to activate when the user speaks
* one of the target words
* @author Greg Milette <<a
* href="mailto:gregorym@gmail.com">gregorym@gmail.com</a>>
*/
public class WordActivator implements SpeechActivator, RecognitionListener
{
private static final String TAG = "WordActivator";
private Context context;
private SpeechRecognizer recognizer;
private SoundsLikeWordMatcher matcher;
private SpeechActivationListener resultListener;
public WordActivator(Context context,
SpeechActivationListener resultListener, String... targetWords)
{
this.context = context;
this.matcher = new SoundsLikeWordMatcher(targetWords);
this.resultListener = resultListener;
}
@Override
public void detectActivation()
{
recognizeSpeechDirectly();
}
private void recognizeSpeechDirectly()
{
Intent recognizerIntent =
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
// accept partial results if they come
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
SpeechRecognitionUtil.recognizeSpeechDirectly(context,
recognizerIntent, this, getSpeechRecognizer());
}
public void stop()
{
if (getSpeechRecognizer() != null)
{
getSpeechRecognizer().stopListening();
getSpeechRecognizer().cancel();
getSpeechRecognizer().destroy();
}
}
@Override
public void onResults(Bundle results)
{
Log.d(TAG, "full results");
receiveResults(results);
}
@Override
public void onPartialResults(Bundle partialResults)
{
Log.d(TAG, "partial results");
receiveResults(partialResults);
}
/**
* common method to process any results bundle from {@link SpeechRecognizer}
*/
private void receiveResults(Bundle results)
{
if ((results != null)
&& results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION))
{
List<String> heard =
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
float[] scores =
results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
receiveWhatWasHeard(heard, scores);
}
else
{
Log.d(TAG, "no results");
}
}
private void receiveWhatWasHeard(List<String> heard, float[] scores)
{
boolean heardTargetWord = false;
// find the target word
for (String possible : heard)
{
WordList wordList = new WordList(possible);
if (matcher.isIn(wordList.getWords()))
{
Log.d(TAG, "HEARD IT!");
heardTargetWord = true;
break;
}
}
if (heardTargetWord)
{
stop();
resultListener.activated(true);
}
else
{
// keep going
recognizeSpeechDirectly();
}
}
@Override
public void onError(int errorCode)
{
if ((errorCode == SpeechRecognizer.ERROR_NO_MATCH)
|| (errorCode == SpeechRecognizer.ERROR_SPEECH_TIMEOUT))
{
Log.d(TAG, "didn't recognize anything");
// keep going
recognizeSpeechDirectly();
}
else
{
Log.d(TAG,
"FAILED "
+ SpeechRecognitionUtil
.diagnoseErrorCode(errorCode));
}
}
/**
* lazy initialize the speech recognizer
*/
private SpeechRecognizer getSpeechRecognizer()
{
if (recognizer == null)
{
recognizer = SpeechRecognizer.createSpeechRecognizer(context);
}
return recognizer;
}
// other unused methods from RecognitionListener...
@Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "ready for speech " + params);
}
@Override
public void onEndOfSpeech()
{
}
/**
* @see android.speech.RecognitionListener#onBeginningOfSpeech()
*/
@Override
public void onBeginningOfSpeech()
{
}
@Override
public void onBufferReceived(byte[] buffer)
{
}
@Override
public void onRmsChanged(float rmsdB)
{
}
@Override
public void onEvent(int eventType, Bundle params)
{
}
}