Search in sources :

Example 1 with AudioStream

use of org.eclipse.smarthome.core.audio.AudioStream in project smarthome by eclipse.

the class DialogProcessor method say.

/**
 * Says the passed command
 *
 * @param text The text to say
 */
protected void say(String text) {
    try {
        Voice voice = null;
        for (Voice currentVoice : tts.getAvailableVoices()) {
            if (this.locale.getLanguage().equals(currentVoice.getLocale().getLanguage())) {
                voice = currentVoice;
                break;
            }
        }
        if (null == voice) {
            throw new TTSException("Unable to find a suitable voice");
        }
        AudioStream audioStream = tts.synthesize(text, voice, null);
        if (sink.getSupportedStreams().stream().anyMatch(clazz -> clazz.isInstance(audioStream))) {
            try {
                sink.process(audioStream);
            } catch (UnsupportedAudioFormatException | UnsupportedAudioStreamException e) {
                logger.warn("Error saying '{}': {}", text, e.getMessage(), e);
            }
        } else {
            logger.warn("Failed playing audio stream '{}' as audio doesn't support it.", audioStream);
        }
    } catch (TTSException e) {
        logger.error("Error saying '{}': {}", text, e.getMessage());
    }
}
Also used : AudioStream(org.eclipse.smarthome.core.audio.AudioStream) UnsupportedAudioFormatException(org.eclipse.smarthome.core.audio.UnsupportedAudioFormatException) TTSException(org.eclipse.smarthome.core.voice.TTSException) Voice(org.eclipse.smarthome.core.voice.Voice) UnsupportedAudioStreamException(org.eclipse.smarthome.core.audio.UnsupportedAudioStreamException)

Example 2 with AudioStream

use of org.eclipse.smarthome.core.audio.AudioStream in project smarthome by eclipse.

the class AudioManagerImpl method stream.

@Override
public void stream(String url, String sinkId) throws AudioException {
    AudioStream audioStream = url != null ? new URLAudioStream(url) : null;
    play(audioStream, sinkId, null);
}
Also used : AudioStream(org.eclipse.smarthome.core.audio.AudioStream) FileAudioStream(org.eclipse.smarthome.core.audio.FileAudioStream) URLAudioStream(org.eclipse.smarthome.core.audio.URLAudioStream) URLAudioStream(org.eclipse.smarthome.core.audio.URLAudioStream)

Example 3 with AudioStream

use of org.eclipse.smarthome.core.audio.AudioStream in project smarthome by eclipse.

the class AudioServlet method prepareInputStream.

private InputStream prepareInputStream(final String streamId, final HttpServletResponse resp) throws AudioException {
    final AudioStream stream;
    final boolean multiAccess;
    if (oneTimeStreams.containsKey(streamId)) {
        stream = oneTimeStreams.remove(streamId);
        multiAccess = false;
    } else if (multiTimeStreams.containsKey(streamId)) {
        stream = multiTimeStreams.get(streamId);
        multiAccess = true;
    } else {
        return null;
    }
    logger.debug("Stream to serve is {}", streamId);
    // try to set the content-type, if possible
    final String mimeType;
    if (stream.getFormat().getCodec() == AudioFormat.CODEC_MP3) {
        mimeType = "audio/mpeg";
    } else if (stream.getFormat().getContainer() == AudioFormat.CONTAINER_WAVE) {
        mimeType = "audio/wav";
    } else if (stream.getFormat().getContainer() == AudioFormat.CONTAINER_OGG) {
        mimeType = "audio/ogg";
    } else {
        mimeType = null;
    }
    if (mimeType != null) {
        resp.setContentType(mimeType);
    }
    // try to set the content-length, if possible
    if (stream instanceof FixedLengthAudioStream) {
        final Long size = ((FixedLengthAudioStream) stream).length();
        if (size != null) {
            resp.setContentLength(size.intValue());
        }
    }
    if (multiAccess) {
        // we need to care about concurrent access and have a separate stream for each thread
        return ((FixedLengthAudioStream) stream).getClonedStream();
    } else {
        return stream;
    }
}
Also used : FixedLengthAudioStream(org.eclipse.smarthome.core.audio.FixedLengthAudioStream) AudioStream(org.eclipse.smarthome.core.audio.AudioStream) FixedLengthAudioStream(org.eclipse.smarthome.core.audio.FixedLengthAudioStream)

Example 4 with AudioStream

use of org.eclipse.smarthome.core.audio.AudioStream 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);
    }
}
Also used : AudioSink(org.eclipse.smarthome.core.audio.AudioSink) AudioStream(org.eclipse.smarthome.core.audio.AudioStream) UnsupportedAudioFormatException(org.eclipse.smarthome.core.audio.UnsupportedAudioFormatException) TTSService(org.eclipse.smarthome.core.voice.TTSService) TTSException(org.eclipse.smarthome.core.voice.TTSException) PercentType(org.eclipse.smarthome.core.library.types.PercentType) AudioFormat(org.eclipse.smarthome.core.audio.AudioFormat) Voice(org.eclipse.smarthome.core.voice.Voice) UnsupportedAudioStreamException(org.eclipse.smarthome.core.audio.UnsupportedAudioStreamException)

Example 5 with AudioStream

use of org.eclipse.smarthome.core.audio.AudioStream in project smarthome by eclipse.

the class TTSServiceMacOSTest method synthesizeTest.

/**
 * Test TTSServiceMacOS.synthesize(String,Voice,AudioFormat)
 */
@Test
public void synthesizeTest() {
    Assume.assumeTrue("Mac OS X".equals(System.getProperty("os.name")));
    MacTTSService ttsServiceMacOS = new MacTTSService();
    Set<Voice> voices = ttsServiceMacOS.getAvailableVoices();
    Set<AudioFormat> audioFormats = ttsServiceMacOS.getSupportedFormats();
    try (AudioStream audioStream = ttsServiceMacOS.synthesize("Hello", voices.iterator().next(), audioFormats.iterator().next())) {
        Assert.assertNotNull("The test synthesizeTest() created null AudioSource", audioStream);
        Assert.assertNotNull("The test synthesizeTest() created an AudioSource w/o AudioFormat", audioStream.getFormat());
        Assert.assertNotNull("The test synthesizeTest() created an AudioSource w/o InputStream", audioStream);
        Assert.assertTrue("The test synthesizeTest() returned an AudioSource with no data", (-1 != audioStream.read(new byte[2])));
    } catch (TTSException e) {
        Assert.fail("synthesizeTest() failed with TTSException: " + e.getMessage());
    } catch (IOException e) {
        Assert.fail("synthesizeTest() failed with IOException: " + e.getMessage());
    }
}
Also used : AudioStream(org.eclipse.smarthome.core.audio.AudioStream) TTSException(org.eclipse.smarthome.core.voice.TTSException) IOException(java.io.IOException) AudioFormat(org.eclipse.smarthome.core.audio.AudioFormat) Voice(org.eclipse.smarthome.core.voice.Voice) Test(org.junit.Test)

Aggregations

AudioStream (org.eclipse.smarthome.core.audio.AudioStream)5 TTSException (org.eclipse.smarthome.core.voice.TTSException)3 Voice (org.eclipse.smarthome.core.voice.Voice)3 AudioFormat (org.eclipse.smarthome.core.audio.AudioFormat)2 UnsupportedAudioFormatException (org.eclipse.smarthome.core.audio.UnsupportedAudioFormatException)2 UnsupportedAudioStreamException (org.eclipse.smarthome.core.audio.UnsupportedAudioStreamException)2 IOException (java.io.IOException)1 AudioSink (org.eclipse.smarthome.core.audio.AudioSink)1 FileAudioStream (org.eclipse.smarthome.core.audio.FileAudioStream)1 FixedLengthAudioStream (org.eclipse.smarthome.core.audio.FixedLengthAudioStream)1 URLAudioStream (org.eclipse.smarthome.core.audio.URLAudioStream)1 PercentType (org.eclipse.smarthome.core.library.types.PercentType)1 TTSService (org.eclipse.smarthome.core.voice.TTSService)1 Test (org.junit.Test)1