| 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 | |
| 8 | Licensed under the Oculus Audio SDK License Version 3.3 (the "License"); |
| 9 | you may not use the Oculus Audio SDK except in compliance with the License, |
| 10 | which is provided at the time of installation or download, or which |
| 11 | otherwise accompanies this software in either electronic or hard copy form. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | |
| 15 | https://developer.oculus.com/licenses/audio-3.3/ |
| 16 | |
| 17 | Unless required by applicable law or agreed to in writing, the Oculus Audio SDK |
| 18 | distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | See the License for the specific language governing permissions and |
| 21 | limitations under the License. |
| 22 | ************************************************************************************/ |
| 23 | using UnityEngine; |
| 24 | |
| 25 | |
| 26 | [RequireComponent(typeof(AudioSource))] |
| 27 | |
| 28 | //------------------------------------------------------------------------------------- |
| 29 | // ***** OVRLipSyncContextBase |
| 30 | // |
| 31 | /// <summary> |
| 32 | /// OVRLipSyncContextBase interfaces into the Oculus phoneme recognizer. |
| 33 | /// This component should be added into the scene once for each Audio Source. |
| 34 | /// |
| 35 | /// </summary> |
| 36 | public class OVRLipSyncContextBase : MonoBehaviour |
| 37 | { |
| 38 | // * * * * * * * * * * * * * |
| 39 | // Public members |
| 40 | public AudioSource audioSource = null; |
| 41 | |
| 42 | [Tooltip("Which lip sync provider to use for viseme computation.")] |
| 43 | public OVRLipSync.ContextProviders provider = OVRLipSync.ContextProviders.Enhanced; |
| 44 | [Tooltip("Enable DSP offload on supported Android devices.")] |
| 45 | public bool enableAcceleration = true; |
| 46 | |
| 47 | // * * * * * * * * * * * * * |
| 48 | // Private members |
| 49 | private OVRLipSync.Frame frame = new OVRLipSync.Frame(); |
| 50 | private uint context = 0; // 0 is no context |
| 51 | |
| 52 | private int _smoothing; |
| 53 | public int Smoothing |
| 54 | { |
| 55 | set |
| 56 | { |
| 57 | OVRLipSync.Result result = |
| 58 | OVRLipSync.SendSignal(context, OVRLipSync.Signals.VisemeSmoothing, value, 0); |
| 59 | |
| 60 | if (result != OVRLipSync.Result.Success) |
| 61 | { |
| 62 | if (result == OVRLipSync.Result.InvalidParam) |
| 63 | { |
| 64 | Debug.LogError("OVRLipSyncContextBase.SetSmoothing: A viseme smoothing" + |
| 65 | " parameter is invalid, it should be between 1 and 100!"); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | Debug.LogError("OVRLipSyncContextBase.SetSmoothing: An unexpected" + |
| 70 | " error occured."); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | _smoothing = value; |
| 75 | } |
| 76 | get |
| 77 | { |
| 78 | return _smoothing; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public uint Context |
| 83 | { |
| 84 | get |
| 85 | { |
| 86 | return context; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | protected OVRLipSync.Frame Frame |
| 91 | { |
| 92 | get |
| 93 | { |
| 94 | return frame; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// <summary> |
| 99 | /// Awake this instance. |
| 100 | /// </summary> |
| 101 | void Awake() |
| 102 | { |
| 103 | // Cache the audio source we are going to be using to pump data to the SR |
| 104 | if (!audioSource) |
| 105 | { |
| 106 | audioSource = GetComponent<AudioSource>(); |
| 107 | } |
| 108 | |
| 109 | lock (this) |
| 110 | { |
| 111 | if (context == 0) |
| 112 | { |
| 113 | if (OVRLipSync.CreateContext(ref context, provider, 0, enableAcceleration) |
| 114 | != OVRLipSync.Result.Success) |
| 115 | { |
| 116 | Debug.LogError("OVRLipSyncContextBase.Start ERROR: Could not create" + |
| 117 | " Phoneme context."); |
| 118 | return; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /// <summary> |
| 126 | /// Raises the destroy event. |
| 127 | /// </summary> |
| 128 | void OnDestroy() |
| 129 | { |
| 130 | // Create the context that we will feed into the audio buffer |
| 131 | lock (this) |
| 132 | { |
| 133 | if (context != 0) |
| 134 | { |
| 135 | if (OVRLipSync.DestroyContext(context) != OVRLipSync.Result.Success) |
| 136 | { |
| 137 | Debug.LogError("OVRLipSyncContextBase.OnDestroy ERROR: Could not delete" + |
| 138 | " Phoneme context."); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // * * * * * * * * * * * * * |
| 145 | // Public Functions |
| 146 | |
| 147 | /// <summary> |
| 148 | /// Gets the current phoneme frame (lock and copy current frame to caller frame) |
| 149 | /// </summary> |
| 150 | /// <returns>error code</returns> |
| 151 | /// <param name="inFrame">In frame.</param> |
| 152 | public OVRLipSync.Frame GetCurrentPhonemeFrame() |
| 153 | { |
| 154 | return frame; |
| 155 | } |
| 156 | |
| 157 | /// <summary> |
| 158 | /// Sets a given viseme id blend weight to a given amount |
| 159 | /// </summary> |
| 160 | /// <param name="viseme">Integer viseme ID</param> |
| 161 | /// <param name="amount">Integer viseme amount</param> |
| 162 | public void SetVisemeBlend(int viseme, int amount) |
| 163 | { |
| 164 | OVRLipSync.Result result = |
| 165 | OVRLipSync.SendSignal(context, OVRLipSync.Signals.VisemeAmount, viseme, amount); |
| 166 | |
| 167 | if (result != OVRLipSync.Result.Success) |
| 168 | { |
| 169 | if (result == OVRLipSync.Result.InvalidParam) |
| 170 | { |
| 171 | Debug.LogError("OVRLipSyncContextBase.SetVisemeBlend: Viseme ID is invalid."); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | Debug.LogError("OVRLipSyncContextBase.SetVisemeBlend: An unexpected" + |
| 176 | " error occured."); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /// <summary> |
| 182 | /// Sets a given viseme id blend weight to a given amount |
| 183 | /// </summary> |
| 184 | /// <param name="amount">Integer viseme amount</param> |
| 185 | public void SetLaughterBlend(int amount) |
| 186 | { |
| 187 | OVRLipSync.Result result = |
| 188 | OVRLipSync.SendSignal(context, OVRLipSync.Signals.LaughterAmount, amount, 0); |
| 189 | |
| 190 | if (result != OVRLipSync.Result.Success) |
| 191 | { |
| 192 | Debug.LogError("OVRLipSyncContextBase.SetLaughterBlend: An unexpected" + |
| 193 | " error occured."); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /// <summary> |
| 198 | /// Resets the context. |
| 199 | /// </summary> |
| 200 | /// <returns>error code</returns> |
| 201 | public OVRLipSync.Result ResetContext() |
| 202 | { |
| 203 | // Reset visemes to silence etc. |
| 204 | frame.Reset(); |
| 205 | |
| 206 | return OVRLipSync.ResetContext(context); |
| 207 | } |
| 208 | } |