using System; using System.Collections.Generic; using UnityEngine; [Serializable] public class AMAudioTrack : AMTrack { public override string getTrackType() { return "Audio"; } public bool setAudioSource(AudioSource audioSource) { if (this.audioSource != audioSource) { this.audioSource = audioSource; return true; } return false; } public void ChangeAudioSource(AudioSource audio) { if (this.setAudioSource(audio)) { for (int i = 0; i < this.keys.Count; i++) { (this.keys[i] as AMAudioKey).audioClip = this.audioSource.clip; } this.updateCache(); } } public override void updateCache() { this.asName = this.audioSource.name; base.destroyCache(); this.cache = new List(); base.sortKeys(); for (int i = 0; i < this.keys.Count; i++) { AMAudioAction amaudioAction = ScriptableObject.CreateInstance(); amaudioAction.startFrame = this.keys[i].frame; amaudioAction.audioSource = this.audioSource; amaudioAction.audioClip = (this.keys[i] as AMAudioKey).audioClip; amaudioAction.loop = (this.keys[i] as AMAudioKey).loop; this.cache.Add(amaudioAction); } base.updateCache(); } public void addKey(int _frame, AudioClip _clip, bool _loop) { this.asName = this.audioSource.name; foreach (AMKey amkey in this.keys) { AMAudioKey amaudioKey = (AMAudioKey)amkey; if (amaudioKey.frame == _frame) { amaudioKey.audioClip = _clip; amaudioKey.loop = _loop; this.updateCache(); return; } } AMAudioKey amaudioKey2 = ScriptableObject.CreateInstance(); amaudioKey2.frame = _frame; amaudioKey2.audioClip = _clip; amaudioKey2.loop = _loop; this.keys.Add(amaudioKey2); this.updateCache(); } public override void previewFrame(float frame, AMTrack extraTrack = null) { if (this.audioSource != null) { this.asName = this.audioSource.name; } } public void sampleAudio(float frame, float speed, int frameRate) { if (!this.audioSource) { return; } int i = this.cache.Count - 1; while (i >= 0) { if (!(this.cache[i] as AMAudioAction).audioClip) { return; } if ((float)this.cache[i].startFrame <= frame) { float num = (frame - (float)this.cache[i].startFrame) / (float)frameRate; if (!(this.cache[i] as AMAudioAction).loop && num > (this.cache[i] as AMAudioAction).audioClip.length) { return; } num %= (this.cache[i] as AMAudioAction).audioClip.length; if (this.audioSource.isPlaying) { this.audioSource.Stop(); } this.audioSource.clip = null; this.audioSource.clip = (this.cache[i] as AMAudioAction).audioClip; this.audioSource.loop = (this.cache[i] as AMAudioAction).loop; this.audioSource.time = num; this.audioSource.pitch = speed; this.audioSource.Play(); return; } else { i--; } } } public void sampleAudioAtFrame(int frame, float speed, int frameRate) { if (!this.audioSource) { return; } for (int i = this.cache.Count - 1; i >= 0; i--) { if (this.cache[i].startFrame == frame) { if (this.audioSource.isPlaying) { this.audioSource.Stop(); } this.audioSource.clip = null; this.audioSource.clip = (this.cache[i] as AMAudioAction).audioClip; this.audioSource.time = 0f; this.audioSource.loop = (this.cache[i] as AMAudioAction).loop; this.audioSource.pitch = speed; this.audioSource.Play(); return; } } } public void stopAudio() { if (!this.audioSource) { return; } if (this.audioSource.isPlaying) { this.audioSource.Stop(); } } public ulong getTimeInSamples(int frequency, float time) { return (ulong)((float)(44100 / frequency * frequency) * time); } public override AnimatorTimeline.JSONInit getJSONInit() { return null; } public override List getDependencies() { List list = new List(); if (this.audioSource) { list.Add(this.audioSource.gameObject); } return list; } public override List updateDependencies(List newReferences, List oldReferences) { List list = new List(); if (!this.audioSource) { return list; } int i = 0; while (i < oldReferences.Count) { if (oldReferences[i] == this.audioSource.gameObject) { AudioSource exists = (AudioSource)newReferences[i].GetComponent(typeof(AudioSource)); if (!exists) { Debug.LogWarning("Animator: Audio Track component 'AudioSource' not found on new reference for GameObject '" + this.audioSource.gameObject.name + "'. Duplicate not replaced."); list.Add(oldReferences[i]); return list; } this.audioSource = exists; break; } else { i++; } } return list; } public AudioSource audioSource; public string asName; }