Skip to content

Commit e193164

Browse files
committed
eyy it works
1 parent abeca5a commit e193164

File tree

5 files changed

+85
-62
lines changed

5 files changed

+85
-62
lines changed

TTSJava/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>simplexity.clitts</groupId>
8-
<artifactId>TTSJava</artifactId>
9-
<version>1.0-SNAPSHOT</version>
8+
<artifactId>CLI-TTS</artifactId>
9+
<version>0.0.1</version>
1010

1111
<build>
1212
<plugins>
@@ -17,7 +17,7 @@
1717
<configuration>
1818
<archive>
1919
<manifest>
20-
<mainClass>simplexity.clitts.Main</mainClass>
20+
<mainClass>simplexity.clitts.TextToSpeech</mainClass>
2121
</manifest>
2222
</archive>
2323
</configuration>

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,60 @@ public class TTSConfig {
1818

1919
public static void reloadConfig() {
2020
Config config = ConfigFactory.load();
21+
reloadReplaceText(config);
22+
reloadVoicePrefixes(config);
23+
reloadRegion(config);
24+
reloadDefaultVoice(config);
25+
}
26+
27+
private static void reloadReplaceText(Config config) {
2128
replaceText.clear();
22-
voicePrefixes.clear();
2329
config.getConfig("replace-text").entrySet().forEach(entry -> {
24-
replaceText.put(entry.getKey(), entry.getValue().unwrapped().toString());
30+
replaceText.put(entry.getKey().replace("\"", ""), entry.getValue().unwrapped().toString());
2531
});
32+
}
33+
34+
private static void reloadVoicePrefixes(Config config) {
35+
voicePrefixes.clear();
2636
config.getConfig("voice-prefixes").entrySet().forEach(entry -> {
2737
try {
2838
VoiceId voiceId = VoiceId.fromValue(String.valueOf(entry.getValue().unwrapped()));
29-
voicePrefixes.put(entry.getKey(), voiceId);
39+
voicePrefixes.put(entry.getKey().replace("\"", ""), voiceId);
3040
} catch (IllegalArgumentException e) {
31-
System.out.println("Error: " + entry.getValue().unwrapped() + " is not a valid voice. " +
32-
"Please make sure you are only choosing from standard voices"
33-
+ "\nStandard voices can be found here: " + "https://docs.aws.amazon.com/polly/latest/dg/voicelist.html");
41+
System.out.println("Error: " + entry.getValue().unwrapped() +
42+
" is not a valid voice. " +
43+
"Please make sure you are only choosing from standard voices" +
44+
"\nStandard voices can be found here: " +
45+
"https://docs.aws.amazon.com/polly/latest/dg/voicelist.html");
3446
}
3547
});
48+
}
49+
50+
private static void reloadRegion(Config config) {
3651
String region = config.getString("aws-region");
3752
try {
3853
AWS_REGION = Region.getRegion(Regions.valueOf(region));
3954
} catch (IllegalArgumentException e) {
40-
System.out.println("Error: " + region + " is not a valid region. "
41-
+ "\nAWS Regions can be found here: " + "https://aws.amazon.com/about-aws/global-infrastructure/regions_az/");
55+
System.out.println("Error: " + region +
56+
" is not a valid region. " +
57+
"\nAWS Regions can be found here: " +
58+
"https://aws.amazon.com/about-aws/global-infrastructure/regions_az/");
4259
AWS_REGION = Region.getRegion(Regions.US_EAST_1); // Default to US East 1 if region is invalid.
4360
System.out.println("Using default region: " + AWS_REGION.getName());
4461
System.out.println("Please update your config file to use the correct region.");
4562
}
63+
}
64+
65+
private static void reloadDefaultVoice(Config config) {
4666
String voice = config.getString("default-voice");
47-
System.out.println(voice);
4867
try {
4968
defaultVoice = VoiceId.valueOf(voice);
5069
} catch (IllegalArgumentException e) {
5170
System.out.println("Error: " + voice + " is not a valid voice. " +
52-
"Please make sure you are only choosing from standard voices"
53-
+ "\nStandard voices can be found here: " + "https://docs.aws.amazon.com/polly/latest/dg/voicelist.html");
71+
"Please make sure you are only choosing from standard voices" +
72+
"\nStandard voices can be found here: " +
73+
"https://docs.aws.amazon.com/polly/latest/dg/voicelist.html");
5474
defaultVoice = VoiceId.Kimberly; // Default to Kimberly if voice is invalid.
55-
System.out.println("Using default voice: " + VoiceId.Kimberly);
5675
}
5776
}
5877

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

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import com.amazonaws.regions.Region;
66
import com.amazonaws.services.polly.AmazonPolly;
77
import 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;
912
import javazoom.jl.decoder.JavaLayerException;
1013
import javazoom.jl.player.advanced.AdvancedPlayer;
11-
import javazoom.jl.player.advanced.PlaybackEvent;
12-
import javazoom.jl.player.advanced.PlaybackListener;
1314

1415
import java.io.InputStream;
1516
import 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
}

TTSJava/src/main/resources/application.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ replace-text {
1010
"__" = "<emphasis level=\"strong\">"
1111
"/_" = "</emphasis>"
1212
"_/" = "</emphasis>"
13-
"++" = "<prosody volume=\"x-loud\" rate=\"fast\">"
13+
"++" = "<prosody volume=\"x-loud\" rate=\"x-fast\" pitch=\"x-high\">"
1414
"/+" = "</prosody>"
1515
"+/" = "</prosody>"
1616
"!!" = "<say-as interpret-as=\"expletive\">"
1717
"/!" = "</say-as>"
1818
"!/" = "</say-as>"
19-
"-" = "<break time=\"300ms\"/>"
19+
" - " = "<break time=\"300ms\"/>"
20+
"<3" = "heart emoji"
2021
}
2122
voice-prefixes {
2223
"R:" = "Salli"

text_replace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
"!!": "<say-as interpret-as=\"expletive\">",
1515
"/!": "</say-as>",
1616
"!/": "</say-as>",
17-
"-": "<break time=\"300ms\"/>"
17+
" - ": "<break time=\"300ms\"/>"
1818
}

0 commit comments

Comments
 (0)