Search in sources :

Example 1 with AudioPlayer

use of marytts.util.data.audio.AudioPlayer in project openhab1-addons by openhab.

the class TTSServiceMaryTTS method say.

/**
     * {@inheritDoc}
     */
public void say(String text, String voiceName, String outputDevice) {
    if (marytts == null) {
        logger.error("Mary TTS is not available");
        return;
    }
    if (text == null) {
        return;
    }
    Voice voice = null;
    if (StringUtils.isBlank(voiceName)) {
        logger.debug("Mary TTS: {} (Voice not set. Using default voice {}).", new String[] { text, defaultVoice.toString() });
        voice = defaultVoice;
    } else {
        voice = Voice.getVoice(voiceName);
        logger.debug("Mary TTS: {} (Voice: {})", new String[] { text, voiceName });
    }
    if (voice != null) {
        // Workaround: we have to set the Locale first, because only in the LocalMaryInterface.setLocale() method
        // the required private method
        // LocalMaryInterface.setAudioFileFormatForVoice() method is called. After that we can set the voice,
        // otherwise an NPE occurs
        marytts.setLocale(voice.getLocale());
        marytts.setVoice(voice.getName());
        try {
            AudioInputStream audio = marytts.generateAudio(text);
            AudioPlayer player = new AudioPlayer(audio);
            player.start();
            player.join();
        } catch (SynthesisException e) {
            logger.error("Error during tts generation: {}", e.getLocalizedMessage(), e);
        } catch (InterruptedException e) {
            logger.error("Error during tts playback: {}", e.getLocalizedMessage(), e);
        }
    } else {
        logger.error("Could not find voice: {}", voiceName);
        logger.info("Available Voices are {} ", StringUtils.join(marytts.getAvailableVoices(), ", "));
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioPlayer(marytts.util.data.audio.AudioPlayer) Voice(marytts.modules.synthesis.Voice) SynthesisException(marytts.exceptions.SynthesisException)

Aggregations

AudioInputStream (javax.sound.sampled.AudioInputStream)1 SynthesisException (marytts.exceptions.SynthesisException)1 Voice (marytts.modules.synthesis.Voice)1 AudioPlayer (marytts.util.data.audio.AudioPlayer)1