Search in sources :

Example 1 with AudioSink

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

the class AudioManagerImpl method getParameterOptions.

@Override
public Collection<ParameterOption> getParameterOptions(URI uri, String param, Locale locale) {
    if (uri.toString().equals(CONFIG_URI)) {
        if (CONFIG_DEFAULT_SOURCE.equals(param)) {
            List<ParameterOption> options = new ArrayList<>();
            for (AudioSource source : audioSources.values()) {
                ParameterOption option = new ParameterOption(source.getId(), source.getLabel(locale));
                options.add(option);
            }
            return options;
        } else if (CONFIG_DEFAULT_SINK.equals(param)) {
            List<ParameterOption> options = new ArrayList<>();
            for (AudioSink sink : audioSinks.values()) {
                ParameterOption option = new ParameterOption(sink.getId(), sink.getLabel(locale));
                options.add(option);
            }
            return options;
        }
    }
    return null;
}
Also used : AudioSource(org.eclipse.smarthome.core.audio.AudioSource) AudioSink(org.eclipse.smarthome.core.audio.AudioSink) ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with AudioSink

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

the class MediaActionTypeProvider method getSinkOptions.

/**
 * This method creates one option for every sink that is found in the system.
 *
 * @return a list of parameter options representing the audio sinks
 */
private List<ParameterOption> getSinkOptions(Locale locale) {
    List<ParameterOption> options = new ArrayList<>();
    for (String sinkId : audioManager.getSinkIds()) {
        AudioSink sink = audioManager.getSink(sinkId);
        options.add(new ParameterOption(sinkId, sink.getLabel(locale)));
    }
    return options;
}
Also used : AudioSink(org.eclipse.smarthome.core.audio.AudioSink) ParameterOption(org.eclipse.smarthome.config.core.ParameterOption) ArrayList(java.util.ArrayList)

Example 3 with AudioSink

use of org.eclipse.smarthome.core.audio.AudioSink 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);
    }
}
Also used : AudioSource(org.eclipse.smarthome.core.audio.AudioSource) AudioSink(org.eclipse.smarthome.core.audio.AudioSink) Locale(java.util.Locale) STTService(org.eclipse.smarthome.core.voice.STTService) KSService(org.eclipse.smarthome.core.voice.KSService) TTSService(org.eclipse.smarthome.core.voice.TTSService) HumanLanguageInterpreter(org.eclipse.smarthome.core.voice.text.HumanLanguageInterpreter)

Example 4 with AudioSink

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

the class SonosHandlerFactory method createHandler.

@Override
protected ThingHandler createHandler(Thing thing) {
    ThingTypeUID thingTypeUID = thing.getThingTypeUID();
    if (SonosBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
        logger.debug("Creating a ZonePlayerHandler for thing '{}' with UDN '{}'", thing.getUID(), thing.getConfiguration().get(UDN));
        ZonePlayerHandler handler = new ZonePlayerHandler(thing, upnpIOService, opmlUrl);
        // register the speaker as an audio sink
        String callbackUrl = createCallbackUrl();
        SonosAudioSink audioSink = new SonosAudioSink(handler, audioHTTPServer, callbackUrl);
        @SuppressWarnings("unchecked") ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) getBundleContext().registerService(AudioSink.class.getName(), audioSink, new Hashtable<String, Object>());
        audioSinkRegistrations.put(thing.getUID().toString(), reg);
        return handler;
    }
    return null;
}
Also used : AudioSink(org.eclipse.smarthome.core.audio.AudioSink) ZonePlayerHandler(org.eclipse.smarthome.binding.sonos.internal.handler.ZonePlayerHandler) Hashtable(java.util.Hashtable) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 5 with AudioSink

use of org.eclipse.smarthome.core.audio.AudioSink 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)

Aggregations

AudioSink (org.eclipse.smarthome.core.audio.AudioSink)5 ArrayList (java.util.ArrayList)2 ParameterOption (org.eclipse.smarthome.config.core.ParameterOption)2 AudioSource (org.eclipse.smarthome.core.audio.AudioSource)2 TTSService (org.eclipse.smarthome.core.voice.TTSService)2 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Locale (java.util.Locale)1 ZonePlayerHandler (org.eclipse.smarthome.binding.sonos.internal.handler.ZonePlayerHandler)1 AudioFormat (org.eclipse.smarthome.core.audio.AudioFormat)1 AudioStream (org.eclipse.smarthome.core.audio.AudioStream)1 UnsupportedAudioFormatException (org.eclipse.smarthome.core.audio.UnsupportedAudioFormatException)1 UnsupportedAudioStreamException (org.eclipse.smarthome.core.audio.UnsupportedAudioStreamException)1 PercentType (org.eclipse.smarthome.core.library.types.PercentType)1 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)1 KSService (org.eclipse.smarthome.core.voice.KSService)1 STTService (org.eclipse.smarthome.core.voice.STTService)1 TTSException (org.eclipse.smarthome.core.voice.TTSException)1 Voice (org.eclipse.smarthome.core.voice.Voice)1 HumanLanguageInterpreter (org.eclipse.smarthome.core.voice.text.HumanLanguageInterpreter)1