Giter VIP home page Giter VIP logo

audio-effects's Introduction

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(); // Some audio stream
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; // Change the volume to 50%
      volume.mute = true; // Mute the volume

Distortion

Add a distortion effect

import {Distortion} from 'audio-effects';

const distortion = new Distortion(audioContext);
      distortion.intensity = 200; // Set the intensity to 200
      distortion.gain = 100; // Set the gain to 100
      distortion.lowPassFilter = true; // Enable the lowpass filter

Delay

Add a delay effect

import {Delay} from 'audio-effects';

const delay = new Delay(audioContext);
      delay.wet = 1; // Set the wetness to 100%
      delay.speed = 1; // Set the speed to 1 second
      delay.duration = 0.4; // Set the delay duration to 40%

Flanger

Add a Flanger effect

import {Flanger} from 'audio-effects';

const flanger = new Flanger(audioContext);
      flanger.delay = 0.005; // Set the delay to 0.005 seconds
      flanger.depth = 0.002; // Set the depth to 0.002
      flanger.feedback = 0.5; // Set the feedback to 50%
      flanger.speed = 0.25; // Set the speed to 0.25 Hz

Reverb

Add a Reverb effect

import {Reverb} from 'audio-effects';

const reverb = new Reverb(audioContext)
      reverb.wet = 0.5; // Set the wetness to 50%
      reverb.level = 1; // Set the level to 100%
      ReverbNode.getInputResponseFile('path/to/input-response-file').then(buffer => {
        reverb.buffer = buffer;
      });

Tremolo

Add a Tremolo effect

import {Tremolo} from 'audio-effects';

const tremolo = new Tremolo(audioContext);
      tremolo.speed = 1; // Set the speed to 1Hz

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) {
    // The current browser supports the web-audio-api.
}

if (HasGetUserMedia) {
    // The current browser supports getUserMedia.
}

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);

        // All audio nodes needed for the effect should be kept in the nodes object.
        this.nodes = {
            node1: audioContext.createGain(),
            node2: audioContext.createGain(),
            node3: audioContext.createGain(),
        };

        // Connect all nodes
        // [node 1]-->>--[node 2]-->>--[node 3]
        this.nodes.node1.connect(this.nodes.node2);
        this.nodes.node2.connect(this.nodes.node3);

        // Set the  input-node, this is the first node in the effect's chain.
        this._node = this.nodes.node1;

        // Set the output-node, this is the last node in the effect's chain.
        this._outputNode = this.nodes.node3;
    }

    // Create getters and setters for the parameters you want to be customizable.
    get gain() {
        return this.nodes.node1.gain.value;
    }

    set gain(gain) {
        this.nodes.node1.gain.value = parseFloat(gain);
    }

    ...
}

Todo

  • Write tests!
  • Add tuner
  • Add chorus
  • Add auto-wah
  • ...

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.

audio-effects's People

Contributors

romainberger avatar sambego avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

audio-effects's Issues

Using AudioNodes instead of MediaStream

Hi! Thanks for the lib!
Is it possible to create an Input node with other than a MediaStream?
I've read the src and in the set signature it accepts AudioNode|MediaStream but the audioContext.createMediaStreamSource function tha'ts called only accepts MediaStreams..

I'm using local audio files, so I think I'm 'stuck' with regular Audio Nodes, and cannot generate a stream with them... Is this feasible?

Reverb not working as expected?

Hey, I've been toying around with this for a presentation, but I can't seem to make the reverb work even after trying a couple of different IR files (.wav).

Since I've been implementing most of the stuff myself, the only thing I'm using form the lib are the effects (particularly Distortion, Tremolo and Reverb), meaning I'm manually handling the connections and I'm not using the Output or Input classes provided by the lib.

class Mixer {
public reverb: IEffect;

    constructor() {
        this.reverb = { status: false, effect: new Reverb(this.audioContext) };
        fetch('./src/ir-files/baptist-church-nashville.wav')
            .then(response => response.arrayBuffer())
            .then(audioBuffer => this.reverb.effect.buffer = audioBuffer);
        this.reverb.effect.wet = 1;
        this.reverb.effect.level = 1;

...

I'm guessing the decoding of the arrayBuffer is not needed as the Reverb.js file seems to have it sorted on the setter, should I also omit making it an arrayBuffer in the first place?

The lib is a bit old but I got here after watching your live presentation and it has inspired some ideas for mine. Hope you can share some thoughts on how to solve this issue. Cheers!

Edit: as an extra bit of info, the sound does come out, only the Reverb doesn't work. I've tested using some Anechoic audio files.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.