Skip to content

iiDkRemastered/gorillacaptions

Ownership not verified 0Apache-2.0
Open on GitHub

Viewing main at commit 18e583edbd“Add files via upload”· 2 months ago

65 lines · 1.9 KBRaw on GitHub
using Photon.Voice.Unity;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using UnityEngine;
using System.IO;
using System.Reflection;
using System.Diagnostics;

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 FileStream(voskPath, FileMode.Create, FileAccess.Write);
            stream.CopyTo(fs);
        }

        public async Task<string> SynthesizeAudio(string audioPath)
        {
            ProcessStartInfo psi = new ProcessStartInfo
            {
                FileName = voskPath,
                Arguments = audioPath,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            };

            using Process proc = new Process { 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;
        }
    }
}

Code is shown from GitHub as-is. Linking a repository doesn’t prove that a release file on Canopy was built from it.