From f2d69139e15f6f40f2bca4a5cbfe0924a32a890f Mon Sep 17 00:00:00 2001 From: lolodomo Date: Mon, 2 Sep 2019 17:13:33 +0200 Subject: [PATCH] {voice] say: log something when the audio sink cannot be found (#1018) Signed-off-by: Laurent Garnier --- .../core/voice/internal/VoiceManagerImpl.java | 87 +++++++++---------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/bundles/org.openhab.core.voice/src/main/java/org/eclipse/smarthome/core/voice/internal/VoiceManagerImpl.java b/bundles/org.openhab.core.voice/src/main/java/org/eclipse/smarthome/core/voice/internal/VoiceManagerImpl.java index fb95b3e4b3..9428c3f166 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/eclipse/smarthome/core/voice/internal/VoiceManagerImpl.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/eclipse/smarthome/core/voice/internal/VoiceManagerImpl.java @@ -206,54 +206,53 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider { } Set audioFormats = tts.getSupportedFormats(); AudioSink sink = audioManager.getSink(sinkId); + if (sink == null) { + throw new TTSException("Unable to find the audio sink " + sinkId); + } - if (sink != null) { - AudioFormat audioFormat = getBestMatch(audioFormats, sink.getSupportedFormats()); - if (audioFormat != null) { - AudioStream audioStream = tts.synthesize(text, voice, audioFormat); + AudioFormat audioFormat = getBestMatch(audioFormats, sink.getSupportedFormats()); + if (audioFormat == null) { + throw new TTSException("No compatible audio format found for TTS '" + tts.getId() + "' and sink '" + + sink.getId() + "'"); + } - if (sink.getSupportedStreams().stream().anyMatch(clazz -> clazz.isInstance(audioStream))) { - PercentType oldVolume = null; - try { - // get current volume - oldVolume = audioManager.getVolume(sinkId); - } catch (IOException e) { - logger.debug("An exception occurred while getting the volume of sink '{}' : {}", - sink.getId(), e.getMessage(), e); - } - // set notification sound volume - if (volume != null) { - try { - audioManager.setVolume(volume, sinkId); - } catch (IOException e) { - logger.debug("An exception occurred while setting the volume of sink '{}' : {}", - sink.getId(), e.getMessage(), e); - } - } - try { - sink.process(audioStream); - } catch (UnsupportedAudioFormatException | UnsupportedAudioStreamException e) { - logger.warn("Error saying '{}': {}", text, e.getMessage(), e); - } finally { - if (volume != null && oldVolume != null) { - // restore volume only if it was set before - try { - audioManager.setVolume(oldVolume, sinkId); - } catch (IOException e) { - logger.debug("An exception occurred while setting the volume of sink '{}' : {}", - sink.getId(), e.getMessage(), e); - } - } - } - } 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()); + AudioStream audioStream = tts.synthesize(text, voice, audioFormat); + if (!sink.getSupportedStreams().stream().anyMatch(clazz -> clazz.isInstance(audioStream))) { + throw new TTSException( + "Failed playing audio stream '" + audioStream + "' as audio sink doesn't support it"); + } + + PercentType oldVolume = null; + try { + // get current volume + oldVolume = audioManager.getVolume(sinkId); + } catch (IOException e) { + logger.debug("An exception occurred while getting the volume of sink '{}' : {}", sink.getId(), + e.getMessage(), e); + } + // set notification sound volume + if (volume != null) { + try { + audioManager.setVolume(volume, sinkId); + } catch (IOException e) { + logger.debug("An exception occurred while setting the volume of sink '{}' : {}", sink.getId(), + e.getMessage(), e); } } - } catch (TTSException e) { + try { + sink.process(audioStream); + } finally { + if (volume != null && oldVolume != null) { + // restore volume only if it was set before + try { + audioManager.setVolume(oldVolume, sinkId); + } catch (IOException e) { + logger.debug("An exception occurred while setting the volume of sink '{}' : {}", sink.getId(), + e.getMessage(), e); + } + } + } + } catch (TTSException | UnsupportedAudioFormatException | UnsupportedAudioStreamException e) { logger.warn("Error saying '{}': {}", text, e.getMessage(), e); } }