Web Audio for Node.js. Implemented via @kmamal/sdl.
const { AudioContext, Audio } = require('@kmamal/audio')
const context = new AudioContext()
const oscillator = context.createOscillator()
const gain = context.createGain()
gain.gain.value = 0.2
oscillator.connect(gain).connect(context.destination)
oscillator.start()
oscillator.stop(context.currentTime + 1)
const song = new Audio('music.mp3')
song.loop = true
song.play()Microphone input goes through getMediaDevices():
const { AudioContext, getMediaDevices } = require('@kmamal/audio')
const context = new AudioContext()
const stream = await getMediaDevices().getUserMedia({ audio: true })
context.createMediaStreamSource(stream).connect(context.destination)Relative paths and file: URLs are read from disk, http(s): and data: URLs go through fetch. To change that, for example to resolve paths relative to an asset directory:
const { configure } = require('@kmamal/audio')
configure({ fetch: (url) => myFetch(url) })The function must return a Response. It is used by Audio elements and by audioWorklet.addModule().
The main thread must import @kmamal/sdl before the worker opens a device.
// main.js
require('@kmamal/sdl')
const { Worker } = require('node:worker_threads')
new Worker('./synth.js')
// synth.js
const { AudioContext } = require('@kmamal/audio')
const context = new AudioContext()All contexts share one render thread, which also runs the AudioWorklet processors and owns the playback and recording devices.
OfflineAudioContext renders on the calling thread.
To render everything on the calling thread instead, call configure({ workers: false }) before creating any context or element.
That is the default inside a worker_thread.
- An
AudioContextstarts rendering as soon as it is created and becomesrunningwithout a user gesture. There is no autoplay policy. - Device ids are
default,playback:<id>andrecording:<id>, and they are the same in every process. AsinkIdthat names no device makessetSinkId()reject withNotFoundError; in the constructor it falls back to the default device and fireserror. - There are no permission prompts:
enumerateDevices()always returns labels, andgetUserMedia()opens the microphone at once. Only audio is captured, and avideoconstraint rejects. renderSizeHintis honoured by both context types, sorenderQuantumSizecan be anything from 1 frame to 6 seconds.ConvolverNodeand HRTF panning add up to 127 frames of latency when the quantum is not a multiple of 128.OfflineAudioContextsupports incremental rendering:length: nullmakes an unbounded context,startRendering(chunkSize)renders one chunk at a time, andclose()stops it.- The process stays alive while a context is running on a real device, while a decode or
addModule()is pending, while adevicechangelistener is attached, and while a port ofAudioWorkletNode,audioWorkletor a processor has amessagelistener and its context is not closed. A context withsinkId: { type: 'none' }does not keep it alive.
- Processors registered with
registerProcessor()on your own thread are only available to anOfflineAudioContext, or to every context afterconfigure({ workers: false }). Load processors into anAudioContextwithaudioWorklet.addModule(). - Each context gets its own
AudioWorkletGlobalScope, so two contexts can register different processors under the same name.context.audioWorklet.porttalks to the globalportof that scope. When a context renders on the calling thread, modules that useimportorexportonly get their own scope undernode --experimental-vm-modules; without the flag they are loaded withimport()and share one registry. onaudioprocessof aScriptProcessorNoderuns on your thread. Its output is played at the event'splaybackTime, one buffer after the input was collected, and a buffer that arrives after that time is dropped. A node with anaudioprocesslistener is not garbage collected while its context is open, as in Chrome.createScriptProcessor(0)picks 4096 frames, or the smallest listed size that is a multiple of the render quantum, and a size that is not a multiple throwsNotSupportedError.
- The supported formats are wav, aiff, mp3, ogg vorbis, ogg opus and flac, for
decodeAudioData()and media elements alike.canPlayType()reports exactly those. - Setting
srcloads and decodes the whole file right away, whateverpreloadsays: about 1.4 GB per hour of stereo audio at 48 kHz. preservesPitchhas no effect, so changingplaybackRatechanges the pitch.controlsandcrossOriginhave no effect.- Elements play through one shared
AudioContexton the default playback device untilcreateMediaElementSource()takes them over.getDefaultContext()returns that context andsetDefaultContext()replaces it. - Microphone tracks honour only the
deviceIdandchannelCountconstraints, with up to 8 channels.applyConstraints()does nothing,getCapabilities()is empty andmutedis always false.
- When a source starts it takes the content of its
AudioBuffer, as the spec requires, so editing a buffer while it plays is not audible, but the arrays returned bygetChannelData()before the start are not detached as they are in Firefox. They stay readable and keep their values, writes to them are no longer part of the buffer, and the nextgetChannelData()returns a fresh copy of the content. WaveShaperNodeoversampling is a plain up-sample and average, andDynamicsCompressorNodefollows the spec's shape but is not bit-exact with any browser.
npm run wpt runs the Web Audio part of the web-platform-tests against OfflineAudioContext.
npm run wpt-fetch downloads the pinned test files into wpt/upstream/ first.
Known failures are listed in wpt/expectations.json, and the run fails on any change from that list.
npm run wpt-realtime runs the same tests with every OfflineAudioContext replaced by an AudioContext on the render thread that renders as fast as it can into memory, so the objects on your thread and their copies on the render thread are tested too.
Its known failures are in wpt/expectations-realtime.json.
Files that need an iframe, navigation, an autoplay gesture or a page that is not cross-origin isolated are skipped, as is one test that streams between contexts with different sample rates.