2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.voice.picotts.internal;
15 import java.util.Collections;
16 import java.util.Locale;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.audio.AudioException;
24 import org.openhab.core.audio.AudioFormat;
25 import org.openhab.core.audio.AudioStream;
26 import org.openhab.core.voice.AbstractCachedTTSService;
27 import org.openhab.core.voice.TTSCache;
28 import org.openhab.core.voice.TTSException;
29 import org.openhab.core.voice.TTSService;
30 import org.openhab.core.voice.Voice;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
36 * @author Florian Schmidt - Initial Contribution
38 @Component(service = TTSService.class)
40 public class PicoTTSService extends AbstractCachedTTSService {
43 public PicoTTSService(@Reference TTSCache ttsCache) {
47 private final Set<Voice> voices = Stream
48 .of(new PicoTTSVoice("de-DE"), new PicoTTSVoice("en-US"), new PicoTTSVoice("en-GB"),
49 new PicoTTSVoice("es-ES"), new PicoTTSVoice("fr-FR"), new PicoTTSVoice("it-IT"))
50 .collect(Collectors.toSet());
52 private final Set<AudioFormat> audioFormats = Collections.singleton(
53 new AudioFormat(AudioFormat.CONTAINER_WAVE, AudioFormat.CODEC_PCM_SIGNED, false, 16, null, 16000L));
56 public Set<Voice> getAvailableVoices() {
61 public Set<AudioFormat> getSupportedFormats() {
62 return this.audioFormats;
66 public AudioStream synthesizeForCache(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
68 throw new TTSException("The passed text can not be empty");
71 if (!this.voices.contains(voice)) {
72 throw new TTSException("The passed voice is unsupported");
75 boolean isAudioFormatSupported = this.audioFormats.stream().anyMatch(audioFormat -> {
76 return audioFormat.isCompatible(requestedFormat);
79 if (!isAudioFormatSupported) {
80 throw new TTSException("The passed AudioFormat is unsupported");
84 return new PicoTTSAudioStream(text, voice, requestedFormat);
85 } catch (AudioException e) {
86 throw new TTSException(e);
91 public String getId() {
96 public String getLabel(@Nullable Locale locale) {