Trigger audio loop on beat with PlayScheduled

Hey all. I’ve been back and forth between answers and forum and script reference all day but I can’t seem to find an answer to my question. I suspect that I’m missing something very simple.

I have a 5 single measure loops at 60bpm in 4/4 time. I have 5 game objects and each has one of the loops attached. If I manually put these 5 objects in a scene (with ‘play on awake’ checked or using audio.play() in start/awake) they all sync nicely, as would be expected.

In game, these objects can be created by the player at any time but the loop for each object needs to wait to start playing until the next measure rolls around. (In this case, any 4 second increment.) It looks like launching PlayScheduled from Start() is the way to go but I’m having a difficult time wrapping my head around the time calculation:

audio.PlayScheduled(TimeWhichIsTheDifferenceBetweenNowAndTheNext4SecondMultiple)

Like I said I’ve been looking at posts and answers for hours but I sense that there is a very simple solution that is somehow eluding me. If someone could lend some insight into that time calculation that would be most excellent.

Couple years late, but here’s how I did something similar. musicLoop is the “time-keeper” AudioSource.

public float Quantize(float intstep) { // given a timing interval (16th notes for example), returns the amount of time to the next interval synced to the loop
		intstep *= measuresPerLoop;
		float step = musicLoop.clip.length / intstep;
		float multiplicand = Mathf.Ceil (musicLoop.time / step);
		return (step * multiplicand) - musicLoop.time;
	}

Then you can call:

sound.PlayScheduled(Quantize(4));

…to play on the next quarter note (etc).

(I’m sure you’ve solved this by now, but maybe this’ll help someone else!)