BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / LipSync / Scripts / OVRLipSyncMicInput.cs

OVRLipSyncMicInput.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRLipSyncMicInput.cs 10.4 KiB Download Raw
1/************************************************************************************
2Filename : OVRLipSyncMicInput.cs
3Content : Interface to microphone input
4Created : May 12, 2015
5Copyright : Copyright Facebook Technologies, LLC and its affiliates.
6 All rights reserved.
7
8Licensed under the Oculus Audio SDK License Version 3.3 (the "License");
9you may not use the Oculus Audio SDK except in compliance with the License,
10which is provided at the time of installation or download, or which
11otherwise accompanies this software in either electronic or hard copy form.
12
13You may obtain a copy of the License at
14
15https://developer.oculus.com/licenses/audio-3.3/
16
17Unless required by applicable law or agreed to in writing, the Oculus Audio SDK
18distributed under the License is distributed on an "AS IS" BASIS,
19WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20See the License for the specific language governing permissions and
21limitations under the License.
22************************************************************************************/
23
24using System;
25using UnityEngine;
26using System.Diagnostics;
27using Debug = UnityEngine.Debug;
28using System.Threading;
29
30[RequireComponent(typeof(AudioSource))]
31
32public class OVRLipSyncMicInput : MonoBehaviour
33{
34 public enum micActivation
35 {
36 HoldToSpeak,
37 PushToSpeak,
38 ConstantSpeak
39 }
40
41 // PUBLIC MEMBERS
42 [Tooltip("Manual specification of Audio Source - " +
43 "by default will use any attached to the same object.")]
44 public AudioSource audioSource = null;
45
46
47 [Tooltip("Enable a keypress to toggle the microphone device selection GUI.")]
48 public bool enableMicSelectionGUI = false;
49 [Tooltip("Key to toggle the microphone selection GUI if enabled.")]
50 public KeyCode micSelectionGUIKey = KeyCode.M;
51
52 [SerializeField]
53 [Range(0.0f, 100.0f)]
54 [Tooltip("Microphone input volume control.")]
55 private float micInputVolume = 100;
56
57 [SerializeField]
58 [Tooltip("Requested microphone input frequency")]
59 private int micFrequency = 48000;
60 public float MicFrequency
61 {
62 get { return micFrequency; }
63 set { micFrequency = (int)Mathf.Clamp((float)value, 0, 96000); }
64 }
65
66 [Tooltip("Microphone input control method. Hold To Speak and Push" +
67 " To Speak are driven with the Mic Activation Key.")]
68 public micActivation micControl = micActivation.ConstantSpeak;
69 [Tooltip("Key used to drive Hold To Speak and Push To Speak methods" +
70 " of microphone input control.")]
71 public KeyCode micActivationKey = KeyCode.Space;
72
73 [Tooltip("Will contain the string name of the selected microphone device - read only.")]
74 public string selectedDevice;
75
76 // PRIVATE MEMBERS
77 private bool micSelected = false;
78 private int minFreq, maxFreq;
79 private bool focused = true;
80 private bool initialized = false;
81
82 //----------------------------------------------------
83 // MONOBEHAVIOUR OVERRIDE FUNCTIONS
84 //----------------------------------------------------
85
86 /// <summary>
87 /// Awake this instance.
88 /// </summary>
89 void Awake()
90 {
91 // First thing to do, cache the unity audio source (can be managed by the
92 // user if audio source can change)
93 if (!audioSource) audioSource = GetComponent<AudioSource>();
94 if (!audioSource) return; // this should never happen
95 }
96
97 /// <summary>
98 /// Start this instance.
99 /// </summary>
100 void Start()
101 {
102 audioSource.loop = true; // Set the AudioClip to loop
103 audioSource.mute = false;
104
105 InitializeMicrophone();
106 }
107
108 /// <summary>
109 /// Initializes the microphone.
110 /// </summary>
111 private void InitializeMicrophone()
112 {
113 if (initialized)
114 {
115 return;
116 }
117 if (Microphone.devices.Length == 0)
118 {
119 return;
120 }
121 selectedDevice = Microphone.devices[0].ToString();
122 micSelected = true;
123 GetMicCaps();
124 initialized = true;
125 }
126
127
128 /// <summary>
129 /// Update this instance.
130 /// </summary>
131 void Update()
132 {
133 if (!focused)
134 {
135 if (Microphone.IsRecording(selectedDevice))
136 {
137 StopMicrophone();
138 }
139 return;
140 }
141
142 if (!Application.isPlaying)
143 {
144 StopMicrophone();
145 return;
146 }
147
148 // Lazy Microphone initialization (needed on Android)
149 if (!initialized)
150 {
151 InitializeMicrophone();
152 }
153
154 audioSource.volume = (micInputVolume / 100);
155
156 //Hold To Speak
157 if (micControl == micActivation.HoldToSpeak)
158 {
159 if (Input.GetKey(micActivationKey))
160 {
161 if (!Microphone.IsRecording(selectedDevice))
162 {
163 StartMicrophone();
164 }
165 }
166 else
167 {
168 if (Microphone.IsRecording(selectedDevice))
169 {
170 StopMicrophone();
171 }
172 }
173 }
174
175 //Push To Talk
176 if (micControl == micActivation.PushToSpeak)
177 {
178 if (Input.GetKeyDown(micActivationKey))
179 {
180 if (Microphone.IsRecording(selectedDevice))
181 {
182 StopMicrophone();
183 }
184 else if (!Microphone.IsRecording(selectedDevice))
185 {
186 StartMicrophone();
187 }
188 }
189 }
190
191 //Constant Speak
192 if (micControl == micActivation.ConstantSpeak)
193 {
194 if (!Microphone.IsRecording(selectedDevice))
195 {
196 StartMicrophone();
197 }
198 }
199
200
201 //Mic Selected = False
202 if (enableMicSelectionGUI)
203 {
204 if (Input.GetKeyDown(micSelectionGUIKey))
205 {
206 micSelected = false;
207 }
208 }
209 }
210
211
212 /// <summary>
213 /// Raises the application focus event.
214 /// </summary>
215 /// <param name="focus">If set to <c>true</c>: focused.</param>
216 void OnApplicationFocus(bool focus)
217 {
218 focused = focus;
219
220 if (!focused)
221 StopMicrophone();
222 }
223
224 /// <summary>
225 /// Raises the application pause event.
226 /// </summary>
227 /// <param name="pauseStatus">If set to <c>true</c>: paused.</param>
228 void OnApplicationPause(bool pauseStatus)
229 {
230 focused = !pauseStatus;
231
232 if (!focused)
233 StopMicrophone();
234 }
235
236 void OnDisable()
237 {
238 StopMicrophone();
239 }
240
241 /// <summary>
242 /// Raises the GU event.
243 /// </summary>
244 void OnGUI()
245 {
246 MicDeviceGUI((Screen.width / 2) - 150, (Screen.height / 2) - 75, 300, 50, 10, -300);
247 }
248
249 //----------------------------------------------------
250 // PUBLIC FUNCTIONS
251 //----------------------------------------------------
252
253 /// <summary>
254 /// Mics the device GU.
255 /// </summary>
256 /// <param name="left">Left.</param>
257 /// <param name="top">Top.</param>
258 /// <param name="width">Width.</param>
259 /// <param name="height">Height.</param>
260 /// <param name="buttonSpaceTop">Button space top.</param>
261 /// <param name="buttonSpaceLeft">Button space left.</param>
262 public void MicDeviceGUI(
263 float left,
264 float top,
265 float width,
266 float height,
267 float buttonSpaceTop,
268 float buttonSpaceLeft)
269 {
270 //If there is more than one device, choose one.
271 if (Microphone.devices.Length >= 1 && enableMicSelectionGUI == true && micSelected == false)
272 {
273 for (int i = 0; i < Microphone.devices.Length; ++i)
274 {
275 if (GUI.Button(new Rect(left + ((width + buttonSpaceLeft) * i),
276 top + ((height + buttonSpaceTop) * i), width, height),
277 Microphone.devices[i].ToString()))
278 {
279 StopMicrophone();
280 selectedDevice = Microphone.devices[i].ToString();
281 micSelected = true;
282 GetMicCaps();
283 StartMicrophone();
284 }
285 }
286 }
287 }
288
289 /// <summary>
290 /// Gets the mic caps.
291 /// </summary>
292 public void GetMicCaps()
293 {
294 if (micSelected == false) return;
295
296 //Gets the frequency of the device
297 Microphone.GetDeviceCaps(selectedDevice, out minFreq, out maxFreq);
298
299 if (minFreq == 0 && maxFreq == 0)
300 {
301 Debug.LogWarning("GetMicCaps warning:: min and max frequencies are 0");
302 minFreq = 44100;
303 maxFreq = 44100;
304 }
305
306 if (micFrequency > maxFreq)
307 micFrequency = maxFreq;
308 }
309
310 /// <summary>
311 /// Starts the microphone.
312 /// </summary>
313 public void StartMicrophone()
314 {
315 if (micSelected == false) return;
316
317 //Starts recording
318 audioSource.clip = Microphone.Start(selectedDevice, true, 1, micFrequency);
319
320 Stopwatch timer = Stopwatch.StartNew();
321
322 // Wait until the recording has started
323 while (!(Microphone.GetPosition(selectedDevice) > 0) && timer.Elapsed.TotalMilliseconds < 1000) {
324 Thread.Sleep(50);
325 }
326
327 if (Microphone.GetPosition(selectedDevice) <= 0)
328 {
329 throw new Exception("Timeout initializing microphone " + selectedDevice);
330 }
331 // Play the audio source
332 audioSource.Play();
333 }
334
335 /// <summary>
336 /// Stops the microphone.
337 /// </summary>
338 public void StopMicrophone()
339 {
340 if (micSelected == false) return;
341
342 // Overriden with a clip to play? Don't stop the audio source
343 if ((audioSource != null) &&
344 (audioSource.clip != null) &&
345 (audioSource.clip.name == "Microphone"))
346 {
347 audioSource.Stop();
348 }
349
350 // Reset to stop mouth movement
351 OVRLipSyncContext context = GetComponent<OVRLipSyncContext>();
352 context.ResetContext();
353
354 Microphone.End(selectedDevice);
355 }
356
357
358 //----------------------------------------------------
359 // PRIVATE FUNCTIONS
360 //----------------------------------------------------
361
362 /// <summary>
363 /// Gets the averaged volume.
364 /// </summary>
365 /// <returns>The averaged volume.</returns>
366 float GetAveragedVolume()
367 {
368 // We will use the SR to get average volume
369 // return OVRSpeechRec.GetAverageVolume();
370 return 0.0f;
371 }
372}