]> git.basschouten.com Git - openhab-addons.git/blob
1d89152ace1da9d0ffca64582c3fcd54ae402731
[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 String apiKey;
68
69     private final Logger logger = LoggerFactory.getLogger(VoiceRSSTTSService.class);
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.error("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             if (cacheAudioFile == null) {
145                 throw new TTSException("Could not read from VoiceRSS service");
146             }
147             return new VoiceRSSAudioStream(cacheAudioFile, requestedFormat);
148         } catch (AudioException ex) {
149             throw new TTSException("Could not create AudioStream: " + ex.getMessage(), ex);
150         } catch (IOException ex) {
151             throw new TTSException("Could not read from VoiceRSS service: " + ex.getMessage(), ex);
152         }
153     }
154
155     /**
156      * Initializes voices.
157      *
158      * @return The voices of this instance
159      */
160     private Set<Voice> initVoices() {
161         Set<Voice> voices = new HashSet<>();
162         for (Locale locale : voiceRssImpl.getAvailableLocales()) {
163             for (String voiceLabel : voiceRssImpl.getAvailableVoices(locale)) {
164                 voices.add(new VoiceRSSVoice(locale, voiceLabel));
165             }
166         }
167         return voices;
168     }
169
170     /**
171      * Initializes audioFormats
172      *
173      * @return The audio formats of this instance
174      */
175     private Set<AudioFormat> initAudioFormats() {
176         return voiceRssImpl.getAvailableAudioFormats();
177     }
178
179     /**
180      * Map {@link AudioFormat#getCodec() codec} to VoiceRSS API codec.
181      *
182      * @throws TTSException if {@code format} is not supported
183      */
184     private String getApiAudioCodec(AudioFormat format) throws TTSException {
185         final String internalCodec = format.getCodec();
186         final String apiCodec = CODEC_MAP.get(internalCodec != null ? internalCodec : AudioFormat.CODEC_PCM_SIGNED);
187
188         if (apiCodec == null) {
189             throw new TTSException("Unsupported audio format: " + format);
190         }
191
192         return apiCodec;
193     }
194
195     /**
196      * Map {@link AudioFormat#getBitDepth() bit depth} and {@link AudioFormat#getFrequency() frequency} to VoiceRSS API
197      * format.
198      *
199      * @throws TTSException if {@code format} is not supported
200      */
201     private String getApiAudioFormat(AudioFormat format) throws TTSException {
202         final int bitDepth = format.getBitDepth() != null ? format.getBitDepth() : 16;
203         final Long frequency = format.getFrequency() != null ? format.getFrequency() : 44_100L;
204         final String apiFrequency = FREQUENCY_MAP.get(frequency);
205
206         if (apiFrequency == null || (bitDepth != 8 && bitDepth != 16)) {
207             throw new TTSException("Unsupported audio format: " + format);
208         }
209
210         switch (format.getCodec() != null ? format.getCodec() : AudioFormat.CODEC_PCM_SIGNED) {
211             case AudioFormat.CODEC_PCM_ALAW:
212                 return "alaw_" + apiFrequency + "_mono";
213             case AudioFormat.CODEC_PCM_ULAW:
214                 return "ulaw_" + apiFrequency + "_mono";
215             case AudioFormat.CODEC_PCM_SIGNED:
216             case AudioFormat.CODEC_PCM_UNSIGNED:
217             case AudioFormat.CODEC_MP3:
218             case AudioFormat.CODEC_VORBIS:
219             case AudioFormat.CODEC_AAC:
220                 return apiFrequency + "_" + bitDepth + "_mono";
221             default:
222                 throw new TTSException("Unsupported audio format: " + format);
223         }
224     }
225
226     private CachedVoiceRSSCloudImpl initVoiceImplementation() {
227         return new CachedVoiceRSSCloudImpl(getCacheFolderName());
228     }
229
230     private String getCacheFolderName() {
231         // we assume that this folder does NOT have a trailing separator
232         return OpenHAB.getUserDataFolder() + File.separator + CACHE_FOLDER_NAME;
233     }
234
235     @Override
236     public String getId() {
237         return "voicerss";
238     }
239
240     @Override
241     public String getLabel(Locale locale) {
242         return "VoiceRSS";
243     }
244 }