Skip to content

Commit abeca5a

Browse files
committed
less than three
1 parent 0e5f87c commit abeca5a

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

TTSJava/src/main/java/simplexity/clitts/TTSConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public static void reloadConfig() {
4444
System.out.println("Please update your config file to use the correct region.");
4545
}
4646
String voice = config.getString("default-voice");
47+
System.out.println(voice);
4748
try {
48-
VoiceId.valueOf(voice);
49+
defaultVoice = VoiceId.valueOf(voice);
4950
} catch (IllegalArgumentException e) {
5051
System.out.println("Error: " + voice + " is not a valid voice. " +
5152
"Please make sure you are only choosing from standard voices"

TTSJava/src/main/java/simplexity/clitts/TextToSpeech.java

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import com.amazonaws.ClientConfiguration;
44
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
55
import com.amazonaws.regions.Region;
6-
import com.amazonaws.regions.Regions;
76
import com.amazonaws.services.polly.AmazonPolly;
87
import 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.*;
129
import javazoom.jl.decoder.JavaLayerException;
1310
import javazoom.jl.player.advanced.AdvancedPlayer;
1411
import javazoom.jl.player.advanced.PlaybackEvent;
@@ -19,8 +16,7 @@
1916

2017
public 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() {

TTSJava/src/main/resources/application.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
aws-region = "US_EAST_1"
22
default-voice = "Kimberly"
33
replace-text {
4+
"**" = "<prosody volume=\"x-loud\" pitch=\"low\" rate=\"slow\">"
5+
"/*" = "</prosody>"
6+
"*/" = "</prosody>"
7+
"~~" = "<amazon:effect name=\"whispered\">"
8+
"/~" = "</amazon:effect>"
9+
"~/" = "</amazon:effect>"
10+
"__" = "<emphasis level=\"strong\">"
411
"/_" = "</emphasis>"
512
"_/" = "</emphasis>"
613
"++" = "<prosody volume=\"x-loud\" rate=\"fast\">"

0 commit comments

Comments
 (0)