1
|
package com.mizo0203.timeline.talker;
|
2
|
|
3
|
import com.mizo0203.timeline.talker.util.RuntimeUtil;
|
4
|
import org.jetbrains.annotations.NotNull;
|
5
|
|
6
|
import java.io.*;
|
7
|
import java.util.concurrent.ExecutorService;
|
8
|
import java.util.concurrent.Executors;
|
9
|
|
10
|
public class Talker {
|
11
|
|
12
|
private static final String AQUESTALK_PI_PATH = "./aquestalkpi/AquesTalkPi";
|
13
|
|
14
|
private final ExecutorService mSingleThreadExecutor = Executors.newSingleThreadExecutor();
|
15
|
|
16
|
@NotNull private YukkuriVoice mNextVoice = Talker.YukkuriVoice.REIMU;
|
17
|
|
18
|
public Talker() throws IllegalStateException, SecurityException {
|
19
|
File file = new File(AQUESTALK_PI_PATH);
|
20
|
if (!file.isFile()) {
|
21
|
throw new IllegalStateException(
|
22
|
file.getPath()
|
23
|
+ " に AquesTalk Pi がありません。\n"
|
24
|
+ "https://www.a-quest.com/products/aquestalkpi.html\n"
|
25
|
+ "からダウンロードしてください。");
|
26
|
}
|
27
|
if (!file.canExecute()) {
|
28
|
throw new IllegalStateException(file.getPath() + " に実行権限がありません。");
|
29
|
}
|
30
|
}
|
31
|
|
32
|
public void talkAlternatelyAsync(final String text) {
|
33
|
mSingleThreadExecutor.submit(
|
34
|
new Runnable() {
|
35
|
|
36
|
@Override
|
37
|
public void run() {
|
38
|
try {
|
39
|
File file = new File("text.txt");
|
40
|
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
|
41
|
pw.println(text);
|
42
|
pw.flush();
|
43
|
pw.close();
|
44
|
RuntimeUtil.execute(
|
45
|
new String[] {
|
46
|
AQUESTALK_PI_PATH, "-v", mNextVoice.value, "-f", "text.txt", "-o", "out.wav"
|
47
|
});
|
48
|
RuntimeUtil.execute(new String[] {"sh", "-c", "aplay < out.wav"}); // 起動コマンドを指定する
|
49
|
|
50
|
// 読み上げは、霊夢と魔理沙が交互に行なう
|
51
|
if (mNextVoice == Talker.YukkuriVoice.REIMU) {
|
52
|
mNextVoice = Talker.YukkuriVoice.MARISA;
|
53
|
} else {
|
54
|
mNextVoice = Talker.YukkuriVoice.REIMU;
|
55
|
}
|
56
|
|
57
|
Thread.sleep(2000);
|
58
|
} catch (@NotNull IOException | InterruptedException e) {
|
59
|
e.printStackTrace();
|
60
|
}
|
61
|
}
|
62
|
});
|
63
|
}
|
64
|
|
65
|
public enum YukkuriVoice {
|
66
|
|
67
|
/** ゆっくりボイス - 霊夢 */
|
68
|
REIMU("f1"), //
|
69
|
|
70
|
/** ゆっくりボイス - 魔理沙 */
|
71
|
MARISA("f2"), //
|
72
|
;
|
73
|
|
74
|
private final String value;
|
75
|
|
76
|
YukkuriVoice(String value) {
|
77
|
this.value = value;
|
78
|
}
|
79
|
}
|
80
|
}
|