Why exactly t%500? Remember that the first beat of the music doesn't start at t=0. This should sync better with the music: (t+magicVariableGoesHere)%500
Or if a beat starts at relativeT = 0, then use relativeT instead