OVRLipSyncMicInput.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Audio;
  4. using wf;
  5. [RequireComponent(typeof(AudioSource))]
  6. public class OVRLipSyncMicInput : MonoBehaviour
  7. {
  8. public float SourceVolume
  9. {
  10. get
  11. {
  12. return this.sourceVolume;
  13. }
  14. set
  15. {
  16. this.sourceVolume = Mathf.Clamp(value, 0f, 1f);
  17. }
  18. }
  19. public float MicFrequency
  20. {
  21. get
  22. {
  23. return (float)this.micFrequency;
  24. }
  25. set
  26. {
  27. this.micFrequency = (int)Mathf.Clamp(value, 0f, 96000f);
  28. }
  29. }
  30. private OVRLipSync.VariableData m_Variable
  31. {
  32. get
  33. {
  34. return GameMain.Instance.LipSyncMgr.Variable;
  35. }
  36. }
  37. public void Init(Maid maid)
  38. {
  39. if (!this.audioSource)
  40. {
  41. this.audioSource = base.GetComponent<AudioSource>();
  42. }
  43. if (!this.audioSource)
  44. {
  45. return;
  46. }
  47. this.TargetMaid = maid;
  48. this.audioSource.loop = true;
  49. this.audioSource.mute = false;
  50. this.m_orijinMixer = this.audioSource.outputAudioMixerGroup;
  51. this.m_outputMixer = Resources.Load<AudioMixerGroup>("OvrLipSync/MicInput");
  52. this.audioSource.outputAudioMixerGroup = this.m_outputMixer.audioMixer.FindMatchingGroups("MicInput")[0];
  53. this.lipsyncContext = base.GetComponent<OVRLipSyncContextBase>();
  54. this.micFrequency = OVRLipSync.SamplingRate;
  55. if (Microphone.devices.Length != 0)
  56. {
  57. this.selectedDevice = Microphone.devices[0].ToString();
  58. this.micSelected = true;
  59. this.GetMicCaps();
  60. }
  61. }
  62. private void OnDestroy()
  63. {
  64. if (this.audioSource)
  65. {
  66. this.audioSource.outputAudioMixerGroup = this.m_orijinMixer;
  67. }
  68. }
  69. private void Update()
  70. {
  71. if (!Microphone.IsRecording(this.selectedDevice))
  72. {
  73. this.StartMicrophone();
  74. }
  75. this.m_outputMixer.audioMixer.SetFloat("OutVolume", (!this.m_Variable.Mute) ? Utility.VolumeToDecibel(this.m_Variable.MicVolume) : -80f);
  76. }
  77. private void OnApplicationFocus(bool focus)
  78. {
  79. this.focused = focus;
  80. if (!this.focused)
  81. {
  82. this.StopMicrophone();
  83. }
  84. }
  85. private void OnApplicationPause(bool focus)
  86. {
  87. this.focused = focus;
  88. if (!this.focused)
  89. {
  90. this.StopMicrophone();
  91. }
  92. }
  93. private void OnDisable()
  94. {
  95. this.StopMicrophone();
  96. }
  97. private void OnEnable()
  98. {
  99. this.StartMicrophone();
  100. }
  101. public void GetMicCaps()
  102. {
  103. if (!this.micSelected)
  104. {
  105. return;
  106. }
  107. Microphone.GetDeviceCaps(this.selectedDevice, out this.minFreq, out this.maxFreq);
  108. if (this.minFreq == 0 && this.maxFreq == 0)
  109. {
  110. Debug.LogWarning("GetMicCaps warning:: min and max frequencies are 0");
  111. this.minFreq = 44100;
  112. this.maxFreq = 44100;
  113. }
  114. if (this.micFrequency > this.maxFreq)
  115. {
  116. this.micFrequency = this.maxFreq;
  117. }
  118. }
  119. public void StartMicrophone()
  120. {
  121. if (!this.micSelected)
  122. {
  123. return;
  124. }
  125. this.audioSource.clip = Microphone.Start(this.selectedDevice, true, 10, this.micFrequency);
  126. while (Microphone.GetPosition(this.selectedDevice) <= 0)
  127. {
  128. }
  129. this.audioSource.Play();
  130. }
  131. public void StopMicrophone()
  132. {
  133. if (!this.micSelected)
  134. {
  135. return;
  136. }
  137. if (this.audioSource != null && this.audioSource.clip != null && this.audioSource.clip.name == "Microphone")
  138. {
  139. this.audioSource.Stop();
  140. }
  141. Microphone.End(this.selectedDevice);
  142. }
  143. public const int RECORD = 10;
  144. public Maid TargetMaid;
  145. public AudioSource audioSource;
  146. [SerializeField]
  147. private float sourceVolume = 100f;
  148. [SerializeField]
  149. private int micFrequency = 44100;
  150. public string selectedDevice;
  151. public float loudness;
  152. private bool micSelected;
  153. private int minFreq;
  154. private int maxFreq;
  155. private bool focused = true;
  156. private OVRLipSyncContextBase lipsyncContext;
  157. private AudioMixerGroup m_orijinMixer;
  158. private AudioMixerGroup m_outputMixer;
  159. }