]> git.basschouten.com Git - openhab-addons.git/blob
6e9fed935f98ae759f3931a50098b7f758bb8f16
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.voice.voicerss.tool;
14
15 import java.io.BufferedReader;
16 import java.io.File;
17 import java.io.FileReader;
18 import java.io.IOException;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.voice.voicerss.internal.cloudapi.CachedVoiceRSSCloudImpl;
22
23 /**
24  * This class fills a cache with data from the VoiceRSS TTS service.
25  *
26  * @author Jochen Hiller - Initial contribution
27  */
28 @NonNullByDefault
29 public class CreateTTSCache {
30
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;
36
37     public static void main(String[] args) throws IOException {
38         CreateTTSCache tool = new CreateTTSCache();
39         int rc = tool.doMain(args);
40         System.exit(rc);
41     }
42
43     public int doMain(String[] args) throws IOException {
44         if (args.length < 6) {
45             usage();
46             return RC_USAGE;
47         }
48         if (!args[0].equalsIgnoreCase("--api-key")) {
49             usage();
50             return RC_API_KEY_MISSING;
51         }
52         String apiKey = args[1];
53         String cacheDir = args[2];
54         String locale = args[3];
55         String voice = args[4];
56         String codec = "MP3";
57         if (args.length >= 7) {
58             switch (args[6]) {
59                 case "MP3":
60                 case "WAV":
61                 case "OGG":
62                 case "AAC":
63                     codec = args[6];
64                     break;
65                 default:
66                     usage();
67                     return RC_INVALID_CODEC;
68             }
69         }
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()) {
75                 usage();
76                 System.err.println("File " + inputFileName + " not found");
77                 return RC_INPUT_FILE_NOT_FOUND;
78             }
79             generateCacheForFile(apiKey, cacheDir, locale, voice, codec, format, inputFileName);
80         } else {
81             String text = args[5];
82             generateCacheForMessage(apiKey, cacheDir, locale, voice, codec, format, text);
83         }
84         return RC_OK;
85     }
86
87     private void usage() {
88         System.out.println("Usage: java org.openhab.voice.voicerss.tool.CreateTTSCache <args>");
89         System.out.println(
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\"");
96         System.out.println(
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();
101         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();
104     }
105
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))) {
110             String line;
111             while ((line = br.readLine()) != null) {
112                 // process the line.
113                 generateCacheForMessage(apiKey, cacheDir, locale, voice, codec, format, line);
114             }
115         }
116     }
117
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=''");
123             return;
124         }
125         try {
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='"
132                     + trimmedMsg + "'");
133         }
134     }
135 }