]> git.basschouten.com Git - openhab-addons.git/blob
0f7cd5e38e91fc975a4f8d2bfbbfaaa7cd679834
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.openhab.voice.voicerss.internal.cloudapi.CachedVoiceRSSCloudImpl;
21
22 /**
23  * This class fills a cache with data from the VoiceRSS TTS service.
24  *
25  * @author Jochen Hiller - Initial contribution
26  */
27 public class CreateTTSCache {
28
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;
33
34     public static void main(String[] args) throws IOException {
35         CreateTTSCache tool = new CreateTTSCache();
36         int rc = tool.doMain(args);
37         System.exit(rc);
38     }
39
40     public int doMain(String[] args) throws IOException {
41         if ((args == null) || (args.length != 5)) {
42             usage();
43             return RC_USAGE;
44         }
45         if (!args[0].equalsIgnoreCase("--api-key")) {
46             usage();
47             return RC_API_KEY_MISSING;
48         }
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()) {
57                 usage();
58                 System.err.println("File " + inputFileName + " not found");
59                 return RC_INPUT_FILE_NOT_FOUND;
60             }
61             generateCacheForFile(apiKey, cacheDir, locale, voice, inputFileName);
62         } else {
63             String text = args[5];
64             generateCacheForMessage(apiKey, cacheDir, locale, voice, text);
65         }
66         return RC_OK;
67     }
68
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\"");
77         System.out.println(
78                 "  inputfile a name of a file, where all lines will be translatet to text, e.g. \"@message.txt\"");
79         System.out.println();
80         System.out.println(
81                 "Sample: java org.openhab.voice.voicerss.tool.CreateTTSCache --api-key 1234567890 cache en-US @messages.txt");
82         System.out.println();
83     }
84
85     private void generateCacheForFile(String apiKey, String cacheDir, String locale, String voice, String inputFileName)
86             throws IOException {
87         File inputFile = new File(inputFileName);
88         try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
89             String line;
90             while ((line = br.readLine()) != null) {
91                 // process the line.
92                 generateCacheForMessage(apiKey, cacheDir, locale, voice, line);
93             }
94         }
95     }
96
97     private void generateCacheForMessage(String apiKey, String cacheDir, String locale, String voice, String msg)
98             throws IOException {
99         if (msg == null) {
100             System.err.println("Ignore msg=null");
101             return;
102         }
103         String trimmedMsg = msg.trim();
104         if (trimmedMsg.length() == 0) {
105             System.err.println("Ignore msg=''");
106             return;
107         }
108         CachedVoiceRSSCloudImpl impl = new CachedVoiceRSSCloudImpl(cacheDir);
109         File cachedFile = impl.getTextToSpeechAsFile(apiKey, trimmedMsg, locale, voice, "MP3");
110         System.out.println(
111                 "Created cached audio for locale='" + locale + "', msg='" + trimmedMsg + "' to file=" + cachedFile);
112     }
113 }