Web Audio API Test Utilities

0.5.2 · abandoned · verified Sun Apr 19

web-audio-test-api is a JavaScript library designed for testing applications that use the Web Audio API, primarily in CI environments. It provides a mock implementation of the Web Audio API interfaces, which, when imported, replaces the native browser or Node.js-emulated Web Audio API globally. Key features include stricter type checking for audio parameters compared to browser implementations, the ability to serialize an audio graph to a JSON object for assertion and debugging, and tracking of OscillatorNode/BufferSourceNode states. The current stable version is 0.5.2. However, the package appears to be unmaintained, with the last publish occurring over eight years ago, suggesting a very slow or effectively abandoned release cadence. Its primary differentiator is its focus on testability and debuggability through explicit state tracking and serialization, rather than mimicking real-time audio processing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up an AudioContext, create and connect several audio nodes (OscillatorNode, GainNode), configure their properties, and then serialize the entire audio graph to a JSON object for testing or debugging purposes.

import 'web-audio-test-api';

// Create a test AudioContext instance
const audioContext = new AudioContext();

// Create some nodes and connect them
const osc = audioContext.createOscillator();
const lfo = audioContext.createOscillator();
const amp = audioContext.createGain();

// Assign an ID for easier debugging in the JSON output
lfo.$id = 'LFO';

// Configure node properties
osc.type = 'sawtooth';
osc.frequency.value = 880;
lfo.frequency.value = 2;

// Connect the audio graph
lfo.connect(amp.gain);
osc.connect(amp);
amp.connect(audioContext.destination);

// Output the audio graph as a JSON object for inspection
const graphJson = audioContext.toJSON();

// In a test environment, you would assert on this JSON structure
// For demonstration, we'll log it.
console.log(JSON.stringify(graphJson, null, 2));

/* Expected output (truncated):
{
  "name": "AudioDestinationNode",
  "inputs": [
    {
      "name": "GainNode",
      "gain": {
        "value": 1,
        "inputs": [
          {
            "name": "OscillatorNode#LFO",
            "type": "sine",
            "frequency": {
              "value": 2,
              "inputs": []
            },
            "detune": {
              "value": 0,
              "inputs": []
            },
            "inputs": []
          }
        ]
      },
      "inputs": [
        {
          "name": "OscillatorNode",
          "type": "sawtooth",
          "frequency": {
            "value": 880,
            "inputs": []
          },
          "detune": {
            "value": 0,
            "inputs": []
          },
          "inputs": []
        }
      ]
    }
  ]
}
*/

view raw JSON →