biquad-coeffs-webaudio-v2

WebAudio formulae for audio EQ biquad filter coefficients

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
biquad-coeffs-webaudio-v2
1.2.08 years ago8 years agoMinified + gzip package size for biquad-coeffs-webaudio-v2 in KB

Readme

biquad-coeffs-webaudio-v2
Build Status NPM Version License
WebAudio formulae for audio EQ biquad filter coefficients
  • https://webaudio.github.io/web-audio-api/

Installation

$ npm install --save biquad-coeffs-webaudio-v2

API

- [filterType](freq, q, gain): number[]
- `filterType: string`
  - "lowpass"
  - "highpass"
  - "bandpass"
  - "notch"
  - "allpass"
  - "peaking"
  - "lowshelf"
  - "highshelf"
- `freq: number` filter cutoff or center frequency
  - This parameter should be normalized (0..1).
  - `normalizedFrequency = frequency / sampleRate`
- `q: number` filter Q (resonance)
  - This parameter is NOT used by "lowshelf" or "highshelf".
- `gain: number` filter gain
  - This parameter is used by "peaking", "lowshelf" or "highshelf".
- returns coeffs `[ b0, b1, b2, a1, a2 ]` (a0 = 1)

Usage

DSP
const coeffs = require("biquad-coeffs-webaudio-v2");

const [ b0, b1, b2, a1, a2 ] = coeffs.lowpass(1200/44100, 6);
const signal = new Float32Array(2048).map(Math.random);

let x0, x1 = 0, x2 = 0;
let y0, y1 = 0, y2 = 0;

for (let i = 0; i < signal.length; i++) {
  x0 = signal[i];
  y0 = (b0 * x0) + (b1 * x1) + (b2 * x2) - (a1 * y1) - (a2 * y2);

  signal[i] = y0;

  x2 = x1;
  x1 = x0;
  y2 = y1;
  y1 = y0;
}

Web Audio API
const coeffs = require("biquad-coeffs-webaudio-v2");

const [ b0, b1, b2, a1, a2 ] = coeffs.lowpass(1200/audioContext.sampleRate, 6);
const audioContext = new AudioContext();
const oscillator = audioContext.createOscillator();
const filter = audioContext.createIIRFilter([ b0, b1, b2 ], [ 1, a1, a2 ]);

oscillator.type = "sawtooth";
oscillator.frequency.value = 880;
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 2);
oscillator.connect(filter);

filter.connect(audioContext.destination);

License

MIT