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.tool;
15 import java.io.BufferedReader;
17 import java.io.FileReader;
18 import java.io.IOException;
19 import java.io.PrintStream;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.voice.voicerss.internal.cloudapi.CachedVoiceRSSCloudImpl;
25 * This class fills a cache with data from the VoiceRSS TTS service.
27 * @author Jochen Hiller - Initial contribution
30 public class CreateTTSCache {
32 public static final int RC_OK = 0;
33 public static final int RC_USAGE = 1;
34 public static final int RC_INPUT_FILE_NOT_FOUND = 2;
35 public static final int RC_API_KEY_MISSING = 3;
36 public static final int RC_INVALID_CODEC = 4;
38 public static void main(String[] args) throws IOException {
39 CreateTTSCache tool = new CreateTTSCache();
40 int rc = tool.doMain(args);
44 public int doMain(String[] args) throws IOException {
45 if (args.length < 6) {
49 if (!"--api-key".equalsIgnoreCase(args[0])) {
51 return RC_API_KEY_MISSING;
53 String apiKey = args[1];
54 String cacheDir = args[2];
55 String locale = args[3];
56 String voice = args[4];
58 if (args.length >= 7) {
68 return RC_INVALID_CODEC;
71 String format = args.length >= 8 ? args[7] : "44khz_16bit_mono";
72 if (args[5].startsWith("@")) {
73 String inputFileName = args[5].substring(1);
74 File inputFile = new File(inputFileName);
75 if (!inputFile.exists()) {
77 PrintStream printStream = System.err;
78 if (printStream != null) {
79 printStream.println("File " + inputFileName + " not found");
81 return RC_INPUT_FILE_NOT_FOUND;
83 generateCacheForFile(apiKey, cacheDir, locale, voice, codec, format, inputFileName);
85 String text = args[5];
86 generateCacheForMessage(apiKey, cacheDir, locale, voice, codec, format, text);
91 private void usage() {
92 PrintStream printStream = System.out;
93 if (printStream == null) {
96 printStream.println("Usage: java org.openhab.voice.voicerss.tool.CreateTTSCache <args>");
98 "Arguments: --api-key <key> <cache-dir> <locale> <voice> { <text> | @inputfile } [ <codec> <format> ]");
99 printStream.println(" key the VoiceRSS API Key, e.g. \"123456789\"");
100 printStream.println(" cache-dir is directory where the files will be stored, e.g. \"voicerss-cache\"");
101 printStream.println(" locale the language locale, has to be valid, e.g. \"en-us\", \"de-de\"");
102 printStream.println(" voice the voice, \"default\" for the default voice");
103 printStream.println(" text the text to create audio file for, e.g. \"Hello World\"");
105 " inputfile a name of a file, where all lines will be translatet to text, e.g. \"@message.txt\"");
106 printStream.println(" codec the audio codec, \"MP3\", \"WAV\", \"OGG\" or \"AAC\", \"MP3\" by default");
107 printStream.println(" format the audio format, \"44khz_16bit_mono\" by default");
108 printStream.println();
110 "Sample: java org.openhab.voice.voicerss.tool.CreateTTSCache --api-key 1234567890 cache en-US default @messages.txt");
111 printStream.println();
114 private void generateCacheForFile(String apiKey, String cacheDir, String locale, String voice, String codec,
115 String format, String inputFileName) throws IOException {
116 File inputFile = new File(inputFileName);
117 try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
119 while ((line = br.readLine()) != null) {
121 generateCacheForMessage(apiKey, cacheDir, locale, voice, codec, format, line);
126 private void generateCacheForMessage(String apiKey, String cacheDir, String locale, String voice, String codec,
127 String format, String msg) throws IOException {
128 PrintStream printStream;
129 String trimmedMsg = msg.trim();
130 if (trimmedMsg.length() == 0) {
131 printStream = System.err;
132 if (printStream != null) {
133 printStream.println("Ignore msg=''");
138 CachedVoiceRSSCloudImpl impl = new CachedVoiceRSSCloudImpl(cacheDir, false);
139 File cachedFile = impl.getTextToSpeechAsFile(apiKey, trimmedMsg, locale, voice, codec, format);
140 printStream = System.out;
141 if (printStream != null) {
142 printStream.println("Created cached audio for locale='" + locale + "', voice='" + voice + "', msg='"
143 + trimmedMsg + "' to file=" + cachedFile);
145 } catch (IllegalStateException | IOException ex) {
146 printStream = System.err;
147 if (printStream != null) {
148 printStream.println("Failed to create cached audio for locale='" + locale + "', voice='" + voice
149 + "',msg='" + trimmedMsg + "'");