Beat Detection

October 26, 2019

GameMaker Studio 2 Tutorials


Want to make a rhythm game? Want to have background elements bop to the beat of a song? If so, great! This tutorial is for you.


  1. Create a variable holding the current beat, and default it to -1.
  2. Convert a song’s BPM to beats/sec (BPS), simply by dividing it by 60.
  3. Constantly check the song’s track position multiplied by its BPS.
  4. If that value is different from the stored beat when floored, update the stored beat, and do stuff.


/// Object Event: Create

// Play song
musicPlayer = audio_play_sound(...); 

// Song information
// I'm setting the song's BPM to 150 in this example
beat = -1;  
bpm = 150;
bps = bpm / 60;  
/// Object Event: Step

var _beatNew = floor(audio_sound_get_track_position(musicPlayer)*bps);
if (_beatNew != beat) {
    beat = _beatNew;
    // Do stuff here!

Final Thoughts

The song’s BPM in the code doesn’t have to be the same as its actual BPM. For getting subdivisions of a beat, multiply the BPM by some amount (2 for eighth notes, 3 for triplets, etc.) For detecting grooves, you can change the check in the Step Event so that it checks only for certain beats. There’s a lot of things you can do this, but that’s all for you to try.

Thanks for reading!