I asked about this topic to many people and also posted questions, but there is very little documentation about it. So i learned it how to do it with the help of jsr and miau in #nesdev irc channel. So i want to put up this thing so people can clarify their doubts.
How To Use the FamiTracker Driver Source to make NSF Music Files
You have made your own game but without sound, it’s uninteresting. So let’s see how to put some sound in your game at your desired position.
This article contains the method to convert the raw music data to NSF file using ca65 compiler. If you haven’t been introduced to NSF or FamiTracker, go to http://famitracker.shoodot.net/ and download it. YouTube contains many videos regarding how to make music using this software.
Firstly many people are making music in FamiTracker software and are exporting it into .nsf format. This file they include in their code using .incbin to play the music. But there are two main disadvantages – Firstly, the nsf file has 3 important address – LOAD, PLAY and INIT. So by directly exporting as nsf file, we have no control over the these addresses. So if you have some code at the same place as LOAD address of the song, then there is a problem. Secondly, the output file size is large. So if you have limited space, then it’s not the best option.
But we can do in another way. When you make your music in FamiTracker, export it as .bin = Raw Music Data file. Then you have two files with you- 1. music.bin and 2. samples.bin
Now go to ftp://ftp.musoftware.de/pub/uz/cc65/ download the cc65 compiler according to the OS you are using. Then go to http://famitracker.shoodot.net/downloads.php and download the NSF Driver Source. This folder contains 14 files. Now copy the compiler ca65, linker ld65 and necessary files into this folder. Also copy your music.bin and samples.bin files to this folder.
Now open up nsf.cfg file. It may look like this:
MEMORY {
ZP: start = $00, size = $100, type = rw, file = "";
RAM: start = $200, size = $400, type = rw, file = "";
HDR: start = $7F80, size = $80, type = ro, file = %O;
PRG: start = $8000, size = $20000, type = rw, file = %O;
}
SEGMENTS {
ZEROPAGE: load = ZP, type = zp;
BSS: load = RAM, type = bss, define = yes;
HEADER: load = HDR, type = ro;
CODE: load = PRG, type = rw, start = $8000;
MUSIC: load = PRG, type = rw, start = $A000;
DPCM: load = PRG, type = rw, start = $C000;
}
So now you can change the address in the CODE and PRG from $8000 to desired position. The address you put will be the LOAD address. The INIT address is same as LOAD address. The PLAY address is 3 more than INIT address. So PLAY = INIT + 3
So for example, if you locate it at $A000 then init = $A000 and play = $A003
Now we can remove the MUSIC and DPCM lines in the SEGMENTS as we don’t use it. Also if you don’t want the header, then remove HEADER line also. But by doing this, you need not remove the first 128 bytes (header) of your nsf file manually.
Now we make the compiler. Make a compiler.bat file and put the following lines in it:
ca65 driver.s -D INC_MUSIC
ld65 -C nsf.cfg -o music.nsf driver.o
pause
So by these commands, the music.bin file converted and put into driver.o file along with other details. Then the nsf.cfg file determines the addresses and the final resultant file will be music.nsf
You can also use these lines if you don’t remove the HEADER line in nsf.cfg file:
ca65 nsf_wrap.s -D INC_MUSIC
ld65 -C nsf.cfg -o music.nsf nsf_wrap.o
pause
Then by doing this, you have to manually remove the header using any hex editor software (I use xvi32).
Now the first objective is achieved. We now have control over the addresses. For the second part, reducing the file size is a tricky task. When you open driver.s file (it opens in an editor. I use ConText) , you will notice that the code contains many lines which are not useful. So we can remove these lines and can save space.
Firstly, if you are not using any external chip like MMC5 or VRC6, you can remove that part of the code. Also if you are not using any bank switching, remove that part also. Notice the following lines in the code :
.include "init.s"
.include "player.s"
.include "effects.s"
.include "instrument.s"
.include "apu.s"
So to reduce space, you can remove unnecessary code from these files also. For example, if you are not using the effects of Vibrato, you can remove its code. Similarly, if the instrument remains same throughout the song, then instrument.s can be removed also. Please don’t get excited and remove code haphazardly as it might affect your music.nsf file. So it is advisable to check the proper working of the file after every modification. Also if you know which system you use – PAL or NTSC, then you can remove the other one and save the space.
So, the second objective is fulfilled. Though large amount of file size will not be reduced, but nonetheless this is useful if you are tight on space.
Finally, after doing all these things, test you code with sound and enjoy!!!
I would really like to thank jsr and miau for their help. They both cleared my doubts and helped me on this topic. Also a really nice place to learn these things http://famitracker.shoodot.net/ and http://nesdev.com/
For knowing how to play song in your code, see this open source code for multi-song NSF: http://no-carrier.com/index.php?/vegaplay/
Also the irc channel #nesdev is the best source to interact with these people and involve in NES mania.
- enigma
How To Use the FamiTracker Driver Source to make NSF Music Files
You have made your own game but without sound, it’s uninteresting. So let’s see how to put some sound in your game at your desired position.
This article contains the method to convert the raw music data to NSF file using ca65 compiler. If you haven’t been introduced to NSF or FamiTracker, go to http://famitracker.shoodot.net/ and download it. YouTube contains many videos regarding how to make music using this software.
Firstly many people are making music in FamiTracker software and are exporting it into .nsf format. This file they include in their code using .incbin to play the music. But there are two main disadvantages – Firstly, the nsf file has 3 important address – LOAD, PLAY and INIT. So by directly exporting as nsf file, we have no control over the these addresses. So if you have some code at the same place as LOAD address of the song, then there is a problem. Secondly, the output file size is large. So if you have limited space, then it’s not the best option.
But we can do in another way. When you make your music in FamiTracker, export it as .bin = Raw Music Data file. Then you have two files with you- 1. music.bin and 2. samples.bin
Now go to ftp://ftp.musoftware.de/pub/uz/cc65/ download the cc65 compiler according to the OS you are using. Then go to http://famitracker.shoodot.net/downloads.php and download the NSF Driver Source. This folder contains 14 files. Now copy the compiler ca65, linker ld65 and necessary files into this folder. Also copy your music.bin and samples.bin files to this folder.
Now open up nsf.cfg file. It may look like this:
MEMORY {
ZP: start = $00, size = $100, type = rw, file = "";
RAM: start = $200, size = $400, type = rw, file = "";
HDR: start = $7F80, size = $80, type = ro, file = %O;
PRG: start = $8000, size = $20000, type = rw, file = %O;
}
SEGMENTS {
ZEROPAGE: load = ZP, type = zp;
BSS: load = RAM, type = bss, define = yes;
HEADER: load = HDR, type = ro;
CODE: load = PRG, type = rw, start = $8000;
MUSIC: load = PRG, type = rw, start = $A000;
DPCM: load = PRG, type = rw, start = $C000;
}
So now you can change the address in the CODE and PRG from $8000 to desired position. The address you put will be the LOAD address. The INIT address is same as LOAD address. The PLAY address is 3 more than INIT address. So PLAY = INIT + 3
So for example, if you locate it at $A000 then init = $A000 and play = $A003
Now we can remove the MUSIC and DPCM lines in the SEGMENTS as we don’t use it. Also if you don’t want the header, then remove HEADER line also. But by doing this, you need not remove the first 128 bytes (header) of your nsf file manually.
Now we make the compiler. Make a compiler.bat file and put the following lines in it:
ca65 driver.s -D INC_MUSIC
ld65 -C nsf.cfg -o music.nsf driver.o
pause
So by these commands, the music.bin file converted and put into driver.o file along with other details. Then the nsf.cfg file determines the addresses and the final resultant file will be music.nsf
You can also use these lines if you don’t remove the HEADER line in nsf.cfg file:
ca65 nsf_wrap.s -D INC_MUSIC
ld65 -C nsf.cfg -o music.nsf nsf_wrap.o
pause
Then by doing this, you have to manually remove the header using any hex editor software (I use xvi32).
Now the first objective is achieved. We now have control over the addresses. For the second part, reducing the file size is a tricky task. When you open driver.s file (it opens in an editor. I use ConText) , you will notice that the code contains many lines which are not useful. So we can remove these lines and can save space.
Firstly, if you are not using any external chip like MMC5 or VRC6, you can remove that part of the code. Also if you are not using any bank switching, remove that part also. Notice the following lines in the code :
.include "init.s"
.include "player.s"
.include "effects.s"
.include "instrument.s"
.include "apu.s"
So to reduce space, you can remove unnecessary code from these files also. For example, if you are not using the effects of Vibrato, you can remove its code. Similarly, if the instrument remains same throughout the song, then instrument.s can be removed also. Please don’t get excited and remove code haphazardly as it might affect your music.nsf file. So it is advisable to check the proper working of the file after every modification. Also if you know which system you use – PAL or NTSC, then you can remove the other one and save the space.
So, the second objective is fulfilled. Though large amount of file size will not be reduced, but nonetheless this is useful if you are tight on space.
Finally, after doing all these things, test you code with sound and enjoy!!!
I would really like to thank jsr and miau for their help. They both cleared my doubts and helped me on this topic. Also a really nice place to learn these things http://famitracker.shoodot.net/ and http://nesdev.com/
For knowing how to play song in your code, see this open source code for multi-song NSF: http://no-carrier.com/index.php?/vegaplay/
Also the irc channel #nesdev is the best source to interact with these people and involve in NES mania.
- enigma