-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOVRLipSync.cs
387 lines (337 loc) · 14.5 KB
/
OVRLipSync.cs
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
/************************************************************************************
Filename : OVRLipSync.cs
Content : Interface to Oculus Lip Sync engine
Created : August 4th, 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;
using System;
using System.Runtime.InteropServices;
namespace Oculus
{
//-------------------------------------------------------------------------------------
// ***** OVRLipSync
//
/// <summary>
/// OVRLipSync interfaces into the Oculus lip sync engine. This component should be added
/// into the scene once.
///
/// </summary>
public class OVRLipSync
{
// Error codes that may return from Lip Sync engine
public enum Result
{
Success = 0,
Unknown = -2200, //< An unknown error has occurred
CannotCreateContext = -2201, //< Unable to create a context
InvalidParam = -2202, //< An invalid parameter, e.g. NULL pointer or out of range
BadSampleRate = -2203, //< An unsupported sample rate was declared
MissingDLL = -2204, //< The DLL or shared library could not be found
BadVersion = -2205, //< Mismatched versions between header and libs
UndefinedFunction = -2206 //< An undefined function
};
// Audio buffer data type
public enum AudioDataType
{
// Signed 16-bit integer mono audio stream
S16_Mono,
// Signed 16-bit integer stereo audio stream
S16_Stereo,
// Signed 32-bit float mono audio stream
F32_Mono,
// Signed 32-bit float stereo audio stream
F32_Stereo
};
// Various visemes
public enum Viseme
{
SIL,
PP,
FF,
TH,
DD,
KK,
CH,
SS,
NN,
RR,
AA,
E,
IH,
OH,
OU
};
public static readonly int VisemeCount = Enum.GetNames(typeof(Viseme)).Length;
// Enum for sending lip-sync engine specific signals
public enum Signals
{
VisemeOn,
VisemeOff,
VisemeAmount,
VisemeSmoothing,
LaughterAmount
};
public static readonly int SignalCount = Enum.GetNames(typeof(Signals)).Length;
// Enum for provider context to create
public enum ContextProviders
{
Original,
Enhanced,
Enhanced_with_Laughter,
};
/// NOTE: Opaque typedef for lip-sync context is an unsigned int (uint)
/// Current phoneme frame results
[System.Serializable]
public class Frame
{
public void CopyInput(Frame input)
{
frameNumber = input.frameNumber;
frameDelay = input.frameDelay;
input.Visemes.CopyTo(Visemes, 0);
laughterScore = input.laughterScore;
}
public void Reset()
{
frameNumber = 0;
frameDelay = 0;
Array.Clear(Visemes, 0, VisemeCount);
laughterScore = 0;
}
public int frameNumber; // count from start of recognition
public int frameDelay; // in ms
public float[] Visemes = new float[VisemeCount]; // Array of floats for viseme frame. Size of Viseme Count, above
public float laughterScore; // probability of laughter presence.
};
// * * * * * * * * * * * * *
// Import functions
public const string strOVRLS = "OVRLipSync";
[DllImport(strOVRLS)]
private static extern int ovrLipSyncDll_Initialize(int samplerate, int buffersize);
[DllImport(strOVRLS)]
private static extern void ovrLipSyncDll_Shutdown();
[DllImport(strOVRLS)]
private static extern IntPtr ovrLipSyncDll_GetVersion(ref int Major,
ref int Minor,
ref int Patch);
[DllImport(strOVRLS)]
private static extern int ovrLipSyncDll_CreateContextEx(ref uint context,
ContextProviders provider,
int sampleRate,
bool enableAcceleration);
[DllImport(strOVRLS)]
private static extern int ovrLipSyncDll_CreateContextWithModelFile(ref uint context,
ContextProviders provider,
string modelPath,
int sampleRate,
bool enableAcceleration);
[DllImport(strOVRLS)]
private static extern int ovrLipSyncDll_DestroyContext(uint context);
[DllImport(strOVRLS)]
private static extern int ovrLipSyncDll_ResetContext(uint context);
[DllImport(strOVRLS)]
private static extern int ovrLipSyncDll_SendSignal(uint context,
Signals signal,
int arg1, int arg2);
[DllImport(strOVRLS)]
private static extern int ovrLipSyncDll_ProcessFrameEx(
uint context,
IntPtr audioBuffer,
uint bufferSize,
AudioDataType dataType,
ref int frameNumber,
ref int frameDelay,
float[] visemes,
int visemeCount,
ref float laughterScore,
float[] laughterCategories,
int laughterCategoriesLength);
// * * * * * * * * * * * * *
// Public members
// * * * * * * * * * * * * *
// Static members
private static Result sInitialized = Result.Unknown;
// interface through this static member.
public static OVRLipSync sInstance = null;
// * * * * * * * * * * * * *
// Public Functions
public static Result Initialize()
{
int sampleRate;
int bufferSize;
// Get the current sample rate
sampleRate = 44100;
// Get the current buffer size and number of buffers
bufferSize = 1024;
String str = System.String.Format
("OvrLipSync Awake: Queried SampleRate: {0:F0} BufferSize: {1:F0}", sampleRate, bufferSize);
Debug.LogWarning(str);
sInitialized = (Result)ovrLipSyncDll_Initialize(sampleRate, bufferSize);
return sInitialized;
}
public static Result Initialize(int sampleRate, int bufferSize)
{
String str = System.String.Format
("OvrLipSync Awake: Queried SampleRate: {0:F0} BufferSize: {1:F0}", sampleRate, bufferSize);
Debug.LogWarning(str);
sInitialized = (Result)ovrLipSyncDll_Initialize(sampleRate, bufferSize);
return sInitialized;
}
public static void Shutdown()
{
ovrLipSyncDll_Shutdown();
sInitialized = Result.Unknown;
}
/// <summary>
/// Determines if is initialized.
/// </summary>
/// <returns><c>true</c> if is initialized; otherwise, <c>false</c>.</returns>
public static Result IsInitialized()
{
return sInitialized;
}
/// <summary>
/// Creates a lip-sync context.
/// </summary>
/// <returns>error code</returns>
/// <param name="context">Context.</param>
/// <param name="provider">Provider.</param>
/// <param name="enableAcceleration">Enable DSP Acceleration.</param>
public static Result CreateContext(
ref uint context,
ContextProviders provider,
int sampleRate = 0,
bool enableAcceleration = false)
{
if (IsInitialized() != Result.Success && Initialize() != Result.Success)
return Result.CannotCreateContext;
return (Result)ovrLipSyncDll_CreateContextEx(ref context, provider, sampleRate, enableAcceleration);
}
/// <summary>
/// Creates a lip-sync context with specified model file.
/// </summary>
/// <returns>error code</returns>
/// <param name="context">Context.</param>
/// <param name="provider">Provider.</param>
/// <param name="modelPath">Model Dir.</param>
/// <param name="sampleRate">Sampling Rate.</param>
/// <param name="enableAcceleration">Enable DSP Acceleration.</param>
public static Result CreateContextWithModelFile(
ref uint context,
ContextProviders provider,
string modelPath,
int sampleRate = 0,
bool enableAcceleration = false)
{
if (IsInitialized() != Result.Success && Initialize() != Result.Success)
return Result.CannotCreateContext;
return (Result)ovrLipSyncDll_CreateContextWithModelFile(
ref context,
provider,
modelPath,
sampleRate,
enableAcceleration);
}
/// <summary>
/// Destroy a lip-sync context.
/// </summary>
/// <returns>The context.</returns>
/// <param name="context">Context.</param>
public static Result DestroyContext(uint context)
{
if (IsInitialized() != Result.Success)
return Result.Unknown;
return (Result)ovrLipSyncDll_DestroyContext(context);
}
/// <summary>
/// Resets the context.
/// </summary>
/// <returns>error code</returns>
/// <param name="context">Context.</param>
public static Result ResetContext(uint context)
{
if (IsInitialized() != Result.Success)
return Result.Unknown;
return (Result)ovrLipSyncDll_ResetContext(context);
}
/// <summary>
/// Sends a signal to the lip-sync engine.
/// </summary>
/// <returns>error code</returns>
/// <param name="context">Context.</param>
/// <param name="signal">Signal.</param>
/// <param name="arg1">Arg1.</param>
/// <param name="arg2">Arg2.</param>
public static Result SendSignal(uint context, Signals signal, int arg1, int arg2)
{
if (IsInitialized() != Result.Success)
return Result.Unknown;
return (Result)ovrLipSyncDll_SendSignal(context, signal, arg1, arg2);
}
/// <summary>
/// Process float[] audio buffer by lip-sync engine.
/// </summary>
/// <returns>error code</returns>
/// <param name="context">Context.</param>
/// <param name="audioBuffer"> PCM audio buffer.</param>
/// <param name="frame">Lip-sync Frame.</param>
/// <param name="stereo">Whether buffer is part of stereo or mono stream.</param>
public static Result ProcessFrame(
uint context, float[] audioBuffer, Frame frame, bool stereo = true)
{
if (IsInitialized() != Result.Success)
return Result.Unknown;
var dataType = stereo ? AudioDataType.F32_Stereo : AudioDataType.F32_Mono;
var numSamples = (uint)(stereo ? audioBuffer.Length / 2 : audioBuffer.Length);
var handle = GCHandle.Alloc(audioBuffer, GCHandleType.Pinned);
var rc = ovrLipSyncDll_ProcessFrameEx(context,
handle.AddrOfPinnedObject(), numSamples, dataType,
ref frame.frameNumber, ref frame.frameDelay,
frame.Visemes, frame.Visemes.Length,
ref frame.laughterScore,
null, 0
);
handle.Free();
return (Result)rc;
}
/// <summary>
/// Process short[] audio buffer by lip-sync engine.
/// </summary>
/// <returns>error code</returns>
/// <param name="context">Context.</param>
/// <param name="audioBuffer"> PCM audio buffer.</param>
/// <param name="frame">Lip-sync Frame.</param>
/// <param name="stereo">Whether buffer is part of stereo or mono stream.</param>
public static Result ProcessFrame(
uint context, short[] audioBuffer, Frame frame, bool stereo = true)
{
if (IsInitialized() != Result.Success)
return Result.Unknown;
var dataType = stereo ? AudioDataType.S16_Stereo : AudioDataType.S16_Mono;
var numSamples = (uint)(stereo ? audioBuffer.Length / 2 : audioBuffer.Length);
var handle = GCHandle.Alloc(audioBuffer, GCHandleType.Pinned);
var rc = ovrLipSyncDll_ProcessFrameEx(context,
handle.AddrOfPinnedObject(), numSamples, dataType,
ref frame.frameNumber, ref frame.frameDelay,
frame.Visemes, frame.Visemes.Length,
ref frame.laughterScore,
null, 0
);
handle.Free();
return (Result)rc;
}
}
}