123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /************************************************************************************
- Filename : OVRLipSyncContext.cs
- Content : Interface to Oculus Lip-Sync engine
- Created : August 6th, 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;
- [RequireComponent(typeof(AudioSource))]
- //-------------------------------------------------------------------------------------
- // ***** OVRLipSyncContextBase
- //
- /// <summary>
- /// OVRLipSyncContextBase interfaces into the Oculus phoneme recognizer.
- /// This component should be added into the scene once for each Audio Source.
- ///
- /// </summary>
- public class OVRLipSyncContextBase : MonoBehaviour
- {
- // * * * * * * * * * * * * *
- // Public members
- public AudioSource audioSource = null;
- [Tooltip("Which lip sync provider to use for viseme computation.")]
- public OVRLipSync.ContextProviders provider = OVRLipSync.ContextProviders.Enhanced;
- [Tooltip("Enable DSP offload on supported Android devices.")]
- public bool enableAcceleration = true;
- // * * * * * * * * * * * * *
- // Private members
- private OVRLipSync.Frame frame = new OVRLipSync.Frame();
- private uint context = 0; // 0 is no context
- private int _smoothing;
- public int Smoothing
- {
- set
- {
- OVRLipSync.Result result =
- OVRLipSync.SendSignal(context, OVRLipSync.Signals.VisemeSmoothing, value, 0);
- if (result != OVRLipSync.Result.Success)
- {
- if (result == OVRLipSync.Result.InvalidParam)
- {
- Debug.LogError("OVRLipSyncContextBase.SetSmoothing: A viseme smoothing" +
- " parameter is invalid, it should be between 1 and 100!");
- }
- else
- {
- Debug.LogError("OVRLipSyncContextBase.SetSmoothing: An unexpected" +
- " error occured.");
- }
- }
- _smoothing = value;
- }
- get
- {
- return _smoothing;
- }
- }
- public uint Context
- {
- get
- {
- return context;
- }
- }
- protected OVRLipSync.Frame Frame
- {
- get
- {
- return frame;
- }
- }
- /// <summary>
- /// Awake this instance.
- /// </summary>
- void Awake()
- {
- // Cache the audio source we are going to be using to pump data to the SR
- if (!audioSource)
- {
- audioSource = GetComponent<AudioSource>();
- }
- lock (this)
- {
- if (context == 0)
- {
- if (OVRLipSync.CreateContext(ref context, provider, 0, enableAcceleration)
- != OVRLipSync.Result.Success)
- {
- Debug.LogError("OVRLipSyncContextBase.Start ERROR: Could not create" +
- " Phoneme context.");
- return;
- }
- }
- }
- }
- /// <summary>
- /// Raises the destroy event.
- /// </summary>
- void OnDestroy()
- {
- // Create the context that we will feed into the audio buffer
- lock (this)
- {
- if (context != 0)
- {
- if (OVRLipSync.DestroyContext(context) != OVRLipSync.Result.Success)
- {
- Debug.LogError("OVRLipSyncContextBase.OnDestroy ERROR: Could not delete" +
- " Phoneme context.");
- }
- }
- }
- }
- // * * * * * * * * * * * * *
- // Public Functions
- /// <summary>
- /// Gets the current phoneme frame (lock and copy current frame to caller frame)
- /// </summary>
- /// <returns>error code</returns>
- /// <param name="inFrame">In frame.</param>
- public OVRLipSync.Frame GetCurrentPhonemeFrame()
- {
- return frame;
- }
- /// <summary>
- /// Sets a given viseme id blend weight to a given amount
- /// </summary>
- /// <param name="viseme">Integer viseme ID</param>
- /// <param name="amount">Integer viseme amount</param>
- public void SetVisemeBlend(int viseme, int amount)
- {
- OVRLipSync.Result result =
- OVRLipSync.SendSignal(context, OVRLipSync.Signals.VisemeAmount, viseme, amount);
- if (result != OVRLipSync.Result.Success)
- {
- if (result == OVRLipSync.Result.InvalidParam)
- {
- Debug.LogError("OVRLipSyncContextBase.SetVisemeBlend: Viseme ID is invalid.");
- }
- else
- {
- Debug.LogError("OVRLipSyncContextBase.SetVisemeBlend: An unexpected" +
- " error occured.");
- }
- }
- }
- /// <summary>
- /// Sets a given viseme id blend weight to a given amount
- /// </summary>
- /// <param name="amount">Integer viseme amount</param>
- public void SetLaughterBlend(int amount)
- {
- OVRLipSync.Result result =
- OVRLipSync.SendSignal(context, OVRLipSync.Signals.LaughterAmount, amount, 0);
- if (result != OVRLipSync.Result.Success)
- {
- Debug.LogError("OVRLipSyncContextBase.SetLaughterBlend: An unexpected" +
- " error occured.");
- }
- }
- /// <summary>
- /// Resets the context.
- /// </summary>
- /// <returns>error code</returns>
- public OVRLipSync.Result ResetContext()
- {
- // Reset visemes to silence etc.
- frame.Reset();
- return OVRLipSync.ResetContext(context);
- }
- }
|