From 35a1f0d2debcfa4da7aeb9c10839abc8b2638934 Mon Sep 17 00:00:00 2001 From: Deez Date: Thu, 26 Nov 2026 14:41:09 -0500 Subject: [PATCH] Upload files to "Classes" --- Classes/SpeakerHook.cs | 131 ++++++++++++++++++++++++++++++++++++++++ Classes/SpeechBubble.cs | 27 +++++++++ 2 files changed, 158 insertions(+) create mode 100644 Classes/SpeakerHook.cs create mode 100644 Classes/SpeechBubble.cs diff --git a/Classes/SpeakerHook.cs b/Classes/SpeakerHook.cs new file mode 100644 index 0000000..bc0e45a --- /dev/null +++ b/Classes/SpeakerHook.cs @@ -0,0 +1,131 @@ +using GorillaTag.Audio; +using Photon.Pun; +using Photon.Realtime; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace GorillaCaptions.Classes +{ + public class SpeakerHook : MonoBehaviour + { + public GTSpeaker SpeakerInstance; + public VRRig OwnerRig; + + public void Start() + { + if (Managers.SynthesizerManager.instance == null) + { + Debug.LogError(" Synthesizer manager is null"); + Destroy(this); + return; + } + + SpeakerInstance = gameObject.GetComponent(); + if (SpeakerInstance == null) + { + Debug.LogError(" GameObject has no speaker"); + Destroy(this); + return; + } + + if (!SpeakerInstance.IsLinked) + { + Debug.LogError(" Speaker is not linked"); + Destroy(this); + return; + } + + OwnerRig = GetComponentInParent(); + } + + public void Update() + { + if (DateTime.Now > voiceDataTimestamp) + { + float[] audioData = null; + lock (audioLock) + { + if (audioBuffer.Count > 0) + { + audioData = audioBuffer.ToArray(); + audioBuffer.Clear(); + } + } + + if (audioData != null) + { + _ = ProcessAudioAsync(audioData); + } + } + } + + private List audioBuffer = new List(); + private DateTime voiceDataTimestamp = DateTime.Now; + private object audioLock = new object(); + + public void ProcessAudioFrame(float[] frame) + { + voiceDataTimestamp = DateTime.Now.AddSeconds(0.4); + lock (audioLock) + { + audioBuffer.AddRange(frame); + } + } + + private async Task ProcessAudioAsync(float[] audioData) + { + string audioPath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.wav"); + + await Task.Run(() => + { + byte[] byteData = new byte[audioData.Length * sizeof(short)]; + for (int i = 0; i < audioData.Length; i++) + { + short sample = (short)(Math.Clamp(audioData[i], -1f, 1f) * short.MaxValue); + byteData[i * 2] = (byte)(sample & 0xff); + byteData[i * 2 + 1] = (byte)((sample >> 8) & 0xff); + } + WriteWavFile(audioPath, byteData, 16000); + }); + + string output = await Managers.SynthesizerManager.instance.SynthesizeAudio(audioPath); + + await Task.Run(() => + { + if (File.Exists(audioPath)) + File.Delete(audioPath); + }); + + if (output != null) + Managers.BubbleManager.instance.SpawnBubble(OwnerRig, output); + } + + private void WriteWavFile(string path, byte[] pcmData, int sampleRate) + { + using (var fs = new FileStream(path, FileMode.Create)) + using (var bw = new BinaryWriter(fs)) + { + bw.Write(Encoding.ASCII.GetBytes("RIFF")); + bw.Write(36 + pcmData.Length); + bw.Write(Encoding.ASCII.GetBytes("WAVE")); + + bw.Write(Encoding.ASCII.GetBytes("fmt ")); + bw.Write(16); + bw.Write((short)1); + bw.Write((short)1); + bw.Write(sampleRate); + bw.Write(sampleRate * 2); + bw.Write((short)2); + bw.Write((short)16); + + bw.Write(Encoding.ASCII.GetBytes("data")); + bw.Write(pcmData.Length); + bw.Write(pcmData); + } + } + } +} \ No newline at end of file diff --git a/Classes/SpeechBubble.cs b/Classes/SpeechBubble.cs new file mode 100644 index 0000000..0d213a1 --- /dev/null +++ b/Classes/SpeechBubble.cs @@ -0,0 +1,27 @@ +using UnityEngine; + +namespace GorillaCaptions.Classes +{ + public class SpeechBubble : MonoBehaviour + { + public VRRig Owner; + private GameObject uiTarget; + private float spawnTime; + + void Awake() + { + spawnTime = Time.time; + uiTarget = transform.Find("Canvas").gameObject; + gameObject.transform.localPosition = Vector3.up * 0.6f; + } + + void Update() + { + if (Time.time > spawnTime + 10f || Owner == null || !Owner.gameObject.activeInHierarchy) + Managers.BubbleManager.instance.DestroyBubble(gameObject); + } + + void LateUpdate() => + uiTarget.transform.LookAt(GorillaTagger.Instance.headCollider.transform.position); + } +}