public override void PlayStart(ITarget itarget, float frame, int frameRate, float animScale) { if (frame > 0) { AudioSource src = GetTarget(itarget) as AudioSource; if (!src) { return; } float time; for (int i = keys.Count - 1; i >= 0; i--) { AudioKey key = keys[i] as AudioKey; if (!key.audioClip) { break; } if (key.frame <= frame) { src.pitch = itarget.animScale * animScale; if (!key.oneShot) { // get time time = ((frame - key.frame) / frameRate); // if loop is set to false and is beyond length, then return if (!key.loop && time > key.audioClip.length) { break; } if (src.isPlaying && src.clip != key.audioClip) { src.Stop(); } // find time based on length time = time % key.audioClip.length; src.clip = key.audioClip; src.loop = key.loop; src.time = time; src.Play(); } mLastSampleKeyIndex = i; break; } } } }
public override void previewFrame(ITarget target, float frame, int frameRate, bool play, float playSpeed) { AudioSource src = GetTarget(target) as AudioSource; if (!src) { return; } if (play) { int iFrame = Mathf.RoundToInt(frame); for (int i = keys.Count - 1; i >= 0; i--) { if (keys[i].frame <= iFrame && mLastSampleKeyIndex != i) { AudioKey key = keys[i] as AudioKey; src.pitch = target.animScale * playSpeed; if (key.oneShot) { src.PlayOneShot(key.audioClip); } else { if (src.isPlaying && src.clip != key.audioClip) { src.Stop(); } src.clip = key.audioClip; src.loop = key.loop; src.time = keys[i].frame == iFrame ? 0f : ((frame - key.frame) / frameRate) % key.audioClip.length; src.Play(); } mLastSampleKeyIndex = i; break; } } } else { src.loop = false; mLastSampleKeyIndex = -1; } }
// add a new key public void addKey(ITarget itarget, int _frame, AudioClip _clip, bool _loop) { foreach (AudioKey key in keys) { // if key exists on frame, update key if (key.frame == _frame) { key.audioClip = _clip; key.loop = _loop; // update cache updateCache(itarget); } } AudioKey a = new AudioKey(); a.frame = _frame; a.audioClip = _clip; a.loop = _loop; // add a new key keys.Add(a); // update cache updateCache(itarget); }