web-audio-engine
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: 44100opts.numberOfChannels: number
audio channels (e.g. 2: stereo) - default: 2opts.blockSize: number
samples each rendering quantum - default: 128opts.bitDepth: number
bits per sample - default: 16opts.float: boolean
use floating-point values - default: false
context.pipe(destination: stream.Writable): stream.Writable
👷 TODO: WRITE DESCRIPTION
const AudioContext = StreamAudioContext;const context = ; // Set the output for audio streamingcontext; // 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 audiocontext; // 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: 44100opts.numberOfChannels: number
audio channels (e.g. 2: stereo) - default: 2opts.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: 16opts.float: boolean
use floating-point values - default: false
const fs = ;const AudioContext = RenderingAudioContext;const context = ; // composeWith(context); context;// context.currentTime -> 90.00054421768708 context;// context.currentTime -> 120.00072562358277 const audioData = context; context;
Class: WebAudioContext
👷 TODO: WRITE DESCRIPTION
new WebAudioContext(opts?: object)
Creates new WebAudioContext
instance.
opts.context?: AudioContext
the native Web Audio API AudioContext instanceopts.destination?: AudioNode
- default: opts.context.destinationopts.numberOfChannels: number
audio channels (e.g. 2: stereo) - default: 2opts.blockSize: number
samples each rendering quantum - default: 128
Class: OfflineAudioContext
This context is compatible with the natvie Web Audio API OfflineAudioContext
.
const OfflineAudioContext = OfflineAudioContext;const context = 2 44100 * 10 44100; // composeWith(context); context;
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 = ;const mp3decoder = ; waedecoder; const fs = ;const AudioContext = RenderingAudioContext;const context = ;const audioData = fs; context;
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 = ;const mp3encoder = ; waeencoder; const fs = ;const AudioContext = RenderingAudioContext;const context = ;const audioData = context; context;
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 = ;const AudioContext = StreamAudioContext;const context = ; const osc = context;const amp = context; osctype = "square";oscfrequency;oscfrequency;oscstart0;osc;osc;osc { context;}; ampgain;ampgain;ampgain;amp; context;context;
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 git@github.com: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 git@github.com:mohayonao/web-audio-engine.git
$ cd web-audio-engine
$ npm install && npm run build
$ cd benchmark
$ npm install
$ node .
License
MIT