Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
12 KiB

  1. /************************************************************************************
  2. Filename : OVRLipSyncContext.cs
  3. Content : Interface to Oculus Lip-Sync engine
  4. Created : August 6th, 2015
  5. Copyright : Copyright Facebook Technologies, LLC and its affiliates.
  6. All rights reserved.
  7. Licensed under the Oculus Audio SDK License Version 3.3 (the "License");
  8. you may not use the Oculus Audio SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. https://developer.oculus.com/licenses/audio-3.3/
  13. Unless required by applicable law or agreed to in writing, the Oculus Audio SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. ************************************************************************************/
  19. using UnityEngine;
  20. [RequireComponent(typeof(AudioSource))]
  21. //-------------------------------------------------------------------------------------
  22. // ***** OVRLipSyncContext
  23. //
  24. /// <summary>
  25. /// OVRLipSyncContext interfaces into the Oculus phoneme recognizer.
  26. /// This component should be added into the scene once for each Audio Source.
  27. ///
  28. /// </summary>
  29. public class OVRLipSyncContext : OVRLipSyncContextBase
  30. {
  31. // * * * * * * * * * * * * *
  32. // Public members
  33. [Tooltip("Allow capturing of keyboard input to control operation.")]
  34. public bool enableKeyboardInput = false;
  35. [Tooltip("Register a mouse/touch callback to control loopback and gain (requires script restart).")]
  36. public bool enableTouchInput = false;
  37. [Tooltip("Play input audio back through audio output.")]
  38. public bool audioLoopback = false;
  39. [Tooltip("Key to toggle audio loopback.")]
  40. public KeyCode loopbackKey = KeyCode.L;
  41. [Tooltip("Show viseme scores in an OVRLipSyncDebugConsole display.")]
  42. public bool showVisemes = false;
  43. [Tooltip("Key to toggle viseme score display.")]
  44. public KeyCode debugVisemesKey = KeyCode.D;
  45. [Tooltip("Skip data from the Audio Source. Use if you intend to pass audio data in manually.")]
  46. public bool skipAudioSource = false;
  47. [Tooltip("Adjust the linear audio gain multiplier before processing lipsync")]
  48. public float gain = 1.0f;
  49. private bool hasDebugConsole = false;
  50. public KeyCode debugLaughterKey = KeyCode.H;
  51. public bool showLaughter = false;
  52. public float laughterScore = 0.0f;
  53. // * * * * * * * * * * * * *
  54. // Private members
  55. /// <summary>
  56. /// Start this instance.
  57. /// Note: make sure to always have a Start function for classes that have editor scripts.
  58. /// </summary>
  59. void Start()
  60. {
  61. // Add a listener to the OVRTouchpad for touch events
  62. if (enableTouchInput)
  63. {
  64. OVRTouchpad.AddListener(LocalTouchEventCallback);
  65. }
  66. // Find console
  67. OVRLipSyncDebugConsole[] consoles = FindObjectsOfType<OVRLipSyncDebugConsole>();
  68. if (consoles.Length > 0)
  69. {
  70. hasDebugConsole = consoles[0];
  71. }
  72. }
  73. /// <summary>
  74. /// Handle keyboard input
  75. /// </summary>
  76. void HandleKeyboard()
  77. {
  78. // Turn loopback on/off
  79. if (Input.GetKeyDown(loopbackKey))
  80. {
  81. ToggleAudioLoopback();
  82. }
  83. else if (Input.GetKeyDown(debugVisemesKey))
  84. {
  85. showVisemes = !showVisemes;
  86. if (showVisemes)
  87. {
  88. if (hasDebugConsole)
  89. {
  90. Debug.Log("DEBUG SHOW VISEMES: ENABLED");
  91. }
  92. else
  93. {
  94. Debug.LogWarning("Warning: No OVRLipSyncDebugConsole in the scene!");
  95. showVisemes = false;
  96. }
  97. }
  98. else
  99. {
  100. if (hasDebugConsole)
  101. {
  102. OVRLipSyncDebugConsole.Clear();
  103. }
  104. Debug.Log("DEBUG SHOW VISEMES: DISABLED");
  105. }
  106. }
  107. else if (Input.GetKeyDown(debugLaughterKey))
  108. {
  109. showLaughter = !showLaughter;
  110. if (showLaughter)
  111. {
  112. if (hasDebugConsole)
  113. {
  114. Debug.Log("DEBUG SHOW LAUGHTER: ENABLED");
  115. }
  116. else
  117. {
  118. Debug.LogWarning("Warning: No OVRLipSyncDebugConsole in the scene!");
  119. showLaughter = false;
  120. }
  121. }
  122. else
  123. {
  124. if (hasDebugConsole)
  125. {
  126. OVRLipSyncDebugConsole.Clear();
  127. }
  128. Debug.Log("DEBUG SHOW LAUGHTER: DISABLED");
  129. }
  130. }
  131. else if (Input.GetKeyDown(KeyCode.LeftArrow))
  132. {
  133. gain -= 1.0f;
  134. if (gain < 1.0f) gain = 1.0f;
  135. string g = "LINEAR GAIN: ";
  136. g += gain;
  137. if (hasDebugConsole)
  138. {
  139. OVRLipSyncDebugConsole.Clear();
  140. OVRLipSyncDebugConsole.Log(g);
  141. OVRLipSyncDebugConsole.ClearTimeout(1.5f);
  142. }
  143. }
  144. else if (Input.GetKeyDown(KeyCode.RightArrow))
  145. {
  146. gain += 1.0f;
  147. if (gain > 15.0f)
  148. gain = 15.0f;
  149. string g = "LINEAR GAIN: ";
  150. g += gain;
  151. if (hasDebugConsole)
  152. {
  153. OVRLipSyncDebugConsole.Clear();
  154. OVRLipSyncDebugConsole.Log(g);
  155. OVRLipSyncDebugConsole.ClearTimeout(1.5f);
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// Run processes that need to be updated in our game thread
  161. /// </summary>
  162. void Update()
  163. {
  164. if (enableKeyboardInput)
  165. {
  166. HandleKeyboard();
  167. }
  168. laughterScore = this.Frame.laughterScore;
  169. DebugShowVisemesAndLaughter();
  170. }
  171. /// <summary>
  172. /// Preprocess F32 PCM audio buffer
  173. /// </summary>
  174. /// <param name="data">Data.</param>
  175. /// <param name="channels">Channels.</param>
  176. public void PreprocessAudioSamples(float[] data, int channels)
  177. {
  178. // Increase the gain of the input
  179. for (int i = 0; i < data.Length; ++i)
  180. {
  181. data[i] = data[i] * gain;
  182. }
  183. }
  184. /// <summary>
  185. /// Postprocess F32 PCM audio buffer
  186. /// </summary>
  187. /// <param name="data">Data.</param>
  188. /// <param name="channels">Channels.</param>
  189. public void PostprocessAudioSamples(float[] data, int channels)
  190. {
  191. // Turn off output (so that we don't get feedback from mics too close to speakers)
  192. if (!audioLoopback)
  193. {
  194. for (int i = 0; i < data.Length; ++i)
  195. data[i] = data[i] * 0.0f;
  196. }
  197. }
  198. /// <summary>
  199. /// Pass F32 PCM audio buffer to the lip sync module
  200. /// </summary>
  201. /// <param name="data">Data.</param>
  202. /// <param name="channels">Channels.</param>
  203. public void ProcessAudioSamplesRaw(float[] data, int channels)
  204. {
  205. // Send data into Phoneme context for processing (if context is not 0)
  206. lock (this)
  207. {
  208. if (Context == 0 || OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
  209. {
  210. return;
  211. }
  212. var frame = this.Frame;
  213. OVRLipSync.ProcessFrame(Context, data, frame, channels == 2);
  214. }
  215. }
  216. /// <summary>
  217. /// Pass S16 PCM audio buffer to the lip sync module
  218. /// </summary>
  219. /// <param name="data">Data.</param>
  220. /// <param name="channels">Channels.</param>
  221. public void ProcessAudioSamplesRaw(short[] data, int channels)
  222. {
  223. // Send data into Phoneme context for processing (if context is not 0)
  224. lock (this)
  225. {
  226. if (Context == 0 || OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
  227. {
  228. return;
  229. }
  230. var frame = this.Frame;
  231. OVRLipSync.ProcessFrame(Context, data, frame, channels == 2);
  232. }
  233. }
  234. /// <summary>
  235. /// Process F32 audio sample and pass it to the lip sync module for computation
  236. /// </summary>
  237. /// <param name="data">Data.</param>
  238. /// <param name="channels">Channels.</param>
  239. public void ProcessAudioSamples(float[] data, int channels)
  240. {
  241. // Do not process if we are not initialized, or if there is no
  242. // audio source attached to game object
  243. if ((OVRLipSync.IsInitialized() != OVRLipSync.Result.Success) || audioSource == null)
  244. {
  245. return;
  246. }
  247. PreprocessAudioSamples(data, channels);
  248. ProcessAudioSamplesRaw(data, channels);
  249. PostprocessAudioSamples(data, channels);
  250. }
  251. /// <summary>
  252. /// Raises the audio filter read event.
  253. /// </summary>
  254. /// <param name="data">Data.</param>
  255. /// <param name="channels">Channels.</param>
  256. void OnAudioFilterRead(float[] data, int channels)
  257. {
  258. if (!skipAudioSource)
  259. {
  260. ProcessAudioSamples(data, channels);
  261. }
  262. }
  263. /// <summary>
  264. /// Print the visemes and laughter score to game window
  265. /// </summary>
  266. void DebugShowVisemesAndLaughter()
  267. {
  268. if (hasDebugConsole)
  269. {
  270. string seq = "";
  271. if (showLaughter)
  272. {
  273. seq += "Laughter:";
  274. int count = (int)(50.0f * this.Frame.laughterScore);
  275. for (int c = 0; c < count; c++)
  276. seq += "*";
  277. seq += "\n";
  278. }
  279. if (showVisemes)
  280. {
  281. for (int i = 0; i < this.Frame.Visemes.Length; i++)
  282. {
  283. seq += ((OVRLipSync.Viseme)i).ToString();
  284. seq += ":";
  285. int count = (int)(50.0f * this.Frame.Visemes[i]);
  286. for (int c = 0; c < count; c++)
  287. seq += "*";
  288. seq += "\n";
  289. }
  290. }
  291. OVRLipSyncDebugConsole.Clear();
  292. if (seq != "")
  293. {
  294. OVRLipSyncDebugConsole.Log(seq);
  295. }
  296. }
  297. }
  298. void ToggleAudioLoopback()
  299. {
  300. audioLoopback = !audioLoopback;
  301. if (hasDebugConsole)
  302. {
  303. OVRLipSyncDebugConsole.Clear();
  304. OVRLipSyncDebugConsole.ClearTimeout(1.5f);
  305. if (audioLoopback)
  306. OVRLipSyncDebugConsole.Log("LOOPBACK MODE: ENABLED");
  307. else
  308. OVRLipSyncDebugConsole.Log("LOOPBACK MODE: DISABLED");
  309. }
  310. }
  311. // LocalTouchEventCallback
  312. void LocalTouchEventCallback(OVRTouchpad.TouchEvent touchEvent)
  313. {
  314. string g = "LINEAR GAIN: ";
  315. switch (touchEvent)
  316. {
  317. case (OVRTouchpad.TouchEvent.SingleTap):
  318. ToggleAudioLoopback();
  319. break;
  320. case (OVRTouchpad.TouchEvent.Up):
  321. gain += 1.0f;
  322. if (gain > 15.0f)
  323. gain = 15.0f;
  324. g += gain;
  325. if (hasDebugConsole)
  326. {
  327. OVRLipSyncDebugConsole.Clear();
  328. OVRLipSyncDebugConsole.Log(g);
  329. OVRLipSyncDebugConsole.ClearTimeout(1.5f);
  330. }
  331. break;
  332. case (OVRTouchpad.TouchEvent.Down):
  333. gain -= 1.0f;
  334. if (gain < 1.0f) gain = 1.0f;
  335. g += gain;
  336. if (hasDebugConsole)
  337. {
  338. OVRLipSyncDebugConsole.Clear();
  339. OVRLipSyncDebugConsole.Log(g);
  340. OVRLipSyncDebugConsole.ClearTimeout(1.5f);
  341. }
  342. break;
  343. }
  344. }
  345. }