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.

166 lines
5.2 KiB

  1. /************************************************************************************
  2. Filename : OVRLipSyncTool.cs
  3. Content : Editor tool for generating lip sync assets
  4. Created : May 17th, 2018
  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. using UnityEditor;
  21. using System.IO;
  22. using System.Collections;
  23. using System.Collections.Generic;
  24. class OVRLipSyncToolLoader
  25. {
  26. public static List<AudioClip> clipQueue;
  27. public static IEnumerator processor;
  28. // To show progress we use the total seconds of clip
  29. public static float totalLengthOfClips;
  30. public static float totalLengthOfClipsProcessed;
  31. public static IEnumerator ProcessClips(bool useOfflineModel)
  32. {
  33. if (clipQueue == null || clipQueue.Count == 0)
  34. {
  35. yield break;
  36. }
  37. while (clipQueue.Count > 0)
  38. {
  39. // Pop a clip off the list
  40. AudioClip clip = clipQueue[0];
  41. clipQueue.RemoveAt(0);
  42. if (clip.loadType != AudioClipLoadType.DecompressOnLoad)
  43. {
  44. Debug.LogError(clip.name +
  45. ": Cannot process phonemes from an audio clip unless " +
  46. "its load type is set to DecompressOnLoad.");
  47. continue;
  48. }
  49. // Update progress
  50. if (totalLengthOfClips > 0.0f)
  51. {
  52. EditorUtility.DisplayProgressBar("Generating Lip Sync Assets...", "Processing clip " + clip.name + "...",
  53. totalLengthOfClipsProcessed / totalLengthOfClips);
  54. }
  55. if (!clip.preloadAudioData)
  56. {
  57. clip.LoadAudioData();
  58. Debug.LogWarning(clip.name +
  59. ": Audio data is not pre-loaded. Data will be loaded then" +
  60. "unloaded on completion.");
  61. while (clip.loadState != AudioDataLoadState.Loaded)
  62. {
  63. yield return new WaitForSeconds(0.1f);
  64. }
  65. }
  66. var sequence =
  67. OVRLipSyncSequence.CreateSequenceFromAudioClip(clip, useOfflineModel);
  68. if (sequence != null)
  69. {
  70. var path = AssetDatabase.GetAssetPath(clip);
  71. var newPath = path.Replace(Path.GetExtension(path), "_lipSync.asset");
  72. var existingSequence = AssetDatabase.LoadAssetAtPath<OVRLipSyncSequence>(newPath);
  73. if (existingSequence != null)
  74. {
  75. EditorUtility.CopySerialized(sequence, existingSequence);
  76. AssetDatabase.SaveAssets();
  77. }
  78. else
  79. {
  80. AssetDatabase.CreateAsset(sequence, newPath);
  81. }
  82. }
  83. AssetDatabase.Refresh();
  84. if (!clip.preloadAudioData)
  85. {
  86. clip.UnloadAudioData();
  87. }
  88. totalLengthOfClipsProcessed += clip.length;
  89. }
  90. EditorUtility.ClearProgressBar();
  91. }
  92. static OVRLipSyncToolLoader()
  93. {
  94. processor = null;
  95. EditorApplication.update += Update;
  96. }
  97. static void Update()
  98. {
  99. if (processor != null)
  100. {
  101. processor.MoveNext();
  102. }
  103. }
  104. }
  105. class OVRLipSyncTool
  106. {
  107. [MenuItem("Oculus/Lip Sync/Generate Lip Sync Assets", false, 2000000)]
  108. static void GenerateLipSyncAssets()
  109. {
  110. GenerateLipSyncAssetsInternal(false);
  111. }
  112. [MenuItem("Oculus/Lip Sync/Generate Lip Sync Assets With Offline Model", false, 2500000)]
  113. static void GenerateLipSyncAssetsOffline()
  114. {
  115. GenerateLipSyncAssetsInternal(true);
  116. }
  117. private static void GenerateLipSyncAssetsInternal(bool useOfflineModel)
  118. {
  119. if (OVRLipSyncToolLoader.clipQueue == null)
  120. {
  121. OVRLipSyncToolLoader.clipQueue = new List<AudioClip>();
  122. }
  123. OVRLipSyncToolLoader.totalLengthOfClips = 0.0f;
  124. OVRLipSyncToolLoader.totalLengthOfClipsProcessed = 0.0f;
  125. for (int i = 0; i < Selection.objects.Length; ++i)
  126. {
  127. Object obj = Selection.objects[i];
  128. if (obj is AudioClip)
  129. {
  130. AudioClip clip = (AudioClip)obj;
  131. OVRLipSyncToolLoader.clipQueue.Add(clip);
  132. OVRLipSyncToolLoader.totalLengthOfClips += clip.length;
  133. }
  134. }
  135. OVRLipSyncToolLoader.processor = OVRLipSyncToolLoader.ProcessClips(useOfflineModel);
  136. }
  137. }