]> git.basschouten.com Git - openhab-addons.git/blob
e3b436fa58d01af239d205aac7057408c05b384a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import java.io.PrintStream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.voice.voicerss.internal.cloudapi.CachedVoiceRSSCloudImpl;
23
24 /**
25  * This class fills a cache with data from the VoiceRSS TTS service.
26  *
27  * @author Jochen Hiller - Initial contribution
28  */
29 @NonNullByDefault
30 public class CreateTTSCache {
31
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;
37
38     public static void main(String[] args) throws IOException {
39         CreateTTSCache tool = new CreateTTSCache();
40         int rc = tool.doMain(args);
41         System.exit(rc);
42     }
43
44     public int doMain(String[] args) throws IOException {
45         if (args.length < 6) {
46             usage();
47             return RC_USAGE;
48         }
49         if (!args[0].equalsIgnoreCase("--api-key")) {
50             usage();
51             return RC_API_KEY_MISSING;
52         }
53         String apiKey = args[1];
54         String cacheDir = args[2];
55         String locale = args[3];
56         String voice = args[4];
57         String codec = "MP3";
58         if (args.length >= 7) {
59             switch (args[6]) {
60                 case "MP3":
61                 case "WAV":
62                 case "OGG":
63                 case "AAC":
64                     codec = args[6];
65                     break;
66                 default:
67                     usage();
68                     return RC_INVALID_CODEC;
69             }
70         }
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()) {
76                 usage();
77                 PrintStream printStream = System.err;
78                 if (printStream != null) {
79                     printStream.println("File " + inputFileName + " not found");
80                 }
81                 return RC_INPUT_FILE_NOT_FOUND;
82             }
83             generateCacheForFile(apiKey, cacheDir, locale, voice, codec, format, inputFileName);
84         } else {
85             String text = args[5];
86             generateCacheForMessage(apiKey, cacheDir, locale, voice, codec, format, text);
87         }
88         return RC_OK;
89     }
90
91     private void usage() {
92         PrintStream printStream = System.out;
93         if (printStream == null) {
94             return;
95         }
96         printStream.println("Usage: java org.openhab.voice.voicerss.tool.CreateTTSCache <args>");
97         printStream.println(
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\"");
104         printStream.println(
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();
109         printStream.println(
110                 "Sample: java org.openhab.voice.voicerss.tool.CreateTTSCache --api-key 1234567890 cache en-US default @messages.txt");
111         printStream.println();
112     }
113
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))) {
118             String line;
119             while ((line = br.readLine()) != null) {
120                 // process the line.
121                 generateCacheForMessage(apiKey, cacheDir, locale, voice, codec, format, line);
122             }
123         }
124     }
125
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=''");
134             }
135             return;
136         }
137         try {
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);
144             }
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 + "'");
150             }
151         }
152     }
153 }