]> git.basschouten.com Git - openhab-addons.git/blob
277218450c287ed211a6bca2a61eb7b0ab0a7643
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.cloudapi;
14
15 import static java.util.stream.Collectors.toSet;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.UnsupportedEncodingException;
20 import java.net.HttpURLConnection;
21 import java.net.URL;
22 import java.net.URLConnection;
23 import java.net.URLEncoder;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map.Entry;
29 import java.util.Set;
30 import java.util.stream.Stream;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class implements the Cloud service from VoiceRSS. For more information,
37  * see API documentation at http://www.voicerss.org/api/documentation.aspx.
38  *
39  * Current state of implementation:
40  * <ul>
41  * <li>All API languages supported</li>
42  * <li>Only default voice supported with good audio quality</li>
43  * <li>Only MP3, OGG and AAC audio formats supported</li>
44  * <li>It uses HTTP and not HTTPS (for performance reasons)</li>
45  * </ul>
46  *
47  * @author Jochen Hiller - Initial contribution
48  * @author Laurent Garnier - add support for all API languages
49  * @author Laurent Garnier - add support for OGG and AAC audio formats
50  */
51 public class VoiceRSSCloudImpl implements VoiceRSSCloudAPI {
52
53     private final Logger logger = LoggerFactory.getLogger(VoiceRSSCloudImpl.class);
54
55     private static final Set<String> SUPPORTED_AUDIO_FORMATS = Stream.of("MP3", "OGG", "AAC").collect(toSet());
56
57     private static final Set<Locale> SUPPORTED_LOCALES = new HashSet<>();
58     static {
59         SUPPORTED_LOCALES.add(Locale.forLanguageTag("ca-es"));
60         SUPPORTED_LOCALES.add(Locale.forLanguageTag("da-dk"));
61         SUPPORTED_LOCALES.add(Locale.forLanguageTag("de-de"));
62         SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-au"));
63         SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-ca"));
64         SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-gb"));
65         SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-in"));
66         SUPPORTED_LOCALES.add(Locale.forLanguageTag("en-us"));
67         SUPPORTED_LOCALES.add(Locale.forLanguageTag("es-es"));
68         SUPPORTED_LOCALES.add(Locale.forLanguageTag("es-mx"));
69         SUPPORTED_LOCALES.add(Locale.forLanguageTag("fi-fi"));
70         SUPPORTED_LOCALES.add(Locale.forLanguageTag("fr-ca"));
71         SUPPORTED_LOCALES.add(Locale.forLanguageTag("fr-fr"));
72         SUPPORTED_LOCALES.add(Locale.forLanguageTag("it-it"));
73         SUPPORTED_LOCALES.add(Locale.forLanguageTag("ja-jp"));
74         SUPPORTED_LOCALES.add(Locale.forLanguageTag("ko-kr"));
75         SUPPORTED_LOCALES.add(Locale.forLanguageTag("nb-no"));
76         SUPPORTED_LOCALES.add(Locale.forLanguageTag("nl-nl"));
77         SUPPORTED_LOCALES.add(Locale.forLanguageTag("pl-pl"));
78         SUPPORTED_LOCALES.add(Locale.forLanguageTag("pt-br"));
79         SUPPORTED_LOCALES.add(Locale.forLanguageTag("pt-pt"));
80         SUPPORTED_LOCALES.add(Locale.forLanguageTag("ru-ru"));
81         SUPPORTED_LOCALES.add(Locale.forLanguageTag("sv-se"));
82         SUPPORTED_LOCALES.add(Locale.forLanguageTag("zh-cn"));
83         SUPPORTED_LOCALES.add(Locale.forLanguageTag("zh-hk"));
84         SUPPORTED_LOCALES.add(Locale.forLanguageTag("zh-tw"));
85     }
86
87     private static final Set<String> SUPPORTED_VOICES = Collections.singleton("VoiceRSS");
88
89     @Override
90     public Set<String> getAvailableAudioFormats() {
91         return SUPPORTED_AUDIO_FORMATS;
92     }
93
94     @Override
95     public Set<Locale> getAvailableLocales() {
96         return SUPPORTED_LOCALES;
97     }
98
99     @Override
100     public Set<String> getAvailableVoices() {
101         return SUPPORTED_VOICES;
102     }
103
104     @Override
105     public Set<String> getAvailableVoices(Locale locale) {
106         for (Locale voiceLocale : SUPPORTED_LOCALES) {
107             if (voiceLocale.toLanguageTag().equalsIgnoreCase(locale.toLanguageTag())) {
108                 return SUPPORTED_VOICES;
109             }
110         }
111         return new HashSet<>();
112     }
113
114     /**
115      * This method will return an input stream to an audio stream for the given
116      * parameters.
117      *
118      * It will do that using a plain URL connection to avoid any external
119      * dependencies.
120      */
121     @Override
122     public InputStream getTextToSpeech(String apiKey, String text, String locale, String audioFormat)
123             throws IOException {
124         String url = createURL(apiKey, text, locale, audioFormat);
125         logger.debug("Call {}", url);
126         URLConnection connection = new URL(url).openConnection();
127
128         // we will check return codes. The service will ALWAYS return a HTTP
129         // 200, but for error messages, it will return a text/plain format and
130         // the error message in body
131         int status = ((HttpURLConnection) connection).getResponseCode();
132         if (HttpURLConnection.HTTP_OK != status) {
133             logger.error("Call {} returned HTTP {}", url, status);
134             throw new IOException("Could not read from service: HTTP code " + status);
135         }
136         if (logger.isTraceEnabled()) {
137             for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
138                 logger.trace("Response.header: {}={}", header.getKey(), header.getValue());
139             }
140         }
141         String contentType = connection.getHeaderField("Content-Type");
142         InputStream is = connection.getInputStream();
143         // check if content type is text/plain, then we have an error
144         if (contentType.contains("text/plain")) {
145             byte[] bytes = new byte[256];
146             is.read(bytes, 0, 256);
147             // close before throwing an exception
148             try {
149                 is.close();
150             } catch (IOException ex) {
151                 logger.debug("Failed to close inputstream", ex);
152             }
153             throw new IOException(
154                     "Could not read audio content, service return an error: " + new String(bytes, "UTF-8"));
155         } else {
156             return is;
157         }
158     }
159
160     // internal
161
162     /**
163      * This method will create the URL for the cloud service. The text will be
164      * URI encoded as it is part of the URL.
165      *
166      * It is in package scope to be accessed by tests.
167      */
168     private String createURL(String apiKey, String text, String locale, String audioFormat) {
169         String encodedMsg;
170         try {
171             encodedMsg = URLEncoder.encode(text, "UTF-8");
172         } catch (UnsupportedEncodingException ex) {
173             logger.error("UnsupportedEncodingException for UTF-8 MUST NEVER HAPPEN! Check your JVM configuration!", ex);
174             // fall through and use msg un-encoded
175             encodedMsg = text;
176         }
177         return "http://api.voicerss.org/?key=" + apiKey + "&hl=" + locale + "&c=" + audioFormat
178                 + "&f=44khz_16bit_mono&src=" + encodedMsg;
179     }
180 }