Getting accurate audio playback timing with lots of clips

I have a number of short clips that need to be played with the best timing possible (basically it’s an instrument that plays based on visual timing of moving objects). Is it best to use lots of AudioSources so I don’t have to keep swapping clips? I’ve done a lot of testing and there are considerable differences with playback in the Editor, the EXE, and on Android. (Which is expected, though there were some surprises here and there, like optimal DSP buffer size not being what I expected on Android to avoid dropouts…it was backwards from the usual, so higher settings did better.)

I use PlayOneShot because it allows multiple clips to be played with one AudioSource without cutting the previous ones off. However, even with that, playing a bunch of clips in rapid succession can cause a strange issue: the sounds still play, but are much quieter and seem “far off”…hard to explain. Once the tempo slows down, it returns to normal, and I hear a short series of clicks, as though the previous clips have finished (the timing is about right for this). Wondering if this makes sense to anyone and if you have suggestions…should I use enough AudioSources that I don’t ever have to swap clips? So one for each possible note?

I’m using frames to determine when to play the clips, which I know isn’t ideal, but frames are used to track the visual part and it would be tricky (not sure if even possible) to use PlayScheduled(). Also, as long as the tempo isn’t too high, the timing is good in the EXE and on quality Android devices. It’s just the playback “overload” that’s causing problems.

Also a general question on clips (and any GameObject that holds properties like them): One AudioSource can have different notes in my setup, so when I check which note to play, I always swap the clip (and this is right before playing). If the clip is the same, does the swap still happen? I’m assuming swapping clips is a performance bottleneck even with short ones. I could check earlier (which I plan to), and I could check if the clip is the same before swapping, so don’t bother if it is. But am curious in general: does Unity know if you’re swapping a property that’s the same as what’s there and doesn’t actually do it? This could go for textures etc too.

Thanks!

have you got spacial sound turned on for the audio source? that could be you distance problem, as for the other issue, how about a coreroutine with a delay?

IEnumerator PlayTrack()
{
yield return new WaitForSeconds(track.length);
}

No spatial sound, everything is 2D. Also the “distance” thing seems to be an audio issue, not an effect. It’s hard to describe, and it goes away when the rate of playback lessens.

I don’t think I can use the coroutine example because it’s not known how long it will be until the next clip plays, it’s checked every N frames for whether it’s positioned in a “play” spot. But I like the idea of the delay for managing sound in general so that they don’t overlap and play right after each other, thanks!

Changed things so that the clips aren’t swapped out every time they’re played. Seemed to make no discernible difference (maybe a little but not enough to be valuable). So I guess this setup for playing audio isn’t ideal, it simply chokes when too many clips are triggered in rapid succession. Maybe Tracker Modules would perform better, or SoundFonts, or FMOD? Also wondering if having one big audio file with all the clips and jumping around would be better than separate clips. Any advice appreciated!

Figured out the best workaround was simply to shorten the clips. They were 3 seconds, now they’re 1. Much improved during fast-tempo playback. I didn’t want to trim them so much because they need some reverb, but I put an AudioReverbFilter on the AudioListener and that works great to “stretch” the clips, no issues. (Didn’t work well on the AudioSource because it cuts off at 1 second and there’s still reverb happening, so it clicks.)

Going to mark this as Resolved, though any other tips are of course welcome. Thanks!

1 Like

Annnnnd…in Project Settings, Audio, Max Real Voices, default = 32, I set it to 128. World of difference. In hindsight this was obvious. Works great on builds to exe and Android. So, if anyone gets audio dropouts with too many clips playing at once, this is an easy tweak.