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.tool;
15 import java.io.BufferedReader;
17 import java.io.FileReader;
18 import java.io.IOException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.voice.voicerss.internal.cloudapi.CachedVoiceRSSCloudImpl;
24 * This class fills a cache with data from the VoiceRSS TTS service.
26 * @author Jochen Hiller - Initial contribution
29 public class CreateTTSCache {
31 public static final int RC_OK = 0;
32 public static final int RC_USAGE = 1;
33 public static final int RC_INPUT_FILE_NOT_FOUND = 2;
34 public static final int RC_API_KEY_MISSING = 3;
35 public static final int RC_INVALID_CODEC = 4;
37 public static void main(String[] args) throws IOException {
38 CreateTTSCache tool = new CreateTTSCache();
39 int rc = tool.doMain(args);
43 public int doMain(String[] args) throws IOException {
44 if (args.length < 6) {
48 if (!args[0].equalsIgnoreCase("--api-key")) {
50 return RC_API_KEY_MISSING;
52 String apiKey = args[1];
53 String cacheDir = args[2];
54 String locale = args[3];
55 String voice = args[4];
57 if (args.length >= 7) {
67 return RC_INVALID_CODEC;
70 String format = args.length >= 8 ? args[7] : "44khz_16bit_mono";
71 if (args[5].startsWith("@")) {
72 String inputFileName = args[5].substring(1);
73 File inputFile = new File(inputFileName);
74 if (!inputFile.exists()) {
76 System.err.println("File " + inputFileName + " not found");
77 return RC_INPUT_FILE_NOT_FOUND;
79 generateCacheForFile(apiKey, cacheDir, locale, voice, codec, format, inputFileName);
81 String text = args[5];
82 generateCacheForMessage(apiKey, cacheDir, locale, voice, codec, format, text);
87 private void usage() {
88 System.out.println("Usage: java org.openhab.voice.voicerss.tool.CreateTTSCache <args>");
90 "Arguments: --api-key <key> <cache-dir> <locale> <voice> { <text> | @inputfile } [ <codec> <format> ]");
91 System.out.println(" key the VoiceRSS API Key, e.g. \"123456789\"");
92 System.out.println(" cache-dir is directory where the files will be stored, e.g. \"voicerss-cache\"");
93 System.out.println(" locale the language locale, has to be valid, e.g. \"en-us\", \"de-de\"");
94 System.out.println(" voice the voice, \"default\" for the default voice");
95 System.out.println(" text the text to create audio file for, e.g. \"Hello World\"");
97 " inputfile a name of a file, where all lines will be translatet to text, e.g. \"@message.txt\"");
98 System.out.println(" codec the audio codec, \"MP3\", \"WAV\", \"OGG\" or \"AAC\", \"MP3\" by default");
99 System.out.println(" format the audio format, \"44khz_16bit_mono\" by default");
100 System.out.println();
102 "Sample: java org.openhab.voice.voicerss.tool.CreateTTSCache --api-key 1234567890 cache en-US default @messages.txt");
103 System.out.println();
106 private void generateCacheForFile(String apiKey, String cacheDir, String locale, String voice, String codec,
107 String format, String inputFileName) throws IOException {
108 File inputFile = new File(inputFileName);
109 try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
111 while ((line = br.readLine()) != null) {
113 generateCacheForMessage(apiKey, cacheDir, locale, voice, codec, format, line);
118 private void generateCacheForMessage(String apiKey, String cacheDir, String locale, String voice, String codec,
119 String format, String msg) throws IOException {
120 String trimmedMsg = msg.trim();
121 if (trimmedMsg.length() == 0) {
122 System.err.println("Ignore msg=''");
126 CachedVoiceRSSCloudImpl impl = new CachedVoiceRSSCloudImpl(cacheDir, false);
127 File cachedFile = impl.getTextToSpeechAsFile(apiKey, trimmedMsg, locale, voice, codec, format);
128 System.out.println("Created cached audio for locale='" + locale + "', voice='" + voice + "', msg='"
129 + trimmedMsg + "' to file=" + cachedFile);
130 } catch (IllegalStateException | IOException ex) {
131 System.err.println("Failed to create cached audio for locale='" + locale + "', voice='" + voice + "',msg='"