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.voicerss.internal.cloudapi;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.HttpURLConnection;
19 import java.net.URLConnection;
20 import java.net.URLEncoder;
21 import java.nio.charset.StandardCharsets;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Locale;
27 import java.util.Map.Entry;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.openhab.core.audio.AudioFormat;
32 import org.openhab.voice.voicerss.internal.VoiceRSSRawAudioStream;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * This class implements the Cloud service from VoiceRSS. For more information,
38 * see API documentation at http://www.voicerss.org/api .
40 * Current state of implementation:
42 * <li>All API languages supported</li>
43 * <li>Only default voice supported with good audio quality</li>
44 * <li>MP3, OGG, AAC and WAV audio formats supported</li>
45 * <li>It uses HTTP and not HTTPS (for performance reasons)</li>
48 * @author Jochen Hiller - Initial contribution
49 * @author Laurent Garnier - add support for all API languages
50 * @author Laurent Garnier - add support for OGG and AAC audio formats
51 * @author Andreas Brenk - add support for WAV audio format
54 public class VoiceRSSCloudImpl implements VoiceRSSCloudAPI {
56 public static final String DEFAULT_VOICE = "default";
58 public static final String API_URL = "https://api.voicerss.org/?key=%s&hl=%s&c=%s&f=%s&src=%s";
59 public static final String API_URL_WITH_VOICE = API_URL + "&v=%s";
61 private static final Set<String> SUPPORTED_AUDIO_CODECS = Set.of("MP3", "OGG", "AAC", "WAV", "CAF");
63 private static final Set<Locale> SUPPORTED_LOCALES = new HashSet<>();
65 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ar-eg"));
66 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ar-sa"));
67 SUPPORTED_LOCALES.add(Locale.forLanguageTag("bg-bg"));
68 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ca-es"));
69 SUPPORTED_LOCALES.add(Locale.forLanguageTag("cs-cz"));
70 SUPPORTED_LOCALES.add(Locale.forLanguageTag("da-dk"));
71 SUPPORTED_LOCALES.add(Locale.forLanguageTag("de-at"));
72 SUPPORTED_LOCALES.add(Locale.forLanguageTag("de-de"));
73 SUPPORTED_LOCALES.add(Locale.forLanguageTag("de-ch"));
74 SUPPORTED_LOCALES.add(Locale.forLanguageTag("el-gr"));
75 SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-au"));
76 SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-ca"));
77 SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-gb"));
78 SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-ie"));
79 SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-in"));
80 SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-us"));
81 SUPPORTED_LOCALES.add(Locale.forLanguageTag("es-es"));
82 SUPPORTED_LOCALES.add(Locale.forLanguageTag("es-mx"));
83 SUPPORTED_LOCALES.add(Locale.forLanguageTag("fi-fi"));
84 SUPPORTED_LOCALES.add(Locale.forLanguageTag("fr-ca"));
85 SUPPORTED_LOCALES.add(Locale.forLanguageTag("fr-fr"));
86 SUPPORTED_LOCALES.add(Locale.forLanguageTag("fr-ch"));
87 SUPPORTED_LOCALES.add(Locale.forLanguageTag("he-il"));
88 SUPPORTED_LOCALES.add(Locale.forLanguageTag("hi-in"));
89 SUPPORTED_LOCALES.add(Locale.forLanguageTag("hr-hr"));
90 SUPPORTED_LOCALES.add(Locale.forLanguageTag("hu-hu"));
91 SUPPORTED_LOCALES.add(Locale.forLanguageTag("id-id"));
92 SUPPORTED_LOCALES.add(Locale.forLanguageTag("it-it"));
93 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ja-jp"));
94 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ko-kr"));
95 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ms-my"));
96 SUPPORTED_LOCALES.add(Locale.forLanguageTag("nb-no"));
97 SUPPORTED_LOCALES.add(Locale.forLanguageTag("nl-be"));
98 SUPPORTED_LOCALES.add(Locale.forLanguageTag("nl-nl"));
99 SUPPORTED_LOCALES.add(Locale.forLanguageTag("pl-pl"));
100 SUPPORTED_LOCALES.add(Locale.forLanguageTag("pt-br"));
101 SUPPORTED_LOCALES.add(Locale.forLanguageTag("pt-pt"));
102 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ro-ro"));
103 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ru-ru"));
104 SUPPORTED_LOCALES.add(Locale.forLanguageTag("sk-sk"));
105 SUPPORTED_LOCALES.add(Locale.forLanguageTag("sl-si"));
106 SUPPORTED_LOCALES.add(Locale.forLanguageTag("sv-se"));
107 SUPPORTED_LOCALES.add(Locale.forLanguageTag("ta-in"));
108 SUPPORTED_LOCALES.add(Locale.forLanguageTag("th-th"));
109 SUPPORTED_LOCALES.add(Locale.forLanguageTag("tr-tr"));
110 SUPPORTED_LOCALES.add(Locale.forLanguageTag("vi-vn"));
111 SUPPORTED_LOCALES.add(Locale.forLanguageTag("zh-cn"));
112 SUPPORTED_LOCALES.add(Locale.forLanguageTag("zh-hk"));
113 SUPPORTED_LOCALES.add(Locale.forLanguageTag("zh-tw"));
116 private static final Map<String, Set<String>> SUPPORTED_VOICES = new HashMap<>();
118 SUPPORTED_VOICES.put("ar-eg", Set.of("Oda"));
119 SUPPORTED_VOICES.put("ar-sa", Set.of("Salim"));
120 SUPPORTED_VOICES.put("bg-bg", Set.of("Dimo"));
121 SUPPORTED_VOICES.put("ca-es", Set.of("Rut"));
122 SUPPORTED_VOICES.put("cs-cz", Set.of("Josef"));
123 SUPPORTED_VOICES.put("da-dk", Set.of("Freja"));
124 SUPPORTED_VOICES.put("de-at", Set.of("Lukas"));
125 SUPPORTED_VOICES.put("de-de", Set.of("Hanna", "Lina", "Jonas"));
126 SUPPORTED_VOICES.put("de-ch", Set.of("Tim"));
127 SUPPORTED_VOICES.put("el-gr", Set.of("Neo"));
128 SUPPORTED_VOICES.put("en-au", Set.of("Zoe", "Isla", "Evie", "Jack"));
129 SUPPORTED_VOICES.put("en-ca", Set.of("Rose", "Clara", "Emma", "Mason"));
130 SUPPORTED_VOICES.put("en-gb", Set.of("Alice", "Nancy", "Lily", "Harry"));
131 SUPPORTED_VOICES.put("en-ie", Set.of("Oran"));
132 SUPPORTED_VOICES.put("en-in", Set.of("Eka", "Jai", "Ajit"));
133 SUPPORTED_VOICES.put("en-us", Set.of("Linda", "Amy", "Mary", "John", "Mike"));
134 SUPPORTED_VOICES.put("es-es", Set.of("Camila", "Sofia", "Luna", "Diego"));
135 SUPPORTED_VOICES.put("es-mx", Set.of("Juana", "Silvia", "Teresa", "Jose"));
136 SUPPORTED_VOICES.put("fi-fi", Set.of("Aada"));
137 SUPPORTED_VOICES.put("fr-ca", Set.of("Emile", "Olivia", "Logan", "Felix"));
138 SUPPORTED_VOICES.put("fr-fr", Set.of("Bette", "Iva", "Zola", "Axel"));
139 SUPPORTED_VOICES.put("fr-ch", Set.of("Theo"));
140 SUPPORTED_VOICES.put("he-il", Set.of("Rami"));
141 SUPPORTED_VOICES.put("hi-in", Set.of("Puja", "Kabir"));
142 SUPPORTED_VOICES.put("hr-hr", Set.of("Nikola"));
143 SUPPORTED_VOICES.put("hu-hu", Set.of("Mate"));
144 SUPPORTED_VOICES.put("id-id", Set.of("Intan"));
145 SUPPORTED_VOICES.put("it-it", Set.of("Bria", "Mia", "Pietro"));
146 SUPPORTED_VOICES.put("ja-jp", Set.of("Hina", "Airi", "Fumi", "Akira"));
147 SUPPORTED_VOICES.put("ko-kr", Set.of("Nari"));
148 SUPPORTED_VOICES.put("ms-my", Set.of("Aqil"));
149 SUPPORTED_VOICES.put("nb-no", Set.of("Marte", "Erik"));
150 SUPPORTED_VOICES.put("nl-be", Set.of("Daan"));
151 SUPPORTED_VOICES.put("nl-nl", Set.of("Lotte", "Bram"));
152 SUPPORTED_VOICES.put("pl-pl", Set.of("Julia", "Jan"));
153 SUPPORTED_VOICES.put("pt-br", Set.of("Marcia", "Ligia", "Yara", "Dinis"));
154 SUPPORTED_VOICES.put("pt-pt", Set.of("Leonor"));
155 SUPPORTED_VOICES.put("ro-ro", Set.of("Doru"));
156 SUPPORTED_VOICES.put("ru-ru", Set.of("Olga", "Marina", "Peter"));
157 SUPPORTED_VOICES.put("sk-sk", Set.of("Beda"));
158 SUPPORTED_VOICES.put("sl-si", Set.of("Vid"));
159 SUPPORTED_VOICES.put("sv-se", Set.of("Molly", "Hugo"));
160 SUPPORTED_VOICES.put("ta-in", Set.of("Sai"));
161 SUPPORTED_VOICES.put("th-th", Set.of("Ukrit"));
162 SUPPORTED_VOICES.put("tr-tr", Set.of("Omer"));
163 SUPPORTED_VOICES.put("vi-vn", Set.of("Chi"));
164 SUPPORTED_VOICES.put("zh-cn", Set.of("Luli", "Shu", "Chow", "Wang"));
165 SUPPORTED_VOICES.put("zh-hk", Set.of("Jia", "Xia", "Chen"));
166 SUPPORTED_VOICES.put("zh-tw", Set.of("Akemi", "Lin", "Lee"));
169 protected boolean logging;
171 public VoiceRSSCloudImpl(boolean logging) {
172 this.logging = logging;
176 public Set<String> getAvailableAudioCodecs() {
177 return SUPPORTED_AUDIO_CODECS;
181 public Set<Locale> getAvailableLocales() {
182 return SUPPORTED_LOCALES;
186 public Set<String> getAvailableVoices() {
187 // different locales support different voices, so let's list all here in one big set when no locale is provided
188 Set<String> allvoxes = new HashSet<>();
189 allvoxes.add(DEFAULT_VOICE);
190 for (Set<String> langvoxes : SUPPORTED_VOICES.values()) {
191 for (String langvox : langvoxes) {
192 allvoxes.add(langvox);
199 public Set<String> getAvailableVoices(Locale locale) {
200 Set<String> allvoxes = new HashSet<>();
201 allvoxes.add(DEFAULT_VOICE);
202 // all maps must be defined with key in lowercase
203 String langtag = locale.toLanguageTag().toLowerCase();
204 if (SUPPORTED_VOICES.containsKey(langtag)) {
205 for (String langvox : SUPPORTED_VOICES.get(langtag)) {
206 allvoxes.add(langvox);
213 * This method will return an input stream to an audio stream for the given
216 * It will do that using a plain URL connection to avoid any external
220 public VoiceRSSRawAudioStream getTextToSpeech(String apiKey, String text, String locale, String voice,
221 String audioCodec, String audioFormat) throws IOException {
222 String url = createURL(apiKey, text, locale, voice, audioCodec, audioFormat);
224 LoggerFactory.getLogger(VoiceRSSCloudImpl.class).debug("Call {}", url.replace(apiKey, "***"));
226 URLConnection connection = new URL(url).openConnection();
228 // we will check return codes. The service will ALWAYS return a HTTP
229 // 200, but for error messages, it will return a text/plain format and
230 // the error message in body
231 int status = ((HttpURLConnection) connection).getResponseCode();
232 if (HttpURLConnection.HTTP_OK != status) {
234 LoggerFactory.getLogger(VoiceRSSCloudImpl.class).warn("Call {} returned HTTP {}",
235 url.replace(apiKey, "***"), status);
237 throw new IOException("Could not read from service: HTTP code " + status);
240 Logger logger = LoggerFactory.getLogger(VoiceRSSCloudImpl.class);
241 if (logger.isTraceEnabled()) {
242 for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
243 logger.trace("Response.header: {}={}", header.getKey(), header.getValue());
247 String contentType = connection.getHeaderField("Content-Type");
248 InputStream is = connection.getInputStream();
249 // check if content type is text/plain, then we have an error
250 if (contentType.contains("text/plain")) {
251 byte[] bytes = new byte[256];
252 is.read(bytes, 0, 256);
253 // close before throwing an exception
256 } catch (IOException ex) {
258 LoggerFactory.getLogger(VoiceRSSCloudImpl.class).debug("Failed to close inputstream", ex);
261 throw new IOException(
262 "Could not read audio content, service returned an error: " + new String(bytes, "UTF-8"));
264 // Set any audio format
265 return new VoiceRSSRawAudioStream(is, AudioFormat.MP3, connection.getContentLengthLong());
272 * This method will create the URL for the cloud service. The text will be
273 * URI encoded as it is part of the URL.
275 * It is in package scope to be accessed by tests.
277 private String createURL(String apiKey, String text, String locale, String voice, String audioCodec,
278 String audioFormat) {
279 String encodedMsg = URLEncoder.encode(text, StandardCharsets.UTF_8);
281 if (!DEFAULT_VOICE.equals(voice)) {
282 url = String.format(API_URL_WITH_VOICE, apiKey, locale, audioCodec, audioFormat, encodedMsg, voice);
284 url = String.format(API_URL, apiKey, locale, audioCodec, audioFormat, encodedMsg);