1+ package simplexity .clitts ;
2+
3+ import com .amazonaws .ClientConfiguration ;
4+ import com .amazonaws .auth .DefaultAWSCredentialsProviderChain ;
5+ import com .amazonaws .regions .Region ;
6+ import com .amazonaws .regions .Regions ;
7+ import com .amazonaws .services .polly .AmazonPolly ;
8+ import com .amazonaws .services .polly .AmazonPollyClient ;
9+ import com .amazonaws .services .polly .model .OutputFormat ;
10+ import com .amazonaws .services .polly .model .SynthesizeSpeechRequest ;
11+ import com .amazonaws .services .polly .model .SynthesizeSpeechResult ;
12+ import javazoom .jl .decoder .JavaLayerException ;
13+ import javazoom .jl .player .advanced .AdvancedPlayer ;
14+ import javazoom .jl .player .advanced .PlaybackEvent ;
15+ import javazoom .jl .player .advanced .PlaybackListener ;
16+
17+ import java .io .InputStream ;
18+ import java .util .Scanner ;
19+
20+ public class TextToSpeech {
21+
22+ private static final Region AWS_REGION = Region .getRegion (Regions .US_EAST_1 );
23+ private static final String VOICE_ID = "Joanna" ; // Change the voice ID as needed
24+ private static final Scanner scanner = new Scanner (System .in );
25+ private static AmazonPollyClient polly ;
26+ private static boolean runProgram = true ;
27+
28+ public TextToSpeech () {
29+ polly = new AmazonPollyClient (new DefaultAWSCredentialsProviderChain (), new ClientConfiguration ());
30+ polly .setRegion (AWS_REGION );
31+ }
32+
33+ public InputStream synthesizeSpeech (AmazonPolly polly , String text ) {
34+ SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest ().withText (text ).withVoiceId (VOICE_ID ).withOutputFormat (OutputFormat .Mp3 );
35+ SynthesizeSpeechResult synthesizeSpeechResult = polly .synthesizeSpeech (synthesizeSpeechRequest );
36+ return synthesizeSpeechResult .getAudioStream ();
37+ }
38+
39+ public InputStream synthesizeSSMLSpeech (AmazonPolly polly , String text ) {
40+ SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest ().withText (text ).withTextType ("ssml" ).withVoiceId (VOICE_ID ).withOutputFormat (OutputFormat .Mp3 );
41+ SynthesizeSpeechResult synthesizeSpeechResult = polly .synthesizeSpeech (synthesizeSpeechRequest );
42+ return synthesizeSpeechResult .getAudioStream ();
43+ }
44+
45+ public String replaceText (String text ) {
46+ for (String key : TTSConfig .getReplaceText ().keySet ()) {
47+ text = text .replace (key , TTSConfig .getReplaceText ().get (key ));
48+ }
49+ return text ;
50+ }
51+
52+
53+ public static void main (String [] args ) {
54+ System .out .println ("Type your text, press Enter to convert to speech. Type 'exit' to end the program." );
55+ TextToSpeech tts = new TextToSpeech ();
56+ InputStream speechStream ;
57+ TTSConfig .reloadConfig ();
58+ while (runProgram ) {
59+ System .out .println ("Enter text:" );
60+ String text = scanner .nextLine ();
61+ String textRef = text ;
62+ switch (text ) {
63+ case ("--exit" ) -> {
64+ runProgram = false ;
65+ System .out .println ("Program ended." );
66+ }
67+ case ("--help" ) ->
68+ System .out .println ("Type your text, press Enter to convert to speech. Type '--exit' to end the program." );
69+ case ("--reload" ) -> {
70+ TTSConfig .reloadConfig ();
71+ System .out .println ("Config reloaded." );
72+ }
73+ default -> {
74+ text = tts .replaceText (text );
75+ boolean useSSML = !textRef .equals (text );
76+ try {
77+ if (!useSSML ) {
78+ speechStream = tts .synthesizeSpeech (polly , text );
79+ } else {
80+ speechStream = tts .synthesizeSSMLSpeech (polly , text );
81+ }
82+ AdvancedPlayer player = new AdvancedPlayer (speechStream , javazoom .jl .player .FactoryRegistry .systemRegistry ().createAudioDevice ());
83+ player .setPlayBackListener (new PlaybackListener () {
84+ @ Override
85+ public void playbackStarted (PlaybackEvent event ) {
86+ System .out .println ("Playing speech..." );
87+ }
88+
89+ @ Override
90+ public void playbackFinished (PlaybackEvent event ) {
91+ System .out .println ("Speech finished playing." );
92+ }
93+ });
94+ player .play ();
95+ } catch (JavaLayerException e ) {
96+ System .out .println ("Error playing speech. " + e );
97+ }
98+ }
99+ }
100+ }
101+ }
102+ }
0 commit comments