Smash Bros Circle

Blog > Comments

Matt Soukup's Avatar

Playing NSF files in GameMaker

Posted 2011-12-14 11:07 PM CST by Matt Soukup

NES Sound Format (NSF) is a file format for emulating music as it emanated from the Nintendo Entertainment System. Thanks to the S-WinAmp GameMaker extension, including these rockin' chip tunes in your GameMaker game has become a reality.

NSF files include a number of tracks in a single file. To advance tracks in an NSF, you have to issue a Play command or a Next command to your audio player. So if you have an NSF file with 20 tracks, and you want to play track 19 in your game, you normally have to press Play/Next 18 times. If our GameMaker game wants to play track 18, do we really need to issue 18 Play commands to the S-WinAmp extension?

This is one solution to the problem, but there's another method that's quite a bit more elegant. For each track that you need out of an NSF file, you create a copy of the NSF file. (Don't worry, they are small!) You then open up each NSF in a hex editor and modify the value of the starting track. When the NSF player opens an NSF, it checks which track it should start with. Normally, this is set to the first track, but we could set it to the 18th track! Then, all we would need to do is open the NSF and issue a single play command!

The NSF spec tells us what bytes mean what inside the NSF file. As you can see, the byte at offset 0007 is the starting song. (01 means the first, 02 means the second, etc.). Let's look at an example:

I have the Zelda 2: Adventure of Link NSF, and I want the boss battle track. This NSF has 16 tracks and the boss battle IS track 16. I open my Hex Editor (I use one called HxD). I then open up my Zelda 2 NSF. I look at the offset 0007, which means the eighth byte (numbering starts with zero). At offset 0007, I find a value of 01. AHA! That means currently, it is set to start at track 1. But we want to change it to track 16, so we must edit that byte and change it to "10". These numbers are in hexadecimal format, which is base 16. "10" in hex is 16. We should actually be writing that hex number as "0x10". The "0x" prefix tells us it's hex. After making the change, we hit save. We then open it up in Winamp (with our NSF plugin installed) and VOILA! It starts off at track 16, and we hear the epic boss battle music!

In a perfect world, we could take apart the NSF track by track so each is its own file and doesn't still contain all of the other tracks. If you don't know about the NES sound processor (I don't), this would be quite a feat. Given the time savings, I think this is a great alternative.

Have fun adding crystal clear NES music to your games. I look forward to playing them!

Write Comment


(Enter your email to receive updates from Welkup about SSBC)