• This repository has been archived on 09/Jun/2019
  • Stars
    star
    242
  • Rank 167,048 (Top 4 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated almost 7 years ago

Reviews

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

Repository Details

Pure JS implementation of the Web Audio API

web-audio-engine

Build Status NPM Version License

Pure JS implementation of the Web Audio API

Installation

npm install --save web-audio-engine
download

API

web-audio-engine provides some AudioContext class for each use-case: audio playback, rendering and simulation.

Class: StreamAudioContext

StreamAudioContext writes raw PCM audio data to a writable node stream. It can be used to playback audio in realtime.

new StreamAudioContext(opts?: object)

Creates new StreamAudioContext instance.

  • opts.sampleRate: number audio sample rate (in Hz) - default: 44100
  • opts.numberOfChannels: number audio channels (e.g. 2: stereo) - default: 2
  • opts.blockSize: number samples each rendering quantum - default: 128
  • opts.bitDepth: number bits per sample - default: 16
  • opts.float: boolean use floating-point values - default: false
context.pipe(destination: stream.Writable): stream.Writable

๐Ÿ‘ท TODO: WRITE DESCRIPTION

const AudioContext = require("web-audio-engine").StreamAudioContext;
const context = new AudioContext();

// Set the output for audio streaming
context.pipe(process.stdout);

// If you want to playback sound directly in this process, you can use 'node-speaker'.
// const Speaker = require("speaker");
// context.pipe(new Speaker());

// Start to render audio
context.resume();

// composeWith(context);

Class: RenderingAudioContext

RenderingAudioContext records audio data with stepwise processing. It is used to export to a wav file or test a web audio application.

new RenderingAudioContext(opts?: object)

Creates new RenderingAudioContext instance.

  • opts.sampleRate: number audio sample rate (in Hz) - default: 44100
  • opts.numberOfChannels: number audio channels (e.g. 2: stereo) - default: 2
  • opts.blockSize: number samples each rendering quantum - default: 128
context.processTo(time: number|string)

Executes rendering process until the provided time.

  • time: e.g. 10 (10 seconds), "01:30.500" (convert to 90.5 seconds)
context.exportAsAudioData(): AudioData

Exports the rendered data as AudioData format.

context.encodeAudioData(audioData: AudioData, opts?:object): Promise< ArrayBuffer >

Encode audio data to the binary format.

  • audioData: AudioData
  • opts.bitDepth: number bits per sample - default: 16
  • opts.float: boolean use floating-point values - default: false
const fs = require("fs");
const AudioContext = require("web-audio-engine").RenderingAudioContext;
const context = new AudioContext();

// composeWith(context);

context.processTo("00:01:30.000");
// context.currentTime -> 90.00054421768708

context.processTo("00:02:00.000");
// context.currentTime -> 120.00072562358277

const audioData = context.exportAsAudioData();

context.encodeAudioData(audioData).then((arrayBuffer) => {
  fs.writeFile("output.wav", new Buffer(arrayBuffer));
});

Class: WebAudioContext

๐Ÿ‘ท TODO: WRITE DESCRIPTION

new WebAudioContext(opts?: object)

Creates new WebAudioContext instance.

  • opts.context?: AudioContext the native Web Audio API AudioContext instance
  • opts.destination?: AudioNode - default: opts.context.destination
  • opts.numberOfChannels: number audio channels (e.g. 2: stereo) - default: 2
  • opts.blockSize: number samples each rendering quantum - default: 128
<script src="/path/to/web-audio-engine.js"></script>
<script>
var context = new WebAudioEngine.WebAudioContext({ context: new AudioContext() });

// composeWith(context);

context.resume();
</script>

Class: OfflineAudioContext

This context is compatible with the natvie Web Audio API OfflineAudioContext.

const OfflineAudioContext = require("web-audio-engine").OfflineAudioContext;
const context = new OfflineAudioContext(2, 44100 * 10, 44100);

// composeWith(context);

context.startRendering().then((audioBuffer) => {
  console.log(audioBuffer);
});

Interface: AudioData

interface AudioData {
  numberOfChannels?: number;
  length?: number;
  sampleRate: number;
  channelData: Float32Array[];
}

decoder

The default decoder of web-audio-engine supports "wav" format only. If you need to support other audio format, you are necessary to prepare a decoder yourself.

decoder.get(type: string): function

Returns the function for decoding currently set.

decoder.set(type: string, decodeFn: function)

Sets the function for decoding.

  • decodeFn: (audioData: ArrayBuffer, opts?: object) => Promise< AudioData > The decoding to use.
decoder.decode(audioData: ArrayBuffer, opts?: object): Promise< AudioData >

Executes decoding.

  • audioData: ArrayBuffer
mp3 decoder example
const wae = require("web-audio-engine");
const mp3decoder = require("/path/to/mp3decoder");

wae.decoder.set("mp3", mp3decoder);

const fs = require("fs");
const AudioContext = require("web-audio-engine").RenderingAudioContext;
const context = new AudioContext();
const audioData = fs.readFileSync("amen.mp3");

context.decodeAudioData(audioData).then((audioBuffer) => {
  console.log(audioBuffer);
});

encoder

The default encoder of web-audio-engine supports "wav" format only. If you need to support other audio format, you are necessary to prepare an encoder yourself.

encoder.get(type: string): function

Returns the function for encoding currently set.

encoder.set(type: string, encodeFn: function)

Sets the function for encoding.

  • encodeFn: (audioData: AudioData, opts?: object) => Promise< ArrayBuffer > The encoding to use.
encoder.encode(audioData: AudioData, opts?: object): Promise< ArrayBuffer >

Executes encoding.

  • audioData: AudioData
  • opts.type: string audio format type - default: "wav"
mp3 encoder example
const wae = require("web-audio-engine");
const mp3encoder = require("/path/to/mp3encoder");

wae.encoder.set("mp3", mp3encoder);

const fs = require("fs");
const AudioContext = require("web-audio-engine").RenderingAudioContext;
const context = new AudioContext();
const audioData = context.exportAsAudioData();

context.encodeAudioData(audioData, { type: "mp3" }).then((arrayBuffer) => {
  fs.writeFile("output.mp3", new Buffer(arrayBuffer));
});

Implemented API

  • AnalyserNode
  • AudioBuffer
  • AudioBufferSourceNode
  • AudioContext
  • AudioDestinationNode
  • AudioNode
  • AudioParam
  • BiquadFilterNode (audio rate parameter is not supported)
  • ChannelMergerNode
  • ChannelSplitterNode
  • DelayNode (noisy..)
  • GainNode
  • IIRFIlterNode
  • OscillatorNode (use wave-table synthesis, not use periodic wave)
  • PeriodicWave
  • ScriptProcessorNode
  • StereoPannerNode
  • WaveShaperNode
  • The other not implemented nodes will pass its input to its output without modification.
  • See: Comparison Chart of implemented nodes

Example

const Speaker = require("speaker");
const AudioContext = require("web-audio-engine").StreamAudioContext;
const context = new AudioContext();

const osc = context.createOscillator();
const amp = context.createGain();

osc.type = "square";
osc.frequency.setValueAtTime(987.7666, 0);
osc.frequency.setValueAtTime(1318.5102, 0.075);
osc.start(0);
osc.stop(2);
osc.connect(amp);
osc.onended = () => {
  context.close().then(() => {
    process.exit(0);
  });
};

amp.gain.setValueAtTime(0.25, 0);
amp.gain.setValueAtTime(0.25, 0.075);
amp.gain.linearRampToValueAtTime(0, 2);
amp.connect(context.destination);

context.pipe(new Speaker());
context.resume();

Online Demo

The online demo is here. In this site, you can compare web-audio-engine and the native Web Audio API.

Offline Demo

$ git clone [email protected]:mohayonao/web-audio-engine.git
$ cd web-audio-engine
$ npm install && npm run build
$ cd demo
$ npm install
$ node demo --help

Simplest play demo with node-speaker.

$ node demo sines

Rendering and export to the wav file.

$ node demo -o out.wav sines

Online Benchmark

Currently, this benchmark doesn't work in Chrome or Safari, please use Firefox.

Offline Benchmark

$ git clone [email protected]:mohayonao/web-audio-engine.git
$ cd web-audio-engine
$ npm install && npm run build
$ cd benchmark
$ npm install
$ node .

License

MIT

More Repositories

1

timbre.js

JavaScript library for objective sound programming
JavaScript
962
star
2

timbre

345
star
3

pico.js

Audio processor for the cross-platform
JavaScript
312
star
4

CoffeeCollider

Sound Processing Language for Web Audio
JavaScript
216
star
5

gretro

๐ŸŽจ JavaScript graphic library for retro CG
JavaScript
125
star
6

web-audio-scheduler

Event Scheduler for Web Audio API
JavaScript
90
star
7

ciseaux

JavaScript utility to chop an audio buffer
JavaScript
81
star
8

old-neume.js

Neume.js is a Web Audio API library for developing browser music.
JavaScript
79
star
9

subcollider

๐Ÿ‘ท
JavaScript
71
star
10

web-audio-test-api

Web Audio API for CI
JavaScript
67
star
11

inline-worker

JavaScript utility to create a WebWorker from a function
JavaScript
65
star
12

wave-tables

JavaScript
54
star
13

mml-emitter

MML(Music Macro Language) event emitter for Web Audio API.
JavaScript
52
star
14

virtual-midi-keyboard

Electron-based Virtual MIDI Keyboard
JavaScript
52
star
15

web-audio-lesson

JavaScript
49
star
16

virtual-midi-matrix-pad

Electron-based Virtual MIDI Controller that like Novation Launchpad
JavaScript
40
star
17

wav-decoder

promise-based wav decoder
JavaScript
38
star
18

wav-encoder

promise-based wav encoder
JavaScript
36
star
19

adsr-envelope

ADSREnvelope for Web Audio API
JavaScript
36
star
20

web-audio-api-shim

shim for legacy Web Audio API
JavaScript
33
star
21

audio-worker-shim

AudioWorker compatibility shim for legacy Web Audio API
JavaScript
26
star
22

seurat

๐ŸŽจ JavaScript utility to generate a braille text from an image
JavaScript
25
star
23

fm-synth

simple frequency modulation synthesizer
JavaScript
22
star
24

scsynth

JavaScript
20
star
25

mohayonao.herokuapp.com

ใ‚ฆใ‚งใƒ–ๆฅฝๅ™จใจใ‹ใฎใ‚ณใƒผใƒ‰
JavaScript
18
star
26

audio-worker-node

AudioWorkerNode for legacy Web Audio API
JavaScript
18
star
27

pluck-string-node

Pluck String AudioNode for Web Audio API
JavaScript
17
star
28

osc-msg

OSC message decoder/encoder with fault tolerant
JavaScript
17
star
29

launch-control

JavaScript utility for novation LAUNCH CONTROL
JavaScript
17
star
30

stereo-panner-node

StereoPannerNode for legacy Web Audio API
JavaScript
17
star
31

osc-hub

broadcast osc messages over the network
JavaScript
17
star
32

biquad-coeffs

collection of formulae for biquad filter coefficients
JavaScript
16
star
33

MMLEmitter

this package is renamed to mml-emitter
16
star
34

worker-timer

Timer API that works stable in background tabs
JavaScript
14
star
35

wae-cli

web-audio-engine command line
JavaScript
14
star
36

es6-project-boilerplate

JavaScript
13
star
37

the.mohayonao.com

JavaScript
13
star
38

mml-iterator

MML(Music Macro Language) Iterator
JavaScript
13
star
39

midi-device

isomorphic abstract class for MIDI device
JavaScript
12
star
40

promise-decode-audio-data

Promise-based decodeAudioData for legacy Web Audio API
HTML
12
star
41

xs-808

JavaScript
11
star
42

web-audio-api-spec-checker

HTML
11
star
43

waa-lab

HTML
11
star
44

midi-keyboard

JavaScript utility for MIDI keyboard
JavaScript
10
star
45

pseudo-audio-param

Simulate scheduled AudioParam values
JavaScript
10
star
46

web-audio-utils

Utility functions for Web Audio API
JavaScript
10
star
47

stereo-analyser-node

JavaScript
10
star
48

get-float-time-domain-data

getFloatTimeDomainData for legacy Web Audio API
JavaScript
10
star
49

virtual-midi-knob-knob-pad

Electron-based Virtual MIDI Controller that like Novation Launch Control
JavaScript
9
star
50

xs-seq

minimum sequencer with React/Redux
JavaScript
9
star
51

sushi-repl

Yet Another JavaScript REPL
JavaScript
9
star
52

MutekiTimer

A stable timer that run in the background
CoffeeScript
9
star
53

synthdef-json-decoder

Decode SuperCollider Synth Definition File Format and convert to JSON
JavaScript
9
star
54

web-midi-test-api

Web MIDI API for CI
JavaScript
8
star
55

CnvUI

A tiny UI base on HTMLCanvasElement
JavaScript
8
star
56

react-code-reading

8
star
57

perlin-noise

simple 1d perlin noise generator
JavaScript
8
star
58

SCScript

JavaScript port of SuperCollider
JavaScript
8
star
59

script-synthesizer

Script Syntesizer with timbre.js and WebMidiLink
JavaScript
8
star
60

prominence

Call a callback-based function as ES6 Promise
JavaScript
8
star
61

cubic-sequencer

JavaScript
7
star
62

duolet

2 layered architecture for sound programming in JavaScript
JavaScript
7
star
63

web-music-hackathon-04

project for web music hackathon 04
JavaScript
7
star
64

start-web-audio

JavaScript
6
star
65

mimi-grep

auralization grep command
JavaScript
6
star
66

seq-emitter

EventEmitter for Web Audio API sequencer
JavaScript
6
star
67

ksdn808

้–ข่ฅฟ้›ปๆฐ—ไฟๅฎ‰ๅ”ไผšใƒชใ‚บใƒ ใƒžใ‚ทใƒผใƒณ
JavaScript
6
star
68

white-noise-node

WhiteNoiseNode for Web Audio API
HTML
6
star
69

launch-pad-color

color code of Novation LaunchPad
JavaScript
6
star
70

libogg.js

JavaScript port to the libogg 1.3.0
JavaScript
6
star
71

tickable-timer

Manual ticking timer API
JavaScript
5
star
72

fetch-as-audio-buffer

util function to fetch audio buffer
JavaScript
5
star
73

is-zero

is ยฑ0 ?
JavaScript
5
star
74

worker-builder

WorkerBuilder builds a WebWorker from a funtion
JavaScript
5
star
75

brown-noise-node

BrownNoiseNode for Web Audio API
HTML
5
star
76

triolet

3 layered architecture for sound programming in JavaScript
JavaScript
5
star
77

json-touch-patch

Immutable JSON Patch implementation based on RFC 6902
JavaScript
5
star
78

web-audio-sound-examples

JavaScript
4
star
79

envelope

simple envelope
JavaScript
4
star
80

web-music-hack0913

JavaScript
4
star
81

pico.driver

audio driver for sound programming in JavaScript
JavaScript
4
star
82

xs-key

minimum keyboard with React/Redux
JavaScript
4
star
83

biquad-filter-kernel

collection of biquad filter kernel
JavaScript
4
star
84

ouroboros-worker

WebWorker to share the code of the main thread
JavaScript
4
star
85

pink-noise-node

PinkNoiseNode for Web Audio API
JavaScript
4
star
86

web-audio-mock-api

mock API which implements the Web Audio API's interfaces only
JavaScript
4
star
87

drunk-timer

Staggering timer API
JavaScript
4
star
88

GLISTAGRAM

glitched instagram viewer
JavaScript
3
star
89

ableton-live-utils

JavaScript utility for ableton live
JavaScript
3
star
90

thisis

็ฟ’ไฝœ
JavaScript
3
star
91

web-audio-test-api-2

Web Audio API for CI
JavaScript
3
star
92

request-midi-access

requestMIDIAccess for all
JavaScript
3
star
93

dorilasound

JavaScript
3
star
94

chai-deep-closeto

deep.closeTo for chai
JavaScript
3
star
95

virtual-midi-device

๐Ÿ‘ท Utils for Virtual MIDI Device
JavaScript
3
star
96

gretro-text

JavaScript
3
star
97

audiodata

definition of audio data interface
JavaScript
3
star
98

constant-source-node

ConstantSourceNode for legacy Web Audio API
JavaScript
3
star
99

power-audio-node

enhance the connecting interface of AudioNode for custom AudioNode
JavaScript
3
star
100

freqr

Compute the frequency response of a digital filter.
JavaScript
3
star