Skip to content

Commit a37c8a9

Browse files
committed
Extracting to BaseSpeechRecognizer.java
1 parent c8852e7 commit a37c8a9

File tree

14 files changed

+247
-214
lines changed

14 files changed

+247
-214
lines changed

sdk-addon-android-speech/src/main/java/com/chattylabs/sdk/android/voice/AndroidSpeechRecognitionAdapter.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import android.os.Bundle;
44
import android.speech.RecognitionListener;
55
import android.support.annotation.CallSuper;
6-
import android.util.Log;
76

8-
import com.chattylabs.sdk.android.common.Tag;
7+
import static com.chattylabs.sdk.android.voice.ConversationalFlowComponent.*;
98

109

11-
abstract class AndroidSpeechRecognitionAdapter implements RecognitionListener {
12-
private static final String TAG = Tag.make("AndroidSpeechRecognitionAdapter");
13-
10+
abstract class AndroidSpeechRecognitionAdapter
11+
implements RecognitionListener, RecognizerUtteranceListener {
1412
// Settings
1513
private static final int MINIMUM_REACHED_LEVEL_INTENTS = 3;
1614
private static final float DEFAULT_THRESHOLD_LOW_SOUND = 5f;
@@ -22,11 +20,11 @@ abstract class AndroidSpeechRecognitionAdapter implements RecognitionListener {
2220
static final int LOW_SOUND = 2;
2321
static final int NORMAL_SOUND = 3;
2422

25-
private ConversationalFlowComponent.OnRecognizerReady onReady;
26-
private ConversationalFlowComponent.OnRecognizerResults onResults;
27-
private ConversationalFlowComponent.OnRecognizerPartialResults onPartialResults;
28-
private ConversationalFlowComponent.OnRecognizerMostConfidentResult onMostConfidentResult;
29-
private ConversationalFlowComponent.OnRecognizerError onError;
23+
private OnRecognizerReady onReady;
24+
private OnRecognizerResults onResults;
25+
private OnRecognizerPartialResults onPartialResults;
26+
private OnRecognizerMostConfidentResult onMostConfidentResult;
27+
private OnRecognizerError onError;
3028

3129
private boolean tryAgain;
3230
private int soundLevel = UNKNOWN;
@@ -36,44 +34,53 @@ abstract class AndroidSpeechRecognitionAdapter implements RecognitionListener {
3634
private int lowSoundIntents;
3735
private int normalSoundIntents;
3836

39-
public void setOnReady(ConversationalFlowComponent.OnRecognizerReady onReady) {
37+
@Override
38+
public void _setOnReady(OnRecognizerReady onReady) {
4039
this.onReady = onReady;
4140
}
4241

43-
public ConversationalFlowComponent.OnRecognizerResults getOnResults() {
42+
@Override
43+
public OnRecognizerResults _getOnResults() {
4444
return onResults;
4545
}
4646

47-
public AndroidSpeechRecognitionAdapter setOnResults(ConversationalFlowComponent.OnRecognizerResults onResults) {
47+
@Override
48+
public AndroidSpeechRecognitionAdapter _setOnResults(OnRecognizerResults onResults) {
4849
this.onResults = onResults;
4950
return this;
5051
}
5152

52-
public ConversationalFlowComponent.OnRecognizerPartialResults getOnPartialResults() {
53+
@Override
54+
public OnRecognizerPartialResults _getOnPartialResults() {
5355
return onPartialResults;
5456
}
5557

56-
public AndroidSpeechRecognitionAdapter setOnPartialResults(
57-
ConversationalFlowComponent.OnRecognizerPartialResults onPartialResults) {
58+
@Override
59+
public AndroidSpeechRecognitionAdapter _setOnPartialResults(
60+
OnRecognizerPartialResults onPartialResults) {
5861
this.onPartialResults = onPartialResults;
5962
return this;
6063
}
6164

62-
public ConversationalFlowComponent.OnRecognizerMostConfidentResult getOnMostConfidentResult() {
65+
@Override
66+
public OnRecognizerMostConfidentResult _getOnMostConfidentResult() {
6367
return onMostConfidentResult;
6468
}
6569

66-
public AndroidSpeechRecognitionAdapter setOnMostConfidentResult(
67-
ConversationalFlowComponent.OnRecognizerMostConfidentResult onMostConfidentResult) {
70+
@Override
71+
public AndroidSpeechRecognitionAdapter _setOnMostConfidentResult(
72+
OnRecognizerMostConfidentResult onMostConfidentResult) {
6873
this.onMostConfidentResult = onMostConfidentResult;
6974
return this;
7075
}
7176

72-
public ConversationalFlowComponent.OnRecognizerError getOnError() {
77+
@Override
78+
public OnRecognizerError _getOnError() {
7379
return onError;
7480
}
7581

76-
public AndroidSpeechRecognitionAdapter setOnError(ConversationalFlowComponent.OnRecognizerError onError) {
82+
@Override
83+
public AndroidSpeechRecognitionAdapter _setOnError(OnRecognizerError onError) {
7784
this.onError = onError;
7885
return this;
7986
}
@@ -109,10 +116,6 @@ public void setSoundLevel(int soundLevel) {
109116
this.soundLevel = soundLevel;
110117
}
111118

112-
public abstract void startTimeout();
113-
114-
public abstract void reset();
115-
116119
String getSoundLevelAsString(int level) {
117120
switch (level) {
118121
case NO_SOUND:
@@ -128,7 +131,6 @@ String getSoundLevelAsString(int level) {
128131

129132
@Override @CallSuper
130133
public void onReadyForSpeech(Bundle params) {
131-
//if (BuildConfig.DEBUG) System.out.println("onReadyForSpeech: " + params);
132134
if (onReady != null) onReady.execute(params);
133135
}
134136

@@ -137,19 +139,19 @@ public void onBeginningOfSpeech() {
137139
}
138140

139141
@Override
140-
public void onRmsChanged(float rmsdB) {
141-
if (rmsdB % 1 == 0) return;
142-
if (rmsDebug) Log.v(TAG, "RECOGNITION - Rms db: " + rmsdB);
142+
public void onRmsChanged(float rmsDb) {
143+
if (rmsDb % 1 == 0) return;
144+
if (rmsDebug) System.out.printf("Rms db: %s", rmsDb);
143145

144-
if (rmsdB <= noSoundThreshold && soundLevel <= NO_SOUND) {
146+
if (rmsDb <= noSoundThreshold && soundLevel <= NO_SOUND) {
145147
soundLevel = NO_SOUND;
146148
// quiet
147-
} else if (rmsdB > noSoundThreshold && rmsdB < lowSoundThreshold && soundLevel <= LOW_SOUND) {
149+
} else if (rmsDb > noSoundThreshold && rmsDb < lowSoundThreshold && soundLevel <= LOW_SOUND) {
148150
lowSoundIntents++;
149151
if (lowSoundIntents > MINIMUM_REACHED_LEVEL_INTENTS)
150152
soundLevel = LOW_SOUND;
151153
// medium
152-
} else if (rmsdB >= lowSoundThreshold && soundLevel <= NORMAL_SOUND) {
154+
} else if (rmsDb >= lowSoundThreshold && soundLevel <= NORMAL_SOUND) {
153155
lowSoundIntents++;
154156
if (lowSoundIntents > MINIMUM_REACHED_LEVEL_INTENTS && soundLevel <= LOW_SOUND)
155157
soundLevel = LOW_SOUND;
@@ -182,6 +184,5 @@ public void onPartialResults(Bundle partialResults) {
182184

183185
@Override
184186
public void onEvent(int eventType, Bundle params) {
185-
//if (BuildConfig.DEBUG) System.out.println("onEvent: " + eventType + " - " + params);
186187
}
187188
}

sdk-addon-android-speech/src/main/java/com/chattylabs/sdk/android/voice/AndroidSpeechRecognizer.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static com.chattylabs.sdk.android.voice.ConversationalFlowComponent.RecognizerListener;
2929
import static com.chattylabs.sdk.android.voice.ConversationalFlowComponent.selectMostConfidentResult;
3030

31-
public final class AndroidSpeechRecognizer implements ConversationalFlowComponent.SpeechRecognizer {
31+
public final class AndroidSpeechRecognizer extends BaseSpeechRecognizer {
3232
private static final String TAG = Tag.make("AndroidSpeechRecognizer");
3333

3434
private final ReentrantLock lock = new ReentrantLock();
@@ -41,8 +41,6 @@ public final class AndroidSpeechRecognizer implements ConversationalFlowComponen
4141
// Resources
4242
private final Application application;
4343
private final AndroidHandler mainHandler;
44-
private final AndroidAudioManager audioManager;
45-
private final BluetoothSco bluetoothSco;
4644
private final Intent speechRecognizerIntent;
4745
private final SpeechRecognizerCreator<android.speech.SpeechRecognizer> recognizerCreator;
4846
private final ExecutorService executorService;
@@ -237,7 +235,8 @@ private boolean needRetry(int error) {
237235
this.recognizerCreator = recognizerCreator;
238236
}
239237

240-
private void startListening() {
238+
@Override
239+
void startListening() {
241240
executorService.submit(() -> {
242241
lock.lock();
243242
try {
@@ -263,29 +262,6 @@ private void startListening() {
263262
});
264263
}
265264

266-
private void handleListeners(RecognizerListener... listeners) {
267-
recognitionListener.reset();
268-
if (listeners != null && listeners.length > 0) {
269-
for (RecognizerListener item : listeners) {
270-
if (item instanceof OnRecognizerReady) {
271-
recognitionListener.setOnReady((OnRecognizerReady) item);
272-
}
273-
else if (item instanceof OnRecognizerResults) {
274-
recognitionListener.setOnResults((OnRecognizerResults) item);
275-
}
276-
else if (item instanceof OnRecognizerMostConfidentResult) {
277-
recognitionListener.setOnMostConfidentResult((OnRecognizerMostConfidentResult) item);
278-
}
279-
else if (item instanceof OnRecognizerPartialResults) {
280-
recognitionListener.setOnPartialResults((OnRecognizerPartialResults) item);
281-
}
282-
else if (item instanceof OnRecognizerError) {
283-
recognitionListener.setOnError((OnRecognizerError) item);
284-
}
285-
}
286-
}
287-
}
288-
289265
public void setTryAgain(boolean tryAgain) {
290266
this.recognitionListener.setTryAgain(tryAgain);
291267
}

sdk-addon-android-speech/src/main/java/com/chattylabs/sdk/android/voice/AndroidSpeechSynthesizer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void setup(OnSynthesizerSetup onSynthesizerSetup) {
8989
}
9090

9191
@Override
92-
UtteranceListener createUtteranceListener(@NonNull SynthesizerListener... listeners) {
92+
SynthesizerUtteranceListener createUtteranceListener(@NonNull SynthesizerListener... listeners) {
9393
return new AndroidSpeechSynthesizerAdapter()
9494
{
9595
@Override
@@ -182,8 +182,8 @@ void initTts(OnSynthesizerInitialised onSynthesizerInitialised) {
182182
onSynthesizerInitialised.execute(status == TextToSpeech.SUCCESS ?
183183
SynthesizerListener.SUCCESS : SynthesizerListener.ERROR);
184184
});
185-
setUtteranceListener(createUtterancesListener());
186-
tts.setOnUtteranceProgressListener((UtteranceProgressListener) getUtteranceListener());
185+
setSynthesizerUtteranceListener(createUtterancesListener());
186+
tts.setOnUtteranceProgressListener((UtteranceProgressListener) getSynthesizerUtteranceListener());
187187
}
188188
else if (isReady()) {
189189
setupLanguage();
@@ -199,7 +199,7 @@ private void setupLanguage() {
199199
logger.logException(ignored); }
200200
}
201201

202-
private UtteranceListener createUtterancesListener() {
202+
private SynthesizerUtteranceListener createUtterancesListener() {
203203
return new AndroidSpeechSynthesizerAdapter() {
204204
private long timestamp;
205205
private TimerTask task;
@@ -248,7 +248,7 @@ public void onStart(String utteranceId) {
248248
timestamp = System.currentTimeMillis();
249249

250250
if (getListenersMap().size() > 0) {
251-
UtteranceListener listener = getListenersMap().get(utteranceId);
251+
SynthesizerUtteranceListener listener = getListenersMap().get(utteranceId);
252252
if (listener != null) {
253253
listener.onStart(utteranceId);
254254
}
@@ -271,7 +271,7 @@ public void onDone(String utteranceId) {
271271
}
272272
setSpeaking(false);
273273
if (getListenersMap().size() > 0) {
274-
UtteranceListener listener = removeListener(utteranceId);
274+
SynthesizerUtteranceListener listener = removeListener(utteranceId);
275275
logger.v(getTag(), "ANDROID TTS[%s] - on done <%s> - execute listener.onDone", utteranceId, getCurrentQueueId());
276276
if (listener != null) {
277277
listener.onDone(utteranceId);
@@ -303,7 +303,7 @@ public void onError(String utteranceId, int errorCode) {
303303
}
304304
setSpeaking(false);
305305
if (getListenersMap().size() > 0 && getListenersMap().containsKey(utteranceId)) {
306-
UtteranceListener listener = removeListener(utteranceId);
306+
SynthesizerUtteranceListener listener = removeListener(utteranceId);
307307
shutdown();
308308
if (listener != null) {
309309
listener.onError(utteranceId, errorCode);
@@ -396,7 +396,7 @@ void playSilence(String utteranceId, long durationInMillis) {
396396
}
397397
else {
398398
logger.e(TAG, "ANDROID TTS[%s] - silence status ERROR", utteranceId);
399-
getUtteranceListener().onError(utteranceId, SynthesizerListener.ERROR);
399+
getSynthesizerUtteranceListener().onError(utteranceId, SynthesizerListener.ERROR);
400400
}
401401
});
402402
}
@@ -414,7 +414,7 @@ private void play(String utteranceId, String text, HashMap<String, String> param
414414
}
415415
else {
416416
logger.e(TAG, "ANDROID TTS[%s] - internal playText status ERROR ", utteranceId);
417-
getUtteranceListener().onError(utteranceId, SynthesizerListener.ERROR);
417+
getSynthesizerUtteranceListener().onError(utteranceId, SynthesizerListener.ERROR);
418418
}
419419
});
420420
}

sdk-addon-android-speech/src/main/java/com/chattylabs/sdk/android/voice/AndroidSpeechSynthesizerAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import static com.chattylabs.sdk.android.voice.ConversationalFlowComponent.*;
66

7-
abstract class AndroidSpeechSynthesizerAdapter extends UtteranceProgressListener implements UtteranceListener {
7+
abstract class AndroidSpeechSynthesizerAdapter extends UtteranceProgressListener
8+
implements SynthesizerUtteranceListener {
89
private OnSynthesizerStart onStartedListener;
910
private OnSynthesizerDone onDoneListener;
1011
private OnSynthesizerError onErrorListener;

0 commit comments

Comments
 (0)