Playing Multiple Music Tracks

Posted July 14, 2022

This tutorial is most likely not compatible with versions of SGDK above 1.70. Unfortunately, I simply do not have the capacity or ability to update them right now. Read more about it here. Sorry.

Hold up!

If you like 90s platformers, check out my new game Kid Bubblegum!

We already talked about how to play music in SGDK, but the other day I was asked how one would play one song after another…and I actually had to pause and think for a second. But now that I’ve figured out a way, here it is!

As far as I’m aware, SGDK doesn’t have any kind of built-in playlist functionality, so if you want to interrupt or swap music tracks, you’ll have to do it manually. For this tutorial I’ll use the Sonic 1 title music, along with the Green Hill music to show how to automatically switch from the first to the next, while also then infinitely looping the second one.

I’m not gonna put up the music here, but you can easily find the tracks in .vgm format online.

Okay, first of all, we import the tracks via the resources.res file:

XGM track1 "title.vgm"
XGM track2 "greenhill.vgm"

Next, we set up a little variable to help us track which song is currently playing:

u8 current_track = 0;

Then, in main(), we play the first track using these commands:

current_track = 1;

Setting the loop number to 0 will only play the track once, which is what we want for the title jingle.

Okay, so far so good. But how do we know when to play the next track?

SGDK actually provides us with the XGM_getElapsed() function, which tells us how many frames the current music track has been playing for. So, if we know how long a track is, or rather, how long we want it to play, we can use this function to check when the next song should start up. In practice, this looks like this:

    if(current_track == 1 && XGM_getElapsed() == 490)
        // Make the second track play on an infinite loop
        current_track = 2;

Note that we set the loop number to -1 here, meaning that track 2 will play an infinite number of times…and by “infinite”, I mean it’ll loop 255 times. For 16bit, that’s infinite enough.

And there you go, as you can see it’s a somewhat crude, but flexible solution. Do note however that the return value of XGM_getElapsed() is dependant on the framerate of the game! By default, XGM sets the music tempo according to the region of the console, but if you change it manually, you’ll have to keep the different timings in mind for 50/60 Hz playback. You can double-check the current music tempo using XGM_getMusicTempo(), and if you need to figure out the region your game is running in, check out my tutorial on the topic!

If you've got problems or questions, join the official SGDK Discord! It's full of people a lot smarter and skilled than me. Of course you're also welcome to just hang out and have fun!

Join my Discord Server!

Hang out, get news, be excellent!

Come hang out!

Want To Buy Me a Coffee?

Coffee rules, and it keeps me going! I'll take beer too, though.

Check out the rest of this tutorial series!

  • Creating Graphics for the Mega Drive
  • How to Quickly Generate C Prototype Functions in VSCode
  • Color Swapping
  • 4 Programs For Creating Mega Drive Graphics
  • Editing the Rom Header
  • Simple Game States
  • Creating a Simple Menu
  • Changing The Text Color in SGDK
  • Playing Music in SGDK
  • Converting VGZ to VGM
  • Processing Resets
  • Drawing Tiles From Code
  • Make a Text Crawl, Streets of Rage Style
  • Scrolling Maps
  • Placing Tiles
  • Simple Animated Tiles in SGDK
  • Simple Password System
  • Checking the Region
  • Playing Multiple Music Tracks
  • By using the Disqus service you confirm that you have read and agreed to the privacy policy.

    comments powered by Disqus