forked from cgvr/DeltaVR
98 lines
2.4 KiB
C#
98 lines
2.4 KiB
C#
using System.Diagnostics;
|
|
using TMPro;
|
|
using Unity.XR.CoreUtils;
|
|
using UnityEngine;
|
|
using Whisper;
|
|
using Whisper.Utils;
|
|
|
|
public class VoiceTranscriptionTestBox : MonoBehaviour
|
|
{
|
|
public Material activeMaterial;
|
|
public Material inactiveMaterial;
|
|
|
|
private MeshRenderer meshRenderer;
|
|
|
|
|
|
public WhisperManager whisper;
|
|
public MicrophoneRecord microphoneRecord;
|
|
public TextMeshProUGUI outputText;
|
|
|
|
private string _buffer;
|
|
public string currentTextOutput;
|
|
|
|
private void Awake()
|
|
{
|
|
whisper.OnNewSegment += OnNewSegment;
|
|
whisper.OnProgress += OnProgressHandler;
|
|
|
|
microphoneRecord.OnRecordStop += OnRecordStop;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
meshRenderer = GetComponent<MeshRenderer>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
void OnTriggerEnter(Collider other)
|
|
{
|
|
KbmController controller = other.GetComponent<KbmController>();
|
|
XROrigin playerOrigin = other.GetComponent<XROrigin>();
|
|
if (controller != null || playerOrigin != null)
|
|
{
|
|
meshRenderer.material = activeMaterial;
|
|
microphoneRecord.StartRecord();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
KbmController controller = other.GetComponent<KbmController>();
|
|
XROrigin playerOrigin = other.GetComponent<XROrigin>();
|
|
if (controller != null | playerOrigin != null)
|
|
{
|
|
meshRenderer.material = inactiveMaterial;
|
|
microphoneRecord.StopRecord();
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
currentTextOutput = text;
|
|
outputText.text = text;
|
|
}
|
|
|
|
private void OnProgressHandler(int progress)
|
|
{
|
|
UnityEngine.Debug.Log($"Progress: {progress}%");
|
|
}
|
|
|
|
private void OnNewSegment(WhisperSegment segment)
|
|
{
|
|
_buffer += segment.Text;
|
|
UnityEngine.Debug.Log(_buffer + "...");
|
|
}
|
|
}
|