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.googletts.internal;
16 import java.io.FileNotFoundException;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.math.BigInteger;
20 import java.nio.charset.StandardCharsets;
21 import java.nio.file.Files;
22 import java.security.MessageDigest;
23 import java.security.NoSuchAlgorithmException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Base64;
27 import java.util.Dictionary;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Locale;
35 import org.eclipse.jdt.annotation.Nullable;
36 import org.eclipse.jetty.http.HttpHeader;
37 import org.eclipse.jetty.http.MimeTypes;
38 import org.openhab.core.audio.AudioFormat;
39 import org.openhab.core.auth.AuthenticationException;
40 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
41 import org.openhab.core.auth.client.oauth2.OAuthClientService;
42 import org.openhab.core.auth.client.oauth2.OAuthException;
43 import org.openhab.core.auth.client.oauth2.OAuthFactory;
44 import org.openhab.core.auth.client.oauth2.OAuthResponseException;
45 import org.openhab.core.i18n.CommunicationException;
46 import org.openhab.core.io.net.http.HttpRequestBuilder;
47 import org.openhab.voice.googletts.internal.dto.AudioConfig;
48 import org.openhab.voice.googletts.internal.dto.AudioEncoding;
49 import org.openhab.voice.googletts.internal.dto.ListVoicesResponse;
50 import org.openhab.voice.googletts.internal.dto.SsmlVoiceGender;
51 import org.openhab.voice.googletts.internal.dto.SynthesisInput;
52 import org.openhab.voice.googletts.internal.dto.SynthesizeSpeechRequest;
53 import org.openhab.voice.googletts.internal.dto.SynthesizeSpeechResponse;
54 import org.openhab.voice.googletts.internal.dto.Voice;
55 import org.openhab.voice.googletts.internal.dto.VoiceSelectionParams;
56 import org.osgi.service.cm.Configuration;
57 import org.osgi.service.cm.ConfigurationAdmin;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 import com.google.gson.Gson;
62 import com.google.gson.GsonBuilder;
63 import com.google.gson.JsonSyntaxException;
66 * Google Cloud TTS API call implementation.
68 * @author Gabor Bicskei - Initial contribution and API
70 class GoogleCloudAPI {
72 private static final char EXTENSION_SEPARATOR = '.';
73 private static final char UNIX_SEPARATOR = '/';
74 private static final char WINDOWS_SEPARATOR = '\\';
76 private static final String BEARER = "Bearer ";
78 private static final String GCP_AUTH_URI = "https://accounts.google.com/o/oauth2/auth";
79 private static final String GCP_TOKEN_URI = "https://accounts.google.com/o/oauth2/token";
80 private static final String GCP_REDIRECT_URI = "https://www.google.com";
82 * Google Cloud Platform authorization scope
84 private static final String GCP_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
87 * URL used for retrieving the list of available voices
89 private static final String LIST_VOICES_URL = "https://texttospeech.googleapis.com/v1/voices";
92 * URL used for synthesizing text to speech
94 private static final String SYTNHESIZE_SPEECH_URL = "https://texttospeech.googleapis.com/v1/text:synthesize";
99 private final Logger logger = LoggerFactory.getLogger(GoogleCloudAPI.class);
102 * Supported voices and locales
104 private final Map<Locale, Set<GoogleTTSVoice>> voices = new HashMap<>();
109 private File cacheFolder;
114 private @Nullable GoogleTTSConfig config;
119 private boolean initialized;
121 private final Gson gson = new GsonBuilder().create();
122 private final ConfigurationAdmin configAdmin;
123 private final OAuthFactory oAuthFactory;
125 private @Nullable OAuthClientService oAuthService;
130 * @param cacheFolder Service cache folder
132 GoogleCloudAPI(ConfigurationAdmin configAdmin, OAuthFactory oAuthFactory, File cacheFolder) {
133 this.configAdmin = configAdmin;
134 this.oAuthFactory = oAuthFactory;
135 this.cacheFolder = cacheFolder;
139 * Configuration update.
141 * @param config New configuration.
143 void setConfig(GoogleTTSConfig config) {
144 this.config = config;
146 String clientId = config.clientId;
147 String clientSecret = config.clientSecret;
148 if (clientId != null && !clientId.isEmpty() && clientSecret != null && !clientSecret.isEmpty()) {
150 final OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(
151 GoogleTTSService.SERVICE_PID, GCP_TOKEN_URI, GCP_AUTH_URI, clientId, clientSecret, GCP_SCOPE,
153 this.oAuthService = oAuthService;
157 } catch (AuthenticationException | CommunicationException e) {
158 logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage());
170 if (config.purgeCache) {
171 File[] files = cacheFolder.listFiles();
172 if (files != null && files.length > 0) {
173 Arrays.stream(files).forEach(File::delete);
175 logger.debug("Cache purged.");
180 * Fetches the OAuth2 tokens from Google Cloud Platform if the auth-code is set in the configuration. If successful
181 * the auth-code will be removed from the configuration.
183 * @throws AuthenticationException
184 * @throws CommunicationException
186 @SuppressWarnings("null")
187 private void getAccessToken() throws AuthenticationException, CommunicationException {
188 String authcode = config.authcode;
189 if (authcode != null && !authcode.isEmpty()) {
190 logger.debug("Trying to get access and refresh tokens.");
192 AccessTokenResponse response = oAuthService.getAccessTokenResponseByAuthorizationCode(authcode,
194 if (response.getRefreshToken() == null || response.getRefreshToken().isEmpty()) {
195 throw new AuthenticationException("Error fetching refresh token. Please reauthorize");
197 } catch (OAuthException | OAuthResponseException e) {
198 logger.debug("Error fetching access token: {}", e.getMessage(), e);
199 throw new AuthenticationException(
200 "Error fetching access token. Invalid authcode? Please generate a new one.");
201 } catch (IOException e) {
202 throw new CommunicationException(
203 String.format("An unexpected IOException occurred: %s", e.getMessage()));
206 config.authcode = null;
209 Configuration serviceConfig = configAdmin.getConfiguration(GoogleTTSService.SERVICE_PID);
210 Dictionary<String, Object> configProperties = serviceConfig.getProperties();
211 if (configProperties != null) {
212 configProperties.put(GoogleTTSService.PARAM_AUTHCODE, "");
213 serviceConfig.update(configProperties);
215 } catch (IOException e) {
218 "Failed to update configuration for Google Cloud TTS service. Please clear the 'authcode' configuration parameter manualy.");
223 @SuppressWarnings("null")
224 private String getAuthorizationHeader() throws AuthenticationException, CommunicationException {
225 final AccessTokenResponse accessTokenResponse;
227 accessTokenResponse = oAuthService.getAccessTokenResponse();
228 } catch (OAuthException | OAuthResponseException e) {
229 logger.debug("Error fetching access token: {}", e.getMessage(), e);
230 throw new AuthenticationException(
231 "Error fetching access token. Invalid authcode? Please generate a new one.");
232 } catch (IOException e) {
233 throw new CommunicationException(String.format("An unexpected IOException occurred: %s", e.getMessage()));
235 if (accessTokenResponse == null || accessTokenResponse.getAccessToken() == null
236 || accessTokenResponse.getAccessToken().isEmpty()) {
237 throw new AuthenticationException("No access token. Is this thing authorized?");
239 if (accessTokenResponse.getRefreshToken() == null || accessTokenResponse.getRefreshToken().isEmpty()) {
240 throw new AuthenticationException("No refresh token. Please reauthorize");
242 return BEARER + accessTokenResponse.getAccessToken();
246 * Loads supported audio formats
248 * @return Set of audio formats
250 Set<String> getSupportedAudioFormats() {
251 Set<String> formats = new HashSet<>();
252 for (AudioEncoding audioEncoding : AudioEncoding.values()) {
253 if (audioEncoding != AudioEncoding.AUDIO_ENCODING_UNSPECIFIED) {
254 formats.add(audioEncoding.toString());
263 * @return Set of locales
265 Set<Locale> getSupportedLocales() {
266 return voices.keySet();
270 * Supported voices for locale.
272 * @param locale Locale
273 * @return Set of voices
275 Set<GoogleTTSVoice> getVoicesForLocale(Locale locale) {
276 Set<GoogleTTSVoice> localeVoices = voices.get(locale);
277 return localeVoices != null ? localeVoices : Set.of();
281 * Google API call to load locales and voices.
283 * @throws AuthenticationException
284 * @throws CommunicationException
286 private void initVoices() throws AuthenticationException, CommunicationException {
287 if (oAuthService != null) {
289 for (GoogleTTSVoice voice : listVoices()) {
290 Locale locale = voice.getLocale();
291 Set<GoogleTTSVoice> localeVoices;
292 if (!voices.containsKey(locale)) {
293 localeVoices = new HashSet<>();
294 voices.put(locale, localeVoices);
296 localeVoices = voices.get(locale);
298 localeVoices.add(voice);
301 logger.error("Google client is not initialized!");
305 @SuppressWarnings("null")
306 private List<GoogleTTSVoice> listVoices() throws AuthenticationException, CommunicationException {
307 HttpRequestBuilder builder = HttpRequestBuilder.getFrom(LIST_VOICES_URL)
308 .withHeader(HttpHeader.AUTHORIZATION.name(), getAuthorizationHeader());
311 ListVoicesResponse listVoicesResponse = gson.fromJson(builder.getContentAsString(),
312 ListVoicesResponse.class);
314 if (listVoicesResponse == null || listVoicesResponse.getVoices() == null) {
318 List<GoogleTTSVoice> result = new ArrayList<>();
319 for (Voice voice : listVoicesResponse.getVoices()) {
320 for (String languageCode : voice.getLanguageCodes()) {
321 result.add(new GoogleTTSVoice(Locale.forLanguageTag(languageCode), voice.getName(),
322 voice.getSsmlGender().name()));
326 } catch (JsonSyntaxException e) {
328 } catch (IOException e) {
329 throw new CommunicationException(String.format("An unexpected IOException occurred: %s", e.getMessage()));
335 * Converts audio format to Google parameters.
337 * @param codec Requested codec
338 * @return String array of Google audio format and the file extension to use.
340 private String[] getFormatForCodec(String codec) {
342 case AudioFormat.CODEC_MP3:
343 return new String[] { AudioEncoding.MP3.toString(), "mp3" };
344 case AudioFormat.CODEC_PCM_SIGNED:
345 return new String[] { AudioEncoding.LINEAR16.toString(), "wav" };
347 throw new IllegalArgumentException("Audio format " + codec + " is not yet supported");
351 public byte[] synthesizeSpeech(String text, GoogleTTSVoice voice, String codec) {
352 String[] format = getFormatForCodec(codec);
353 String fileNameInCache = getUniqueFilenameForText(text, voice.getTechnicalName());
354 File audioFileInCache = new File(cacheFolder, fileNameInCache + "." + format[1]);
357 if (audioFileInCache.exists()) {
358 logger.debug("Audio file {} was found in cache.", audioFileInCache.getName());
359 return Files.readAllBytes(audioFileInCache.toPath());
362 // if not in cache, get audio data and put to cache
363 byte[] audio = synthesizeSpeechByGoogle(text, voice, format[0]);
365 saveAudioAndTextToFile(text, audioFileInCache, audio, voice.getTechnicalName());
368 } catch (AuthenticationException | CommunicationException e) {
369 logger.warn("Error initializing Google Cloud TTS service: {}", e.getMessage());
373 } catch (FileNotFoundException e) {
374 logger.warn("Could not write file {} to cache: {}", audioFileInCache, e.getMessage());
375 } catch (IOException e) {
376 logger.debug("An unexpected IOException occurred: {}", e.getMessage());
382 * Create cache entry.
384 * @param text Converted text.
385 * @param cacheFile Cache entry file.
386 * @param audio Byte array of the audio.
387 * @param voiceName Used voice
388 * @throws FileNotFoundException
389 * @throws IOException in case of file handling exceptions
391 private void saveAudioAndTextToFile(String text, File cacheFile, byte[] audio, String voiceName)
392 throws IOException, FileNotFoundException {
393 logger.debug("Caching audio file {}", cacheFile.getName());
394 try (FileOutputStream audioFileOutputStream = new FileOutputStream(cacheFile)) {
395 audioFileOutputStream.write(audio);
398 // write text to file for transparency too
399 // this allows to know which contents is in which audio file
400 String textFileName = removeExtension(cacheFile.getName()) + ".txt";
401 logger.debug("Caching text file {}", textFileName);
402 try (FileOutputStream textFileOutputStream = new FileOutputStream(new File(cacheFolder, textFileName))) {
404 StringBuilder sb = new StringBuilder("Config: ")
405 .append(config.toConfigString())
408 .append(System.lineSeparator())
411 .append(System.lineSeparator());
413 textFileOutputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8));
418 * Removes the extension of a file name.
420 * @param fileName the file name to remove the extension of
421 * @return the filename without the extension
423 private String removeExtension(String fileName) {
424 int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
425 int lastSeparator = Math.max(fileName.lastIndexOf(UNIX_SEPARATOR), fileName.lastIndexOf(WINDOWS_SEPARATOR));
426 return lastSeparator > extensionPos ? fileName : fileName.substring(0, extensionPos);
430 * Call Google service to synthesize the required text
432 * @param text Text to synthesize
433 * @param voice Voice parameter
434 * @param audioFormat Audio encoding format
435 * @return Audio input stream or {@code null} when encoding exceptions occur
436 * @throws AuthenticationException
437 * @throws CommunicationException
439 @SuppressWarnings("null")
440 private byte[] synthesizeSpeechByGoogle(String text, GoogleTTSVoice voice, String audioFormat)
441 throws AuthenticationException, CommunicationException {
442 AudioConfig audioConfig = new AudioConfig(AudioEncoding.valueOf(audioFormat), config.pitch, config.speakingRate,
443 config.volumeGainDb);
444 SynthesisInput synthesisInput = new SynthesisInput(text);
445 VoiceSelectionParams voiceSelectionParams = new VoiceSelectionParams(voice.getLocale().getLanguage(),
446 voice.getLabel(), SsmlVoiceGender.valueOf(voice.getSsmlGender()));
448 SynthesizeSpeechRequest request = new SynthesizeSpeechRequest(audioConfig, synthesisInput,
449 voiceSelectionParams);
451 HttpRequestBuilder builder = HttpRequestBuilder.postTo(SYTNHESIZE_SPEECH_URL)
452 .withHeader(HttpHeader.AUTHORIZATION.name(), getAuthorizationHeader())
453 .withContent(gson.toJson(request), MimeTypes.Type.APPLICATION_JSON.name());
456 SynthesizeSpeechResponse synthesizeSpeechResponse = gson.fromJson(builder.getContentAsString(),
457 SynthesizeSpeechResponse.class);
459 if (synthesizeSpeechResponse == null) {
463 byte[] encodedBytes = synthesizeSpeechResponse.getAudioContent().getBytes(StandardCharsets.UTF_8);
464 return Base64.getDecoder().decode(encodedBytes);
465 } catch (JsonSyntaxException e) {
467 } catch (IOException e) {
468 throw new CommunicationException(String.format("An unexpected IOException occurred: %s", e.getMessage()));
474 * Gets a unique filename for a give text, by creating a MD5 hash of it. It
475 * will be preceded by the locale.
477 * Sample: "en-US_00a2653ac5f77063bc4ea2fee87318d3"
479 private String getUniqueFilenameForText(String text, String voiceName) {
481 MessageDigest md = MessageDigest.getInstance("MD5");
482 byte[] bytesOfMessage = (config.toConfigString() + text).getBytes(StandardCharsets.UTF_8);
483 String fileNameHash = String.format("%032x", new BigInteger(1, md.digest(bytesOfMessage)));
484 return voiceName + "_" + fileNameHash;
485 } catch (NoSuchAlgorithmException e) {
487 logger.error("Could not create MD5 hash for '{}'", text, e);
492 boolean isInitialized() {