looking for simplest way to program audio

Discuss NSF files, FamiTracker, MML tools, or anything else related to NES music.

Moderator: Moderators

Post Reply
patrick99e99
Posts: 4
Joined: Fri May 31, 2024 7:17 pm

looking for simplest way to program audio

Post by patrick99e99 »

Hi everyone,

I am working on a web project and would like to add real-time generated NES SFX to it. I recently was working on a different project, one that involved porting a c++ DX7 emulator and getting that running with webassembly... Since I have that working, I thought it might not be too much work to swap out the DX7 with an NES sound chip... The difference with that obviously is, the DX7 is a synthesizer and responds to midi, so I was easily able to just send it midi packets of SYSEX data / note events to get it to generate audio, and stream the audio buffer for the DX7 emulator to the web audio api, where as I assume this will be a bit more complicated.

That is why I thought I'd ask here first-- if anyone knows of any good c/c++ projects that perhaps wrap the NES sound chip so that it's "easier" to program sounds with it?

Ideally, what would be great is if I could use some kind of VST/Audio Unit plugin where I can tinker with a GUI to create the sounds effects, and then dump the data for that, and then have my program use it to be able to send to the NES sound chip in real-time to play that audio, and stream that to the webaudi api...

I have never worked with NES audio before, so I'd love to hear whatever suggestions anyone here would have, as far as what approach you'd recommend for me to go with for this.

Thanks.
Pokun
Posts: 2807
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: looking for simplest way to program audio

Post by Pokun »

Audio chips like the NES APU doesn't understand midi or other such high level commands, it basically just generates various waveforms sent to the speakers by writing certain values to various registers found in the chip. It's up to the sound program to write the right values at the right moments to make it musical.

Sound for audio chips like the NES APU is normally made by first making a sound engine that can take notes and other musical commands, then these commands can be typed in as score data directly in the ROM, use an MML implementation or other similar higher level language to compile it to the desired score data or use a tracker or other GUI-program designed for the particular sound engine. Every game has its own sound engine so every developer may have made their own tools to do this. MML and trackers are the two most popular solutions.

However there are a few open source sound engines with ready-made MML and tracker solutions if you search for these terms.
patrick99e99
Posts: 4
Joined: Fri May 31, 2024 7:17 pm

Re: looking for simplest way to program audio

Post by patrick99e99 »

Ok, so I managed to get Blargg's Nes_Snd_Emu integrated into my project, and it is working fine.

I then discovered the wonderful Famistudio program, so that seemed like the perfect tool to use to create my sound effects (which I noticed also uses Nes_Snd_Emu internally).. However, I am a little unclear on how I would go about turning the generated .asm file like:

Code: Select all

; This file is for the FamiStudio Sound Engine and was generated by FamiStudio

sounds:
  .dw .ntsc
  .dw .ntsc
.ntsc:
  .dw .sfx_ntsc_megamanhit
  .dw .sfx_ntsc_mushroom

.sfx_ntsc_megamanhit:
  .db $84,$36,$85,$06,$83,$3f,$8a,$0a,$89,$3f,$01,$84,$20,$8a,$0b,$01
  .db $84,$09,$89,$f0,$01,$84,$f3,$85,$05,$01,$84,$07,$85,$01,$83,$f8
  .db $8a,$09,$89,$37,$01,$84,$2d,$8a,$08,$89,$3f,$01,$84,$53,$8a,$07
  .db $01,$84,$79,$8a,$06,$01,$84,$9f,$8a,$05,$01,$84,$c5,$8a,$04,$01
  .db $84,$eb,$8a,$03,$01,$84,$11,$85,$02,$8a,$02,$01,$84,$37,$8a,$01
  .db $01,$84,$5c,$8a,$00,$01,$8a,$0f,$01,$83,$f0,$00
.sfx_ntsc_mushroom:
  .db $84,$d5,$85,$00,$83,$7d,$89,$f0,$02,$84,$1c,$85,$01,$02,$84,$d5
  .db $85,$00,$02,$84,$a9,$02,$84,$8e,$02,$84,$6a,$02,$84,$8e,$02,$84
  .db $0c,$85,$01,$02,$84,$d5,$85,$00,$02,$84,$b3,$02,$84,$86,$02,$84
  .db $b3,$02,$84,$86,$02,$84,$6a,$02,$84,$59,$02,$84,$42,$02,$84,$59
  .db $02,$84,$ef,$02,$84,$bd,$02,$84,$9f,$02,$84,$77,$02,$84,$9f,$02
  .db $84,$77,$02,$84,$5e,$02,$84,$4f,$02,$84,$3b,$02,$84,$4f,$01,$00
into individual calls to: "apu.write_register(0x4xxx, xx);" to actually play the sounds I created in famistudio.

Does anyone have any suggestions on how I would go about doing this?
User avatar
tokumaru
Posts: 12442
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: looking for simplest way to program audio

Post by tokumaru »

The .asm file contains data meant to be read by the playback engine, which will process it and interface with the APU via its registers. In order to generate the correct register writes you'll probably have to process this data the exact same way the playback engine does, either by emulating the 6502 engine or using a reimplementation of it in another language that works best for you. I'm not very familiar with FamiStudio, so I don't know if there are already any implementations besides 6502 that you can use...
patrick99e99
Posts: 4
Joined: Fri May 31, 2024 7:17 pm

Re: looking for simplest way to program audio

Post by patrick99e99 »

In order to generate the correct register writes you'll probably have to process this data the exact same way the playback engine does, either by emulating the 6502 engine or using a reimplementation of it in another language that works best for you
Ah ok.. Well after doing more digging, it seemed that it was going to be extremely difficult to do what I was wanting to do, so I abandoned Nes_Snd_Emu as I found https://bbbradsmith.github.io/nsfplay/ and spent the last few days getting that integrated into my webassembly project, and now am able to play tracks from NSF files, which is exactly what I was hoping to be able to do.
Pokun
Posts: 2807
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: looking for simplest way to program audio

Post by Pokun »

An NSF contains both the score data and the sound engine used for playback. If you wanted to use score data like you posted above you would have to make a NES ROM with the sound engine Famistudio expects and assemble it as an NSF instead of a conventional NES ROM. Not extremely difficult to do, but I guess Famistudio can export its songs as NSFs already (I'm not very familiar with Famistudio either) so it makes more sense to use those as is with an NSF player like you did.
User avatar
tokumaru
Posts: 12442
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: looking for simplest way to program audio

Post by tokumaru »

patrick99e99 wrote: Tue Jul 09, 2024 4:53 pmI found https://bbbradsmith.github.io/nsfplay/ and spent the last few days getting that integrated into my webassembly project, and now am able to play tracks from NSF files
Ah, that's probably the best approach in this case. Like Pokun said, NSF files contain both the playback engine and the song data, and an NSF player basically emulates a 6502 as well as the APU in order to be able to execute the playback engine.
Post Reply