Conversion chart for APU writes to notes?
Moderator: Moderators
-
- Posts: 160
- Joined: Sat Apr 24, 2021 7:25 am
Conversion chart for APU writes to notes?
Hi, I'm new to this forum, I've been trying NES programming for about four months now. I'm running into a roadblock with music. Specifically, what do I write to each of the APU control registers to achieve a desired note? I've been trying to figure it out by blindly guessing and checking but this isn't going well since it seems the NES can create notes that don't fit on the music staff (e.g. something between A and A sharp).
The closest thing I could find is this chart but it doesn't tell me what binary codes to write to $4000, $4002, and $4003 to achieve the listed note.
https://www.mattmontag.com/uncategorize ... note-table
I'm sure someone has taken the time to figure this out already so I'd rather not re-invent the wheel so to speak.
The closest thing I could find is this chart but it doesn't tell me what binary codes to write to $4000, $4002, and $4003 to achieve the listed note.
https://www.mattmontag.com/uncategorize ... note-table
I'm sure someone has taken the time to figure this out already so I'd rather not re-invent the wheel so to speak.
Re: Conversion chart for APU writes to notes?
The values you want are from the "Pulse Period" column. Once you have the value for the note you want to play, convert it to hexadecimal - the first digit is what you'll write to $4003 (along with the note length in the upper 5 bits), and the rest is what you'll write to $4002.puppydrum64 wrote: ↑Sat Apr 24, 2021 2:25 pm Hi, I'm new to this forum, I've been trying NES programming for about four months now. I'm running into a roadblock with music. Specifically, what do I write to each of the APU control registers to achieve a desired note? I've been trying to figure it out by blindly guessing and checking but this isn't going well since it seems the NES can create notes that don't fit on the music staff (e.g. something between A and A sharp).
The closest thing I could find is this chart but it doesn't tell me what binary codes to write to $4000, $4002, and $4003 to achieve the listed note.
https://www.mattmontag.com/uncategorize ... note-table
I'm sure someone has taken the time to figure this out already so I'd rather not re-invent the wheel so to speak.
On a side note, this probably would've been better posted in the NESdev or Newbie Help Center subforums.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
P.S. If you don't get this note, let me know and I'll write you another.
Re: Conversion chart for APU writes to notes?
Celius made note tables with the hexadecimal values here and here. Edit: Found them on the wiki: NTSC PAL
The values in the table can be calculated using a formula found on the wiki. The triangle channel uses the same formula but one octave lower than pulse. Noise and DPCM doesn't need any formulae because there are only 16 different frequencies each anyway, and their frequencies in Hz are listed on the wiki.
Also I recommend the Nerdy Nights audio tutorial if the wiki is too technical. It teaches you about everything about the APU and how to make a sound engine.
Yeah this should be moved to nesdev or newbie help.
The values in the table can be calculated using a formula found on the wiki. The triangle channel uses the same formula but one octave lower than pulse. Noise and DPCM doesn't need any formulae because there are only 16 different frequencies each anyway, and their frequencies in Hz are listed on the wiki.
Also I recommend the Nerdy Nights audio tutorial if the wiki is too technical. It teaches you about everything about the APU and how to make a sound engine.
Yeah this should be moved to nesdev or newbie help.
Re: Conversion chart for APU writes to notes?
These pages on the Nesdev wiki may also be useful:
APU basics - Playing a musical note
APU period table
APU basics - Playing a musical note
APU period table
-
- Posts: 160
- Joined: Sat Apr 24, 2021 7:25 am
Re: Conversion chart for APU writes to notes?
Thank you all so much! And sorry for posting it in the wrong area. I'm starting to think that music takes up a LOT of cartridge space, I'm trying to write a way to handle it from scratch and I feel like I'm using really redundant data structures that lead to lots of bloat. It's a shame the pitch takes up 11 bits instead of 8, those extra 3 bits make the file size twice as large don't they?
Re: Conversion chart for APU writes to notes?
No music engine is going to store raw frequency/period values directly - instead, it will store note values (which will easily fit inside a single byte) and then use a lookup table to translate those notes into the values written to the sound registers. Furthermore, music engines are going to use multiple levels of indirection to make things smaller - for example, a song could be a list of tracks (one per sound channel), with each track being a list of sequences, and each sequence being a list of notes and rests.puppydrum64 wrote: ↑Sun Apr 25, 2021 9:04 am I'm starting to think that music takes up a LOT of cartridge space, I'm trying to write a way to handle it from scratch and I feel like I'm using really redundant data structures that lead to lots of bloat. It's a shame the pitch takes up 11 bits instead of 8, those extra 3 bits make the file size twice as large don't they?
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
P.S. If you don't get this note, let me know and I'll write you another.
- rainwarrior
- Posts: 8735
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Conversion chart for APU writes to notes?
The pitch table is only ~150 bytes. Though, you could make it even smaller by only storing one octave and shifting it with rounding compensation... but the result would be slightly less accurate (i.e. out of tune) and only save you 100 bytes at most. The actual music note data will take up far more space. In another recent thread about music data size we were guessing that that 100 bytes is probably worth less than 10 seconds of music.puppydrum64 wrote: ↑Sun Apr 25, 2021 9:04 amIt's a shame the pitch takes up 11 bits instead of 8, those extra 3 bits make the file size twice as large don't they?
...and it's definitely not a shame that they used more than 8 bits. The tuning for NES music is much, much nicer than systems with only 8-bit pitch selectors. Actually if you really wanted to, you could use 8 bits only, and shift it over by 3 before writing to the two registers. It would sound a lot worse though.
Re: Conversion chart for APU writes to notes?
(Try looking for music for the VIC-20, which uses 7-bit dividers. It's rough)
Re: Conversion chart for APU writes to notes?
Doesn't the Atari 2600 use only 5 bits to select sound frequencies? I heard it's notoriously hard to get notes to sound right on that system.
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
Re: Conversion chart for APU writes to notes?
hi puppydrum64,puppydrum64 wrote: ↑Sat Apr 24, 2021 2:25 pmI’m sure someone has taken the time to figure this out already so I'd rather not re-invent the wheel so to speak.
There are free music engines. I’m using Shiru’s “famitone2”. That can be used to play music created with Famitracker.
Works excellently!
-
- Posts: 160
- Joined: Sat Apr 24, 2021 7:25 am
Re: Conversion chart for APU writes to notes?
I was using GGSound for my game, which sadly does not support Famitracker's effect table.
-
- Posts: 160
- Joined: Sat Apr 24, 2021 7:25 am
Re: Conversion chart for APU writes to notes?
Thanks to all your help I was able to create a version of Nerdy Nights' sound engine that supports the Konami VRC6 expansion audio.
https://pastebin.com/vfcYZTNz
This link contains the source code, use VASM as your assembler. I've run into a couple problems though:
1. This doesn't seem to work on every emulator (it works on NEStopia but not MESEN)
2. It works up until chapter 6 of the tutorial, once I try to add tempo and note lengths it stops working entirely (even the 2A03 sound channels won't play correctly)
https://pastebin.com/vfcYZTNz
This link contains the source code, use VASM as your assembler. I've run into a couple problems though:
1. This doesn't seem to work on every emulator (it works on NEStopia but not MESEN)
2. It works up until chapter 6 of the tutorial, once I try to add tempo and note lengths it stops working entirely (even the 2A03 sound channels won't play correctly)
Re: Conversion chart for APU writes to notes?
VRC6, that's great!
I'm pretty sure the complete engine from chapter 10 works on real hardware, but it was a long time ago I tested it. I think either you must have done something wrong or there is something wrong with the Nerdy Nights mirror, as I followed the original tutorial (which no longer exists).
My sound engine modified from the Nerdy Nights sound engine do work on both Mesen and real hardware.
I'll attach the original Nerdy Nights tutorials that I downloaded before Nintendo Age disappeared:
I'm pretty sure the complete engine from chapter 10 works on real hardware, but it was a long time ago I tested it. I think either you must have done something wrong or there is something wrong with the Nerdy Nights mirror, as I followed the original tutorial (which no longer exists).
My sound engine modified from the Nerdy Nights sound engine do work on both Mesen and real hardware.
I'll attach the original Nerdy Nights tutorials that I downloaded before Nintendo Age disappeared:
Re: Conversion chart for APU writes to notes?
Exactly, though the waveform type selector is also related to which clock is used to drive the channel (pixel clock directly, or the CPU clock) so there are duplicate waveforms with different pitch ranges in there, most notable the Square wave has I think 3 different ranges depending on the waveform value used for it.
You are much better off trying to compose your own song and base the tuning on an arbitrarily chosen frequency that centers around the existing pitches you want to use. Covering other music is next to impossible unless it more or less lines up with the pitch intervals of the chip, and even then you might have to transpose it a mile away from where it was.