forked from cgvr/DeltaVR
88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using Whisper;
|
|
using Whisper.Utils;
|
|
|
|
public class MicrophoneStand : MonoBehaviour
|
|
{
|
|
public WhisperManager whisper;
|
|
public MicrophoneRecord microphoneRecord;
|
|
public string microphoneDevice;
|
|
public TextMeshProUGUI outputText;
|
|
|
|
private WhisperStream stream;
|
|
|
|
private string textOutput;
|
|
|
|
public GameObject microphoneOffStatus;
|
|
public GameObject microphoneOnStatus;
|
|
|
|
// Start is called before the first frame update
|
|
async void Start()
|
|
{
|
|
Debug.Log("Mic devices: " + string.Join(", ", Microphone.devices));
|
|
Debug.Log("Using mic device: " + microphoneDevice);
|
|
microphoneRecord.SelectedMicDevice = microphoneDevice;
|
|
|
|
// 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;
|
|
|
|
microphoneOffStatus.SetActive(true);
|
|
microphoneOnStatus.SetActive(false);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
void OnTriggerEnter(Collider other)
|
|
{
|
|
KbmController controller = other.GetComponent<KbmController>();
|
|
Debug.Log("Mic stand collided with object " + other.gameObject.name);
|
|
|
|
if (controller != null || other.gameObject.tag == "Player Head")
|
|
{
|
|
microphoneOffStatus.SetActive(false);
|
|
microphoneOnStatus.SetActive(true);
|
|
|
|
stream.StartStream();
|
|
Debug.Log("Whisper stream started.");
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
KbmController controller = other.GetComponent<KbmController>();
|
|
if (controller != null | other.gameObject.tag == "Player Head")
|
|
{
|
|
microphoneOffStatus.SetActive(true);
|
|
microphoneOnStatus.SetActive(false);
|
|
|
|
stream.StopStream();
|
|
textOutput = outputText.text;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|