I am working on a rhythm game with unity. I want player to tap to beat. The visuals also change according to beat
I am using following method;
public void StartPlayingAudio()
{
double startTime = AudioSettings.dspTime;
samplePeriod = 60f * audioSourceBeat.clip.frequency / musicTrackBPM;
sampleOffset = (60f * audioSourceBeat.clip.frequency) * 0.5f / musicTrackBPM;
nextBeatSample = (float)(startTime) * audioSourceBeat.clip.frequency;
audioSourceBeat.PlayScheduled(startTime);
StartCoroutine(CountBeats());
}
IEnumerator CountBeats()
{
while (audioSourceBeat.isPlaying)
{
currentSample = (float)AudioSettings.dspTime * audioSourceBeat.clip.frequency;
if (currentSample >= (nextBeatSample + sampleOffset))
{
nextBeatSample += samplePeriod;
beatDisplay.DoSomething();
}
yield return new WaitForSeconds(0);
}
}
When I test this code on different Android devices there is significant latency. And it is changing from device to device.
Is there some workaround for this?