Search in sources :

Example 1 with Voice

use of org.eclipse.smarthome.core.voice.Voice in project smarthome by eclipse.

the class TTSServiceStub method getAvailableVoices.

@Override
public Set<Voice> getAvailableVoices() {
    availableVoices = new HashSet<Voice>();
    Collection<ServiceReference<Voice>> refs;
    try {
        refs = context.getServiceReferences(Voice.class, null);
        if (refs != null) {
            for (ServiceReference<Voice> ref : refs) {
                Voice service = context.getService(ref);
                if (service.getUID().startsWith(getId())) {
                    availableVoices.add(service);
                }
            }
        }
    } catch (InvalidSyntaxException e) {
        // If the specified filter contains an invalid filter expression that cannot be parsed.
        return null;
    }
    return availableVoices;
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Voice(org.eclipse.smarthome.core.voice.Voice) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with Voice

use of org.eclipse.smarthome.core.voice.Voice 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 3 with Voice

use of org.eclipse.smarthome.core.voice.Voice in project smarthome by eclipse.

the class VoiceManagerImpl method getPreferredVoice.

@Override
public Voice getPreferredVoice(Set<Voice> voices) {
    // Express preferences with a Language Priority List
    Locale locale = localeProvider.getLocale();
    // Get collection of voice locales
    Collection<Locale> locales = new ArrayList<Locale>();
    for (Voice currentVoice : voices) {
        locales.add(currentVoice.getLocale());
    }
    // Determine preferred locale based on RFC 4647
    String ranges = locale.toLanguageTag();
    List<Locale.LanguageRange> languageRanges = Locale.LanguageRange.parse(ranges + "-*");
    Locale preferredLocale = Locale.lookup(languageRanges, locales);
    // As a last resort choose some Locale
    if (preferredLocale == null) {
        preferredLocale = locales.iterator().next();
    }
    // Determine preferred voice
    Voice preferredVoice = null;
    for (Voice currentVoice : voices) {
        if (preferredLocale.equals(currentVoice.getLocale())) {
            preferredVoice = currentVoice;
        }
    }
    assert (preferredVoice != null);
    // Return preferred voice
    return preferredVoice;
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) Voice(org.eclipse.smarthome.core.voice.Voice)

Example 4 with Voice

use of org.eclipse.smarthome.core.voice.Voice 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 Voice

use of org.eclipse.smarthome.core.voice.Voice 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;
}
Also used : STTService(org.eclipse.smarthome.core.voice.STTService) ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) KSService(org.eclipse.smarthome.core.voice.KSService) ArrayList(java.util.ArrayList) HumanLanguageInterpreter(org.eclipse.smarthome.core.voice.text.HumanLanguageInterpreter) TTSService(org.eclipse.smarthome.core.voice.TTSService) ArrayList(java.util.ArrayList) List(java.util.List) Voice(org.eclipse.smarthome.core.voice.Voice)

Aggregations

Voice (org.eclipse.smarthome.core.voice.Voice)7 AudioStream (org.eclipse.smarthome.core.audio.AudioStream)3 TTSException (org.eclipse.smarthome.core.voice.TTSException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AudioFormat (org.eclipse.smarthome.core.audio.AudioFormat)2 UnsupportedAudioFormatException (org.eclipse.smarthome.core.audio.UnsupportedAudioFormatException)2 UnsupportedAudioStreamException (org.eclipse.smarthome.core.audio.UnsupportedAudioStreamException)2 TTSService (org.eclipse.smarthome.core.voice.TTSService)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 ParameterOption (org.eclipse.smarthome.config.core.ParameterOption)1 AudioSink (org.eclipse.smarthome.core.audio.AudioSink)1 PercentType (org.eclipse.smarthome.core.library.types.PercentType)1 KSService (org.eclipse.smarthome.core.voice.KSService)1 STTService (org.eclipse.smarthome.core.voice.STTService)1 HumanLanguageInterpreter (org.eclipse.smarthome.core.voice.text.HumanLanguageInterpreter)1