OVRLipSyncMicInput.cs 2.7 KB

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