Files
2026-11-26 14:43:51 -05:00

60 lines
1.7 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
namespace GorillaCaptions.Managers;
public class SynthesizerManager : MonoBehaviour
{
public static SynthesizerManager instance { get; private set; }
public static string voskPath;
public void Awake()
{
instance = this;
string resourcePath = "GorillaCaptions.Resources.QuickVosk.exe";
voskPath = Path.Combine(Path.GetTempPath(), "QuickVosk.exe");
if (File.Exists(voskPath))
File.Delete(voskPath);
using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
using FileStream fs = new(voskPath, FileMode.Create, FileAccess.Write);
stream.CopyTo(fs);
}
public async Task<string> SynthesizeAudio(string audioPath)
{
ProcessStartInfo psi = new()
{
FileName = voskPath,
Arguments = audioPath,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using Process proc = new() { StartInfo = psi };
string output = await Task.Run(() =>
{
proc.Start();
string outStr = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
return outStr;
});
string[] lines = output.Split("\n");
string finalText = lines[lines.Length - 2];
if (finalText.Length <= 1)
return null;
finalText = char.ToUpper(finalText[0]) + finalText.Substring(1);
return finalText;
}
}