use of org.eclipse.smarthome.core.voice.TTSService in project smarthome by eclipse.
the class VoiceManagerImpl method startDialog.
@Override
public void startDialog(KSService ksService, STTService sttService, TTSService ttsService, HumanLanguageInterpreter interpreter, AudioSource audioSource, AudioSink audioSink, Locale locale, String keyword, String listeningItem) {
// use defaults, if null
KSService ks = (ksService == null) ? getKS() : ksService;
STTService stt = (sttService == null) ? getSTT() : sttService;
TTSService tts = (ttsService == null) ? getTTS() : ttsService;
HumanLanguageInterpreter hli = (interpreter == null) ? getHLI() : interpreter;
AudioSource source = (audioSource == null) ? audioManager.getSource() : audioSource;
AudioSink sink = (audioSink == null) ? audioManager.getSink() : audioSink;
Locale loc = (locale == null) ? localeProvider.getLocale() : locale;
String kw = (keyword == null) ? this.keyword : keyword;
String item = (listeningItem == null) ? this.listeningItem : listeningItem;
if (ks != null && stt != null && tts != null && hli != null && source != null && sink != null && loc != null && kw != null) {
DialogProcessor processor = new DialogProcessor(ks, stt, tts, hli, source, sink, loc, kw, item, this.eventPublisher);
processor.start();
} else {
String msg = "Cannot start dialog as services are missing.";
logger.error(msg);
throw new IllegalStateException(msg);
}
}
use of org.eclipse.smarthome.core.voice.TTSService in project smarthome by eclipse.
the class VoiceManagerImpl method say.
@Override
public void say(String text, String voiceId, String sinkId, PercentType volume) {
Objects.requireNonNull(text, "Text cannot be said as it is null.");
try {
TTSService tts = null;
Voice voice = null;
String selectedVoiceId = voiceId;
if (selectedVoiceId == null) {
// use the configured default, if set
selectedVoiceId = defaultVoice;
}
if (selectedVoiceId == null) {
tts = getTTS();
if (tts != null) {
voice = getPreferredVoice(tts.getAvailableVoices());
}
} else if (selectedVoiceId.contains(":")) {
// it is a fully qualified unique id
String[] segments = selectedVoiceId.split(":");
tts = getTTS(segments[0]);
if (tts != null) {
voice = getVoice(tts.getAvailableVoices(), segments[1]);
}
} else {
// voiceId is not fully qualified
tts = getTTS();
if (tts != null) {
voice = getVoice(tts.getAvailableVoices(), selectedVoiceId);
}
}
if (tts == null) {
throw new TTSException("No TTS service can be found for voice " + selectedVoiceId);
}
if (voice == null) {
throw new TTSException("Unable to find a voice for language " + localeProvider.getLocale().getLanguage());
}
Set<AudioFormat> audioFormats = tts.getSupportedFormats();
AudioSink sink = audioManager.getSink(sinkId);
if (sink != null) {
AudioFormat audioFormat = getBestMatch(audioFormats, sink.getSupportedFormats());
if (audioFormat != null) {
AudioStream audioStream = tts.synthesize(text, voice, audioFormat);
if (sink.getSupportedStreams().stream().anyMatch(clazz -> clazz.isInstance(audioStream))) {
// get current volume
PercentType oldVolume = audioManager.getVolume(sinkId);
// set notification sound volume
if (volume != null) {
audioManager.setVolume(volume, sinkId);
}
try {
sink.process(audioStream);
} catch (UnsupportedAudioFormatException | UnsupportedAudioStreamException e) {
logger.warn("Error saying '{}': {}", text, e.getMessage(), e);
} finally {
if (volume != null) {
// restore volume only if it was set before
audioManager.setVolume(oldVolume, sinkId);
}
}
} else {
logger.warn("Failed playing audio stream '{}' as audio sink doesn't support it.", audioStream);
}
} else {
logger.warn("No compatible audio format found for TTS '{}' and sink '{}'", tts.getId(), sink.getId());
}
}
} catch (TTSException e) {
logger.warn("Error saying '{}': {}", text, e.getMessage(), e);
}
}
use of org.eclipse.smarthome.core.voice.TTSService in project smarthome by eclipse.
the class VoiceManagerImpl method getParameterOptions.
@Override
public Collection<ParameterOption> getParameterOptions(URI uri, String param, Locale locale) {
if (uri.toString().equals(CONFIG_URI)) {
if (CONFIG_DEFAULT_HLI.equals(param)) {
List<ParameterOption> options = new ArrayList<>();
for (HumanLanguageInterpreter hli : humanLanguageInterpreters.values()) {
ParameterOption option = new ParameterOption(hli.getId(), hli.getLabel(locale));
options.add(option);
}
return options;
} else if (CONFIG_DEFAULT_KS.equals(param)) {
List<ParameterOption> options = new ArrayList<>();
for (KSService ks : ksServices.values()) {
ParameterOption option = new ParameterOption(ks.getId(), ks.getLabel(locale));
options.add(option);
}
return options;
} else if (CONFIG_DEFAULT_STT.equals(param)) {
List<ParameterOption> options = new ArrayList<>();
for (STTService stt : sttServices.values()) {
ParameterOption option = new ParameterOption(stt.getId(), stt.getLabel(locale));
options.add(option);
}
return options;
} else if (CONFIG_DEFAULT_TTS.equals(param)) {
List<ParameterOption> options = new ArrayList<>();
for (TTSService tts : ttsServices.values()) {
ParameterOption option = new ParameterOption(tts.getId(), tts.getLabel(locale));
options.add(option);
}
return options;
} else if (CONFIG_DEFAULT_VOICE.equals(param)) {
List<ParameterOption> options = new ArrayList<>();
for (Voice voice : getAllVoices()) {
ParameterOption option = new ParameterOption(voice.getUID(), voice.getLabel() + " - " + voice.getLocale().getDisplayName());
options.add(option);
}
return options;
}
}
return null;
}
Aggregations