Skip to content

Commit

Permalink
Handle exception on the main program better
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 30, 2024
1 parent 3c76f14 commit c2db756
Showing 1 changed file with 60 additions and 51 deletions.
111 changes: 60 additions & 51 deletions ButterSTT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,69 @@
using ButterSTT.Config;
using ButterSTT.MessageSystem;

// Load config
JsonConfigHandler<STTConfig> configHandler =
new("config.json", JsonConfigHandler<STTConfig>.Context.STTConfig);
STTConfig config = configHandler.InitializeConfig(STTConfig.Default);

// Fill in new values if the version changed
if (config.ConfigVersion < STTConfig.Default.ConfigVersion)
try
{
var backupFile = $"{configHandler.ConfigFilePath}.old";
configHandler.MakeBackup(backupFile);
Console.WriteLine(
$"Backed up the current config file to \"{backupFile}\", upgrading from v{config.ConfigVersion} to v{STTConfig.Default.ConfigVersion}..."
);
config.ConfigVersion = STTConfig.Default.ConfigVersion;
configHandler.WriteConfig(config);
}
// Load config
JsonConfigHandler<STTConfig> configHandler =
new("config.json", JsonConfigHandler<STTConfig>.Context.STTConfig);
STTConfig config = configHandler.InitializeConfig(STTConfig.Default);

// Setup model dir
var modelsDir = new DirectoryInfo(config.ModelsPath);
modelsDir.Create();
// Fill in new values if the version changed
if (config.ConfigVersion < STTConfig.Default.ConfigVersion)
{
var backupFile = $"{configHandler.ConfigFilePath}.old";
configHandler.MakeBackup(backupFile);
Console.WriteLine(
$"Backed up the current config file to \"{backupFile}\", upgrading from v{config.ConfigVersion} to v{STTConfig.Default.ConfigVersion}..."
);
config.ConfigVersion = STTConfig.Default.ConfigVersion;
configHandler.WriteConfig(config);
}

// Find the first model available
var modelFile =
modelsDir
.GetFiles()
.FirstOrDefault(
file =>
file != null
&& file.Extension.Equals(".april", StringComparison.CurrentCultureIgnoreCase),
null
)
?? throw new FileNotFoundException(
$"Could not find any available AprilAsr models (*.april) in \"{modelsDir}\"."
);
// Setup model dir
var modelsDir = new DirectoryInfo(config.ModelsPath);
modelsDir.Create();

var messageQueue = new MessageQueue()
{
MessageLength = config.MessageLength,
MaxWordsDequeued = config.MaxWordsDequeued < 0 ? int.MaxValue : config.MaxWordsDequeued,
RealtimeQueuePadding = config.RealtimeQueuePadding,
WordTime = config.WordTime,
HardWordTime = config.HardWordTime,
};
using var oscHandler = new OSCMessageHandler(messageQueue, config.OSCEndpoint)
{
RateLimit = config.OSCChatboxRateLimit
};
oscHandler.StartMessageLoop();
// Find the first model available
var modelFile =
modelsDir
.GetFiles()
.FirstOrDefault(
file =>
file != null
&& file.Extension.Equals(".april", StringComparison.CurrentCultureIgnoreCase),
null
)
?? throw new FileNotFoundException(
$"Could not find any available AprilAsr models (*.april) in \"{modelsDir}\"."
);

using var speechToTextHandler = new SpeechToTextHandler(
modelFile,
oscHandler.MessageQueue,
deviceNumber: config.MicrophoneDeviceNumber
);
speechToTextHandler.StartRecording();
var messageQueue = new MessageQueue()
{
MessageLength = config.MessageLength,
MaxWordsDequeued = config.MaxWordsDequeued < 0 ? int.MaxValue : config.MaxWordsDequeued,
RealtimeQueuePadding = config.RealtimeQueuePadding,
WordTime = config.WordTime,
HardWordTime = config.HardWordTime,
};
using var oscHandler = new OSCMessageHandler(messageQueue, config.OSCEndpoint)
{
RateLimit = config.OSCChatboxRateLimit
};
oscHandler.StartMessageLoop();

Console.ReadLine();
using var speechToTextHandler = new SpeechToTextHandler(
modelFile,
oscHandler.MessageQueue,
deviceNumber: config.MicrophoneDeviceNumber
);
speechToTextHandler.StartRecording();

Console.ReadLine();
}
catch (Exception e)
{
Console.Error.WriteLine(e);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}

0 comments on commit c2db756

Please sign in to comment.