1
0
forked from cgvr/DeltaVR
Files
DeltaVR3DModelGeneration/Assets/_PROJECT/Scripts/ModeGeneration/VoiceTranscriptionBox.cs

84 lines
2.2 KiB
C#

using TMPro;
using Unity.XR.CoreUtils;
using UnityEngine;
using Whisper;
using Whisper.Utils;
public class VoiceTranscriptionBox : MonoBehaviour
{
public Material activeMaterial;
public Material inactiveMaterial;
private MeshRenderer meshRenderer;
public WhisperManager whisper;
public MicrophoneRecord microphoneRecord;
public TextMeshProUGUI outputText;
private WhisperStream stream;
private string textOutput;
// Start is called before the first frame update
async void Start()
{
meshRenderer = GetComponent<MeshRenderer>();
Debug.Log("Mic devices: " + string.Join(", ", Microphone.devices));
// This causes about 1 sec long freeze, has to be done once at the start of the game
microphoneRecord.StartRecord();
stream = await whisper.CreateStream(microphoneRecord);
stream.OnResultUpdated += OnWhisperResult;
Debug.Log("Microphone started and Whisper stream created.");
}
// 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;
stream.StartStream();
Debug.Log("Whisper stream started.");
}
}
private void OnTriggerExit(Collider other)
{
KbmController controller = other.GetComponent<KbmController>();
XROrigin playerOrigin = other.GetComponent<XROrigin>();
if (controller != null | playerOrigin != null)
{
stream.StopStream();
textOutput = outputText.text;
meshRenderer.material = inactiveMaterial;
}
}
private void OnWhisperResult(string result)
{
Debug.Log("Whisper result processed: " + result);
outputText.text = result;
}
private void OnDestroy()
{
microphoneRecord.StopRecord();
Destroy(gameObject);
}
public string GetTextOutput()
{
return textOutput;
}
}