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