]> git.basschouten.com Git - openhab-addons.git/blob
ecb6a2ebb559173c961395d6a230e7a85e7437ef
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.voice.picotts.internal;
14
15 import java.util.Collections;
16 import java.util.Locale;
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
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;
34
35 /**
36  * @author Florian Schmidt - Initial Contribution
37  */
38 @Component(service = TTSService.class)
39 @NonNullByDefault
40 public class PicoTTSService extends AbstractCachedTTSService {
41
42     @Activate
43     public PicoTTSService(@Reference TTSCache ttsCache) {
44         super(ttsCache);
45     }
46
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());
51
52     private final Set<AudioFormat> audioFormats = Collections.singleton(
53             new AudioFormat(AudioFormat.CONTAINER_WAVE, AudioFormat.CODEC_PCM_SIGNED, false, 16, null, 16000L));
54
55     @Override
56     public Set<Voice> getAvailableVoices() {
57         return this.voices;
58     }
59
60     @Override
61     public Set<AudioFormat> getSupportedFormats() {
62         return this.audioFormats;
63     }
64
65     @Override
66     public AudioStream synthesizeForCache(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
67         if (text.isEmpty()) {
68             throw new TTSException("The passed text can not be empty");
69         }
70
71         if (!this.voices.contains(voice)) {
72             throw new TTSException("The passed voice is unsupported");
73         }
74
75         boolean isAudioFormatSupported = this.audioFormats.stream().anyMatch(audioFormat -> {
76             return audioFormat.isCompatible(requestedFormat);
77         });
78
79         if (!isAudioFormatSupported) {
80             throw new TTSException("The passed AudioFormat is unsupported");
81         }
82
83         try {
84             return new PicoTTSAudioStream(text, voice, requestedFormat);
85         } catch (AudioException e) {
86             throw new TTSException(e);
87         }
88     }
89
90     @Override
91     public String getId() {
92         return "picotts";
93     }
94
95     @Override
96     public String getLabel(@Nullable Locale locale) {
97         return "PicoTTS";
98     }
99 }