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.openhab.voice.voicerss.internal.cloudapi.CachedVoiceRSSCloudImpl;
23 * This class fills a cache with data from the VoiceRSS TTS service.
25 * @author Jochen Hiller - Initial contribution
27 public class CreateTTSCache {
29 public static final int RC_OK = 0;
30 public static final int RC_USAGE = 1;
31 public static final int RC_INPUT_FILE_NOT_FOUND = 2;
32 public static final int RC_API_KEY_MISSING = 3;
34 public static void main(String[] args) throws IOException {
35 CreateTTSCache tool = new CreateTTSCache();
36 int rc = tool.doMain(args);
40 public int doMain(String[] args) throws IOException {
41 if ((args == null) || (args.length != 5)) {
45 if (!args[0].equalsIgnoreCase("--api-key")) {
47 return RC_API_KEY_MISSING;
49 String apiKey = args[1];
50 String cacheDir = args[2];
51 String locale = args[3];
52 String voice = args[4];
53 if (args[5].startsWith("@")) {
54 String inputFileName = args[5].substring(1);
55 File inputFile = new File(inputFileName);
56 if (!inputFile.exists()) {
58 System.err.println("File " + inputFileName + " not found");
59 return RC_INPUT_FILE_NOT_FOUND;
61 generateCacheForFile(apiKey, cacheDir, locale, voice, inputFileName);
63 String text = args[5];
64 generateCacheForMessage(apiKey, cacheDir, locale, voice, text);
69 private void usage() {
70 System.out.println("Usage: java org.openhab.voice.voicerss.tool.CreateTTSCache <args>");
71 System.out.println("Arguments: --api-key <key> <cache-dir> <locale> { <text> | @inputfile }");
72 System.out.println(" key the VoiceRSS API Key, e.g. \"123456789\"");
73 System.out.println(" cache-dir is directory where the files will be stored, e.g. \"voicerss-cache\"");
74 System.out.println(" locale the language locale, has to be valid, e.g. \"en-us\", \"de-de\"");
75 System.out.println(" voice the voice, \"default\" for the default voice");
76 System.out.println(" text the text to create audio file for, e.g. \"Hello World\"");
78 " inputfile a name of a file, where all lines will be translatet to text, e.g. \"@message.txt\"");
81 "Sample: java org.openhab.voice.voicerss.tool.CreateTTSCache --api-key 1234567890 cache en-US @messages.txt");
85 private void generateCacheForFile(String apiKey, String cacheDir, String locale, String voice, String inputFileName)
87 File inputFile = new File(inputFileName);
88 try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
90 while ((line = br.readLine()) != null) {
92 generateCacheForMessage(apiKey, cacheDir, locale, voice, line);
97 private void generateCacheForMessage(String apiKey, String cacheDir, String locale, String voice, String msg)
100 System.err.println("Ignore msg=null");
103 String trimmedMsg = msg.trim();
104 if (trimmedMsg.length() == 0) {
105 System.err.println("Ignore msg=''");
108 CachedVoiceRSSCloudImpl impl = new CachedVoiceRSSCloudImpl(cacheDir);
109 File cachedFile = impl.getTextToSpeechAsFile(apiKey, trimmedMsg, locale, voice, "MP3", null);
111 "Created cached audio for locale='" + locale + "', msg='" + trimmedMsg + "' to file=" + cachedFile);