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.

208 lines
6.2 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. // ***** OVRLipSyncContextBase
  23. //
  24. /// <summary>
  25. /// OVRLipSyncContextBase 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 OVRLipSyncContextBase : MonoBehaviour
  30. {
  31. // * * * * * * * * * * * * *
  32. // Public members
  33. public AudioSource audioSource = null;
  34. [Tooltip("Which lip sync provider to use for viseme computation.")]
  35. public OVRLipSync.ContextProviders provider = OVRLipSync.ContextProviders.Enhanced;
  36. [Tooltip("Enable DSP offload on supported Android devices.")]
  37. public bool enableAcceleration = true;
  38. // * * * * * * * * * * * * *
  39. // Private members
  40. private OVRLipSync.Frame frame = new OVRLipSync.Frame();
  41. private uint context = 0; // 0 is no context
  42. private int _smoothing;
  43. public int Smoothing
  44. {
  45. set
  46. {
  47. OVRLipSync.Result result =
  48. OVRLipSync.SendSignal(context, OVRLipSync.Signals.VisemeSmoothing, value, 0);
  49. if (result != OVRLipSync.Result.Success)
  50. {
  51. if (result == OVRLipSync.Result.InvalidParam)
  52. {
  53. Debug.LogError("OVRLipSyncContextBase.SetSmoothing: A viseme smoothing" +
  54. " parameter is invalid, it should be between 1 and 100!");
  55. }
  56. else
  57. {
  58. Debug.LogError("OVRLipSyncContextBase.SetSmoothing: An unexpected" +
  59. " error occured.");
  60. }
  61. }
  62. _smoothing = value;
  63. }
  64. get
  65. {
  66. return _smoothing;
  67. }
  68. }
  69. public uint Context
  70. {
  71. get
  72. {
  73. return context;
  74. }
  75. }
  76. protected OVRLipSync.Frame Frame
  77. {
  78. get
  79. {
  80. return frame;
  81. }
  82. }
  83. /// <summary>
  84. /// Awake this instance.
  85. /// </summary>
  86. void Awake()
  87. {
  88. // Cache the audio source we are going to be using to pump data to the SR
  89. if (!audioSource)
  90. {
  91. audioSource = GetComponent<AudioSource>();
  92. }
  93. lock (this)
  94. {
  95. if (context == 0)
  96. {
  97. if (OVRLipSync.CreateContext(ref context, provider, 0, enableAcceleration)
  98. != OVRLipSync.Result.Success)
  99. {
  100. Debug.LogError("OVRLipSyncContextBase.Start ERROR: Could not create" +
  101. " Phoneme context.");
  102. return;
  103. }
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Raises the destroy event.
  109. /// </summary>
  110. void OnDestroy()
  111. {
  112. // Create the context that we will feed into the audio buffer
  113. lock (this)
  114. {
  115. if (context != 0)
  116. {
  117. if (OVRLipSync.DestroyContext(context) != OVRLipSync.Result.Success)
  118. {
  119. Debug.LogError("OVRLipSyncContextBase.OnDestroy ERROR: Could not delete" +
  120. " Phoneme context.");
  121. }
  122. }
  123. }
  124. }
  125. // * * * * * * * * * * * * *
  126. // Public Functions
  127. /// <summary>
  128. /// Gets the current phoneme frame (lock and copy current frame to caller frame)
  129. /// </summary>
  130. /// <returns>error code</returns>
  131. /// <param name="inFrame">In frame.</param>
  132. public OVRLipSync.Frame GetCurrentPhonemeFrame()
  133. {
  134. return frame;
  135. }
  136. /// <summary>
  137. /// Sets a given viseme id blend weight to a given amount
  138. /// </summary>
  139. /// <param name="viseme">Integer viseme ID</param>
  140. /// <param name="amount">Integer viseme amount</param>
  141. public void SetVisemeBlend(int viseme, int amount)
  142. {
  143. OVRLipSync.Result result =
  144. OVRLipSync.SendSignal(context, OVRLipSync.Signals.VisemeAmount, viseme, amount);
  145. if (result != OVRLipSync.Result.Success)
  146. {
  147. if (result == OVRLipSync.Result.InvalidParam)
  148. {
  149. Debug.LogError("OVRLipSyncContextBase.SetVisemeBlend: Viseme ID is invalid.");
  150. }
  151. else
  152. {
  153. Debug.LogError("OVRLipSyncContextBase.SetVisemeBlend: An unexpected" +
  154. " error occured.");
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// Sets a given viseme id blend weight to a given amount
  160. /// </summary>
  161. /// <param name="amount">Integer viseme amount</param>
  162. public void SetLaughterBlend(int amount)
  163. {
  164. OVRLipSync.Result result =
  165. OVRLipSync.SendSignal(context, OVRLipSync.Signals.LaughterAmount, amount, 0);
  166. if (result != OVRLipSync.Result.Success)
  167. {
  168. Debug.LogError("OVRLipSyncContextBase.SetLaughterBlend: An unexpected" +
  169. " error occured.");
  170. }
  171. }
  172. /// <summary>
  173. /// Resets the context.
  174. /// </summary>
  175. /// <returns>error code</returns>
  176. public OVRLipSync.Result ResetContext()
  177. {
  178. // Reset visemes to silence etc.
  179. frame.Reset();
  180. return OVRLipSync.ResetContext(context);
  181. }
  182. }