Skip to content

Commit fbdbde5

Browse files
committed
more logging
1 parent 8c6b7eb commit fbdbde5

10 files changed

Lines changed: 104 additions & 38 deletions

File tree

pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,6 @@
8080
<artifactId>jlayer</artifactId>
8181
<version>1.0.1-1</version>
8282
</dependency>
83-
<dependency>
84-
<groupId>com.github.twitch4j</groupId>
85-
<artifactId>twitch4j</artifactId>
86-
<version>1.20.0</version>
87-
</dependency>
88-
<!-- https://mvnrepository.com/artifact/org.json/json -->
89-
<dependency>
90-
<groupId>org.json</groupId>
91-
<artifactId>json</artifactId>
92-
<version>20240303</version>
93-
</dependency>
9483
<dependency>
9584
<groupId>org.slf4j</groupId>
9685
<artifactId>slf4j-api</artifactId>

src/main/java/simplexity/Main.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import org.slf4j.event.Level;
56
import simplexity.amazon.PollyHandler;
67
import simplexity.amazon.SpeechHandler;
78
import simplexity.commands.CommandManager;
@@ -11,6 +12,7 @@
1112
import simplexity.config.TTSConfig;
1213
import simplexity.httpserver.LocalServer;
1314
import simplexity.setup.PollySetup;
15+
import simplexity.util.Util;
1416

1517
import java.util.Scanner;
1618

@@ -20,20 +22,18 @@ public class Main {
2022
public static PollyHandler pollyHandler;
2123
private static SpeechHandler speechHandler;
2224
public static Scanner scanner;
25+
public static boolean runApp = true;
2326

2427
public static void main(String[] args) {
25-
logger.info("Starting application");
28+
Util.log(logger, "Starting application", Level.INFO);
2629
scanner = new Scanner(System.in);
2730
commandManager = new CommandManager();
2831
registerCommands(commandManager);
2932
TTSConfig.getInstance().reloadConfig();
3033
PollySetup.setupPollyAndSpeech();
3134
LocalServer.run();
32-
while (true) {
35+
while (runApp) {
3336
String input = scanner.nextLine();
34-
if (input.equals("--exit")) {
35-
return;
36-
}
3737
if (!commandManager.runCommand(input)) {
3838
speechHandler.processSpeech(input);
3939
}

src/main/java/simplexity/amazon/SpeechHandler.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/java/simplexity/commands/CommandManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CommandManager {
1313
private static final Logger logger = LoggerFactory.getLogger(CommandManager.class);
1414

1515
public void registerCommand(Command command) {
16+
Util.log(logger, "Registering command: " + command.name, Level.INFO);
1617
commands.put(command.getName(), command);
1718
}
1819

src/main/java/simplexity/commands/ExitCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package simplexity.commands;
22

33
import org.slf4j.event.Level;
4+
import simplexity.Main;
45
import simplexity.httpserver.LocalServer;
56
import simplexity.messages.Output;
67
import simplexity.util.Util;
@@ -14,6 +15,7 @@ public ExitCommand(String name, String usage) {
1415
@Override
1516
public void execute() {
1617
Util.logAndPrint(logger, Output.SHUTTING_DOWN, Level.INFO);
18+
Main.runApp = false;
1719
LocalServer.stop();
1820
System.exit(0);
1921
}

src/main/java/simplexity/config/SimplexityFileHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,36 @@ private SimplexityFileHandler(){}
1616
private static SimplexityFileHandler instance;
1717
public static SimplexityFileHandler getInstance(){
1818
if(instance == null){
19+
Util.log(logger, "Creating a new instance of SimplexityFileHandler", Level.INFO);
1920
instance = new SimplexityFileHandler();
2021
}
22+
Util.log(logger, "Returning existing instance of SimplexityFileHandler", Level.INFO);
2123
return instance;
2224
}
2325
public File createOrLoadConfigFile(){
26+
Util.log(logger, "Attempting to create or load the config file: tts-config.conf", Level.INFO);
2427
File file = new File("tts-config.conf");
2528
if (file.exists()){
29+
Util.log(logger, "Config file already exists. Returning existing file.", Level.INFO);
2630
return file;
2731
}
32+
Util.log(logger, "Config file does not exist. Creating a new config file.", Level.WARN);
2833
createConfigFile();
2934
return file;
3035
}
3136

3237
private void createConfigFile(){
3338
File file = new File("tts-config.conf");
3439
try (FileWriter writer = new FileWriter(file)) {
40+
Util.log(logger, "Writing default configuration values to tts-config.conf", Level.INFO);
3541
writer.write(ConfigDefaults.AWS_REGION);
3642
writer.write(ConfigDefaults.AWS_ACCESS_KEY);
3743
writer.write(ConfigDefaults.AWS_SECRET_KEY);
3844
writer.write(ConfigDefaults.REPLACE_TEXT);
3945
writer.write(ConfigDefaults.DEFAULT_VOICE);
4046
writer.write(ConfigDefaults.VOICE_PREFIXES);
4147
writer.write(ConfigDefaults.SERVER_PORT);
48+
Util.log(logger, "Successfully created and wrote to tts-config.conf", Level.INFO);
4249
} catch (Exception exception){
4350
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.ERROR);
4451
}

src/main/java/simplexity/config/TTSConfig.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,48 @@ public VoiceId getDefaultVoice() {
4646
}
4747

4848
public void reloadConfig(){
49+
Util.log(logger, "Reloading TTS configuration", Level.INFO);
4950
Config config = initializeConfig();
51+
Util.log(logger, "Reloading replace text configuration", Level.INFO);
5052
reloadReplaceText(config);
53+
Util.log(logger, "Reloading voice prefixes configuration", Level.INFO);
5154
reloadVoicePrefixes(config);
55+
Util.log(logger, "Reloading AWS region configuration", Level.INFO);
5256
reloadRegion(config);
57+
Util.log(logger, "Reloading default voice configuration", Level.INFO);
5358
reloadDefaultVoice(config);
59+
Util.log(logger, "Reloading AWS credentials and server port", Level.INFO);
5460
reloadStrings(config);
5561
reloadInts(config);
62+
Util.log(logger, "TTS configuration reloaded successfully", Level.INFO);
5663

5764
}
5865

5966

6067

6168
private Config initializeConfig() {
69+
Util.log(logger, "Initializing configuration file from SimplexityFileHandler", Level.INFO);
6270
File configFile = SimplexityFileHandler.getInstance().createOrLoadConfigFile();
63-
return ConfigFactory.parseFile(configFile).resolve();
71+
Config config = ConfigFactory.parseFile(configFile).resolve();
72+
Util.log(logger, "Configuration file parsed successfully", Level.INFO);
73+
return config;
6474
}
6575

6676

6777
private void reloadReplaceText(Config config){
6878
replaceText.clear();
79+
Util.log(logger, "Clearing existing replace text entries", Level.DEBUG);
6980
config.getConfig("replace-text").entrySet().forEach(entry -> {
7081
replaceText.put(
7182
entry.getKey().replace("\"", ""),
7283
String.valueOf(entry.getValue().unwrapped()));
7384
});
85+
Util.log(logger, "Replace text configuration reloaded successfully", Level.INFO);
7486
}
7587

7688
private void reloadVoicePrefixes(Config config) {
7789
voicePrefixes.clear();
90+
Util.log(logger, "Clearing existing voice prefix entries", Level.DEBUG);
7891
config.getConfig("voice-prefixes").entrySet().forEach(entry -> {
7992
try {
8093
VoiceId voiceId = VoiceId.fromValue(String.valueOf(entry.getValue().unwrapped()));
@@ -83,36 +96,43 @@ private void reloadVoicePrefixes(Config config) {
8396
Util.logAndPrint(logger, Errors.INVALID_VOICE.replace("%voice%", entry.getValue().unwrapped().toString()), Level.ERROR);
8497
}
8598
});
99+
Util.log(logger, "Voice prefixes configuration reloaded successfully", Level.INFO);
86100
}
87101

88102
private void reloadRegion(Config config) {
89103
String region = config.getString("aws-region");
90104
try {
91105
awsRegion = Region.getRegion(Regions.valueOf(region));
106+
Util.log(logger, "AWS region set to: " + region, Level.INFO);
92107
} catch (IllegalArgumentException e) {
93108
Util.logAndPrint(logger, Errors.INVALID_REGION.replace("%region%", region), Level.ERROR);
94109
awsRegion = Region.getRegion(Regions.US_EAST_1);
110+
Util.log(logger, "AWS region defaulted to US_EAST_1", Level.WARN);
95111
}
96112
}
97113

98114
private void reloadDefaultVoice(Config config){
99115
String voiceString = config.getString("default-voice");
100116
try {
101117
defaultVoice = VoiceId.fromValue(voiceString);
118+
Util.log(logger, "Default voice set to: " + voiceString, Level.INFO);
102119
} catch (IllegalArgumentException e) {
103120
Util.logAndPrint(logger, Errors.INVALID_DEFAULT_VOICE.replace("%voice%", voiceString), Level.ERROR);
104121
defaultVoice = VoiceId.Brian;
122+
Util.log(logger, "Default voice set to fallback: Brian", Level.WARN);
105123
}
106124
}
107125

108126
private void reloadStrings(Config config){
109127
awsAccessID = config.getString("aws-access-id");
110128
awsSecretKey = config.getString("aws-secret-key");
129+
Util.log(logger, "AWS credentials loaded successfully", Level.INFO);
111130

112131
}
113132

114133
private void reloadInts(Config config){
115134
port = config.getInt("server-port");
135+
Util.log(logger, "Server port set to: " + port, Level.INFO);
116136
}
117137

118138

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package simplexity.messages;
2+
3+
public class LogInfo {
4+
5+
}

src/main/java/simplexity/messages/Output.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ public class Output {
1515
ConsoleColors.YELLOW_BRIGHT + "Please save your AWS credentials in tts-config.conf, then click enter to continue"
1616
+ ConsoleColors.RESET;
1717
public static final String SHUTTING_DOWN = ConsoleColors.YELLOW_BRIGHT + "CLI Text To Speech is closing..." + ConsoleColors.RESET;
18-
1918
}

src/main/java/simplexity/util/Util.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
33
import org.slf4j.Logger;
44
import org.slf4j.event.Level;
55

6+
import java.util.regex.Pattern;
7+
68
public class Util {
79

8-
public static void logAndPrint(Logger logger, String message, Level level){
9-
logger.atLevel(level).log(message);
10+
private static final Pattern ANSI_PATTERN = Pattern.compile("\u001B\\[[;\\d]*m");
11+
12+
public static void logAndPrint(Logger logger, String message, Level level) {
13+
String logMessage = stripAnsiCodes(message);
14+
logger.atLevel(level).log(logMessage);
1015
System.out.print(message);
1116
}
17+
18+
public static void log(Logger logger, String message, Level level) {
19+
logger.atLevel(level).log(message);
20+
}
21+
22+
public static String stripAnsiCodes(String text) {
23+
return ANSI_PATTERN.matcher(text).replaceAll("");
24+
}
1225
}

0 commit comments

Comments
 (0)