Sunday, October 31, 2021 at 21:41:00 UTC
So after working on the sound for days, I'm still not done, but I can say that what works so far sounds damn good. Here's a video of my emulator:
For comparison, here's what it sounds like in the emulator I was using before I wrote my own, FCEUX:
If you listen closely, there's all kinds of weird high-frequency noises in the FCEUX version. This is because converting audio from one sample rate to another is actually rather hard, and IDK what FCEUX is doing, but it isn't doing it very well.
The approach I took was to essentially write a MIDI synthesizer. The NES engine just lets the game code write to the audio registers and tracks what's in them, and then the audio synthesizer runs independently and does its own thing, synthesizing the audio at whatever rate the sound card wants it, utilizing a kind of ridiculous 84 MB of pre-generated audio waveforms to ensure that each frequency the NES is capable of generating is completely free of those weird high-frequency noises.