workhorsy / SaltyNES

A NES emulator in WebAssembly
https://workhorsy.org/SaltyNES/
GNU General Public License v3.0
85 stars 6 forks source link

How does the sound get out? #7

Closed BillyWM closed 7 years ago

BillyWM commented 7 years ago

How does this emulator pass the sound out to the browser? I'm aware that WebAssembly has a JS API to import and export functions between the two sides, but I can't see where they're in use here at all.

Since it seems all sound and video is handled by SDL and SDL is getting sound data via its callback, is there a special version of SDL needed?

Is the Web Audio API invoked anywhere?

workhorsy commented 7 years ago

The webaudio API is not used. It uses the normal SDL way of doing audio with SDL_OpenAudio and SDL_AudioSpec:

https://github.com/workhorsy/SaltyNES/blob/master/src/PAPU.cc#L205

When SDL_AudioSpec is compiled, Emscripten uses it's own port of SDL that maps it to the browser's JavaScript APIs. I don't know enough about how it works, or what audio API it is using under the hood. It looks like Emscripten version of SDL is here:

https://github.com/emscripten-ports/SDL2/blob/master/src/audio/SDL_audio.c

workhorsy commented 7 years ago

I think this may be the SDL lib code after it is converted to JS. But I'm not sure:

https://github.com/kripken/emscripten/blob/master/src/library_sdl.js

workhorsy commented 7 years ago

Do you have any more questions about this issue?

BillyWM commented 7 years ago

That answered my question. Thanks for pointing me in the right direction!