Skip to content

Commit

Permalink
implement windows TTS
Browse files Browse the repository at this point in the history
  • Loading branch information
marzent committed Mar 24, 2023
1 parent 88410b6 commit 080688c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 37 deletions.
1 change: 1 addition & 0 deletions IINACT/IINACT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

<ItemGroup>
<PackageReference Include="DalamudPackager" Version="2.1.10" />
<PackageReference Include="System.Speech" Version="7.0.0" />
<Reference Include="FFXIVClientStructs">
<HintPath>$(DalamudLibPath)FFXIVClientStructs.dll</HintPath>
<Private>false</Private>
Expand Down
5 changes: 3 additions & 2 deletions IINACT/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class Plugin : IDalamudPlugin
[PluginService] internal static ChatGui ChatGui { get; private set; } = null!;
// ReSharper restore UnusedAutoPropertyAccessor.Local
internal Configuration Configuration { get; init; }

private TextToSpeechProvider TextToSpeechProvider { get; init; }
private ConfigWindow ConfigWindow { get; init; }
private MainWindow MainWindow { get; init; }
internal FileDialogManager FileDialogManager { get; init; }
Expand All @@ -51,7 +51,8 @@ public Plugin()

Configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
Configuration.Initialize(PluginInterface);


this.TextToSpeechProvider = new TextToSpeechProvider();
Advanced_Combat_Tracker.ActGlobals.oFormActMain.LogFilePath = Configuration.LogFilePath;

FfxivActPluginWrapper = new FfxivActPluginWrapper(Configuration, DataManager.Language, ChatGui);
Expand Down
65 changes: 65 additions & 0 deletions IINACT/TextToSpeechProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Diagnostics;
using System.Speech.Synthesis;
using System.Text.RegularExpressions;

namespace IINACT;

internal class TextToSpeechProvider
{
private string binary = "/usr/bin/say";
private string args = "";
private readonly object speechLock = new();
private readonly SpeechSynthesizer speechSynthesizer = new();

public TextToSpeechProvider()
{
speechSynthesizer.SetOutputToDefaultAudioDevice();
Advanced_Combat_Tracker.ActGlobals.oFormActMain.TextToSpeech += Speak;
}

public void Speak(string message)
{
if (new FileInfo(binary).Exists)
{
try
{
var ttsProcess = new Process
{
StartInfo =
{
FileName = binary,
CreateNoWindow = true,
UseShellExecute = false,
Arguments = args + " \"" +
Regex.Replace(Regex.Replace(message, @"(\\*)" + "\"", @"$1$1\" + "\""),
@"(\\+)$", @"$1$1") + "\""
}
};
lock (speechLock)
{
ttsProcess.Start();
// heuristic pause
Thread.Sleep(500 * message.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length);
}

return;
}
catch (Exception ex)
{
Trace.WriteLine(ex, $"TTS failed to play back {message} with exception {ex.Message}");
return;
}
}

try
{
lock (speechLock)
speechSynthesizer.Speak(message);
}
catch (Exception ex)
{
Trace.WriteLine(ex, $"TTS failed to play back {message} with exception {ex.Message}");
}

}
}
15 changes: 14 additions & 1 deletion IINACT/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"resolved": "2.1.10",
"contentHash": "S6NrvvOnLgT4GDdgwuKVJjbFo+8ZEj+JsEYk9ojjOR/MMfv1dIFpT8aRJQfI24rtDcw1uF+GnSSMN4WW1yt7fw=="
},
"System.Speech": {
"type": "Direct",
"requested": "[7.0.0, )",
"resolved": "7.0.0",
"contentHash": "7E0uB92Cx2sXR67HW9rMKJqDACdLuz9t3I3OwZUFDzAgwKXWuY6CYeRT/NiypHcyZO2be9+0H0w0M6fn7HQtgQ=="
},
"Microsoft.CSharp": {
"type": "Transitive",
"resolved": "4.7.0",
Expand Down Expand Up @@ -64,6 +70,13 @@
}
}
},
"net7.0-windows7.0/win-x64": {}
"net7.0-windows7.0/win-x64": {
"System.Speech": {
"type": "Direct",
"requested": "[7.0.0, )",
"resolved": "7.0.0",
"contentHash": "7E0uB92Cx2sXR67HW9rMKJqDACdLuz9t3I3OwZUFDzAgwKXWuY6CYeRT/NiypHcyZO2be9+0H0w0M6fn7HQtgQ=="
}
}
}
}
39 changes: 5 additions & 34 deletions NotACT/FormActMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public partial class FormActMain : Form, ISynchronizeInvoke
public delegate DateTime DateTimeLogParser(string logLine);

private readonly ConcurrentQueue<MasterSwing> afterActionsQueue = new();
private readonly object ttsLock = new();
private Thread afterActionQueueThread;
public DateTimeLogParser GetDateTimeFromLog;
private volatile bool inCombat;
Expand Down Expand Up @@ -114,6 +113,10 @@ public event LogFileChangedDelegate LogFileChanged

public event CombatActionDelegate AfterCombatAction;

public delegate void TextToSpeechDelegate(string text);

public event TextToSpeechDelegate TextToSpeech;


public void WriteExceptionLog(Exception ex, string MoreInfo)
{
Expand All @@ -140,39 +143,7 @@ public void ParseRawLogLine(string logLine)
}


public void TTS(string message, string binary = "/usr/bin/say", string args = "")
{
lock (ttsLock)
{
if (new FileInfo(binary).Exists)
{
try
{
var ttsProcess = new Process
{
StartInfo =
{
FileName = binary,
CreateNoWindow = true,
UseShellExecute = false,
Arguments = args + " \"" +
Regex.Replace(Regex.Replace(message, @"(\\*)" + "\"", @"$1$1\" + "\""),
@"(\\+)$", @"$1$1") + "\""
}
};
ttsProcess.Start();
}
catch (Exception ex)
{
WriteExceptionLog(ex, $"TTS failed to play back {message}");
}
}
else
Trace.WriteLine($"TTS binary {binary} not found");

Thread.Sleep(500 * message.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length);
}
}
public void TTS(string message) => TextToSpeech(message);

public void ChangeZone(string ZoneName)
{
Expand Down

0 comments on commit 080688c

Please sign in to comment.