@@ -23,11 +23,19 @@ public class SpeechHandler {
2323
2424 public SpeechHandler () {
2525 this .voiceId = TTSConfig .getInstance ().getDefaultVoice ();
26+ Util .log (logger , "Initialized SpeechHandler with default voice: " + voiceId .toString (), Level .INFO );
2627 }
2728
29+ /**
30+ * Processes the given text, optionally replacing it based on configurations,
31+ * and synthesizes and plays the speech.
32+ */
2833 public void processSpeech (String text ) {
34+ // Replace text and determine if SSML is needed
2935 String processedText = replaceText (text );
3036 boolean useSSML = !text .equals (processedText );
37+
38+ //Synthesize speech
3139 InputStream speechStream ;
3240 if (useSSML ) {
3341 speechStream = synthesizeSSMLSpeech (processedText , voiceId );
@@ -38,49 +46,63 @@ public void processSpeech(String text) {
3846 Util .logAndPrint (logger , Errors .CAUGHT_EXCEPTION .replace ("%error%" , "Speech stream is null" ), Level .ERROR );
3947 return ;
4048 }
49+
50+ //play it
4151 playSpeech (speechStream );
4252 }
4353
54+ /**
55+ * Replaces text based on replacement mappings and updates the voice if a prefix is matched.
56+ */
57+
4458 public String replaceText (String text ) {
45- for (String key : TTSConfig .getInstance ().getReplaceText ().keySet ()) {
46- text = text .replace (key , TTSConfig .getInstance ().getReplaceText ().get (key ));
59+ TTSConfig ttsConfig = TTSConfig .getInstance ();
60+ for (String key : ttsConfig .getReplaceText ().keySet ()) {
61+ text = text .replace (key , ttsConfig .getReplaceText ().get (key ));
4762 }
48- for (String key : TTSConfig . getInstance () .getVoicePrefixes ().keySet ()) {
63+ for (String key : ttsConfig .getVoicePrefixes ().keySet ()) {
4964 if (text .startsWith (key )) {
5065 text = text .replace (key , "" );
51- voiceId = TTSConfig . getInstance () .getVoicePrefixes ().get (key );
66+ voiceId = ttsConfig .getVoicePrefixes ().get (key );
5267 }
5368 }
5469 return text ;
5570 }
5671
5772 public InputStream synthesizeSSMLSpeech (String text , VoiceId voice ) {
58- text = "<speak>" + text + "</speak>" ;
59- SynthesizeSpeechRequest synthesizeSpeechRequest ;
73+ String ssmlText = "<speak>" + text + "</speak>" ;
6074 try {
61- synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
62- .withText (text )
75+ SynthesizeSpeechRequest request = new SynthesizeSpeechRequest ()
76+ .withText (ssmlText )
6377 .withTextType (TextType .Ssml )
6478 .withVoiceId (voice )
6579 .withOutputFormat (OutputFormat .Mp3 );
66- SynthesizeSpeechResult synthesizeSpeechResult = Main .getPollyHandler ().getPolly ().synthesizeSpeech (synthesizeSpeechRequest );
67- return synthesizeSpeechResult .getAudioStream ();
80+ SynthesizeSpeechResult result = Main .getPollyHandler ().getPolly ().synthesizeSpeech (request );
81+ return result .getAudioStream ();
6882 } catch (RuntimeException exception ) {
69- Util .logAndPrint (logger , Errors .CAUGHT_EXCEPTION .replace ("%error%" , exception .getMessage ()), Level .ERROR );
70- Util .logAndPrint (logger , Errors .MESSAGE_NOT_PARSABLE .replace ("%message%" , text ), Level .ERROR );
83+ logSynthesisError (exception , ssmlText );
7184 return null ;
7285 }
7386 }
7487
7588 public InputStream synthesizeSpeech (String text , VoiceId voice ) {
76- SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
77- .withText (text )
78- .withVoiceId (voice )
79- .withOutputFormat (OutputFormat .Mp3 );
80- SynthesizeSpeechResult synthesizeSpeechResult = Main .getPollyHandler ().getPolly ().synthesizeSpeech (synthesizeSpeechRequest );
81- return synthesizeSpeechResult .getAudioStream ();
89+ try {
90+ SynthesizeSpeechRequest request = new SynthesizeSpeechRequest ()
91+ .withText (text )
92+ .withVoiceId (voice )
93+ .withOutputFormat (OutputFormat .Mp3 );
94+ SynthesizeSpeechResult result = Main .getPollyHandler ().getPolly ().synthesizeSpeech (request );
95+ return result .getAudioStream ();
96+ } catch (RuntimeException exception ) {
97+ logSynthesisError (exception , text );
98+ return null ;
99+ }
82100 }
83101
102+ /**
103+ * Plays the text as speech
104+ *
105+ */
84106 public void playSpeech (InputStream speechStream ) {
85107 AdvancedPlayer player ;
86108 try {
@@ -90,4 +112,12 @@ public void playSpeech(InputStream speechStream) {
90112 Util .logAndPrint (logger , Errors .CAUGHT_EXCEPTION .replace ("%error%" , exception .getMessage ()), Level .ERROR );
91113 }
92114 }
115+
116+ /**
117+ * Logs errors during speech synthesis.
118+ */
119+ private void logSynthesisError (Exception e , String text ) {
120+ Util .logAndPrint (logger , Errors .CAUGHT_EXCEPTION .replace ("%error%" , e .getMessage ()), Level .ERROR );
121+ Util .logAndPrint (logger , Errors .MESSAGE_NOT_PARSABLE .replace ("%message%" , text ), Level .ERROR );
122+ }
93123}
0 commit comments