I’m currently trying to make a simple metronome, based on a BPM (say 60). The metronome will play a sample every 1 seconds. The user can press a key when they hear a sound, and the application will say how accurate their key press was against the metronome.
Is there anything in Unity 5 to help with this type of rhythm game scenario? Or should I stick to AudioSource.PlayScheduled? Obviously using coroutines that are dependant on frames is a bad idea, so I’ve moved away from that…
Any help is greatly appreciated!
Thanks.
Im making a rhythm game too right now. The metronome is easy. First you need to know the BPM of the song, let’s say 120 BPM. Then because 120 BPM are 120 beats in 60 seconds you can say that: 120/60 = 2. This means that there are 2 beats every second. If you know this, then you can get the samples of each beat and doing that, you can store that number in a variable and do this:
public double BPM = 120;
public double beatSamples;
public double nextBeatSamples;
void Start(){
//44100 are the sample rates, assuming your audio is in 44100hz
beatSamples = (44100 / (BPM / 60));
//beatSamples = 22050
}
void Update(){
//audio.TimeSamples will increase when the song is playing
if (audio.timeSamples >= nextBeatSamples && audio.isPlaying) {
//plays the metronome sound
audio.Play();
//here it increases the nextBeatSamples, so it will not enter the If
//again and it will wait until the timeSamples
//get to the new nextBeatSamples
nextBeatSamples += beatSamples;
}
}
Looks like a wonderful script and also nice info! Likely try this later today!
Hey, any reliable onset beat detection script anywhere?
Thanks in advance!
@pablo_leban