Audio clip playback timing

Hello all, I’ve got a project where I’d like short clips to be played at regular intervals, much like music notes. As of now they can be triggered every N frames (12, 16, 24, 48), if they are active. The overall playback is reasonably solid, but there are slight onset/trigger inconsistencies which seem random.

I’ve set Application.targetFrameRate = 24, and there are 7 AudioSources, one for each sound. Originally I did everything with one AudioSource and swapped clips, using PlayOneShot, but I read that was inefficient. However, changing to the current setup made no difference in the onsets that I could tell. All sound clips are virtually identical: same length, same attack time, etc., just different pitches.

So, is there some way to optimize things so that the starting playback of different sounds is better synchronized? I’m using the frame as the timing marker, but should it be something else? Thanks!

Yeah, you shouldn’t be using frames as a proxy for time, you should just be using time directly.

Use PlayScheduled to schedule audio to play (slightly before it needs to be played), so that the engine can get the audio ready and play it exactly on time on the audio thread. This is how you accurately schedule audio in unity.

1 Like

OK, I think I (sort of) get it, thanks for the info on PlayScheduled(), sounds good. But I’m wondering how to sync that to the frames, because those frame intervals is when the corresponding visual to the audio note happens. Should I tie the visuals to absolute time instead of frames? Or is there a way to use Time.deltaTime to track the frame times, then use that for PlayScheduled?

In the build (.exe) the timing is much better. Hoping it will be that way on mobile too. Thanks for the help!