Conversion chart for APU writes to notes?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
puppydrum64
Posts: 160
Joined: Sat Apr 24, 2021 7:25 am

Conversion chart for APU writes to notes?

Post by puppydrum64 »

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.
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Conversion chart for APU writes to notes?

Post by Quietust »

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.
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.

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.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Conversion chart for APU writes to notes?

Post by Pokun »

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.
Bavi_H
Posts: 193
Joined: Sun Mar 03, 2013 1:52 am
Location: Texas, USA
Contact:

Re: Conversion chart for APU writes to notes?

Post by Bavi_H »

These pages on the Nesdev wiki may also be useful:

APU basics - Playing a musical note
APU period table
puppydrum64
Posts: 160
Joined: Sat Apr 24, 2021 7:25 am

Re: Conversion chart for APU writes to notes?

Post by puppydrum64 »

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?
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Conversion chart for APU writes to notes?

Post by Quietust »

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?
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.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Conversion chart for APU writes to notes?

Post by rainwarrior »

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?
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.

...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.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Conversion chart for APU writes to notes?

Post by lidnariq »

(Try looking for music for the VIC-20, which uses 7-bit dividers. It's rough)
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Conversion chart for APU writes to notes?

Post by tokumaru »

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.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Conversion chart for APU writes to notes?

Post by lidnariq »

unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Conversion chart for APU writes to notes?

Post by unregistered »

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.
hi puppydrum64,

There are free music engines. :) I’m using Shiru’s “famitone2”. That can be used to play music created with Famitracker.

Works excellently!
puppydrum64
Posts: 160
Joined: Sat Apr 24, 2021 7:25 am

Re: Conversion chart for APU writes to notes?

Post by puppydrum64 »

I was using GGSound for my game, which sadly does not support Famitracker's effect table.
puppydrum64
Posts: 160
Joined: Sat Apr 24, 2021 7:25 am

Re: Conversion chart for APU writes to notes?

Post by puppydrum64 »

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)
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Conversion chart for APU writes to notes?

Post by Pokun »

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:
nerdy_nights.7z
Original Nerdy Nights Tuttorials
(1.74 MiB) Downloaded 84 times
User avatar
za909
Posts: 249
Joined: Fri Jan 24, 2014 9:05 am
Location: Mijn hart woont al in Nederland

Re: Conversion chart for APU writes to notes?

Post by za909 »

tokumaru wrote: Sun Apr 25, 2021 2:46 pm 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.
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.
Post Reply