OVRLipSyncContext.cs
11.7 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/************************************************************************************
Filename : OVRLipSyncContext.cs
Content : Interface to Oculus Lip-Sync engine
Created : August 6th, 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 UnityEngine;
[RequireComponent(typeof(AudioSource))]
//-------------------------------------------------------------------------------------
// ***** OVRLipSyncContext
//
/// <summary>
/// OVRLipSyncContext interfaces into the Oculus phoneme recognizer.
/// This component should be added into the scene once for each Audio Source.
///
/// </summary>
public class OVRLipSyncContext : OVRLipSyncContextBase
{
// * * * * * * * * * * * * *
// Public members
[Tooltip("Allow capturing of keyboard input to control operation.")]
public bool enableKeyboardInput = false;
[Tooltip("Register a mouse/touch callback to control loopback and gain (requires script restart).")]
public bool enableTouchInput = false;
[Tooltip("Play input audio back through audio output.")]
public bool audioLoopback = false;
[Tooltip("Key to toggle audio loopback.")]
public KeyCode loopbackKey = KeyCode.L;
[Tooltip("Show viseme scores in an OVRLipSyncDebugConsole display.")]
public bool showVisemes = false;
[Tooltip("Key to toggle viseme score display.")]
public KeyCode debugVisemesKey = KeyCode.D;
[Tooltip("Skip data from the Audio Source. Use if you intend to pass audio data in manually.")]
public bool skipAudioSource = false;
[Tooltip("Adjust the linear audio gain multiplier before processing lipsync")]
public float gain = 1.0f;
private bool hasDebugConsole = false;
public KeyCode debugLaughterKey = KeyCode.H;
public bool showLaughter = false;
public float laughterScore = 0.0f;
// * * * * * * * * * * * * *
// Private members
/// <summary>
/// Start this instance.
/// Note: make sure to always have a Start function for classes that have editor scripts.
/// </summary>
void Start()
{
// Add a listener to the OVRTouchpad for touch events
if (enableTouchInput)
{
OVRTouchpad.AddListener(LocalTouchEventCallback);
}
// Find console
OVRLipSyncDebugConsole[] consoles = FindObjectsOfType<OVRLipSyncDebugConsole>();
if (consoles.Length > 0)
{
hasDebugConsole = consoles[0];
}
}
/// <summary>
/// Handle keyboard input
/// </summary>
void HandleKeyboard()
{
// Turn loopback on/off
if (Input.GetKeyDown(loopbackKey))
{
ToggleAudioLoopback();
}
else if (Input.GetKeyDown(debugVisemesKey))
{
showVisemes = !showVisemes;
if (showVisemes)
{
if (hasDebugConsole)
{
Debug.Log("DEBUG SHOW VISEMES: ENABLED");
}
else
{
Debug.LogWarning("Warning: No OVRLipSyncDebugConsole in the scene!");
showVisemes = false;
}
}
else
{
if (hasDebugConsole)
{
OVRLipSyncDebugConsole.Clear();
}
Debug.Log("DEBUG SHOW VISEMES: DISABLED");
}
}
else if (Input.GetKeyDown(debugLaughterKey))
{
showLaughter = !showLaughter;
if (showLaughter)
{
if (hasDebugConsole)
{
Debug.Log("DEBUG SHOW LAUGHTER: ENABLED");
}
else
{
Debug.LogWarning("Warning: No OVRLipSyncDebugConsole in the scene!");
showLaughter = false;
}
}
else
{
if (hasDebugConsole)
{
OVRLipSyncDebugConsole.Clear();
}
Debug.Log("DEBUG SHOW LAUGHTER: DISABLED");
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
gain -= 1.0f;
if (gain < 1.0f) gain = 1.0f;
string g = "LINEAR GAIN: ";
g += gain;
if (hasDebugConsole)
{
OVRLipSyncDebugConsole.Clear();
OVRLipSyncDebugConsole.Log(g);
OVRLipSyncDebugConsole.ClearTimeout(1.5f);
}
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
gain += 1.0f;
if (gain > 15.0f)
gain = 15.0f;
string g = "LINEAR GAIN: ";
g += gain;
if (hasDebugConsole)
{
OVRLipSyncDebugConsole.Clear();
OVRLipSyncDebugConsole.Log(g);
OVRLipSyncDebugConsole.ClearTimeout(1.5f);
}
}
}
/// <summary>
/// Run processes that need to be updated in our game thread
/// </summary>
void Update()
{
if (enableKeyboardInput)
{
HandleKeyboard();
}
laughterScore = this.Frame.laughterScore;
DebugShowVisemesAndLaughter();
}
/// <summary>
/// Preprocess F32 PCM audio buffer
/// </summary>
/// <param name="data">Data.</param>
/// <param name="channels">Channels.</param>
public void PreprocessAudioSamples(float[] data, int channels)
{
// Increase the gain of the input
for (int i = 0; i < data.Length; ++i)
{
data[i] = data[i] * gain;
}
}
/// <summary>
/// Postprocess F32 PCM audio buffer
/// </summary>
/// <param name="data">Data.</param>
/// <param name="channels">Channels.</param>
public void PostprocessAudioSamples(float[] data, int channels)
{
// Turn off output (so that we don't get feedback from mics too close to speakers)
if (!audioLoopback)
{
for (int i = 0; i < data.Length; ++i)
data[i] = data[i] * 0.0f;
}
}
/// <summary>
/// Pass F32 PCM audio buffer to the lip sync module
/// </summary>
/// <param name="data">Data.</param>
/// <param name="channels">Channels.</param>
public void ProcessAudioSamplesRaw(float[] data, int channels)
{
// Send data into Phoneme context for processing (if context is not 0)
lock (this)
{
if (Context == 0 || OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
{
return;
}
var frame = this.Frame;
OVRLipSync.ProcessFrame(Context, data, frame, channels == 2);
}
}
/// <summary>
/// Pass S16 PCM audio buffer to the lip sync module
/// </summary>
/// <param name="data">Data.</param>
/// <param name="channels">Channels.</param>
public void ProcessAudioSamplesRaw(short[] data, int channels)
{
// Send data into Phoneme context for processing (if context is not 0)
lock (this)
{
if (Context == 0 || OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
{
return;
}
var frame = this.Frame;
OVRLipSync.ProcessFrame(Context, data, frame, channels == 2);
}
}
/// <summary>
/// Process F32 audio sample and pass it to the lip sync module for computation
/// </summary>
/// <param name="data">Data.</param>
/// <param name="channels">Channels.</param>
public void ProcessAudioSamples(float[] data, int channels)
{
// Do not process if we are not initialized, or if there is no
// audio source attached to game object
if ((OVRLipSync.IsInitialized() != OVRLipSync.Result.Success) || audioSource == null)
{
return;
}
PreprocessAudioSamples(data, channels);
ProcessAudioSamplesRaw(data, channels);
PostprocessAudioSamples(data, channels);
}
/// <summary>
/// Raises the audio filter read event.
/// </summary>
/// <param name="data">Data.</param>
/// <param name="channels">Channels.</param>
void OnAudioFilterRead(float[] data, int channels)
{
if (!skipAudioSource)
{
ProcessAudioSamples(data, channels);
}
}
/// <summary>
/// Print the visemes and laughter score to game window
/// </summary>
void DebugShowVisemesAndLaughter()
{
if (hasDebugConsole)
{
string seq = "";
if (showLaughter)
{
seq += "Laughter:";
int count = (int)(50.0f * this.Frame.laughterScore);
for (int c = 0; c < count; c++)
seq += "*";
seq += "\n";
}
if (showVisemes)
{
for (int i = 0; i < this.Frame.Visemes.Length; i++)
{
seq += ((OVRLipSync.Viseme)i).ToString();
seq += ":";
int count = (int)(50.0f * this.Frame.Visemes[i]);
for (int c = 0; c < count; c++)
seq += "*";
seq += "\n";
}
}
OVRLipSyncDebugConsole.Clear();
if (seq != "")
{
OVRLipSyncDebugConsole.Log(seq);
}
}
}
void ToggleAudioLoopback()
{
audioLoopback = !audioLoopback;
if (hasDebugConsole)
{
OVRLipSyncDebugConsole.Clear();
OVRLipSyncDebugConsole.ClearTimeout(1.5f);
if (audioLoopback)
OVRLipSyncDebugConsole.Log("LOOPBACK MODE: ENABLED");
else
OVRLipSyncDebugConsole.Log("LOOPBACK MODE: DISABLED");
}
}
// LocalTouchEventCallback
void LocalTouchEventCallback(OVRTouchpad.TouchEvent touchEvent)
{
string g = "LINEAR GAIN: ";
switch (touchEvent)
{
case (OVRTouchpad.TouchEvent.SingleTap):
ToggleAudioLoopback();
break;
case (OVRTouchpad.TouchEvent.Up):
gain += 1.0f;
if (gain > 15.0f)
gain = 15.0f;
g += gain;
if (hasDebugConsole)
{
OVRLipSyncDebugConsole.Clear();
OVRLipSyncDebugConsole.Log(g);
OVRLipSyncDebugConsole.ClearTimeout(1.5f);
}
break;
case (OVRTouchpad.TouchEvent.Down):
gain -= 1.0f;
if (gain < 1.0f) gain = 1.0f;
g += gain;
if (hasDebugConsole)
{
OVRLipSyncDebugConsole.Clear();
OVRLipSyncDebugConsole.Log(g);
OVRLipSyncDebugConsole.ClearTimeout(1.5f);
}
break;
}
}
}