Skip to content

Commit 24bcbcc

Browse files
committed
Applied some documentation within the code.
1 parent 5c729c7 commit 24bcbcc

32 files changed

+243
-113
lines changed

app/src/main/java/com/chattylabs/demo/voice/CustomConversationActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ private void listen(int index) {
158158
}
159159
}
160160
}
161-
synthesizer.releaseCurrentQueue();
161+
synthesizer.freeCurrentQueue();
162162
if (synthesizer.isEmpty()) {
163163
component.shutdown();
164164
} else synthesizer.resume();
165165
}, (RecognizerListener.OnError) (i, i1) -> {
166166
Log.e(TAG, "Error " + i);
167167

168-
synthesizer.releaseCurrentQueue();
168+
synthesizer.freeCurrentQueue();
169169
if (synthesizer.isEmpty()) {
170170
component.shutdown();
171171
} else synthesizer.resume();

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ public final class AndroidSpeechRecognizer extends BaseSpeechRecognizer {
4242
ComponentConfig configuration,
4343
AndroidAudioManager audioManager,
4444
BluetoothSco bluetoothSco,
45-
Creator<SpeechRecognizer>
46-
recognizerCreator,
4745
ILogger logger) {
48-
super(configuration, bluetoothSco, audioManager, logger);
46+
super(configuration, audioManager, bluetoothSco, logger);
4947
this.application = application;
5048
this.release();
5149
this.mainHandler = new AndroidHandlerImpl(Looper.getMainLooper());
@@ -54,7 +52,7 @@ public final class AndroidSpeechRecognizer extends BaseSpeechRecognizer {
5452
this.speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, application.getPackageName());
5553
this.speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
5654
this.speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 5000L);
57-
this.recognizerCreator = recognizerCreator;
55+
this.recognizerCreator = () -> SpeechRecognizer.createSpeechRecognizer(application);
5856
}
5957

6058
private final AndroidSpeechRecognitionAdapter listener = new AndroidSpeechRecognitionAdapter() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public final class GoogleSpeechRecognizer extends BaseSpeechRecognizer {
5959
AndroidAudioManager audioManager,
6060
BluetoothSco bluetoothSco,
6161
ILogger logger) {
62-
super(configuration, bluetoothSco, audioManager, logger);
62+
super(configuration, audioManager, bluetoothSco, logger);
6363
this.application = application;
6464
this.release();
6565
this.serialThread = ThreadUtils.newSerialThread();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.chattylabs.sdk.android.voice;
22

3-
import static com.chattylabs.sdk.android.voice.ConversationalFlowComponent.*;
43

54
abstract class GoogleSpeechSynthesizerAdapter implements SynthesizerUtteranceListener {
65
private SynthesizerListener.OnStart onStartedListener;

sdk-conversational-flow-core/src/main/java/com/chattylabs/sdk/android/voice/AndroidAudioManager.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public class AndroidAudioManager {
1212
private static final String TAG = Tag.make("AndroidAudioHandler");
1313

1414
// States
15-
private boolean requestAudioFocusMayDuck; // released
16-
private boolean requestAudioFocusExclusive; // released
17-
private boolean requestAudioExclusive; // released
18-
private int audioMode = AudioManager.MODE_CURRENT; // released
15+
private boolean requestAudioFocusMayDuck;
16+
private boolean requestAudioFocusExclusive;
17+
private boolean requestAudioExclusive;
18+
private int audioMode = AudioManager.MODE_CURRENT;
1919

2020
private int dtmfVolume;
2121
private int systemVolume;
@@ -138,9 +138,9 @@ public void setAudioMode() {
138138
AudioManager.MODE_IN_CALL :
139139
AudioManager.MODE_NORMAL);
140140

141-
// Enabling this option, the audio is not rooted to the speakers if the sco is activated
142-
// Meaning that we can force bluetooth sco even with speakers connected
143-
// Nice to have feature!
141+
// By enabling this option, the audio is not rooted to the speakers if the sco is activated
142+
// meaning that we can force bluetooth sco even with speakers connected
143+
// TODO: Nice to have feature!
144144
//speakerphoneOn = audioManager.isSpeakerphoneOn();
145145
//boolean isHeadsetConnected = peripheral.get(Peripheral.Type.HEADSET).isConnected();
146146
//if (!isHeadsetConnected) { audioManager.setSpeakerphoneOn(!isBluetoothScoRequired()); }
@@ -150,13 +150,13 @@ public void setAudioMode() {
150150
public void unsetAudioMode() {
151151
audioManager.setMode(audioMode);
152152

153-
// Enabling this option, the audio is not rooted to the speakers if the sco is activated
154-
// Meaning that we can force bluetooth sco even with speakers connected
155-
// Nice to have feature!
153+
// By enabling this option, the audio is not rooted to the speakers if the sco is activated
154+
// meaning that we can force bluetooth sco even with speakers connected
155+
// TODO: Nice to have feature!
156156
//audioManager.setSpeakerphoneOn(speakerphoneOn);
157157
}
158158

159-
private void adjustVolumeForBeep() {
159+
private void setStreamToMaxVolume() {
160160
// Volume
161161
dtmfVolume = audioManager.getStreamVolume(AudioManager.STREAM_DTMF);
162162
audioManager.setStreamVolume(AudioManager.STREAM_DTMF, audioManager.getStreamMaxVolume(AudioManager.STREAM_DTMF), 0);
@@ -174,7 +174,7 @@ private void adjustVolumeForBeep() {
174174
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0);
175175
}
176176

177-
private void resetVolumeForBeep() {
177+
private void resetVolumeToDefaultValues() {
178178
// Volume
179179
audioManager.setStreamVolume(AudioManager.STREAM_DTMF, dtmfVolume, 0);
180180
audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, systemVolume, 0);

sdk-conversational-flow-core/src/main/java/com/chattylabs/sdk/android/voice/AndroidAudioRecorder.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class AndroidAudioRecorder {
4343
private static final int NO_SPEECH_TIMEOUT_MILLIS = 4000;
4444
private static final int MAX_SPEECH_LENGTH_MILLIS = 30 * 1000;
4545

46-
public static abstract class Callback {
46+
public abstract static class Callback {
4747

4848
/**
4949
* Called when the recorder starts hearing voice.
@@ -201,8 +201,14 @@ private AudioRecord createAudioRecord() {
201201
}
202202

203203
/**
204-
* Continuously processes the captured audio and notifies {@link #mCallback} of corresponding
204+
* Continuously processes the captured audio and notifies {@link Callback} of corresponding
205205
* events.
206+
* <p/>
207+
* If no sound is recorder for more than the value of {@link #setNoSpeechTimeout(int)} it notifies
208+
* {@link Callback#onVoiceError(int)} with {@link SpeechRecognizer#ERROR_SPEECH_TIMEOUT}.
209+
* <p/>
210+
* If sound is recorder for more than the value of {@link #setMaxSpeechLength(int)} it notifies
211+
* {@link Callback#onVoiceEnd()}.
206212
*/
207213
private class ProcessVoice implements Runnable {
208214

sdk-conversational-flow-core/src/main/java/com/chattylabs/sdk/android/voice/BaseSpeechRecognizer.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44

55
import com.chattylabs.sdk.android.common.internal.ILogger;
66

7+
/**
8+
* When implementing from this class you must create a constructor that receives the following parameters
9+
* in the same order:
10+
* <p/>
11+
* <pre>{@code
12+
* public Constructor(Application, ComponentConfig, AndroidAudioManager, BluetoothSco, ILogger) {
13+
* super(ComponentConfig, AndroidAudioManager, BluetoothSco, ILogger);
14+
* //...
15+
* }
16+
* }</pre>
17+
* Otherwise the addon initialization will throw and Exception on runtime.
18+
*
19+
* @see SpeechRecognizerComponent
20+
* @see android.app.Application
21+
* @see ComponentConfig
22+
* @see AndroidAudioManager
23+
* @see BluetoothSco
24+
* @see ILogger
25+
*/
726
abstract class BaseSpeechRecognizer implements SpeechRecognizerComponent {
827
// Minimum constants
928
int MIN_VOICE_RECOGNITION_TIME_LISTENING = 2000;
@@ -17,9 +36,9 @@ abstract class BaseSpeechRecognizer implements SpeechRecognizerComponent {
1736
protected final ILogger logger;
1837

1938
BaseSpeechRecognizer(ComponentConfig configuration,
20-
BluetoothSco bluetoothSco,
21-
AndroidAudioManager audioManager,
22-
ILogger logger) {
39+
AndroidAudioManager audioManager,
40+
BluetoothSco bluetoothSco,
41+
ILogger logger) {
2342
this.configuration = configuration;
2443
this.bluetoothSco = bluetoothSco;
2544
this.audioManager = audioManager;
@@ -47,6 +66,10 @@ void abandonAudioFocus() {
4766
audioManager.abandonAudioFocus();
4867
}
4968

69+
/**
70+
* Setting this option will forward a flag onto the {@link RecognizerUtteranceListener} implementation.
71+
* <br/>The developer can then opt to record again the user voice as a second chance if there is no match.
72+
*/
5073
public void setTryAgain(boolean tryAgain) {
5174
getRecognitionListener().setTryAgain(tryAgain);
5275
}
@@ -83,7 +106,7 @@ public void shutdown() {
83106
@CallSuper
84107
@Override
85108
public void release() {
86-
109+
// No resources to release at this level
87110
}
88111

89112
private void handleListeners(RecognizerListener... listeners) {

sdk-conversational-flow-core/src/main/java/com/chattylabs/sdk/android/voice/BaseSpeechSynthesizer.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,29 @@
1818

1919
import static com.chattylabs.sdk.android.voice.ConversationalFlowComponent.TAG;
2020

21+
/**
22+
* When implementing from this class you must create a constructor that receives the following parameters
23+
* in the same order:
24+
* <p/>
25+
* <pre>{@code
26+
* public Constructor(Application, ComponentConfig, AndroidAudioManager, BluetoothSco, ILogger) {
27+
* super(ComponentConfig, AndroidAudioManager, BluetoothSco, ILogger);
28+
* //...
29+
* }
30+
* }</pre>
31+
* Otherwise the addon initialization will throw and Exception on runtime.
32+
*
33+
* @see SpeechSynthesizerComponent
34+
* @see android.app.Application
35+
* @see ComponentConfig
36+
* @see AndroidAudioManager
37+
* @see BluetoothSco
38+
* @see ILogger
39+
*/
2140
abstract class BaseSpeechSynthesizer implements SpeechSynthesizerComponent {
2241

23-
String DEFAULT_QUEUE_ID = "default_queue_id";
24-
2542
// Constants
43+
static final String DEFAULT_QUEUE_ID = "com.chattylabs.sdk.android.voice:default_queue_id";
2644
private static final String DEFAULT_UTTERANCE_ID = "u:";
2745
private static final String MAP_UTTERANCE_ID = "utteranceId";
2846
private static final String MAP_SILENCE = "silence";
@@ -37,13 +55,13 @@ abstract class BaseSpeechSynthesizer implements SpeechSynthesizerComponent {
3755
private final Object lock = new Object();
3856

3957
// States
40-
private boolean isReady; // released
41-
private boolean isOnHold; // released
42-
private boolean isSpeaking; // released
58+
private boolean isReady;
59+
private boolean isOnHold;
60+
private boolean isSpeaking;
4361

4462
// Resources
45-
private String queueId = DEFAULT_QUEUE_ID; // released
46-
private SynthesizerUtteranceListener synthesizerUtteranceListener; // released
63+
private String queueId = DEFAULT_QUEUE_ID;
64+
private SynthesizerUtteranceListener synthesizerUtteranceListener;
4765
private String lastQueueId;
4866
private final AndroidAudioManager audioManager;
4967
private final BluetoothSco bluetoothSco;
@@ -282,7 +300,7 @@ public <T extends SynthesizerListener> void playSilence(long durationInMillis,
282300
}
283301

284302
@Override
285-
public void releaseCurrentQueue() {
303+
public void freeCurrentQueue() {
286304
logger.w(TAG, "TTS - isOnHold set to false");
287305
isOnHold = false;
288306
}
@@ -303,7 +321,7 @@ public void stop() {
303321
// Audio focus
304322
audioManager.abandonAudioFocus();
305323
setSpeaking(false);
306-
releaseCurrentQueue();
324+
freeCurrentQueue();
307325
}
308326

309327
@CallSuper
@@ -315,7 +333,7 @@ public void release() {
315333
queue.put(DEFAULT_QUEUE_ID, new ConcurrentLinkedQueue<>());
316334
}
317335
queueId = DEFAULT_QUEUE_ID;
318-
releaseCurrentQueue();
336+
freeCurrentQueue();
319337
setReady(false);
320338
setSpeaking(false);
321339
logger.v(TAG, "TTS - states and resources released");
@@ -346,7 +364,6 @@ public void resume() {
346364
}
347365

348366
private void run() {
349-
// This is useless because it is being called already in onDone and onError
350367
moveToNextQueueIfNeeded();
351368
if (!isEmpty()) {
352369
// Gets and plays the current message in the queue
@@ -387,7 +404,7 @@ private void playTheCurrentQueue(Map<String, Object> map) {
387404
@Override
388405
public void onConnected() {
389406
logger.i(getTag(), "TTS - Sco onConnected");
390-
runMap(map);
407+
chooseItemToPlay(map);
391408
}
392409

393410
@Override
@@ -405,11 +422,11 @@ public void onDisconnected() {
405422
}
406423
else {
407424
logger.v(getTag(), "TTS - bluetooth sco is: %s", (bluetoothSco.isBluetoothScoOn() ? "on" : "off"));
408-
runMap(map);
425+
chooseItemToPlay(map);
409426
}
410427
}
411428

412-
private void runMap(Map<String, Object> map) {
429+
private void chooseItemToPlay(Map<String, Object> map) {
413430
audioManager.requestAudioFocus(configuration.isAudioExclusiveRequiredForSynthesizer());
414431
if (map.containsKey(MAP_MESSAGE)) {
415432
//noinspection unchecked
@@ -420,7 +437,8 @@ private void runMap(Map<String, Object> map) {
420437
else if (map.containsKey(MAP_SILENCE)) {
421438
//noinspection unchecked
422439
playSilence((String) map.get(MAP_UTTERANCE_ID), (long) map.get(MAP_SILENCE));
423-
}
440+
} else
441+
throw new RuntimeException("No message or silence item to play");
424442
}
425443

426444
@Override

sdk-conversational-flow-core/src/main/java/com/chattylabs/sdk/android/voice/BluetoothSco.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public class BluetoothSco {
2020
private final Condition condition = lock.newCondition();
2121

2222
// States
23-
private boolean isScoReceiverRegistered; // released
23+
private boolean isScoReceiverRegistered;
2424
private boolean isBluetoothScoOn;
2525

2626
// Resources
2727
private Application application;
2828
private AndroidAudioManager audioManager;
29-
private BluetoothScoReceiver bluetoothScoReceiver = new BluetoothScoReceiver();
29+
private BluetoothScoReceiver bluetoothScoReceiver;
3030

3131
// Log stuff
3232
private ILogger logger;
@@ -94,7 +94,7 @@ public void stopSco() {
9494
audioManager.stopBluetoothSco();
9595
lock.lock();
9696
try {
97-
condition.await(5, TimeUnit.SECONDS);
97+
condition.await(2, TimeUnit.SECONDS);
9898
} catch (InterruptedException e) {
9999
logger.logException(e);
100100
} finally {

sdk-conversational-flow-core/src/main/java/com/chattylabs/sdk/android/voice/Conversation.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
package com.chattylabs.sdk.android.voice;
22

3-
import android.support.annotation.IntDef;
43
import android.support.annotation.NonNull;
54

6-
import java.lang.annotation.Retention;
7-
import java.lang.annotation.RetentionPolicy;
8-
95
public interface Conversation {
10-
116
int FLAG_ENABLE_ERROR_MESSAGE_ON_LOW_SOUND = 1;
127

13-
@IntDef({FLAG_ENABLE_ERROR_MESSAGE_ON_LOW_SOUND})
14-
@Retention(RetentionPolicy.SOURCE)
15-
@interface Flag {}
16-
17-
void addFlag(@Flag int flag);
8+
void addFlag(@ConversationFlag int flag);
189

19-
void removeFlag(@Flag int flag);
10+
void removeFlag(@ConversationFlag int flag);
2011

21-
boolean hasFlag(@Flag int flag);
12+
boolean hasFlag(@ConversationFlag int flag);
2213

2314
void addNode(@NonNull VoiceNode node);
2415

0 commit comments

Comments
 (0)