2 * Copyright (c) 2010-2021 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 if (args[4].startsWith("@")) {
53 String inputFileName = args[4].substring(1);
54 File inputFile = new File(inputFileName);
55 if (!inputFile.exists()) {
57 System.err.println("File " + inputFileName + " not found");
58 return RC_INPUT_FILE_NOT_FOUND;
60 generateCacheForFile(apiKey, cacheDir, locale, inputFileName);
62 String text = args[4];
63 generateCacheForMessage(apiKey, cacheDir, locale, text);
68 private void usage() {
69 System.out.println("Usage: java org.openhab.voice.voicerss.tool.CreateTTSCache <args>");
70 System.out.println("Arguments: --api-key <key> <cache-dir> <locale> { <text> | @inputfile }");
71 System.out.println(" key the VoiceRSS API Key, e.g. \"123456789\"");
72 System.out.println(" cache-dir is directory where the files will be stored, e.g. \"voicerss-cache\"");
73 System.out.println(" locale the language locale, has to be valid, e.g. \"en-us\", \"de-de\"");
74 System.out.println(" text the text to create audio file for, e.g. \"Hello World\"");
76 " inputfile a name of a file, where all lines will be translatet to text, e.g. \"@message.txt\"");
79 "Sample: java org.openhab.voice.voicerss.tool.CreateTTSCache --api-key 1234567890 cache en-US @messages.txt");
83 private void generateCacheForFile(String apiKey, String cacheDir, String locale, String inputFileName)
85 File inputFile = new File(inputFileName);
86 try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
88 while ((line = br.readLine()) != null) {
90 generateCacheForMessage(apiKey, cacheDir, locale, line);
95 private void generateCacheForMessage(String apiKey, String cacheDir, String locale, String msg) throws IOException {
97 System.err.println("Ignore msg=null");
100 String trimmedMsg = msg.trim();
101 if (trimmedMsg.length() == 0) {
102 System.err.println("Ignore msg=''");
105 CachedVoiceRSSCloudImpl impl = new CachedVoiceRSSCloudImpl(cacheDir);
106 File cachedFile = impl.getTextToSpeechAsFile(apiKey, trimmedMsg, locale, "MP3");
108 "Created cached audio for locale='" + locale + "', msg='" + trimmedMsg + "' to file=" + cachedFile);