BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / LipSync / Editor / OVRLipSyncTool.cs

OVRLipSyncTool.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRLipSyncTool.cs 5.2 KiB Download Raw
1/************************************************************************************
2Filename : OVRLipSyncTool.cs
3Content : Editor tool for generating lip sync assets
4Created : May 17th, 2018
5Copyright : Copyright Facebook Technologies, LLC and its affiliates.
6 All rights reserved.
7
8Licensed under the Oculus Audio SDK License Version 3.3 (the "License");
9you may not use the Oculus Audio SDK except in compliance with the License,
10which is provided at the time of installation or download, or which
11otherwise accompanies this software in either electronic or hard copy form.
12
13You may obtain a copy of the License at
14
15https://developer.oculus.com/licenses/audio-3.3/
16
17Unless required by applicable law or agreed to in writing, the Oculus Audio SDK
18distributed under the License is distributed on an "AS IS" BASIS,
19WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20See the License for the specific language governing permissions and
21limitations under the License.
22************************************************************************************/
23using UnityEngine;
24using UnityEditor;
25using System.IO;
26using System.Collections;
27using System.Collections.Generic;
28
29class 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
125class 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}