BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VoiceMod / Scripts / OVRVoiceModContext.cs

OVRVoiceModContext.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRVoiceModContext.cs 17.4 KiB Download Raw
1/************************************************************************************
2Filename : OVRVoiceModContext.cs
3Content : Interface to Oculus Lip-Sync engine
4Created : December 14th, 2015
5Copyright : Copyright 2015 Oculus VR, Inc. All Rights reserved.
6
7Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
8you may not use the Oculus VR Rift SDK except in compliance with the License,
9which is provided at the time of installation or download, or which
10otherwise accompanies this software in either electronic or hard copy form.
11
12You may obtain a copy of the License at
13
14http://www.oculusvr.com/licenses/LICENSE-3.1
15
16Unless required by applicable law or agreed to in writing, the Oculus VR SDK
17distributed under the License is distributed on an "AS IS" BASIS,
18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19See the License for the specific language governing permissions and
20limitations under the License.
21************************************************************************************/
22using UnityEngine;
23using System;
24using System.Runtime.InteropServices;
25
26
27[RequireComponent(typeof(AudioSource))]
28
29//-------------------------------------------------------------------------------------
30// ***** OVRVoiceModContext
31//
32/// <summary>
33/// OVRVoiceModContext interfaces into VoiceMod engine.
34/// Includes preset bank.
35/// This component should be added into the scene once for each Audio Source.
36///
37/// </summary>
38public class OVRVoiceModContext : MonoBehaviour
39{
40 // Enum for various voice mod parameters (lines up with native context, OVRVoiceModContext.h)
41 public enum ovrVoiceModParams
42 {
43 MixInputAudio, // 0.0->1.0
44 PitchInputAudio, // 0.5->2.0
45 SetBands, // 1 -> 128
46 FormantCorrection, // 0 = off, 1 = on
47 Carrier1_TrackPitch, // 0 = off, 1 = om
48 Carrier1_Type, // Carrier type: 0 = Noise, 1 = CycledNoise, 2 = SawUp, 3 = Pulse
49 Carrier1_Gain, // Linear multiplier (0.0 -> 1.0)
50 Carrier1_Frequency, // Frequency of carrier (used by SawUp and Pulse) 0.0 -> Sample Rate
51 Carrier1_Note, // Translates to frequency (0-127)
52 Carrier1_PulseWidth, // Used by Pulse carrier (0.0 -> 1.0)
53 Carrier1_CycledNoiseSize, // Number of samples in cycled noise carrier: 1 - 1024
54 Carrier2_TrackPitch, // Same as Carrier 1
55 Carrier2_Type, //
56 Carrier2_Gain, //
57 Carrier2_Frequency, //
58 Carrier2_Note, //
59 Carrier2_PulseWidth, //
60 Carrier2_CycledNoiseSize, //
61 Count
62 };
63
64 // * * * * * * * * * * * * *
65 // Public members
66 public AudioSource audioSource = null;
67 public float gain = 1.0f;
68 public bool audioMute = true;
69 public KeyCode loopback = KeyCode.L;
70
71 // Voice Mod Preset
72 public struct VMPreset
73 {
74 public string info;
75 public Color color;
76 public float mix;
77 public float pitch;
78 public int bands;
79 public int formant;
80 public int c1PTrack;
81 public int c1Type;
82 public float c1Gain;
83 public float c1Freq;
84 public int c1Note;
85 public float c1PW;
86 public int c1CNS;
87 public int c2PTrack;
88 public int c2Type;
89 public float c2Gain;
90 public float c2Freq;
91 public int c2Note;
92 public float c2PW;
93 public int c2CNS;
94 };
95
96 private VMPreset [] VMPresets = new VMPreset[]
97 {
98 // INIT
99 new VMPreset{info="-INIT-\nNo pitch shift, no vocode",
100 color = Color.gray,
101 mix = 1.0f, pitch = 1.0f, bands = 32, formant = 0,
102 c1PTrack = 0, c1Type = 0, c1Gain = 0.0f, c1Freq = 440.0f, c1Note = -1, c1PW = 0.5f, c1CNS = 512,
103 c2PTrack = 0, c2Type = 0, c2Gain = 0.0f, c2Freq = 440.0f, c2Note = -1, c2PW = 0.5f, c2CNS = 512,},
104
105 new VMPreset{info="FULL VOCODE\nCarrier 1: Full noise",
106 color = Color.white,
107 mix = 0.0f, pitch = 1.0f, bands = 32, formant = 0,
108 c1PTrack = 0, c1Type = 0, c1Gain = 1.0f, c1Freq = 440.0f, c1Note = -1, c1PW = 0.5f, c1CNS = 512,
109 c2PTrack = 0, c2Type = 0, c2Gain = 0.0f, c2Freq = 440.0f, c2Note = -1, c2PW = 0.5f, c2CNS = 512,},
110
111 new VMPreset{info="FULL VOCODE\nCarrier 1: Cycled noise 512",
112 color = Color.blue,
113 mix = 0.0f, pitch = 1.0f, bands = 32, formant = 0,
114 c1PTrack = 0, c1Type = 1, c1Gain = 1.0f, c1Freq = 440.0f, c1Note = -1, c1PW = 0.5f, c1CNS = 512,
115 c2PTrack = 0, c2Type = 0, c2Gain = 0.0f, c2Freq = 440.0f, c2Note = -1, c2PW = 0.5f, c2CNS = 512,},
116
117 new VMPreset{info="FULL VOCODE\nCarrier 1: Saw Up, Freq 220",
118 color = Color.magenta,
119 mix = 0.0f, pitch = 1.0f, bands = 32, formant = 0,
120 c1PTrack = 0, c1Type = 2, c1Gain = 1.0f, c1Freq = 220.0f, c1Note = -1, c1PW = 0.5f, c1CNS = 512,
121 c2PTrack = 0, c2Type = 0, c2Gain = 0.0f, c2Freq = 440.0f, c2Note = -1, c2PW = 0.5f, c2CNS = 512,},
122
123 new VMPreset{info="FULL VOCODE\nCarrier 1: Saw Up, Pitch tracked\n",
124 color = Color.cyan,
125 mix = 0.0f, pitch = 1.0f, bands = 32, formant = 0,
126 c1PTrack = 1, c1Type = 2, c1Gain = 0.34f, c1Freq = 440.0f, c1Note = -1, c1PW = 0.1f, c1CNS = 512,
127 c2PTrack = 0, c2Type = 0, c2Gain = 0.0f, c2Freq = 440.0f, c2Note = -1, c2PW = 0.5f, c2CNS = 512,},
128
129 new VMPreset{info="INPUT PLUS VOCODE\nInput 50%, Vocode 50%\nPitch 1.0\nCarrier 1: Full Noise,\nCarrier 2: Cycled Noise 512",
130 color = Color.green,
131 mix = 0.5f, pitch = 1.0f, bands = 32, formant = 0,
132 c1PTrack = 0, c1Type = 0, c1Gain = 0.5f, c1Freq = 440.0f, c1Note = 57, c1PW = 0.5f, c1CNS = 512,
133 c2PTrack = 0, c2Type = 1, c2Gain = 0.5f, c2Freq = 440.0f, c2Note = 45, c2PW = 0.25f, c2CNS = 512,},
134
135 new VMPreset{info="INPUT PLUS VOCODE PLUS PITCH DOWN\nInput 50%, Vocode 50%\nPitch 0.75\nCarrier 1: Cycled Noise 512\nCarrier 2: Cycled Noise 768",
136 color = Color.red,
137 mix = 0.5f, pitch = 0.75f, bands = 32, formant = 0,
138 c1PTrack = 0, c1Type = 1, c1Gain = 0.6f, c1Freq = 440.0f, c1Note = 57, c1PW = 0.5f, c1CNS = 512,
139 c2PTrack = 0, c2Type = 3, c2Gain = 0.2f, c2Freq = 440.0f, c2Note = 40, c2PW = 0.25f, c2CNS = 768,},
140
141 new VMPreset{info="PITCH ONLY\nPitch 1.25 (Formant correction)",
142 color = Color.blue,
143 mix = 1.0f, pitch = 1.25f, bands = 32, formant = 1,
144 c1PTrack = 0, c1Type = 1, c1Gain = 1.0f, c1Freq = 440.0f, c1Note = 57, c1PW = 0.5f, c1CNS = 400,
145 c2PTrack = 0, c2Type = 3, c2Gain = 0.0f, c2Freq = 440.0f, c2Note = 52, c2PW = 0.5f, c2CNS = 512,},
146
147 new VMPreset{info="PITCH ONLY\nPitch 0.5 (Formant correction)",
148 color = Color.green,
149 mix = 1.0f, pitch = 0.5f, bands = 32, formant = 1,
150 c1PTrack = 0, c1Type = 1, c1Gain = 1.0f, c1Freq = 440.0f, c1Note = 57, c1PW = 0.5f, c1CNS = 400,
151 c2PTrack = 0, c2Type = 3, c2Gain = 0.0f, c2Freq = 440.0f, c2Note = 52, c2PW = 0.5f, c2CNS = 512,},
152
153 new VMPreset{info="PITCH ONLY\nPitch 2.0 (Formant correction)",
154 color = Color.yellow,
155 mix = 1.0f, pitch = 2.0f, bands = 32, formant = 1,
156 c1PTrack = 0, c1Type = 1, c1Gain = 1.0f, c1Freq = 440.0f, c1Note = 57, c1PW = 0.5f, c1CNS = 400,
157 c2PTrack = 0, c2Type = 3, c2Gain = 0.0f, c2Freq = 440.0f, c2Note = 52, c2PW = 0.5f, c2CNS = 512,},
158 };
159
160 // Current VoiceMod values (visible in inspector without writing a editor helper class)
161 public float VM_MixAudio = 1.0f;
162 public float VM_Pitch = 1.0f;
163 public int VM_Bands = 32;
164 public int VM_FormantCorrect = 0;
165
166 // Carrier 1
167 public int VM_C1_TrackPitch = 0;
168 public int VM_C1_Type = 0;
169 public float VM_C1_Gain = 0.5f;
170 public float VM_C1_Freq = 440.0f;
171 public int VM_C1_Note = 67;
172 public float VM_C1_PulseWidth = 0.5f;
173 public int VM_C1_CycledNoiseSize = 512;
174
175 // Carrier 2
176 public int VM_C2_TrackPitch = 0;
177 public int VM_C2_Type = 0;
178 public float VM_C2_Gain = 0.5f;
179 public float VM_C2_Freq = 440.0f;
180 public int VM_C2_Note = 67;
181 public float VM_C2_PulseWidth = 0.5f;
182 public int VM_C2_CycledNoiseSize = 512;
183
184 // * * * * * * * * * * * * *
185 // Private members
186 private uint context = 0; // 0 is no context
187 private float prevVol = 0.0f; // used for smoothing avg volume
188
189 // * * * * * * * * * * * * *
190 // Static members
191
192 // * * * * * * * * * * * * *
193 // MonoBehaviour overrides
194
195 /// <summary>
196 /// Awake this instance.
197 /// </summary>
198 void Awake ()
199 {
200 // Cache the audio source we are going to be using to pump data to the SR
201 if (!audioSource) audioSource = GetComponent<AudioSource>();
202 if (!audioSource) return;
203 }
204
205 /// <summary>
206 /// Start this instance.
207 /// Note: make sure to always have a Start function for classes that have editor scripts.
208 /// </summary>
209 void Start()
210 {
211 // Create the context that we will feed into the audio buffer
212 lock(this)
213 {
214 if(context == 0)
215 {
216 if(OVRVoiceMod.CreateContext(ref context) != OVRVoiceMod.ovrVoiceModSuccess)
217 {
218 Debug.Log ("OVRVoiceModContext.Start ERROR: Could not create VoiceMod context.");
219 return;
220 }
221 }
222 }
223
224 // Add a listener to the OVRMessenger for touch events
225 OVRMessenger.AddListener<OVRTouchpad.TouchEvent>("Touchpad", LocalTouchEventCallback);
226
227 // VoiceMod: Set the current state of the voice mod as set in the inspector
228 SendVoiceModUpdate();
229 }
230
231 /// <summary>
232 /// Run processes that need to be updated in our game thread
233 /// </summary>
234 void Update()
235 {
236 // Turn loopback on/off
237 if (Input.GetKeyDown(loopback))
238 {
239 audioMute = !audioMute;
240
241 OVRDebugConsole.Clear();
242 OVRDebugConsole.ClearTimeout(1.5f);
243
244 if(!audioMute)
245 OVRDebugConsole.Log("LOOPBACK MODE: ENABLED");
246 else
247 OVRDebugConsole.Log("LOOPBACK MODE: DISABLED");
248
249 }
250 else if(Input.GetKeyDown(KeyCode.LeftArrow))
251 {
252 gain -= 0.1f;
253 if(gain < 0.5f) gain = 0.5f;
254
255 string g = "LINEAR GAIN: ";
256 g += gain;
257 OVRDebugConsole.Clear();
258 OVRDebugConsole.Log(g);
259 OVRDebugConsole.ClearTimeout(1.5f);
260
261 }
262 else if(Input.GetKeyDown(KeyCode.RightArrow))
263 {
264 gain += 0.1f;
265 if(gain > 3.0f)
266 gain = 3.0f;
267
268 string g = "LINEAR GAIN: ";
269 g += gain;
270 OVRDebugConsole.Clear();
271 OVRDebugConsole.Log(g);
272 OVRDebugConsole.ClearTimeout(1.5f);
273
274 }
275
276 UpdateVoiceModUpdate();
277 }
278
279 /// <summary>
280 /// Raises the destroy event.
281 /// </summary>
282 void OnDestroy()
283 {
284 // Create the context that we will feed into the audio buffer
285 lock(this)
286 {
287 if(context != 0)
288 {
289 if(OVRVoiceMod.DestroyContext(context) != OVRVoiceMod.ovrVoiceModSuccess)
290 {
291 Debug.Log ("OVRVoiceModContext.OnDestroy ERROR: Could not delete VoiceMod context.");
292 }
293 }
294 }
295 }
296
297 /// <summary>
298 /// Raises the audio filter read event.
299 /// </summary>
300 /// <param name="data">Data.</param>
301 /// <param name="channels">Channels.</param>
302 void OnAudioFilterRead(float[] data, int channels)
303 {
304 // Do not spatialize if we are not initialized, or if there is no
305 // audio source attached to game object
306 if ((OVRVoiceMod.IsInitialized() != OVRVoiceMod.ovrVoiceModSuccess) || audioSource == null)
307 return;
308
309 // increase the gain of the input to get a better signal input
310 for (int i = 0; i < data.Length; ++i)
311 data[i] = data[i] * gain;
312
313 // Send data into VoiceMod context for processing (if context is not 0)
314 lock(this)
315 {
316 if(context != 0)
317 {
318 OVRVoiceMod.ProcessFrameInterleaved(context, data);
319 }
320 }
321
322 // Turn off output (so that we don't get feedback from a mic too close to speakers)
323 if(audioMute == true)
324 {
325 for (int i = 0; i < data.Length; ++i)
326 data[i] = data[i] * 0.0f;
327 }
328 }
329
330 // * * * * * * * * * * * * *
331 // Public Functions
332
333 /// <summary>
334 /// Sends the parameter.
335 /// </summary>
336 /// <returns>The parameter.</returns>
337 /// <param name="parameter">Parameter.</param>
338 /// <param name="value">Value.</param>
339 public int SendParameter(ovrVoiceModParams parameter, int value)
340 {
341 if(OVRVoiceMod.IsInitialized() != OVRVoiceMod.ovrVoiceModSuccess)
342 return (int)OVRVoiceMod.ovrVoiceModError.Unknown;
343
344 return OVRVoiceMod.SendParameter(context, (int)parameter, value);
345 }
346
347 /// <summary>
348 /// Sets the preset.
349 /// </summary>
350 /// <returns><c>true</c>, if preset was set, <c>false</c> otherwise.</returns>
351 /// <param name="preset">Preset.</param>
352 public bool SetPreset(int preset)
353 {
354 if(preset < 0 || preset >= VMPresets.Length)
355 return false;
356
357 VM_MixAudio = VMPresets[preset].mix;
358 VM_Pitch = VMPresets[preset].pitch;
359 VM_Bands = VMPresets[preset].bands;
360 VM_FormantCorrect = VMPresets[preset].formant;
361 VM_C1_TrackPitch = VMPresets[preset].c1PTrack;
362 VM_C1_Type = VMPresets[preset].c1Type;
363 VM_C1_Gain = VMPresets[preset].c1Gain;
364 VM_C1_Freq = VMPresets[preset].c1Freq;
365 VM_C1_Note = VMPresets[preset].c1Note;
366 VM_C1_PulseWidth = VMPresets[preset].c1PW;
367 VM_C1_CycledNoiseSize = VMPresets[preset].c1CNS;
368 VM_C2_TrackPitch = VMPresets[preset].c2PTrack;
369 VM_C2_Type = VMPresets[preset].c2Type;
370 VM_C2_Gain = VMPresets[preset].c2Gain;
371 VM_C2_Freq = VMPresets[preset].c2Freq;
372 VM_C2_Note = VMPresets[preset].c2Note;
373 VM_C2_PulseWidth = VMPresets[preset].c2PW;
374 VM_C2_CycledNoiseSize = VMPresets[preset].c2CNS;
375
376 SendVoiceModUpdate();
377
378 OVRDebugConsole.Clear();
379 OVRDebugConsole.Log(VMPresets[preset].info);
380 OVRDebugConsole.ClearTimeout(5.0f);
381
382 return true;
383 }
384
385 /// <summary>
386 /// Gets the number presets.
387 /// </summary>
388 /// <returns>The number presets.</returns>
389 public int GetNumPresets()
390 {
391 return VMPresets.Length;
392 }
393
394 /// <summary>
395 /// Gets the color of the preset.
396 /// </summary>
397 /// <returns>The preset color.</returns>
398 /// <param name="preset">Preset.</param>
399 public Color GetPresetColor(int preset)
400 {
401 if(preset < 0 || preset >= VMPresets.Length)
402 return Color.black;
403
404 return VMPresets[preset].color;
405 }
406
407 /// <summary>
408 /// Gets the average abs volume.
409 /// </summary>
410 /// <returns>The average abs volume.</returns>
411 public float GetAverageAbsVolume()
412 {
413 if(context == 0)
414 return 0.0f;
415
416 float V = prevVol * 0.8f + OVRVoiceMod.GetAverageAbsVolume(context) * 0.2f;
417 prevVol = V;
418
419 return V;
420 }
421
422 // LocalTouchEventCallback
423 // NOTE: We will not worry about gain on Android, since it will be
424 // more important to switch presets. We will keep gain available on
425 // Desktop
426 void LocalTouchEventCallback(OVRTouchpad.TouchEvent touchEvent)
427 {
428 switch(touchEvent)
429 {
430 case(OVRTouchpad.TouchEvent.SingleTap):
431 audioMute = !audioMute;
432
433 OVRDebugConsole.Clear();
434 OVRDebugConsole.ClearTimeout(1.5f);
435
436 if(!audioMute)
437 OVRDebugConsole.Log("LOOPBACK MODE: ENABLED");
438 else
439 OVRDebugConsole.Log("LOOPBACK MODE: DISABLED");
440
441 break;
442 }
443 }
444
445 // Update directly from current state in inspector
446 void UpdateVoiceModUpdate()
447 {
448 // Send directly from inspector
449 if(Input.GetKeyDown(KeyCode.Space))
450 {
451 SendVoiceModUpdate();
452 OVRDebugConsole.Clear();
453 OVRDebugConsole.Log("UPDATED VOICE MOD FROM INSPECTOR");
454 OVRDebugConsole.ClearTimeout(1.0f);
455 }
456 }
457
458 // Sends the current state of voice mod values
459 void SendVoiceModUpdate()
460 {
461 VM_MixAudio = Mathf.Clamp(VM_MixAudio, 0.0f, 1.0f);
462 VM_Pitch = Mathf.Clamp(VM_Pitch, 0.5f, 2.0f);
463 VM_Bands = Mathf.Clamp(VM_Bands, 1, 128);
464 VM_FormantCorrect = Mathf.Clamp(VM_FormantCorrect, 0, 1);
465 VM_C1_TrackPitch = Mathf.Clamp(VM_C1_TrackPitch, 0, 1);
466 VM_C1_Type = Mathf.Clamp(VM_C1_Type, 0, 3);
467 VM_C1_Gain = Mathf.Clamp(VM_C1_Gain, 0.0f, 1.0f);
468 VM_C1_Freq = Mathf.Clamp(VM_C1_Freq, 0.0f, 96000.0f);
469 VM_C1_Note = Mathf.Clamp(VM_C1_Note, -1, 127);
470 VM_C1_PulseWidth = Mathf.Clamp(VM_C1_PulseWidth, 0.0f, 1.0f);
471 VM_C1_CycledNoiseSize = Mathf.Clamp(VM_C1_CycledNoiseSize, 0, 1024);
472 VM_C2_TrackPitch = Mathf.Clamp(VM_C2_TrackPitch, 0, 1);
473 VM_C2_Type = Mathf.Clamp(VM_C2_Type, 0, 3);
474 VM_C2_Gain = Mathf.Clamp(VM_C2_Gain, 0.0f, 1.0f);
475 VM_C2_Freq = Mathf.Clamp(VM_C2_Freq, 0.0f, 96000.0f);
476 VM_C2_Note = Mathf.Clamp(VM_C2_Note, -1, 127);
477 VM_C2_PulseWidth = Mathf.Clamp(VM_C2_PulseWidth, 0.0f, 1.0f);
478 VM_C2_CycledNoiseSize = Mathf.Clamp(VM_C2_CycledNoiseSize, 0, 1024);
479
480 // We will send these in as Int and use 100 as a scale
481 // Might go to 1000 :)
482
483 // VoiceMod and Vocoder values
484 SendParameter(ovrVoiceModParams.MixInputAudio, (int)(100.0f * VM_MixAudio));
485 SendParameter(ovrVoiceModParams.PitchInputAudio,(int)(100.0f * VM_Pitch));
486 SendParameter(ovrVoiceModParams.SetBands, (int)VM_Bands);
487 SendParameter(ovrVoiceModParams.FormantCorrection, (int)VM_FormantCorrect);
488
489 // Carrier 1
490 SendParameter(ovrVoiceModParams.Carrier1_TrackPitch, (int)VM_C1_TrackPitch);
491 SendParameter(ovrVoiceModParams.Carrier1_Type, (int)VM_C1_Type);
492 SendParameter(ovrVoiceModParams.Carrier1_Gain, (int)(100.0f * VM_C1_Gain));
493
494 // Note overrides Frequency if valid range
495 if(VM_C1_Note == -1)
496 SendParameter(ovrVoiceModParams.Carrier1_Frequency, (int)(100.0f * VM_C1_Freq));
497 else
498 SendParameter(ovrVoiceModParams.Carrier1_Note, (int)VM_C1_Note);
499
500 SendParameter(ovrVoiceModParams.Carrier1_PulseWidth, (int)(100.0f * VM_C1_PulseWidth));
501 SendParameter(ovrVoiceModParams.Carrier1_CycledNoiseSize, (int)VM_C1_CycledNoiseSize);
502
503 // Carrier 2
504 SendParameter(ovrVoiceModParams.Carrier2_TrackPitch, (int)VM_C2_TrackPitch);
505 SendParameter(ovrVoiceModParams.Carrier2_Type, (int)VM_C2_Type);
506 SendParameter(ovrVoiceModParams.Carrier2_Gain, (int)(100.0f * VM_C2_Gain));
507
508 // Note overrides Frequency if valid range
509 if(VM_C2_Note == -1)
510 SendParameter(ovrVoiceModParams.Carrier2_Frequency, (int)(100.0f * VM_C2_Freq));
511 else
512 SendParameter(ovrVoiceModParams.Carrier2_Note, (int)VM_C2_Note);
513
514 SendParameter(ovrVoiceModParams.Carrier2_PulseWidth, (int)(100.0f * VM_C2_PulseWidth));
515 SendParameter(ovrVoiceModParams.Carrier2_CycledNoiseSize, (int)VM_C1_CycledNoiseSize);
516 }
517}