# web-audio-api-cjs

> Node.js implementation of Web audio API

Latest version **0.2.4** (published 2022-06-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install web-audio-api-cjs
pnpm add web-audio-api-cjs
yarn add web-audio-api-cjs
bun add web-audio-api-cjs
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.4 |
| Published | 2022-06-27 |
| First published | 2022-06-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 6 |
| Unpacked size | 74.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 950 |
| Author | Sébastien Piquemal |
| Maintainers | akinazuki |
| Keywords | audio, audiojs, sound, music, web audio, web audio api, waa, dsp |

## Links

- npm: https://www.npmjs.com/package/web-audio-api-cjs
- Repository: https://github.com/audiojs/web-audio-api
- Homepage: https://github.com/audiojs/web-audio-api#readme
- Issues: https://github.com/audiojs/web-audio-api/issues
- npm.io page: https://npm.io/package/web-audio-api-cjs

## Dependencies (6)

- [av](https://npm.io/package/av.md) 0.4.x
- [aac](https://npm.io/package/aac.md) 0.1.x
- [mp3](https://npm.io/package/mp3.md) 0.1.x
- [alac](https://npm.io/package/alac.md) 0.1.x
- [flac](https://npm.io/package/flac.md) 0.3.x
- [automation-events](https://npm.io/package/automation-events.md) ^4.0.14

## Recent versions

- 0.2.4 (latest) — 2022-06-27
- 0.2.3 — 2022-06-27
- 0.2.2 — 2022-06-27

## README

# web-audio-api [![test](https://github.com/audiojs/web-audio-api/actions/workflows/test.yml/badge.svg)](https://github.com/audiojs/web-audio-api/actions/workflows/test.yml)

> Node.js implementation of Web audio API

This library implements the [Web Audio API specification](http://webaudio.github.io/web-audio-api/) (also know as WAA) on Node.js.


## What's Implemented

- [x] AudioContext (partially)
- [x] AudioParam (almost there)
- [x] AudioBufferSourceNode
- [x] ScriptProcessorNode
- [x] GainNode
- [ ] OscillatorNode
- [ ] DelayNode


## Installation

```shell
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 :

```javascript
const { AudioContext } = require('web-audio-api')
const Speaker = require('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](http://alsa.opensrc.org/Aplay). For this, simply send the generated sound straight to `stdout` like this :

```javascript
const { AudioContext } = require('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](http://icecast.org/) is a open-source streaming server. It works great, and is very easy to setup. **icecast** accepts connections from [different source clients](http://icecast.org/apps/) which provide the sound to encode and stream. [ices](http://www.icecast.org/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 :

```javascript
const { spawn } = require('child_process')
const { AudioContext } = require('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](http://funktion.fm/#/projects/versificator-rubbish-stream)


## Using Gibber

[Gibber](https://github.com/charlieroberts/Gibber) is a great audiovisual live coding environment for the browser made by [Charlie Roberts](http://charlie-roberts.com). 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:

```javascript
context[Symbol.dispose]()
debugger
```

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

<!--
## Manual testing

You can test the sound output using `node-speaker`.

```
npm run test-speaker
```

To test `AudioParam` against `AudioParam` implemented in a browser, open `test/manual-testing/AudioParam-browser-plots.html` in that browser. -->


## Alternatives

* [web-audio-engine](https://github.com/mohayonao/web-audio-engine) − JS implementation of web-audio-api.
* [web-audio-js](https://github.com/descriptinc/web-audio-js) − fork of web-audio-engine with extra nodes.
* [lab-sound](https://github.com/LabSound/LabSound)
* [node-audio](https://ghub.io/node-audio)
* [WAAPISim](https://github.com/g200kg/WAAPISim)
* [web-audio-api-rs](https://github.com/orottier/web-audio-api-rs)

## License

MIT

<p align="center">🕉<p>

---
_Source: https://npm.io/package/web-audio-api-cjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
