Skip to content

Commit

Permalink
Added Voice 'Speed' menu
Browse files Browse the repository at this point in the history
  • Loading branch information
jame25 authored Mar 26, 2024
1 parent 03ef690 commit ac934c4
Showing 1 changed file with 118 additions and 13 deletions.
131 changes: 118 additions & 13 deletions TrayApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public class TrayApplicationContext : ApplicationContext
private string PiperArgs;
private const string SoxArgs = "-t raw -b 16 -e signed-integer -r 22050 -c 1 - -t waveaudio pad 0 1";
private const string SettingsFile = "settings.conf";
private double currentSpeed = 1.0;
private string model;

private NotifyIcon trayIcon;
private ToolStripMenuItem voiceMenuItem;
Expand Down Expand Up @@ -115,9 +117,52 @@ public class TrayApplicationContext : ApplicationContext
private Thread monitoringThread;
private System.Threading.SynchronizationContext _syncContext;

private void UpdateSpeed(bool isFaster)
{
if (isFaster)
{
if (currentSpeed > 0.1)
currentSpeed = Math.Max(currentSpeed - 0.1, 0.1);
}
else
{
if (currentSpeed < 1.0)
currentSpeed = Math.Min(currentSpeed + 0.1, 1.0);
}

// Round the currentSpeed to one decimal place
currentSpeed = Math.Round(currentSpeed, 1);

// Update the PiperArgs with the new speed value
string currentModel = PiperArgs.Split(new[] { "--model" }, StringSplitOptions.None)[1].Trim().Split(' ')[0];
PiperArgs = $"--model {currentModel} --length_scale {currentSpeed} --output-raw";

// Save the updated settings to the settings.conf file
SaveSettings();
}

private void ResetSpeed()
{
currentSpeed = 1.0;

// Update the PiperArgs with the default speed value
string currentModel = PiperArgs.Split(new[] { "--model" }, StringSplitOptions.None)[1].Trim().Split(' ')[0];
PiperArgs = $"--model {currentModel} --length_scale {currentSpeed} --output-raw";

// Save the updated settings to the settings.conf file
SaveSettings();
}


private void UpdatePiperArgs()
{
PiperArgs = $"--model {model} --length_scale {currentSpeed} --output-raw";
}


private void ShowAboutWindow()
{
string version = "1.1.5";
string version = "1.1.6";
string message = $"Piper Tray\n\nVersion: {version}\n\nDeveloped by jame25";
string url = "https://github.com/jame25/Piper-Tray";

Expand Down Expand Up @@ -221,6 +266,22 @@ public TrayApplicationContext()
PopulateVoiceModels(voiceMenuItem);
contextMenu.Items.Add(voiceMenuItem);

// Add the 'Speed' menu item
ToolStripMenuItem speedMenuItem = new ToolStripMenuItem("Speed");
ToolStripMenuItem fasterMenuItem = new ToolStripMenuItem("Faster");
ToolStripMenuItem slowerMenuItem = new ToolStripMenuItem("Slower");
ToolStripMenuItem resetMenuItem = new ToolStripMenuItem("Reset");

fasterMenuItem.Click += (sender, e) => UpdateSpeed(true);
slowerMenuItem.Click += (sender, e) => UpdateSpeed(false);
resetMenuItem.Click += (sender, e) => ResetSpeed();

speedMenuItem.DropDownItems.Add(fasterMenuItem);
speedMenuItem.DropDownItems.Add(slowerMenuItem);
speedMenuItem.DropDownItems.Add(resetMenuItem);

contextMenu.Items.Add(speedMenuItem);

contextMenu.Items.Add("About", null, AboutItem_Click);
contextMenu.Items.Add("Exit", null, ExitItem_Click);

Expand Down Expand Up @@ -348,6 +409,42 @@ private void LoadSettings()
EnableLogging = enableLogging;
}

private void SaveSettings(string newModel = null)
{
try
{
string[] lines = File.ReadAllLines(SettingsFile);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith("model="))
{
if (newModel != null)
{
lines[i] = $"model={newModel}";
}
}
else if (lines[i].StartsWith("speed="))
{
lines[i] = $"speed={currentSpeed.ToString("0.0")}";
}
else if (lines[i].StartsWith("logging="))
{
lines[i] = $"logging={EnableLogging}";
}
}
File.WriteAllLines(SettingsFile, lines);
}
catch (IOException ex)
{
// Handle the exception if the file is in use or cannot be accessed
LogError(ex);
// You can choose to display an error message to the user or take appropriate action
}
}




private string GetDefaultModel()
{
string[] onnxFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.onnx");
Expand Down Expand Up @@ -393,8 +490,8 @@ private void PopulateVoiceModels(ToolStripMenuItem voiceMenuItem)

private void ChangeVoiceModel(string modelName)
{
PiperArgs = $"--model {modelName}.onnx --length_scale 1 --output-raw";
SaveSettings();
PiperArgs = $"--model {modelName}.onnx --length_scale {currentSpeed} --output-raw";
SaveSettings(modelName);

// Update the check mark for the selected voice model
foreach (ToolStripItem item in voiceMenuItem.DropDownItems)
Expand All @@ -410,21 +507,29 @@ private void ChangeVoiceModel(string modelName)

private void SaveSettings()
{
string model = PiperArgs.Split(new[] { "--model" }, StringSplitOptions.None)[1].Trim().Split(' ')[0];
string speed = PiperArgs.Split(new[] { "--length_scale" }, StringSplitOptions.None)[1].Trim().Split(' ')[0];
string loggingValue = EnableLogging ? "true" : "false";
try
{
string model = PiperArgs.Split(new[] { "--model" }, StringSplitOptions.None)[1].Trim().Split(' ')[0];
string speed = currentSpeed.ToString("0.0");
string loggingValue = EnableLogging ? "true" : "false";

string[] lines = {
$"model={model}",
$"speed={speed}",
$"logging={loggingValue}"
};
string[] lines = {
$"model={model}",
$"speed={speed}",
$"logging={loggingValue}"
};

File.WriteAllLines(SettingsFile, lines);
File.WriteAllLines(SettingsFile, lines);
}
catch (IOException ex)
{
// Handle the exception if the file is in use or cannot be accessed
LogError(ex);
// You can choose to display an error message to the user or take appropriate action
}
}



private void MonitoringItem_Click(object sender, EventArgs e)
{
isMonitoringEnabled = !isMonitoringEnabled;
Expand Down

0 comments on commit ac934c4

Please sign in to comment.