forked from cgvr/DeltaVR
82 lines
2.4 KiB
C#
82 lines
2.4 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class MicrophoneStand : MonoBehaviour
|
|
{
|
|
public TextMeshProUGUI outputText;
|
|
|
|
public GameObject microphoneOffStatus;
|
|
public GameObject microphoneOnStatus;
|
|
|
|
public FMODWhisperBridge fmodWhisperBridge;
|
|
|
|
public delegate void OnPlayerUsedMicrophoneDelegate();
|
|
public event OnPlayerUsedMicrophoneDelegate OnPlayerUsedMicrophone;
|
|
|
|
[Header("Particle System Config")]
|
|
public ParticleSystem particles;
|
|
public float baseEmissionRate = 3;
|
|
public float maxExtraEmissionRate = 50;
|
|
|
|
private ParticleSystem.EmissionModule particleEmission;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
microphoneOffStatus.SetActive(true);
|
|
microphoneOnStatus.SetActive(false);
|
|
|
|
particleEmission = particles.emission;
|
|
particleEmission.enabled = false;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (particleEmission.enabled)
|
|
{
|
|
particleEmission.rateOverTime = baseEmissionRate + fmodWhisperBridge.GetNormalizedVolume01() * maxExtraEmissionRate;
|
|
}
|
|
}
|
|
|
|
public void OnPlayerApproach()
|
|
{
|
|
fmodWhisperBridge.ActivateRecording();
|
|
fmodWhisperBridge.OnWhisperSegmentUpdated += OnPlayerSpeechUpdated;
|
|
fmodWhisperBridge.OnWhisperSegmentFinished += OnPlayerSpeechFinished;
|
|
microphoneOffStatus.SetActive(false);
|
|
microphoneOnStatus.SetActive(true);
|
|
particleEmission.enabled = true;
|
|
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.RadioButton, gameObject);
|
|
}
|
|
|
|
public void OnPlayerLeave()
|
|
{
|
|
fmodWhisperBridge.OnWhisperSegmentUpdated -= OnPlayerSpeechUpdated;
|
|
fmodWhisperBridge.OnWhisperSegmentFinished -= OnPlayerSpeechFinished;
|
|
fmodWhisperBridge.DeactivateRecording();
|
|
|
|
microphoneOffStatus.SetActive(true);
|
|
microphoneOnStatus.SetActive(false);
|
|
particleEmission.enabled = false;
|
|
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.RadioButton, gameObject);
|
|
}
|
|
|
|
private void OnPlayerSpeechUpdated(string text)
|
|
{
|
|
outputText.text = text;
|
|
OnPlayerUsedMicrophone?.Invoke();
|
|
}
|
|
|
|
private void OnPlayerSpeechFinished(string text)
|
|
{
|
|
outputText.text = text;
|
|
OnPlayerUsedMicrophone?.Invoke();
|
|
}
|
|
|
|
public string GetTextOutput()
|
|
{
|
|
return outputText.text;
|
|
}
|
|
}
|