AMAudioTrack.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [Serializable]
  5. public class AMAudioTrack : AMTrack
  6. {
  7. public override string getTrackType()
  8. {
  9. return "Audio";
  10. }
  11. public bool setAudioSource(AudioSource audioSource)
  12. {
  13. if (this.audioSource != audioSource)
  14. {
  15. this.audioSource = audioSource;
  16. return true;
  17. }
  18. return false;
  19. }
  20. public void ChangeAudioSource(AudioSource audio)
  21. {
  22. if (this.setAudioSource(audio))
  23. {
  24. for (int i = 0; i < this.keys.Count; i++)
  25. {
  26. (this.keys[i] as AMAudioKey).audioClip = this.audioSource.clip;
  27. }
  28. this.updateCache();
  29. }
  30. }
  31. public override void updateCache()
  32. {
  33. this.asName = this.audioSource.name;
  34. base.destroyCache();
  35. this.cache = new List<AMAction>();
  36. base.sortKeys();
  37. for (int i = 0; i < this.keys.Count; i++)
  38. {
  39. AMAudioAction amaudioAction = ScriptableObject.CreateInstance<AMAudioAction>();
  40. amaudioAction.startFrame = this.keys[i].frame;
  41. amaudioAction.audioSource = this.audioSource;
  42. amaudioAction.audioClip = (this.keys[i] as AMAudioKey).audioClip;
  43. amaudioAction.loop = (this.keys[i] as AMAudioKey).loop;
  44. this.cache.Add(amaudioAction);
  45. }
  46. base.updateCache();
  47. }
  48. public void addKey(int _frame, AudioClip _clip, bool _loop)
  49. {
  50. this.asName = this.audioSource.name;
  51. foreach (AMKey amkey in this.keys)
  52. {
  53. AMAudioKey amaudioKey = (AMAudioKey)amkey;
  54. if (amaudioKey.frame == _frame)
  55. {
  56. amaudioKey.audioClip = _clip;
  57. amaudioKey.loop = _loop;
  58. this.updateCache();
  59. return;
  60. }
  61. }
  62. AMAudioKey amaudioKey2 = ScriptableObject.CreateInstance<AMAudioKey>();
  63. amaudioKey2.frame = _frame;
  64. amaudioKey2.audioClip = _clip;
  65. amaudioKey2.loop = _loop;
  66. this.keys.Add(amaudioKey2);
  67. this.updateCache();
  68. }
  69. public override void previewFrame(float frame, AMTrack extraTrack = null)
  70. {
  71. if (this.audioSource != null)
  72. {
  73. this.asName = this.audioSource.name;
  74. }
  75. }
  76. public void sampleAudio(float frame, float speed, int frameRate)
  77. {
  78. if (!this.audioSource)
  79. {
  80. return;
  81. }
  82. int i = this.cache.Count - 1;
  83. while (i >= 0)
  84. {
  85. if (!(this.cache[i] as AMAudioAction).audioClip)
  86. {
  87. return;
  88. }
  89. if ((float)this.cache[i].startFrame <= frame)
  90. {
  91. float num = (frame - (float)this.cache[i].startFrame) / (float)frameRate;
  92. if (!(this.cache[i] as AMAudioAction).loop && num > (this.cache[i] as AMAudioAction).audioClip.length)
  93. {
  94. return;
  95. }
  96. num %= (this.cache[i] as AMAudioAction).audioClip.length;
  97. if (this.audioSource.isPlaying)
  98. {
  99. this.audioSource.Stop();
  100. }
  101. this.audioSource.clip = null;
  102. this.audioSource.clip = (this.cache[i] as AMAudioAction).audioClip;
  103. this.audioSource.loop = (this.cache[i] as AMAudioAction).loop;
  104. this.audioSource.time = num;
  105. this.audioSource.pitch = speed;
  106. this.audioSource.Play();
  107. return;
  108. }
  109. else
  110. {
  111. i--;
  112. }
  113. }
  114. }
  115. public void sampleAudioAtFrame(int frame, float speed, int frameRate)
  116. {
  117. if (!this.audioSource)
  118. {
  119. return;
  120. }
  121. for (int i = this.cache.Count - 1; i >= 0; i--)
  122. {
  123. if (this.cache[i].startFrame == frame)
  124. {
  125. if (this.audioSource.isPlaying)
  126. {
  127. this.audioSource.Stop();
  128. }
  129. this.audioSource.clip = null;
  130. this.audioSource.clip = (this.cache[i] as AMAudioAction).audioClip;
  131. this.audioSource.time = 0f;
  132. this.audioSource.loop = (this.cache[i] as AMAudioAction).loop;
  133. this.audioSource.pitch = speed;
  134. this.audioSource.Play();
  135. return;
  136. }
  137. }
  138. }
  139. public void stopAudio()
  140. {
  141. if (!this.audioSource)
  142. {
  143. return;
  144. }
  145. if (this.audioSource.isPlaying)
  146. {
  147. this.audioSource.Stop();
  148. }
  149. }
  150. public ulong getTimeInSamples(int frequency, float time)
  151. {
  152. return (ulong)((float)(44100 / frequency * frequency) * time);
  153. }
  154. public override AnimatorTimeline.JSONInit getJSONInit()
  155. {
  156. return null;
  157. }
  158. public override List<GameObject> getDependencies()
  159. {
  160. List<GameObject> list = new List<GameObject>();
  161. if (this.audioSource)
  162. {
  163. list.Add(this.audioSource.gameObject);
  164. }
  165. return list;
  166. }
  167. public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
  168. {
  169. List<GameObject> list = new List<GameObject>();
  170. if (!this.audioSource)
  171. {
  172. return list;
  173. }
  174. int i = 0;
  175. while (i < oldReferences.Count)
  176. {
  177. if (oldReferences[i] == this.audioSource.gameObject)
  178. {
  179. AudioSource exists = (AudioSource)newReferences[i].GetComponent(typeof(AudioSource));
  180. if (!exists)
  181. {
  182. Debug.LogWarning("Animator: Audio Track component 'AudioSource' not found on new reference for GameObject '" + this.audioSource.gameObject.name + "'. Duplicate not replaced.");
  183. list.Add(oldReferences[i]);
  184. return list;
  185. }
  186. this.audioSource = exists;
  187. break;
  188. }
  189. else
  190. {
  191. i++;
  192. }
  193. }
  194. return list;
  195. }
  196. public AudioSource audioSource;
  197. public string asName;
  198. }