Node Web Audio API

1.0.9 · active · verified Tue Apr 21

Node Web Audio API provides a robust and specification-compliant implementation of the W3C Web Audio API for Node.js environments. Currently at version 1.0.9, it leverages a performant Rust backend (`web-audio-api-rs`) and `napi-rs` bindings to offer core audio processing capabilities, including `AudioContext`, `OscillatorNode`, and `GainNode`, suitable for server-side audio generation and manipulation. While a strict release cadence isn't published, updates align with improvements in its underlying Rust components. Key differentiators include its high performance and strict adherence to the Web Audio API specification, which is crucial for developers porting browser-based audio applications or building new audio services in Node.js. An accompanying `isomorphic-web-audio-api` package is available for cross-platform development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes an AudioContext and creates an oscillating sound with a random frequency and a decaying envelope every 80 milliseconds for a duration of 10 seconds.

import { AudioContext, OscillatorNode, GainNode } from 'node-web-audio-api';

const audioContext = new AudioContext();

console.log('Generating audio for 10 seconds...');

const stopTime = audioContext.currentTime + 10;

const intervalId = setInterval(() => {
  if (audioContext.currentTime >= stopTime) {
    clearInterval(intervalId);
    console.log('Audio generation stopped.');
    audioContext.close(); // Important to close context to release resources
    return;
  }

  const now = audioContext.currentTime;
  const frequency = 200 + Math.random() * 2800; // Random frequency between 200Hz and 3000Hz

  const env = new GainNode(audioContext, { gain: 0 });
  env.connect(audioContext.destination);
  env.gain
    .setValueAtTime(0, now)
    .linearRampToValueAtTime(0.2, now + 0.02)
    .exponentialRampToValueAtTime(0.0001, now + 1);

  const osc = new OscillatorNode(audioContext, { frequency });
  osc.connect(env);
  osc.start(now);
  osc.stop(now + 1);
}, 80);

view raw JSON →