Giter Site home page Giter Site logo

dtinth / nes-apu-worklet Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 0.0 23 KB

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

Home Page: https://dtinth.github.io/nes-apu-worklet

JavaScript 20.86% HTML 79.14%
web-audio-api audio-worklet nes chiptune-synthesizer

nes-apu-worklet's Introduction

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

nes-apu-worklet's People

Contributors

dtinth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.