Quote:
period = round(1789773/16/frequency) - 1
Actually, rounding is not always the best way. In my music engine I used high pitched triangle notes and those sounded incredigly wrong, it was really disturbing. So instead of trying to use the NES to play real world musical frequencies based on A=440Hz, I use the natural tones of the NES and use it for the music. Basically, I compute the detune value for a high pitched octave using NES. Then I compute the average of all detune values, and center all my notes arround this detune value. This makes notes less detuned in average, and made high pitched triangle sounds a lot better.
I don't know whether I still have the calc spreadsheet where I did all those calculations but I'd recommand doing those again anyway just to be sure. Basically by lowering the pitch a bit, playing a A would be frequencies $1ff, $ff, $7f, $3f, $1f, etc... and it sounded much better, if you do it on raw calculation based on A=440Hz you get $fd instead.
On the other hand if you are into deep vibratoes and not into high pitched triangle, it would be a good idea to detune your music
up, so that the A notes are further away from the MSB period changes which triggers horrible snaps and pops in the sound.