• Stars
    star
    826
  • Rank 55,167 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js implementation of Web audio API

web-audio-api test

Node.js implementation of Web audio API

This library implements the Web Audio API specification (also know as WAA) on Node.js.

What's Implemented

  • AudioContext (partially)
  • AudioParam (almost there)
  • AudioBufferSourceNode
  • ScriptProcessorNode
  • GainNode
  • OscillatorNode
  • DelayNode

Installation

npm install --save web-audio-api

Demo

Get ready, this is going to blow up your mind:

npm install
npm run test-speaker

Audio Output

By default, web-audio-api doesn't play back the sound it generates. In fact, an AudioContext has no default output, and you need to give it a writable node stream to which it can write raw PCM audio. After creating an AudioContext, set its output stream like this : audioContext.outStream = writableStream.

Example: Playing back sound with node-speaker

This is probably the simplest way to play back audio. Install node-speaker with npm install speaker, then do something like this :

import { AudioContext } from 'web-audio-api'
import Speaker from 'speaker'

const context = new AudioContext

context.outStream = new Speaker({
  channels: context.format.numberOfChannels,
  bitDepth: context.format.bitDepth,
  sampleRate: context.sampleRate
})

// Create some audio nodes here to make some noise ...

Example : playing back sound with aplay

Linux users can play back sound from web-audio-api by piping its output to aplay. For this, simply send the generated sound straight to stdout like this :

import { AudioContext } from 'web-audio-api'
const context = new AudioContext()

context.outStream = process.stdout

// Create some audio nodes here to make some noise ...

Then start your script, piping it to aplay like so :

node myScript.js | aplay -f cd

Example : creating an audio stream with icecast2

icecast is a open-source streaming server. It works great, and is very easy to setup. icecast accepts connections from different source clients which provide the sound to encode and stream. ices is a client for icecast which accepts raw PCM audio from its standard input, and you can send sound from web-audio-api to ices (which will send it to icecast) by simply doing :

import { spawn } from 'child_process'
import { AudioContext } from 'web-audio-api'
 const context = new AudioContext()

var ices = spawn('ices', ['ices.xml'])
context.outStream = ices.stdin

A live example is available on Sรฉbastien's website

Using Gibber

Gibber is a great audiovisual live coding environment for the browser made by Charlie Roberts. For audio, it uses Web Audio API, so you can run it on web-audio-api. First install gibber with npm :

npm install gibber.audio.lib

Then to you can run the following test to see that everything works:

npm test gibber.audio.lib

Overall view of implementation

Each time you create an AudioNode (like for instance an AudioBufferSourceNode or a GainNode), it inherits from DspObject which is in charge of two things:

  • register schedule events with _schedule
  • compute the appropriate digital signal processing with _tick

Each time you connect an AudioNode using source.connect(destination, output, input) it connects the relevant AudioOutput instances of source node to the relevant AudioInput instance of the destination node.

To instantiate all of these AudioNode, you needed an overall AudioContext instance. This latter has a destination property (where the sound will flow out), instance of AudioDestinationNode, which inherits from AudioNode. The AudioContext instance keeps track of connections to the destination. When that happens, it triggers the audio loop, calling _tick infinitely on the destination, which will itself call _tick on its input ... and so forth go up on the whole audio graph.

Running the debugger

Right now everything runs in one process, so if you set a break point in your code, there's going to be a lot of buffer underflows, and you won't be able to debug anything.

One trick is to kill the AudioContext right before the break point, like this:

context[Symbol.dispose]()
debugger

that way the audio loop is stopped, and you can inspect your objects in peace.

Alternatives

License

MIT

๐Ÿ•‰

More Repositories

1

audio

Class for high-level audio manipulations [NOT MAINTAINED]
JavaScript
237
star
2

audio-decode

Minimal audio decoders layer
JavaScript
125
star
3

audio-speaker

Output audio stream to speaker, browser/node-wise
JavaScript
104
star
4

web-audio-stream

Stream data to web audio
JavaScript
93
star
5

audio-buffer-utils

Utils for audio buffers
JavaScript
81
star
6

audio-loader

A simple but flexible AudioBuffer loader for Web Audio API
JavaScript
81
star
7

audio-shader

Process audio stream with webgl shader
GLSL
73
star
8

audio-buffer

AudioBuffer class for node/browser
JavaScript
72
star
9

audio-play

Play audio buffer in browser/node
JavaScript
60
star
10

a-weighting

๐Ÿ‘‚ A-weighting and other noise weighting
JavaScript
39
star
11

pcm-convert

Convert pcm data from any to any format
JavaScript
32
star
12

audio-generator

Generate audio with a function
JavaScript
32
star
13

sample-rate

List of common sample rates
JavaScript
31
star
14

audio-context

A WebAudio Context singleton
JavaScript
30
star
15

audio-oscillator

Generate periodic oscillation into an array/audiobuffer
JavaScript
27
star
16

audio-type

Detect the audio type of a Buffer/Uint8Array
JavaScript
23
star
17

audio-buffer-from

Create audio-buffer from any source data
JavaScript
22
star
18

audio-biquad

Biquad filter audio stream
JavaScript
12
star
19

decibels

๐Ÿ“ข Convert values to and from decibels.
JavaScript
12
star
20

audio-buffer-list

Sequence of AudioBuffers data structure
JavaScript
12
star
21

web-audio-write

Write data to web-audio
JavaScript
10
star
22

audio-format

Parse or stringify audio format
JavaScript
9
star
23

awesome-audiojs

A curated list of high-quality, small-scope audio JS modules.
9
star
24

audio-pcm-format

Audio stream format transformer
JavaScript
8
star
25

audio-buffer-remix

Upmix or downmix audio buffer channels
JavaScript
8
star
26

audio-lena

The Lena test audio
JavaScript
7
star
27

audio-through

Audio processor/generator constructor
JavaScript
6
star
28

docs

JavaScript
4
star
29

is-audio-buffer

if (object instanceof AudioBuffer) { ... }
JavaScript
2
star
30

contributing

Discussion and guidelines for contributing
2
star
31

audio-extensions

List of audio extensions
JavaScript
2
star
32

audio-source

Create stream from audio buffer or array buffer
JavaScript
1
star