Skip to content

NES APU exposed as an HTML5 AudioWorklet. Generate 8-bit sounds fom Web Audio API! Powered by nes-js’s APU code.

Notifications You must be signed in to change notification settings

dtinth/nes-apu-worklet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nes-apu-worklet

NES APU exposed as HTML5 AudioWorklet. Generate 8-bit sounds fom Web Audio API! Powered by Takahiro’s nes-js’s APU code (imported directly!).

Demo

Usage

import { NesApuNode } from 'https://cdn.jsdelivr.net/npm/@dtinth/nes-apu-worklet@1/nes-apu-node.js'
const context = new AudioContext()

Add a worklet module to your AudioContext

await context.audioWorklet.addModule(
  'https://cdn.jsdelivr.net/npm/@dtinth/nes-apu-worklet@1/nes-apu-worklet.js',
)

Create a NesApuNode

let apu = new NesApuNode(context)
apu.connect(context.destination)

Schedule writes to the APU registers

//                      * Status register
//                      |            * Disable DMC channel
//                      |            |* Disable Noise channel
//                      |            ||* Disable Triangle channel
//                      |            |||* Disable Pulse 2 channel
//                      |            ||||* Enable Pulse 1 channel
apu.storeRegisterAtTime(0x4015, 0b00000001, context.currentTime)
//                      * Pulse 1
//                      |         ** Duty cycle 12.5%
//                      |         ||* Disable length counter (do not silence notes automatically)
//                      |         |||* Use constant volume (no decay)
//                      |         ||||**** Volume 15/15
apu.storeRegisterAtTime(0x4000, 0b00111111)
//                      |         ******** = Timer low = 251
apu.storeRegisterAtTime(0x4002, 0b11111011)
//                      |         ***** Length counter (disabled)
//                      |         |||||*** = Timer high = 1
apu.storeRegisterAtTime(0x4003, 0b00000001)
//                      Timer = 1 * 256 + 251 = 507
//                      Frequency = 1789773 [CPU speed hz] / (16 * (507 [Timer] + 1))
//                                = 220.1984498031496 hz (~A below Middle C)

API

const apu = new NesApuNode(audioContext)

Creates a new NES APU node.

apu.storeRegisterAtTime(address, value, time)

Enqueues a command to write a value to the APU registers. Check out Nesdev wiki for reference of each register.

apu.port.onmessage = fn

Be notified when data is written to the APU register. fn is called with event object.

  • event.data.address — Register address
  • event.data.value — Register value
  • event.data.time — Audio time

About

NES APU exposed as an HTML5 AudioWorklet. Generate 8-bit sounds fom Web Audio API! Powered by nes-js’s APU code.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published