using System.Diagnostics; using UnityEngine; using Whisper; using Whisper.Utils; public class VoiceTranscription : MonoBehaviour { public WhisperManager whisper; public MicrophoneRecord microphoneRecord; private string _buffer; private void Awake() { whisper.OnNewSegment += OnNewSegment; microphoneRecord.OnRecordStop += OnRecordStop; } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } private async void OnRecordStop(AudioChunk recordedAudio) { _buffer = ""; var sw = new Stopwatch(); sw.Start(); var res = await whisper.GetTextAsync(recordedAudio.Data, recordedAudio.Frequency, recordedAudio.Channels); if (res == null) return; var time = sw.ElapsedMilliseconds; var rate = recordedAudio.Length / (time * 0.001f); UnityEngine.Debug.Log($"Time: {time} ms\nRate: {rate:F1}x"); var text = res.Result; UnityEngine.Debug.Log(text); } private void OnNewSegment(WhisperSegment segment) { _buffer += segment.Text; UnityEngine.Debug.Log(_buffer + "..."); } }