The Music Header
At the beginning of each bank that contains music is a series of music headers. Each music header is 3-bytes long and represents 1 sound channel. A complete song is a consecutive group of sound channel headers.
The format of a sound channel header is
<Info Byte> <Pointer>
The info byte tells the start of the song and how many channels the song has, or the current channel in the song. To distinguish between the 2 the info byte is divided into 2 set's of bit-flags.
Bit flags A represent the total number of channels and is found only at the beginning of each song. Bit flags B represent the current channel number and are found all the time including with flags A.
Both bit-flags start their numbering at 0, so the 4th channel or 4 total channels would be represented with the number 3.
Each channel number found in the header has a consecutive channel number for optimization and there are 4 channels total with an imaginary 2nd set of 4 channels covered later.
The channels each have a very specific purpose and the music codes used for them are sometimes dependent of the channel number:
- Channel 1: Tremble
- Channel 2: Tremble
- Channel 3: Bass
- Channel 4: Drums
Imaginary Channel Sets
In the game, there are 2 imaginary set's of 4 channels despite there being only 4 in reality. The upper set (above the current 4) are used for sfx and take precedence over the lower set which is used for music channels.
A full example
PalletTown_mh: ; 0x822E - 0x8236 db $80 dw PalletTown_md_1 ;Channel 1 ($A7C5 - $A85E) db $01 dw PalletTown_md_2 ;Channel 2 ($A85f - $A8DD) db $02 dw PalletTown_md_3 ;Channel 3 ($A8DE - $AA75)
You can see that PalletTown_mh is just the name of the group. "db $80" is the first info-byte and is constructed as:
This means pallet town has 3 total channels and this is the first (Note: There is never a time when any song starts with channels out-of-order so it will always start with channel 0)
The next is a 2-byte pointer pointing to the music data for the channel number listed in the info-byte.
The next 3 bytes are for the next channel and they are laid out as:
This means that the following pointer is for channel #2 (Remember: The numbering starts at 0) the last byte is essentially the same as is for channel number 3.
Programmatically Channel Seeking
From a code perspective, there's no way to tell what song your in, but it is possible to tell when the song starts and ends and which channel your on since the info byte describes channel information and setup.
The game uses a counter based on channel indexes. The counter starts at the beginning of the bank and every 1 increment moves 3 bytes forward. In other words every increment moves to the next channel in the bank. (Remember: The game doesn't see complete songs, rather, a large set of random channels which are grouped)
Pallet town's channel index starts at index 0xBA
The game actually uses a shortcut. Instead of actually seeking to the next channel, it more or less jumps there making the assumption all the channels are in order looking more like this:
Index + 0 (Channel 1) Index + 1 (Channel 2) Index + 2 (Channel 3)
This means that if it wants to load channel 3, it jumps directly to the correct music header (Via Index Multiplication) and then makes another direct jump to the needed channel index without ever looking at info-byte after the first one.