11package simplexity .clitts ;
22
3- import com .amazonaws .ClientConfiguration ;
4- import com .amazonaws .auth .BasicAWSCredentials ;
5- import com .amazonaws .regions .Region ;
6- import com .amazonaws .services .polly .AmazonPolly ;
7- import com .amazonaws .services .polly .AmazonPollyClient ;
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 ;
12- import javazoom .jl .decoder .JavaLayerException ;
13- import javazoom .jl .player .advanced .AdvancedPlayer ;
3+ import simplexity .clitts .twitch .TwitchAuthServer ;
4+ import simplexity .clitts .util .Messages ;
145
15- import java .io .InputStream ;
6+ import java .io .IOException ;
167import java .util .Scanner ;
178
189public class TextToSpeech {
1910
20- private static Region AWS_REGION ;
11+
2112 private static final Scanner scanner = new Scanner (System .in );
22- private static AmazonPolly polly ;
23- private static VoiceId VOICE_ID ;
2413 private static boolean runProgram = true ;
2514 private static TextToSpeech instance ;
26- private static final String boldRed = "\u001b [31;1m" ;
27- private static final String formatReset = "\u001b [0m" ;
28- private static final String boldGreen = "\u001b [32;1m" ;
29- private static final String cyan = "\u001b [36m" ;
30- private static final String yellow = "\u001b [33m" ;
31-
32- public TextToSpeech () {
33- polly = new AmazonPollyClient (new BasicAWSCredentials (TTSConfig .getInstance ().getAccessID (), TTSConfig .getInstance ().getAccessSecret ()), new ClientConfiguration ());
34- polly .setRegion (AWS_REGION );
35- }
3615
3716 public static TextToSpeech getInstance () {
3817 if (instance == null ) {
@@ -41,102 +20,78 @@ public static TextToSpeech getInstance() {
4120 return instance ;
4221 }
4322
44- public InputStream synthesizeSpeech (AmazonPolly polly , String text , VoiceId voice ) {
45- SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
46- .withText (text )
47- .withVoiceId (voice )
48- .withOutputFormat (OutputFormat .Mp3 );
49- SynthesizeSpeechResult synthesizeSpeechResult = polly .synthesizeSpeech (synthesizeSpeechRequest );
50- return synthesizeSpeechResult .getAudioStream ();
51- }
52-
53- public InputStream synthesizeSSMLSpeech (AmazonPolly polly , String text , VoiceId voice ) {
54- String ssml = "<speak>" + text + "</speak>" ;
55- SynthesizeSpeechRequest synthesizeSpeechRequest ;
56- try {
57- synthesizeSpeechRequest = new SynthesizeSpeechRequest ()
58- .withText (ssml )
59- .withTextType (com .amazonaws .services .polly .model .TextType .Ssml )
60- .withVoiceId (voice )
61- .withOutputFormat (OutputFormat .Mp3 );
62- SynthesizeSpeechResult synthesizeSpeechResult = polly .synthesizeSpeech (synthesizeSpeechRequest );
63- return synthesizeSpeechResult .getAudioStream ();
64- } catch (RuntimeException e ) {
65- System .out .println (boldRed + "Error: " + e .getMessage () + formatReset );
66- return null ;
67- }
68- }
69-
70- public String replaceText (String text ) {
71- for (String key : TTSConfig .getInstance ().getReplaceText ().keySet ()) {
72- text = text .replace (key , TTSConfig .getInstance ().getReplaceText ().get (key ));
73- }
74- for (String key : TTSConfig .getInstance ().getVoicePrefixes ().keySet ()) {
75- if (text .startsWith (key )) {
76- text = text .replace (key , "" );
77- VOICE_ID = TTSConfig .getInstance ().getVoicePrefixes ().get (key );
78- }
79- }
80- return text ;
81- }
82-
83- public static void processSpeech (String text ) {
84- TextToSpeech tts = getInstance ();
85- String newText = tts .replaceText (text );
86- boolean useSSML = !text .equals (newText );
87- try {
88- InputStream speechStream ;
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 (boldRed + "Error: Speech stream is null." + formatReset );
96- return ;
97- }
98- AdvancedPlayer player = new AdvancedPlayer (speechStream ,
99- javazoom .jl .player .FactoryRegistry .systemRegistry ().createAudioDevice ());
100- player .play ();
101- } catch (JavaLayerException e ) {
102- System .out .println (boldRed + "Error playing speech. " + e + formatReset );
103- }
104- }
105-
10623 public static void main (String [] args ) {
10724 TTSConfig .getInstance ().reloadConfig ();
108- System .out .println ("Type your text then press" + cyan + " Enter" + formatReset + " to convert to speech."
109- + "\n Type" + cyan + " --reload" + formatReset + " to reload the configuration."
110- + "\n Type " + cyan + "--exit" + formatReset + " to end the program." );
111- VOICE_ID = TTSConfig .getInstance ().getDefaultVoice ();
112- AWS_REGION = TTSConfig .getInstance ().getRegion ();
25+ System .out .println (Messages .startupMessage );
26+ SpeechHandling .VOICE_ID = TTSConfig .getInstance ().getDefaultVoice ();
27+ SpeechHandling .AWS_REGION = TTSConfig .getInstance ().getRegion ();
28+ checkTwitch ();
11329 while (runProgram ) {
114- System .out .println (yellow + "Enter text:" + formatReset );
30+ System .out .println (Messages . enterText );
11531 String text = scanner .nextLine ();
11632 switch (text ) {
11733 case ("--exit" ) -> {
11834 runProgram = false ;
119- System .out .println ("Program ended." );
35+ System .out .println (Messages . exitMessage );
12036 }
121- case ("--help" ) -> System .out .println ("Type your text, press Enter to convert to speech. " +
122- "Type '--exit' to end the program." );
37+ case ("--help" ) -> System .out .println (Messages .helpMessage );
12338 case ("--reload" ) -> {
12439 TTSConfig .getInstance ().reloadConfig ();
125- System .out .println (boldGreen + "Config Reloaded" + formatReset );
40+ System .out .println (Messages . reloadMessage );
12641 }
12742 default -> {
12843 if (TTSConfig .getInstance ().getAccessID ().isBlank () || TTSConfig .getInstance ().getAccessID ().isEmpty ()) {
129- System .out .println (boldRed + "AWS Access ID is not set. Please set your AWS Access ID and Access secret then use '--reload'" + formatReset );
44+ System .out .println (Messages . secretOrIDNotSet );
13045 continue ;
13146 }
13247 if (TTSConfig .getInstance ().getAccessSecret ().isBlank () || TTSConfig .getInstance ().getAccessSecret ().isEmpty ()) {
133- System .out .println (boldRed + "AWS Access Secret is not set. Please set your AWS Access ID and Access secret then use '--reload'" + formatReset );
48+ System .out .println (Messages . secretOrIDNotSet );
13449 continue ;
13550 }
136- processSpeech (text );
51+ SpeechHandling .getInstance ().processSpeech (text );
52+ }
53+ }
54+ }
55+ }
56+
57+ private static void checkTwitch () {
58+ if (TTSConfig .getInstance ().connectToTwitch ()) {
59+ String channel = TTSConfig .getInstance ().getTwitchChannel ();
60+ System .out .println (Messages .currentlyConnectedTo + channel + Messages .stayConnectedPrompt );
61+ boolean validInput = false ;
62+ while (!validInput ) {
63+ String input = scanner .nextLine ();
64+ input = input .toLowerCase ();
65+ switch (input ) {
66+ case ("y" ) -> {
67+ validInput = true ;
68+ checkTwitchAuth ();
69+
70+ }
71+ case ("n" ) -> {
72+ validInput = true ;
73+ }
74+ default -> System .out .println (Messages .stayConnectedError );
13775 }
13876
13977 }
14078 }
14179 }
80+
81+ private static void checkTwitchAuth () {
82+ if (TTSConfig .getInstance ().connectToTwitch ()) {
83+ String authCode = TTSConfig .getInstance ().getTwitchAuthCode ();
84+ if (authCode == null || authCode .isBlank ()) {
85+ System .out .println (Messages .needTwitchAuth );
86+ System .out .println (Messages .visitLink + Messages .twitchAuthLink );
87+ try {
88+ TwitchAuthServer .run ();
89+ } catch (IOException e ) {
90+ System .out .println (Messages .twitchAuthError );
91+ e .printStackTrace ();
92+ }
93+ }
94+ }
95+ }
96+
14297}
0 commit comments