55import com .amazonaws .regions .Region ;
66import com .amazonaws .services .polly .AmazonPolly ;
77import com .amazonaws .services .polly .AmazonPollyClient ;
8- import com .amazonaws .services .polly .model .*;
8+ import com .amazonaws .services .polly .model .OutputFormat ;
9+ import com .amazonaws .services .polly .model .SynthesizeSpeechRequest ;
10+ import com .amazonaws .services .polly .model .SynthesizeSpeechResult ;
11+ import com .amazonaws .services .polly .model .VoiceId ;
912import javazoom .jl .decoder .JavaLayerException ;
1013import javazoom .jl .player .advanced .AdvancedPlayer ;
11- import javazoom .jl .player .advanced .PlaybackEvent ;
12- import javazoom .jl .player .advanced .PlaybackListener ;
1314
1415import java .io .InputStream ;
1516import java .util .Scanner ;
@@ -19,13 +20,23 @@ public class TextToSpeech {
1920 private static Region AWS_REGION ;
2021 private static final Scanner scanner = new Scanner (System .in );
2122 private static AmazonPollyClient polly ;
23+ private static VoiceId VOICE_ID ;
2224 private static boolean runProgram = true ;
25+ private static TextToSpeech instance ;
26+ private static InputStream speechStream ;
2327
2428 public TextToSpeech () {
2529 polly = new AmazonPollyClient (new DefaultAWSCredentialsProviderChain (), new ClientConfiguration ());
2630 polly .setRegion (AWS_REGION );
2731 }
2832
33+ public static TextToSpeech getInstance () {
34+ if (instance == null ) {
35+ instance = new TextToSpeech ();
36+ }
37+ return instance ;
38+ }
39+
2940 public InputStream synthesizeSpeech (AmazonPolly polly , String text , VoiceId voice ) {
3041 SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
3142 .withText (text )
@@ -41,7 +52,7 @@ public InputStream synthesizeSSMLSpeech(AmazonPolly polly, String text, VoiceId
4152 try {
4253 synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
4354 .withText (ssml )
44- .withTextType (TextType .Ssml )
55+ .withTextType (com . amazonaws . services . polly . model . TextType .Ssml )
4556 .withVoiceId (voice )
4657 .withOutputFormat (OutputFormat .Mp3 );
4758 SynthesizeSpeechResult synthesizeSpeechResult = polly .synthesizeSpeech (synthesizeSpeechRequest );
@@ -55,21 +66,43 @@ public InputStream synthesizeSSMLSpeech(AmazonPolly polly, String text, VoiceId
5566 public String replaceText (String text ) {
5667 for (String key : TTSConfig .getReplaceText ().keySet ()) {
5768 text = text .replace (key , TTSConfig .getReplaceText ().get (key ));
58- System .out .println ("key: " + key );
59- System .out .println ("value: " + TTSConfig .getReplaceText ().get (key ));
69+ }
70+ for (String key : TTSConfig .getVoicePrefixes ().keySet ()) {
71+ if (text .startsWith (key )) {
72+ text = text .replace (key , "" );
73+ VOICE_ID = TTSConfig .getVoicePrefixes ().get (key );
74+ }
6075 }
6176 return text ;
6277 }
6378
79+ public static void processSpeech (String text ) {
80+ TextToSpeech tts = getInstance ();
81+ String newText = tts .replaceText (text );
82+ boolean useSSML = !text .equals (newText );
83+ try {
84+ if (!useSSML ) {
85+ speechStream = tts .synthesizeSpeech (polly , newText , VOICE_ID );
86+ } else {
87+ speechStream = tts .synthesizeSSMLSpeech (polly , newText , VOICE_ID );
88+ }
89+ if (speechStream == null ) {
90+ System .out .println ("Error: Speech stream is null." );
91+ return ;
92+ }
93+ AdvancedPlayer player = new AdvancedPlayer (speechStream ,
94+ javazoom .jl .player .FactoryRegistry .systemRegistry ().createAudioDevice ());
95+ player .play ();
96+ } catch (JavaLayerException e ) {
97+ System .out .println ("Error playing speech. " + e );
98+ }
99+ }
64100
65101 public static void main (String [] args ) {
66102 System .out .println ("Type your text, press Enter to convert to speech. Type 'exit' to end the program." );
67103 TTSConfig .reloadConfig ();
68- VoiceId VOICE_ID = TTSConfig .defaultVoice ;
104+ VOICE_ID = TTSConfig .defaultVoice ;
69105 AWS_REGION = TTSConfig .AWS_REGION ;
70- TextToSpeech tts = new TextToSpeech ();
71- InputStream speechStream ;
72- System .out .println ("Using voice: " + VOICE_ID );
73106 while (runProgram ) {
74107 System .out .println ("Enter text:" );
75108 String text = scanner .nextLine ();
@@ -78,40 +111,10 @@ public static void main(String[] args) {
78111 runProgram = false ;
79112 System .out .println ("Program ended." );
80113 }
81- case ("--help" ) ->
82- System .out .println ("Type your text, press Enter to convert to speech. Type '--exit' to end the program." );
83- default -> {
84- System .out .println (text );
85- String newText = tts .replaceText (text );
86- System .out .println (newText );
87- boolean useSSML = !text .equals (newText );
88- try {
89- if (!useSSML ) {
90- speechStream = tts .synthesizeSpeech (polly , newText , VOICE_ID );
91- } else {
92- speechStream = tts .synthesizeSSMLSpeech (polly , newText , VOICE_ID );
93- }
94- if (speechStream == null ) {
95- System .out .println ("Error: Speech stream is null." );
96- continue ;
97- }
98- AdvancedPlayer player = new AdvancedPlayer (speechStream , javazoom .jl .player .FactoryRegistry .systemRegistry ().createAudioDevice ());
99- player .setPlayBackListener (new PlaybackListener () {
100- @ Override
101- public void playbackStarted (PlaybackEvent event ) {
102- System .out .println ("Playing speech..." );
103- }
104-
105- @ Override
106- public void playbackFinished (PlaybackEvent event ) {
107- System .out .println ("Speech finished playing." );
108- }
109- });
110- player .play ();
111- } catch (JavaLayerException e ) {
112- System .out .println ("Error playing speech. " + e );
113- }
114- }
114+ case ("--help" ) -> System .out .println ("Type your text, press Enter to convert to speech. " +
115+ "Type '--exit' to end the program." );
116+ default -> processSpeech (text );
117+
115118 }
116119 }
117120 }
0 commit comments