33import com .amazonaws .ClientConfiguration ;
44import com .amazonaws .auth .DefaultAWSCredentialsProviderChain ;
55import com .amazonaws .regions .Region ;
6- import com .amazonaws .regions .Regions ;
76import com .amazonaws .services .polly .AmazonPolly ;
87import 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 ;
8+ import com .amazonaws .services .polly .model .*;
129import javazoom .jl .decoder .JavaLayerException ;
1310import javazoom .jl .player .advanced .AdvancedPlayer ;
1411import javazoom .jl .player .advanced .PlaybackEvent ;
1916
2017public class TextToSpeech {
2118
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
19+ private static Region AWS_REGION ;
2420 private static final Scanner scanner = new Scanner (System .in );
2521 private static AmazonPollyClient polly ;
2622 private static boolean runProgram = true ;
@@ -30,54 +26,74 @@ public TextToSpeech() {
3026 polly .setRegion (AWS_REGION );
3127 }
3228
33- public InputStream synthesizeSpeech (AmazonPolly polly , String text ) {
34- SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest ().withText (text ).withVoiceId (VOICE_ID ).withOutputFormat (OutputFormat .Mp3 );
29+ public InputStream synthesizeSpeech (AmazonPolly polly , String text , VoiceId voice ) {
30+ SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
31+ .withText (text )
32+ .withVoiceId (voice )
33+ .withOutputFormat (OutputFormat .Mp3 );
3534 SynthesizeSpeechResult synthesizeSpeechResult = polly .synthesizeSpeech (synthesizeSpeechRequest );
3635 return synthesizeSpeechResult .getAudioStream ();
3736 }
3837
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 ();
38+ public InputStream synthesizeSSMLSpeech (AmazonPolly polly , String text , VoiceId voice ) {
39+ String ssml = "<speak>" + text + "</speak>" ;
40+ SynthesizeSpeechRequest synthesizeSpeechRequest ;
41+ try {
42+ synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
43+ .withText (ssml )
44+ .withTextType (TextType .Ssml )
45+ .withVoiceId (voice )
46+ .withOutputFormat (OutputFormat .Mp3 );
47+ SynthesizeSpeechResult synthesizeSpeechResult = polly .synthesizeSpeech (synthesizeSpeechRequest );
48+ return synthesizeSpeechResult .getAudioStream ();
49+ } catch (RuntimeException e ) {
50+ System .out .println ("Error: " + e .getMessage ());
51+ return null ;
52+ }
4353 }
4454
4555 public String replaceText (String text ) {
4656 for (String key : TTSConfig .getReplaceText ().keySet ()) {
4757 text = text .replace (key , TTSConfig .getReplaceText ().get (key ));
58+ System .out .println ("key: " + key );
59+ System .out .println ("value: " + TTSConfig .getReplaceText ().get (key ));
4860 }
4961 return text ;
5062 }
5163
5264
5365 public static void main (String [] args ) {
5466 System .out .println ("Type your text, press Enter to convert to speech. Type 'exit' to end the program." );
67+ TTSConfig .reloadConfig ();
68+ VoiceId VOICE_ID = TTSConfig .defaultVoice ;
69+ AWS_REGION = TTSConfig .AWS_REGION ;
5570 TextToSpeech tts = new TextToSpeech ();
5671 InputStream speechStream ;
57- TTSConfig . reloadConfig ( );
72+ System . out . println ( "Using voice: " + VOICE_ID );
5873 while (runProgram ) {
5974 System .out .println ("Enter text:" );
6075 String text = scanner .nextLine ();
61- String textRef = text ;
6276 switch (text ) {
6377 case ("--exit" ) -> {
6478 runProgram = false ;
6579 System .out .println ("Program ended." );
6680 }
6781 case ("--help" ) ->
6882 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- }
7383 default -> {
74- text = tts .replaceText (text );
75- boolean useSSML = !textRef .equals (text );
84+ System .out .println (text );
85+ String newText = tts .replaceText (text );
86+ System .out .println (newText );
87+ boolean useSSML = !text .equals (newText );
7688 try {
7789 if (!useSSML ) {
78- speechStream = tts .synthesizeSpeech (polly , text );
90+ speechStream = tts .synthesizeSpeech (polly , newText , VOICE_ID );
7991 } else {
80- speechStream = tts .synthesizeSSMLSpeech (polly , text );
92+ speechStream = tts .synthesizeSSMLSpeech (polly , newText , VOICE_ID );
93+ }
94+ if (speechStream == null ) {
95+ System .out .println ("Error: Speech stream is null." );
96+ continue ;
8197 }
8298 AdvancedPlayer player = new AdvancedPlayer (speechStream , javazoom .jl .player .FactoryRegistry .systemRegistry ().createAudioDevice ());
8399 player .setPlayBackListener (new PlaybackListener () {
0 commit comments