using System; using UnityEngine; using UnityEngine.Audio; using wf; [RequireComponent(typeof(AudioSource))] public class OVRLipSyncMicInput : MonoBehaviour { public float SourceVolume { get { return this.sourceVolume; } set { this.sourceVolume = Mathf.Clamp(value, 0f, 1f); } } public float MicFrequency { get { return (float)this.micFrequency; } set { this.micFrequency = (int)Mathf.Clamp(value, 0f, 96000f); } } private OVRLipSync.VariableData m_Variable { get { return GameMain.Instance.LipSyncMgr.Variable; } } public void Init(Maid maid) { if (!this.audioSource) { this.audioSource = base.GetComponent(); } if (!this.audioSource) { return; } this.TargetMaid = maid; this.audioSource.loop = true; this.audioSource.mute = false; this.m_orijinMixer = this.audioSource.outputAudioMixerGroup; this.m_outputMixer = Resources.Load("OvrLipSync/MicInput"); this.audioSource.outputAudioMixerGroup = this.m_outputMixer.audioMixer.FindMatchingGroups("MicInput")[0]; this.lipsyncContext = base.GetComponent(); this.micFrequency = OVRLipSync.SamplingRate; if (Microphone.devices.Length != 0) { this.selectedDevice = Microphone.devices[0].ToString(); this.micSelected = true; this.GetMicCaps(); } } private void OnDestroy() { if (this.audioSource) { this.audioSource.outputAudioMixerGroup = this.m_orijinMixer; } } private void Update() { if (!Microphone.IsRecording(this.selectedDevice)) { this.StartMicrophone(); } this.m_outputMixer.audioMixer.SetFloat("OutVolume", (!this.m_Variable.Mute) ? Utility.VolumeToDecibel(this.m_Variable.MicVolume) : -80f); } private void OnApplicationFocus(bool focus) { this.focused = focus; if (!this.focused) { this.StopMicrophone(); } } private void OnApplicationPause(bool focus) { this.focused = focus; if (!this.focused) { this.StopMicrophone(); } } private void OnDisable() { this.StopMicrophone(); } private void OnEnable() { this.StartMicrophone(); } public void GetMicCaps() { if (!this.micSelected) { return; } Microphone.GetDeviceCaps(this.selectedDevice, out this.minFreq, out this.maxFreq); if (this.minFreq == 0 && this.maxFreq == 0) { Debug.LogWarning("GetMicCaps warning:: min and max frequencies are 0"); this.minFreq = 44100; this.maxFreq = 44100; } if (this.micFrequency > this.maxFreq) { this.micFrequency = this.maxFreq; } } public void StartMicrophone() { if (!this.micSelected) { return; } this.audioSource.clip = Microphone.Start(this.selectedDevice, true, 10, this.micFrequency); while (Microphone.GetPosition(this.selectedDevice) <= 0) { } this.audioSource.Play(); } public void StopMicrophone() { if (!this.micSelected) { return; } if (this.audioSource != null && this.audioSource.clip != null && this.audioSource.clip.name == "Microphone") { this.audioSource.Stop(); } Microphone.End(this.selectedDevice); } public const int RECORD = 10; public Maid TargetMaid; public AudioSource audioSource; [SerializeField] private float sourceVolume = 100f; [SerializeField] private int micFrequency = 44100; public string selectedDevice; public float loudness; private bool micSelected; private int minFreq; private int maxFreq; private bool focused = true; private OVRLipSyncContextBase lipsyncContext; private AudioMixerGroup m_orijinMixer; private AudioMixerGroup m_outputMixer; }