DS Music Hacking Tutorial
What you need
- A NDS ROM
- A midi file of the song you want to insert
- A NDS ROM extractor/packer
- Crystal Tile 2
- FL Studio
- A hex editor
Fixing your midi
The first thing you need to do before converting a midi is fixing it. To do this, play your midi and check if it has a silent section at the start or at the end. If it has, you'll have to remove it (use a midi editor). Also, you need to set up the midi instruments to the ones the song you want to replace uses. This will prevent GB-like sounds from playing.
(Only for 'non-beginning of the song' loop) After this, open your midi with FL Studio. Search for the point you want to loop to, and insert a C0 note there with a volume of 100. Do this for each track, and make sure it's the same bar in all tracks. Save the midi.
Open the midi with a hex editor and jump to 0xD. It should be '60'. Change it to 'C0' and save.
Open the midi again with FL Studio and, without editing anything, save it.
Open the midi one more time with a hex editor, jump to 0xD, and replace '60' with '30'. Save the file again.
Now it's fixed and ready to convert. What we have done here is a trick to prevent the song from desyncing when playing, something usual if you are not experienced in inserting songs.
Converting the midi file to SSEQ
Ok, after fixing our lovely midi file, it's time to convert it to the native NDS' Sequence File (SSEQ). To do this, just drag your midi file to midi2sseq.exe (or use the command window if you want). A new file named 'nameofyourmidifile'.SSEQ will appear.
Now open the SSEQ file with a hex editor. Search for 'FFC0' and replace every coincidence with 'FFC7'. There should be as many FFC0 as tracks the midi had.
Now we have to configure the polyphony of the song, so the game doesn't crash due to a memory overflow. This is done by replacing the byte after 'FFC7'. I would recommend a value between 10 and 16, take in count that a very low value will desync the tracks and a very high value will cause a memory overflow (resulting in a game crash).
After doing this, save the file. You now have a SSEQ file which can be inserted to the game, but it won't loop yet. If you are inserting a song that doesn't loop, you can skip Part 3 and go directly to Part 4.
Looping the SSEQ file
Now the most difficult part of the tutorial comes: looping your song. Before doing anything, you have to find the end of each track. This is easy, as every track ends with a FF byte and starts with a C7 byte. With this information, you can easily find the exact offset of each track (if you search for 'FFC7' in the SSEQ file. Also, note that the end of the last track won't have a C7 byte after the FF one, as it's the last track.
Now, you have to replace the last four bytes before the FF byte. These bytes will tell the game that it should jump to the position where we want to loop. You have to write 94+XXXXXX. 'XXXXXX' is the offset to the position of that track where we put the C0 note, but in flipped hex (0x4839A2 would be 'A23948', for example). To know the correct offset, you must know the type of loop of your song. There are two types of loops:
- Loop from the beginning: This is the 'easy' loop. The offset will be the offset of the C7 byte (start of the track) of that track. You will know it by placing the cursor after the FF byte of the previous track and the before the C7 byte of that track. Flip the offset (in groups of two, like I did before) and place it after the 94.
- Loop from the middle of the song: This is the 'difficult' loop and, unfortunately, the most common one. But your work has been reduced by 75% when you have inserted those C0 notes, trust me. To know the offset, search for '0064' (having marked the FFC7 sections to make the work easier). They will appear in the middle of a track. Now you could use their offsets, but it's not this easy. You may have another note in the exact tick the C0 note is, and if it's just before it, it won't sound when looping. So you have to do a bit of research in each track to discover what it has before the C0 note. Having a midi editor opened at the same time is highly recommended. Also, you should take a look at this document: http://kiwi.ds.googlepages.com/sdat.html.
When you find the correct position (it's normal if it takes you an hour or two), you still have to do something more. Take the offset (without having flipped it) and substract 1C from it. This math operation will give you the absolute offset. Now you can flip it (in groups of two, like I did before) and place it it after the 94.
Save your SSEQ file doing a backup of the unlooped version. It's now ready to insert!
Inserting your song
Finally, it's time to reflect our hard work in the hack. To do this, we must find the Nitro Composer File in our ROM, so we need to unpack it. Inside the folder where we unpacked it, there should be two folders: one called "Overlay" and the other with a name similar to "root" or "data". It's the second one, which is of our interest, as it's the main filesystem of the game (like the HDD in our PC). Search for a file with the .sdat extension, it's the music file. The usual name of the file is "sound_data.sdat", but it may change. There are even some games with multiple SDATs.
Once found, we can open the file with Crystal Tile 2. A window with lots of filenames will appear, it's the list of the music data. Songs have the SSEQ extension, and SBNK files are the soundbanks. I'm not going to explain what SBNK and the other files do, because it's not very important for now.
Go to the song you want to replace (n SDAT player like VGMTrans may be useful) and right-click it. In the menu that appears, there should be an option with the name "Import", or at least with an "I". If you select it, you can now search for the SSEQ file you created and write it over the original one. If a message pops up, it's because the SSEQ you are trying to insert is bigger, and you can't insert it with Crystal Tile 2. If it doesn't appear, the song has been inserted correctly. Save the SDAT file, and repack the ROM. When you play it, the new song should play and loop.
Hope it works, and don't forget to ask if you have any doubts or problems!
PS: If your song is bigger than the original and you want to insert it, you should know there's a second way to insert it which consists in unpacking the SDAT, replacing the file and repacking it. I haven't tried this yet, but it can be done with a program called NDS Editor 0.1, apparently.