Commit b5df9de2 authored by Nicolas Lenz's avatar Nicolas Lenz

Change repriming handling

parent 54068f69
......@@ -53,13 +53,14 @@ public class SpeechEngine extends UtteranceProgressListener implements Recogniti
tts.setLanguage(new Locale("en"));
}
});
/*
this.ttsListen = new TextToSpeech(this.context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
ttsListen.setLanguage(new Locale("en"));
}
});
this.ttsListen.setOnUtteranceProgressListener(this);
});*/
this.tts.setOnUtteranceProgressListener(this);
this.recognizer = SpeechRecognizer.createSpeechRecognizer(this.context);
this.recognizer.setRecognitionListener(this);
......@@ -124,12 +125,26 @@ public class SpeechEngine extends UtteranceProgressListener implements Recogniti
}
}
/**
* Outputs some speech.
*/
public void speak(String string) {
tts.speak(string, TextToSpeech.QUEUE_ADD, null, UUID.randomUUID().toString());
tts.speak(string, TextToSpeech.QUEUE_ADD, null, "N" + UUID.randomUUID().toString());
}
/**
* Outputs some speech and then reprimes the speech recognition.
*/
public void speakAndPrime(String string) {
ttsListen.speak(string, TextToSpeech.QUEUE_ADD, null, UUID.randomUUID().toString());
tts.speak(string, TextToSpeech.QUEUE_ADD, null, "P" + UUID.randomUUID().toString());
}
/**
* Outputs some speech and then primes the hotword recognition.
*/
public void speakAndSleep(String string) {
tts.speak(string, TextToSpeech.QUEUE_ADD, null, "S" + UUID.randomUUID().toString());
}
/**
......@@ -213,13 +228,15 @@ public class SpeechEngine extends UtteranceProgressListener implements Recogniti
* Called on successful end of a TTS (speakAndPrime).
*/
public void onDone(String utteranceId) {
context.runOnUiThread(new Runnable() {
public void run() {
if(handler.getState().getStateType().equals(SpeechEngineState.StateType.NONE)) {
start();
} else {
recognizer.startListening(recognitionIntent);
}
context.runOnUiThread(() -> {
if(utteranceId.charAt(0) == 'N') {
// N is set by speak, so nothing needs to be done
} else if(utteranceId.charAt(0) == 'P') {
// P is set by speakAndPrime, so recognition is turned on
recognizer.startListening(recognitionIntent);
} else if(utteranceId.charAt(0) == 'S') {
// S is set by speakAndSleep, so hotword detection is turned on
start();
}
});
}
......@@ -229,10 +246,16 @@ public class SpeechEngine extends UtteranceProgressListener implements Recogniti
*/
public void onError(String utteranceId) {
Log.e(context.getPackageName(), "TTS error");
if(handler.getState().getStateType().equals(SpeechEngineState.StateType.NONE)) {
start();
} else {
recognizer.startListening(recognitionIntent);
}
context.runOnUiThread(() -> {
if(utteranceId.charAt(0) == 'N') {
// N is set by speak, so nothing needs to be done
} else if(utteranceId.charAt(0) == 'P') {
// P is set by speakAndPrime, so recognition is turned on
recognizer.startListening(recognitionIntent);
} else if(utteranceId.charAt(0) == 'S') {
// S is set by speakAndSleep, so hotword detection is turned on
start();
}
});
}
}
......@@ -33,7 +33,7 @@ public class SpeechHandler {
// If the speech contains "cancel", reset without doing anything else.
if(SpeechParser.isCancel(strings)) {
currentState.reset();
engine.speakAndPrime("Cancelled.");
engine.speakAndSleep("Cancelled.");
return;
}
......@@ -51,7 +51,7 @@ public class SpeechHandler {
case REPEATING: handleRepeating(strings); break;
default:
currentState.reset();
engine.speakAndPrime("Error, try again.");
engine.speakAndSleep("Error: Invalid state.");
break;
}
}
......@@ -60,7 +60,7 @@ public class SpeechHandler {
// Currently no command already running, so parse input as new command
Optional<SpeechParser.Command> command = SpeechParser.parseCommand(strings);
if(!command.isPresent()) {
engine.speakAndPrime("Command not understood.");
engine.speakAndPrime("Command not understood. Try again.");
} else {
switch(command.get()) {
case PICK:
......@@ -93,7 +93,7 @@ public class SpeechHandler {
Optional<Long> container = SpeechParser.parseContainer(strings);
if(container.isPresent()) {
currentState.setContainer(container);
engine.speakAndPrime(String.format("Okay, container %s. Location to pick from?", spellNumber(container.get())));
engine.speakAndPrime(String.format("Okay, %s. Location to pick from?", spellNumber(container.get())));
} else {
engine.speakAndPrime("Container number not understood. Try again.");
}
......@@ -102,7 +102,7 @@ public class SpeechHandler {
Optional<String> location = SpeechParser.parseLocation(strings);
if(location.isPresent()) {
currentState.setLocation(location);
engine.speakAndPrime(String.format("Okay, location %s. Amount of containers?", location.get()));
engine.speakAndPrime(String.format("Okay, %s. Amount of containers?", location.get()));
} else {
engine.speakAndPrime("Location not understood. Try again.");
}
......@@ -130,7 +130,7 @@ public class SpeechHandler {
channel.invokeMethod("pick", args);
currentState.reset();
engine.speakAndPrime("Confirmed.");
engine.speakAndSleep("Confirmed.");
} else {
engine.speakAndPrime("Confirm or cancel not understood. Try again.");
}
......@@ -143,6 +143,7 @@ public class SpeechHandler {
Optional<Long> container = SpeechParser.parseContainer(strings);
if(container.isPresent()) {
currentState.setContainer(container);
engine.speak("Loading...");
channel.invokeMethod("getLocationsByContainer", container.get(), new Result() {
@Override
public void success(Object o) {
......@@ -151,7 +152,7 @@ public class SpeechHandler {
if(locations != null && !locations.isEmpty()) {
currentState.setLocation(Optional.of(locations.get(0)));
currentState.setOtherLocations(locations.subList(1,locations.size())); // All except the first element
engine.speakAndPrime(String.format("Container %d is stored in %s. Say \"Hey Pico, reached\" or \"full\" when arrived.", container.get(), locations.get(0)));
engine.speakAndSleep(String.format("Container %d is stored in %s. Say \"Hey Pico, reached\" or \"full\" when arrived.", container.get(), locations.get(0)));
} else {
engine.speakAndPrime(String.format("Container %d has no locations saved. Choose one.", container.get()));
}
......@@ -160,7 +161,7 @@ public class SpeechHandler {
@Override
public void error(String s, String s1, Object o) {
currentState.reset();
engine.speakAndPrime("Error retreiving locations.");
engine.speakAndSleep("Error: Retreiving locations failed.");
}
@Override
......@@ -196,7 +197,7 @@ public class SpeechHandler {
// Preset location left
currentState.setLocation(Optional.of(otherLocations.get(0)));
currentState.setOtherLocations(otherLocations.subList(1, otherLocations.size())); // All except first
engine.speakAndPrime(String.format("Next location is %s. Say \"Hey Pico, reached\" or \"full\" when arrived.", otherLocations.get(0)));
engine.speakAndSleep(String.format("Next location is %s. Say \"Hey Pico, reached\" or \"full\" when arrived.", otherLocations.get(0)));
} else {
// No preset location left. Ask for one!
currentState.setLocation(Optional.empty());
......@@ -204,12 +205,12 @@ public class SpeechHandler {
}
}
} else {
engine.speakAndPrime("Not understood. Try again.");
engine.speakAndPrime("Reached or full not understood. Try again.");
}
} else {
// Shouldn't happen
currentState.reset();
engine.speakAndPrime("Error.");
engine.speakAndSleep("Error.");
}
}
......@@ -217,7 +218,7 @@ public class SpeechHandler {
if(!currentState.getContainer().isPresent() || !currentState.getLocation().isPresent()) {
// Shouldn't happen!
currentState.reset();
engine.speakAndPrime("Error!");
engine.speakAndSleep("Error: Inconsistent state while putting.");
return;
}
......@@ -244,7 +245,7 @@ public class SpeechHandler {
channel.invokeMethod("putaway", args);
currentState.reset();
engine.speakAndPrime("Confirmed.");
engine.speakAndSleep("Confirmed.");
}
}
}
......@@ -252,7 +253,7 @@ public class SpeechHandler {
private void handleRepeating(List<String> strings) {
if(strings != null && !strings.isEmpty()) {
currentState.reset();
engine.speakAndPrime(strings.get(0));
engine.speakAndSleep(strings.get(0));
} else {
engine.speakAndPrime("Not understood. Try again.");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment