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.Locale;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.audio.AudioException;
23 import org.openhab.core.audio.AudioFormat;
24 import org.openhab.core.audio.AudioStream;
25 import org.openhab.core.voice.AbstractCachedTTSService;
26 import org.openhab.core.voice.TTSCache;
27 import org.openhab.core.voice.TTSException;
28 import org.openhab.core.voice.TTSService;
29 import org.openhab.core.voice.Voice;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
35 * @author Florian Schmidt - Initial Contribution
37 @Component(service = TTSService.class)
39 public class PicoTTSService extends AbstractCachedTTSService {
42 public PicoTTSService(@Reference TTSCache ttsCache) {
46 private final Set<Voice> voices = Stream
47 .of(new PicoTTSVoice("de-DE"), new PicoTTSVoice("en-US"), new PicoTTSVoice("en-GB"),
48 new PicoTTSVoice("es-ES"), new PicoTTSVoice("fr-FR"), new PicoTTSVoice("it-IT"))
49 .collect(Collectors.toSet());
51 private final Set<AudioFormat> audioFormats = Set
52 .of(new AudioFormat(AudioFormat.CONTAINER_WAVE, AudioFormat.CODEC_PCM_SIGNED, false, 16, null, 16000L));
55 public Set<Voice> getAvailableVoices() {
60 public Set<AudioFormat> getSupportedFormats() {
61 return this.audioFormats;
65 public AudioStream synthesizeForCache(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
67 throw new TTSException("The passed text can not be empty");
70 if (!this.voices.contains(voice)) {
71 throw new TTSException("The passed voice is unsupported");
74 boolean isAudioFormatSupported = this.audioFormats.stream().anyMatch(audioFormat -> {
75 return audioFormat.isCompatible(requestedFormat);
78 if (!isAudioFormatSupported) {
79 throw new TTSException("The passed AudioFormat is unsupported");
83 return new PicoTTSAudioStream(text, voice, requestedFormat);
84 } catch (AudioException e) {
85 throw new TTSException(e);
90 public String getId() {
95 public String getLabel(@Nullable Locale locale) {