1111import org .slf4j .LoggerFactory ;
1212import org .slf4j .event .Level ;
1313import simplexity .Main ;
14- import simplexity .config .TTSConfig ;
15- import simplexity .messages .Errors ;
16- import simplexity .util .Util ;
14+ import simplexity .config .config .ConfigHandler ;
15+ import simplexity .config .config .SpeechEffectRule ;
16+ import simplexity .config .locale .Message ;
17+ import simplexity .util .Logging ;
1718
1819import java .io .InputStream ;
1920
@@ -22,73 +23,100 @@ public class SpeechHandler {
2223 private VoiceId voiceId ;
2324
2425 public SpeechHandler () {
25- this .voiceId = TTSConfig .getInstance ().getDefaultVoice ();
26+ this .voiceId = ConfigHandler .getInstance ().getDefaultVoice ();
27+ Logging .log (logger , "Initialized SpeechHandler with default voice: " + voiceId .toString (), Level .INFO );
2628 }
2729
30+ /**
31+ * Processes the given text, optionally replacing it based on configurations,
32+ * and synthesizes and plays the speech.
33+ */
2834 public void processSpeech (String text ) {
29- System . out . println ( text );
35+ // Replace text and determine if SSML is needed
3036 String processedText = replaceText (text );
31- System .out .println (processedText );
3237 boolean useSSML = !text .equals (processedText );
38+
39+ //Synthesize speech
3340 InputStream speechStream ;
3441 if (useSSML ) {
3542 speechStream = synthesizeSSMLSpeech (processedText , voiceId );
3643 } else {
3744 speechStream = synthesizeSpeech (processedText , voiceId );
3845 }
3946 if (speechStream == null ) {
40- Util .logAndPrint (logger , Errors . CAUGHT_EXCEPTION .replace ("%error%" , "Speech stream is null" ), Level .ERROR );
47+ Logging .logAndPrint (logger , Message . GENERAL_ERROR . getMessage () .replace ("%error%" , "Speech stream is null" ), Level .ERROR );
4148 return ;
4249 }
50+
51+ //play it
4352 playSpeech (speechStream );
4453 }
4554
55+ /**
56+ * Replaces text based on replacement mappings and updates the voice if a prefix is matched.
57+ */
58+
4659 public String replaceText (String text ) {
47- for (String key : TTSConfig .getInstance ().getReplaceText (). keySet ()) {
48- text = text . replace ( key , TTSConfig . getInstance (). getReplaceText (). get ( key ) );
60+ for (SpeechEffectRule effectRule : ConfigHandler .getInstance ().getEffectRules ()) {
61+ text = effectRule . applyRule ( text );
4962 }
50- for (String key : TTSConfig .getInstance ().getVoicePrefixes ().keySet ()) {
63+ for (String key : ConfigHandler .getInstance ().getVoicePrefixes ().keySet ()) {
5164 if (text .startsWith (key )) {
5265 text = text .replace (key , "" );
53- voiceId = TTSConfig .getInstance ().getVoicePrefixes ().get (key );
66+ voiceId = ConfigHandler .getInstance ().getVoicePrefixes ().get (key );
5467 }
5568 }
5669 return text ;
5770 }
5871
5972 public InputStream synthesizeSSMLSpeech (String text , VoiceId voice ) {
60- text = "<speak>" + text + "</speak>" ;
61- SynthesizeSpeechRequest synthesizeSpeechRequest ;
73+ String ssmlText = "<speak>" + text + "</speak>" ;
6274 try {
63- synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
64- .withText (text )
75+ SynthesizeSpeechRequest request = new SynthesizeSpeechRequest ()
76+ .withText (ssmlText )
6577 .withTextType (TextType .Ssml )
6678 .withVoiceId (voice )
6779 .withOutputFormat (OutputFormat .Mp3 );
68- SynthesizeSpeechResult synthesizeSpeechResult = Main .getPollyHandler ().getPolly ().synthesizeSpeech (synthesizeSpeechRequest );
69- return synthesizeSpeechResult .getAudioStream ();
80+ SynthesizeSpeechResult result = Main .getPollyHandler ().getPolly ().synthesizeSpeech (request );
81+ return result .getAudioStream ();
7082 } catch (RuntimeException exception ) {
71- Util . logAndPrint ( logger , Errors . CAUGHT_EXCEPTION . replace ( "%error%" , exception . getMessage ()), Level . ERROR );
83+ logSynthesisError ( exception , ssmlText );
7284 return null ;
7385 }
7486 }
7587
7688 public InputStream synthesizeSpeech (String text , VoiceId voice ) {
77- SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
78- .withText (text )
79- .withVoiceId (voice )
80- .withOutputFormat (OutputFormat .Mp3 );
81- SynthesizeSpeechResult synthesizeSpeechResult = Main .getPollyHandler ().getPolly ().synthesizeSpeech (synthesizeSpeechRequest );
82- 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+ }
83100 }
84101
102+ /**
103+ * Plays the text as speech
104+ */
85105 public void playSpeech (InputStream speechStream ) {
86106 AdvancedPlayer player ;
87107 try {
88108 player = new AdvancedPlayer (speechStream , FactoryRegistry .systemRegistry ().createAudioDevice ());
89109 player .play ();
90110 } catch (Exception exception ) {
91- Util .logAndPrint (logger , Errors . CAUGHT_EXCEPTION .replace ("%error%" , exception .getMessage ()), Level .ERROR );
111+ Logging .logAndPrint (logger , Message . GENERAL_ERROR . getMessage () .replace ("%error%" , exception .getMessage ()), Level .ERROR );
92112 }
93113 }
114+
115+ /**
116+ * Logs errors during speech synthesis.
117+ */
118+ private void logSynthesisError (Exception e , String text ) {
119+ Logging .logAndPrint (logger , Message .GENERAL_ERROR .getMessage ().replace ("%error%" , e .getMessage ()), Level .ERROR );
120+ Logging .logAndPrint (logger , Message .MESSAGE_NOT_PARSABLE .getMessage ().replace ("%message%" , text ), Level .ERROR );
121+ }
94122}
0 commit comments