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