-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextSpeech.java
48 lines (37 loc) · 1.27 KB
/
TextSpeech.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Locale;
import javax.speech.Central;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
public class TextSpeech
{
private Synthesizer synthesizer;
private static TextSpeech INSTANCE;
public static TextSpeech getInstance() throws Exception {
if(INSTANCE == null) INSTANCE = new TextSpeech();
return INSTANCE;
}
private TextSpeech(){
}
public void start() throws Exception{
// set property as Kevin Dictionary
System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
// Register Engine
Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
// Create a Synthesizer
synthesizer = Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
// Allocate synthesizer
synthesizer.allocate();
// Resume Synthesizer
synthesizer.resume();
}
public void stop() throws Exception{
// Deallocate the Synthesizer.
synthesizer.deallocate();
}
public void speak(String content) throws Exception
{
// speaks the given text until queue is empty.
synthesizer.speakPlainText(content, null);
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
}
}