-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextToSpeech.java
More file actions
96 lines (82 loc) · 2.5 KB
/
Copy pathTextToSpeech.java
File metadata and controls
96 lines (82 loc) · 2.5 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioInputStream;
import marytts.LocalMaryInterface;
import marytts.MaryInterface;
import marytts.exceptions.MaryConfigurationException;
import marytts.exceptions.SynthesisException;
/**
* @author GOXR3PLUS
* @version modified by JonnyJack (me)
*/
public class TextToSpeech {
private AudioPlayer tts;
private MaryInterface marytts;
/**
* Constructor
*/
public TextToSpeech() {
try {
marytts = new LocalMaryInterface();
} catch (MaryConfigurationException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Change the default voice of the MaryTTS
* @param voice
*/
public void setVoice(String voice) {
marytts.setVoice(voice);
}
/**
* Transform text to speech
*
* @param text
* The text that will be transformed to speech
* @param daemon
* <br>
* <b>True</b> The thread that will start the text to speech
* Player will be a daemon Thread <br>
* <b>False</b> The thread that will start the text to speech
* Player will be a normal non daemon Thread
* @param join
* <br>
* <b>True</b> The current Thread calling this method will
* wait(blocked) until the Thread which is playing the Speech
* finish <br>
* <b>False</b> The current Thread calling this method will
* continue freely after calling this method
*/
public void speak(String text, float gainValue, boolean daemon, boolean join) {
// Stop the previous player
stopSpeaking();
try (AudioInputStream audio = marytts.generateAudio(text)) {
// Player is a thread(threads can only run one time) so it can be
// used has to be initiated every time
tts = new AudioPlayer();
tts.setAudio(audio);
tts.setGain(gainValue);
tts.setDaemon(daemon);
tts.start();
if (join)
tts.join();
} catch (SynthesisException ex) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error saying phrase.", ex);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "IO Exception", ex);
} catch (InterruptedException ex) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Interrupted ", ex);
tts.interrupt();
}
}
/**
* Stop the MaryTTS from Speaking
*/
public void stopSpeaking() {
// Stop the previous player
if (tts != null)
tts.cancel();
}
}