Want some hardware-accurate Gameboy music and sounds in your HTML5 game or app? Don't want to load megabytes of .mp3 files? Overwhelmed or frustrated by the Web Audio API?
Enter apu! This easy-to-use module is a reasonably accurate emulation of the GameBoy's Audio Processing Unit. (APU!) It weighs in at roughly 3 kilobytes. (min + gzip)
Try it on StackBlitz! This demonstrates most of the capabilities and commands.
Here is another demo which plays an actual song. Compare to the original!
apu intends to deliver a good balance between size, accuracy, and ease-of-use. Its internals are still fairly accurate to how the real hardware works, but it omits some of the more obscure behavior. From a user's point of view, it uses an easy-to-read function-based API, rather than retaining the old notion of writing to registers on an APU. A user should be able to make some kind of sound happen with just one import
statement and one function call.
Not thoroughly tested, but Chrome and Firefox seem good. Safari seems to run, but does not always playback correctly.
This project started as a fork of Grant Galitz's JavaScript Gameboy emulator. I wanted to play convincing retro sound effects in the browser and decided that starting from a working emulator might be the best place to start. Grant's emulator was an excellent place to start from; its audio emulation is very accurate, and it was also clearly written with performance in mind.
I stripped out all of the components except the basics required to generate sound (no CPU cycles, no opcodes, no sprites, no interrupts, no joypad, no registers, no ROM...) and refactored it to make it easier to understand (from the perspective of both a user and a contributor) as well as minify better (less duplicate code, no large function prototypes)
Eventually, I rewrote the entire project from scratch. Instead of using the deprecated ScriptProcessorNode, the project now creates various AudioBufferSourceNodes for the different samples, and renders playback to OfflineAudioContexts to loop music playback. Only a dozen or so lines of code from the original project remain.