]> git.basschouten.com Git - openhab-addons.git/blob
cb3677490e279efecb19e3cfbbbc0b84cc6d8028
[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         if (args[4].startsWith("@")) {
53             String inputFileName = args[4].substring(1);
54             File inputFile = new File(inputFileName);
55             if (!inputFile.exists()) {
56                 usage();
57                 System.err.println("File " + inputFileName + " not found");
58                 return RC_INPUT_FILE_NOT_FOUND;
59             }
60             generateCacheForFile(apiKey, cacheDir, locale, inputFileName);
61         } else {
62             String text = args[4];
63             generateCacheForMessage(apiKey, cacheDir, locale, text);
64         }
65         return RC_OK;
66     }
67
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\"");
75         System.out.println(
76                 "  inputfile a name of a file, where all lines will be translatet to text, e.g. \"@message.txt\"");
77         System.out.println();
78         System.out.println(
79                 "Sample: java org.openhab.voice.voicerss.tool.CreateTTSCache --api-key 1234567890 cache en-US @messages.txt");
80         System.out.println();
81     }
82
83     private void generateCacheForFile(String apiKey, String cacheDir, String locale, String inputFileName)
84             throws IOException {
85         File inputFile = new File(inputFileName);
86         try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
87             String line;
88             while ((line = br.readLine()) != null) {
89                 // process the line.
90                 generateCacheForMessage(apiKey, cacheDir, locale, line);
91             }
92         }
93     }
94
95     private void generateCacheForMessage(String apiKey, String cacheDir, String locale, String msg) throws IOException {
96         if (msg == null) {
97             System.err.println("Ignore msg=null");
98             return;
99         }
100         String trimmedMsg = msg.trim();
101         if (trimmedMsg.length() == 0) {
102             System.err.println("Ignore msg=''");
103             return;
104         }
105         CachedVoiceRSSCloudImpl impl = new CachedVoiceRSSCloudImpl(cacheDir);
106         File cachedFile = impl.getTextToSpeechAsFile(apiKey, trimmedMsg, locale, "MP3");
107         System.out.println(
108                 "Created cached audio for locale='" + locale + "', msg='" + trimmedMsg + "' to file=" + cachedFile);
109     }
110 }