]> git.basschouten.com Git - openhab-addons.git/blob
2fdae5808475e3cb9b54cab1a4f3dd8be84dd428
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.voicerss.internal;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.openhab.core.OpenHAB;
24 import org.openhab.core.audio.AudioException;
25 import org.openhab.core.audio.AudioFormat;
26 import org.openhab.core.audio.AudioStream;
27 import org.openhab.core.config.core.ConfigurableService;
28 import org.openhab.core.voice.TTSException;
29 import org.openhab.core.voice.TTSService;
30 import org.openhab.core.voice.Voice;
31 import org.openhab.voice.voicerss.internal.cloudapi.CachedVoiceRSSCloudImpl;
32 import org.osgi.framework.Constants;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Modified;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * This is a TTS service implementation for using VoiceRSS TTS service.
40  *
41  * @author Jochen Hiller - Initial contribution and API
42  * @author Laurent Garnier - add support for OGG and AAC audio formats
43  */
44 @Component(configurationPid = "org.openhab.voicerss", property = Constants.SERVICE_PID + "=org.openhab.voicerss")
45 @ConfigurableService(category = "voice", label = "VoiceRSS Text-to-Speech", description_uri = "voice:voicerss")
46 public class VoiceRSSTTSService implements TTSService {
47
48     /** Cache folder name is below userdata/voicerss/cache. */
49     private static final String CACHE_FOLDER_NAME = "voicerss" + File.separator + "cache";
50
51     // API Key comes from ConfigAdmin
52     private static final String CONFIG_API_KEY = "apiKey";
53
54     /**
55      * Map from openHAB AudioFormat Codec to VoiceRSS API Audio Codec
56      */
57     private static final Map<String, String> CODEC_MAP = Map.of(AudioFormat.CODEC_PCM_SIGNED, "WAV",
58             AudioFormat.CODEC_PCM_UNSIGNED, "WAV", AudioFormat.CODEC_PCM_ALAW, "WAV", AudioFormat.CODEC_PCM_ULAW, "WAV",
59             AudioFormat.CODEC_MP3, "MP3", AudioFormat.CODEC_VORBIS, "OGG", AudioFormat.CODEC_AAC, "AAC");
60
61     /**
62      * Map from openHAB AudioFormat Frequency to VoiceRSS API Audio Frequency
63      */
64     private static final Map<Long, String> FREQUENCY_MAP = Map.of(8_000L, "8khz", 11_025L, "11khz", 12_000L, "12khz",
65             16_000L, "16khz", 22_050L, "22khz", 24_000L, "24khz", 32_000L, "32khz", 44_100L, "44khz", 48_000L, "48khz");
66
67     private final Logger logger = LoggerFactory.getLogger(VoiceRSSTTSService.class);
68
69     private String apiKey;
70
71     /**
72      * We need the cached implementation to allow for FixedLengthAudioStream.
73      */
74     private CachedVoiceRSSCloudImpl voiceRssImpl;
75
76     /**
77      * Set of supported voices
78      */
79     private Set<Voice> voices;
80
81     /**
82      * Set of supported audio formats
83      */
84     private Set<AudioFormat> audioFormats;
85
86     /**
87      * DS activate, with access to ConfigAdmin
88      */
89     protected void activate(Map<String, Object> config) {
90         try {
91             modified(config);
92             voiceRssImpl = initVoiceImplementation();
93             voices = initVoices();
94             audioFormats = initAudioFormats();
95
96             logger.debug("Using VoiceRSS cache folder {}", getCacheFolderName());
97         } catch (IllegalStateException e) {
98             logger.warn("Failed to activate VoiceRSS: {}", e.getMessage(), e);
99         }
100     }
101
102     @Modified
103     protected void modified(Map<String, Object> config) {
104         if (config != null) {
105             apiKey = config.containsKey(CONFIG_API_KEY) ? config.get(CONFIG_API_KEY).toString() : null;
106         }
107     }
108
109     @Override
110     public Set<Voice> getAvailableVoices() {
111         return Collections.unmodifiableSet(voices);
112     }
113
114     @Override
115     public Set<AudioFormat> getSupportedFormats() {
116         return Collections.unmodifiableSet(audioFormats);
117     }
118
119     @Override
120     public AudioStream synthesize(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
121         logger.debug("Synthesize '{}' for voice '{}' in format {}", text, voice.getUID(), requestedFormat);
122         // Validate known api key
123         if (apiKey == null) {
124             throw new TTSException("Missing API key, configure it first before using");
125         }
126         // Validate arguments
127         if (text == null) {
128             throw new TTSException("The passed text is null");
129         }
130         // trim text
131         String trimmedText = text.trim();
132         if (trimmedText.isEmpty()) {
133             throw new TTSException("The passed text is empty");
134         }
135         if (!voices.contains(voice)) {
136             throw new TTSException("The passed voice is unsupported");
137         }
138
139         // now create the input stream for given text, locale, voice, codec and format.
140         try {
141             File cacheAudioFile = voiceRssImpl.getTextToSpeechAsFile(apiKey, trimmedText,
142                     voice.getLocale().toLanguageTag(), voice.getLabel(), getApiAudioCodec(requestedFormat),
143                     getApiAudioFormat(requestedFormat));
144             return new VoiceRSSAudioStream(cacheAudioFile, requestedFormat);
145         } catch (AudioException ex) {
146             throw new TTSException("Could not create AudioStream: " + ex.getMessage(), ex);
147         } catch (IOException ex) {
148             throw new TTSException("Could not read from VoiceRSS service: " + ex.getMessage(), ex);
149         }
150     }
151
152     /**
153      * Initializes voices.
154      *
155      * @return The voices of this instance
156      */
157     private Set<Voice> initVoices() {
158         Set<Voice> voices = new HashSet<>();
159         for (Locale locale : voiceRssImpl.getAvailableLocales()) {
160             for (String voiceLabel : voiceRssImpl.getAvailableVoices(locale)) {
161                 voices.add(new VoiceRSSVoice(locale, voiceLabel));
162             }
163         }
164         return voices;
165     }
166
167     /**
168      * Initializes audioFormats
169      *
170      * @return The audio formats of this instance
171      */
172     private Set<AudioFormat> initAudioFormats() {
173         return voiceRssImpl.getAvailableAudioFormats();
174     }
175
176     /**
177      * Map {@link AudioFormat#getCodec() codec} to VoiceRSS API codec.
178      *
179      * @throws TTSException if {@code format} is not supported
180      */
181     private String getApiAudioCodec(AudioFormat format) throws TTSException {
182         final String internalCodec = format.getCodec();
183         final String apiCodec = CODEC_MAP.get(internalCodec != null ? internalCodec : AudioFormat.CODEC_PCM_SIGNED);
184
185         if (apiCodec == null) {
186             throw new TTSException("Unsupported audio format: " + format);
187         }
188
189         return apiCodec;
190     }
191
192     /**
193      * Map {@link AudioFormat#getBitDepth() bit depth} and {@link AudioFormat#getFrequency() frequency} to VoiceRSS API
194      * format.
195      *
196      * @throws TTSException if {@code format} is not supported
197      */
198     private String getApiAudioFormat(AudioFormat format) throws TTSException {
199         final int bitDepth = format.getBitDepth() != null ? format.getBitDepth() : 16;
200         final Long frequency = format.getFrequency() != null ? format.getFrequency() : 44_100L;
201         final String apiFrequency = FREQUENCY_MAP.get(frequency);
202
203         if (apiFrequency == null || (bitDepth != 8 && bitDepth != 16)) {
204             throw new TTSException("Unsupported audio format: " + format);
205         }
206
207         switch (format.getCodec() != null ? format.getCodec() : AudioFormat.CODEC_PCM_SIGNED) {
208             case AudioFormat.CODEC_PCM_ALAW:
209                 return "alaw_" + apiFrequency + "_mono";
210             case AudioFormat.CODEC_PCM_ULAW:
211                 return "ulaw_" + apiFrequency + "_mono";
212             case AudioFormat.CODEC_PCM_SIGNED:
213             case AudioFormat.CODEC_PCM_UNSIGNED:
214             case AudioFormat.CODEC_MP3:
215             case AudioFormat.CODEC_VORBIS:
216             case AudioFormat.CODEC_AAC:
217                 return apiFrequency + "_" + bitDepth + "bit_mono";
218             default:
219                 throw new TTSException("Unsupported audio format: " + format);
220         }
221     }
222
223     private CachedVoiceRSSCloudImpl initVoiceImplementation() throws IllegalStateException {
224         return new CachedVoiceRSSCloudImpl(getCacheFolderName());
225     }
226
227     private String getCacheFolderName() {
228         // we assume that this folder does NOT have a trailing separator
229         return OpenHAB.getUserDataFolder() + File.separator + CACHE_FOLDER_NAME;
230     }
231
232     @Override
233     public String getId() {
234         return "voicerss";
235     }
236
237     @Override
238     public String getLabel(Locale locale) {
239         return "VoiceRSS";
240     }
241 }