Audio-effects
A javascript library to create audio effects using the web-audio-api. This library contains the following effects:
- Volume
- Distortion
- Delay
- Flanger
- Reverb
- Tremolo
I will try to add more effects in the future.
Install
npm install --save audio-effects
API
Audio context
To start, we need an audio-context, the audio-effect library has a useful helper function to check if the current browser supports the web-audio-api.
import {HasAudioContext} from 'audio-effects';
let audioContext = null;
if (HasAudioContext) {
audioContext = new AudioContext();
}
Input
An input node manages the audio input.
You can either supply an audio stream.
import {Input} from 'audio-effects';
const stream = createAnAudioStream();
const input = new Input(audioContext);
input.input = stream;
Or use the getUserMedia
method to access the devices microphone.
import {Input} from 'audio-effects';
const input = new Input(audioContext);
input.getUserMedia();
Output
This is the audio node which should be at the end of the chain, this connects our audio to the device's speakers.
import {Output} from 'audio-effects';
const output = new Output(audioContext);
Volume
Control the volume of your audio or mute it.
import {Volume} from 'audio-effects';
const volume = new Volume(audioContext);
volume.level = 0.5;
volume.mute = true;
Distortion
Add a distortion effect
import {Distortion} from 'audio-effects';
const distortion = new Distortion(audioContext);
distortion.intensity = 200;
distortion.gain = 100;
distortion.lowPassFilter = true;
Delay
Add a delay effect
import {Delay} from 'audio-effects';
const delay = new Delay(audioContext);
delay.wet = 1;
delay.speed = 1;
delay.duration = 0.4;
Flanger
Add a Flanger effect
import {Flanger} from 'audio-effects';
const flanger = new Flanger(audioContext);
flanger.delay = 0.005;
flanger.depth = 0.002;
flanger.feedback = 0.5;
flanger.speed = 0.25;
Reverb
Add a Reverb effect
import {Reverb} from 'audio-effects';
const reverb = new Reverb(audioContext)
reverb.wet = 0.5;
reverb.level = 1;
By default the reverb effect will use the hall-reverb impulse response file from the awesome web-audio-playground by Chris Wilson.
if you want to use your own impulse response file you can configure it in 2 ways:
const irf = fetch('path/to/impulse-response-file', {
method: 'get'
}).then(response => {
return response.arrayBuffer();
});
irf.then(buffer => {
const reverb = new Reverb(audioContext, buffer);
});
const reverb = new Reverb(audioContext);
irf.then(buffer => {
reverb.buffer = buffer;
});
Tremolo
Add a Tremolo effect
import {Tremolo} from 'audio-effects';
const tremolo = new Tremolo(audioContext);
tremolo.speed = 1;
Chaining
Like regular audio nodes, these nodes need to be chained together to connect the input to effects and the output.
The api is the same as with normal audio nodes.
input.connect(output);
Unlike their native counterparts, audio-effects' audio nodes can also be chained together.
input.connect(volume).connect(distortion).connect(output);
Helper functions
The audio-effects library has some built-in helper functions.
import {HasAudioContext, HasGetUserMedia} from 'audio-effects';
if (HasAudioContext) {
}
if (HasGetUserMedia) {
}
Create your own effects
It is possible to create your own effects.
import {SingleAudioNode} from 'audio-effects';
class CustomEffect extends SingleAudioNode {
constructor(audioContext) {
super(audioContext);
this.nodes = {
node1: audioContext.createGain(),
node2: audioContext.createGain(),
node3: audioContext.createGain(),
};
this.nodes.node1.connect(this.nodes.node2);
this.nodes.node2.connect(this.nodes.node3);
this._node = this.nodes.node1;
this._outputNode = this.nodes.node3;
}
get gain() {
return this.nodes.node1.gain.value;
}
set gain(gain) {
this.nodes.node1.gain.value = parseFloat(gain);
}
...
}
Todo
License
The MIT License (MIT)
Copyright © 2016
Sam Bellen
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.