Syncing seamless audio with animations in Unity

(Requires Unity Web Player)

In order to loop multiple audio clips in a queue without having to create several AudioSources in unity, I made a simple audio engine that uses MonoBehaviour.OnAudioFilterRead(float[], int) to feed sample data directly into Unity’s digital signal processing chain. This is traditionally used for audio filters passes, but you can also just directly pass a clip’s audio data (or a mix of several) and play it that way.

The advantage of working with audio samples directly is that you can stich them together for seamless looping, or mix them at your leisure.

For the animation sync, I made a couple of 2-bar loops in Ableton Live, making sure they are all exported with a sample rate of 44100 and BPM of 120. with that in mind I calculated how many samples per beat I had (SampleRate * 60 / BPM). And then on my OnAudioFilterRead loop, I surface a normalized variable calculated by:


Then I just use that value (that goes from 0 to 1 each beat) to do a linear interpolation of whatever animation I need. In this case the local rotation of that pendulum thing. For the wobbly lines I’m using the AudioListener’s spectrum data as value points for a bunch of line renderers.


Leave a Reply