2 * Copyright (c) 2010-2022 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.voicerss.internal;
16 import java.io.IOException;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.Locale;
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;
39 * This is a TTS service implementation for using VoiceRSS TTS service.
41 * @author Jochen Hiller - Initial contribution and API
42 * @author Laurent Garnier - add support for OGG and AAC audio formats
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 {
48 /** Cache folder name is below userdata/voicerss/cache. */
49 private static final String CACHE_FOLDER_NAME = "voicerss" + File.separator + "cache";
51 // API Key comes from ConfigAdmin
52 private static final String CONFIG_API_KEY = "apiKey";
55 * Map from openHAB AudioFormat Codec to VoiceRSS API Audio Codec
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");
62 * Map from openHAB AudioFormat Frequency to VoiceRSS API Audio Frequency
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");
67 private String apiKey;
69 private final Logger logger = LoggerFactory.getLogger(VoiceRSSTTSService.class);
72 * We need the cached implementation to allow for FixedLengthAudioStream.
74 private CachedVoiceRSSCloudImpl voiceRssImpl;
77 * Set of supported voices
79 private Set<Voice> voices;
82 * Set of supported audio formats
84 private Set<AudioFormat> audioFormats;
87 * DS activate, with access to ConfigAdmin
89 protected void activate(Map<String, Object> config) {
92 voiceRssImpl = initVoiceImplementation();
93 voices = initVoices();
94 audioFormats = initAudioFormats();
96 logger.debug("Using VoiceRSS cache folder {}", getCacheFolderName());
97 } catch (IllegalStateException e) {
98 logger.error("Failed to activate VoiceRSS: {}", e.getMessage(), e);
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;
110 public Set<Voice> getAvailableVoices() {
111 return Collections.unmodifiableSet(voices);
115 public Set<AudioFormat> getSupportedFormats() {
116 return Collections.unmodifiableSet(audioFormats);
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");
126 // Validate arguments
128 throw new TTSException("The passed text is null");
131 String trimmedText = text.trim();
132 if (trimmedText.isEmpty()) {
133 throw new TTSException("The passed text is empty");
135 if (!voices.contains(voice)) {
136 throw new TTSException("The passed voice is unsupported");
139 // now create the input stream for given text, locale, voice, codec and format.
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");
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);
156 * Initializes voices.
158 * @return The voices of this instance
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));
171 * Initializes audioFormats
173 * @return The audio formats of this instance
175 private Set<AudioFormat> initAudioFormats() {
176 return voiceRssImpl.getAvailableAudioFormats();
180 * Map {@link AudioFormat#getCodec() codec} to VoiceRSS API codec.
182 * @throws TTSException if {@code format} is not supported
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);
188 if (apiCodec == null) {
189 throw new TTSException("Unsupported audio format: " + format);
196 * Map {@link AudioFormat#getBitDepth() bit depth} and {@link AudioFormat#getFrequency() frequency} to VoiceRSS API
199 * @throws TTSException if {@code format} is not supported
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);
206 if (apiFrequency == null || (bitDepth != 8 && bitDepth != 16)) {
207 throw new TTSException("Unsupported audio format: " + format);
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";
222 throw new TTSException("Unsupported audio format: " + format);
226 private CachedVoiceRSSCloudImpl initVoiceImplementation() {
227 return new CachedVoiceRSSCloudImpl(getCacheFolderName());
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;
236 public String getId() {
241 public String getLabel(Locale locale) {