123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using System;
- using UnityEngine;
- [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);
- }
- }
- public void Init(Maid maid)
- {
- if (!this.audioSource)
- {
- this.audioSource = base.GetComponent<AudioSource>();
- }
- if (!this.audioSource)
- {
- return;
- }
- this.TargetMaid = maid;
- this.audioSource.loop = true;
- this.audioSource.mute = false;
- this.lipsyncContext = base.GetComponent<OVRLipSyncContextBase>();
- if (Microphone.devices.Length != 0)
- {
- this.selectedDevice = Microphone.devices[0].ToString();
- this.micSelected = true;
- this.GetMicCaps();
- }
- }
- private void Update()
- {
- this.audioSource.volume = this.sourceVolume;
- if (!Microphone.IsRecording(this.selectedDevice))
- {
- this.StartMicrophone();
- }
- }
- 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, 1, 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 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;
- }
|