webaudio.lib
An implementation of the WebAudio API filters (https://www.w3.org/TR/webaudio/). Its official prefix is wa.
This library implement WebAudio filters, using their C++ version as a starting point, taken from Mozilla Firefox implementation.
The cutoff, detune included, is kept between 0.01 and 0.99 times the Nyquist
frequency, a limit the Web Audio API does not have: no f0 below 220.5 Hz at
44.1 kHz, 240 Hz at 48 kHz, 960 Hz at 192 kHz. Below it fi.tf2, which realizes
these filters, loses its precision in single precision. A sweep or a modulation
of f0 stops at that frequency, which moves with the sample rate.
References
BiquadFilter
Environment holding the normalized biquad coefficient sets
(b0,b1,b2,a1,a2, ready for fi.tf2) of the Web Audio filters below,
computed from the Web Audio API specification formulas.
Usage
BiquadFilter(f0, dBgain, Q, aDetune).lowpass2 // or .highpass2, .bandpass2,
// .notch2, .allpass2, .peaking2, .lowshelf2, .highshelf2
Where:
f0: center/cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495dBgain: gain in dB (used by the peaking and shelving filters)Q: quality factoraDetune: detune amount in cents
Test
wa = library("webaudio.lib");
BiquadFilter_test = wa.BiquadFilter(1000, 6, 1, 0).lowpass2;
(wa.)lowpass2
Standard second-order resonant lowpass filter with 12dB/octave rolloff. Frequencies below the cutoff pass through, frequencies above it are attenuated.
Usage
_ : lowpass2(f0, Q, dtune) : _
Where:
f0: cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495Q: the quality factordtune: detuning of the frequency in cents
Test
wa = library("webaudio.lib");
os = library("oscillators.lib");
ba = library("basics.lib");
ma = library("maths.lib");
no = library("noises.lib");
lowpass2_test = os.tosc(440) : wa.lowpass2(1000, 0.707, 0);
lowpass2_slider_test = os.tosc(440) : wa.lowpass2(hslider("lowpass2:f0", 1000, 20, 20000, 1), hslider("lowpass2:Q", 0.707, 0.1, 20, 0.001), hslider("lowpass2:dtune", 0, -1200, 1200, 1));
lowpass2_modulated_test = no.noise : wa.lowpass2(20*pow(250, tri), 0.707, 0) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
lowpass2_jump_test = no.noise : wa.lowpass2(20*pow(250, sq), 0.707, 0) with { P = int(ma.SR/10); sq = ba.period(2*P) < P; };
References
(wa.)highpass2
Standard second-order resonant highpass filter with 12dB/octave rolloff. Frequencies below the cutoff are attenuated, frequencies above it pass through.
Usage
_ : highpass2(f0, Q, dtune) : _
Where:
f0: cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495Q: the quality factordtune: detuning of the frequency in cents
Test
wa = library("webaudio.lib");
os = library("oscillators.lib");
ba = library("basics.lib");
ma = library("maths.lib");
no = library("noises.lib");
highpass2_test = os.tosc(440) : wa.highpass2(1000, 0.707, 0);
highpass2_slider_test = os.tosc(440) : wa.highpass2(hslider("highpass2:f0", 1000, 20, 20000, 1), hslider("highpass2:Q", 0.707, 0.1, 20, 0.001), hslider("highpass2:dtune", 0, -1200, 1200, 1));
highpass2_modulated_test = no.noise : wa.highpass2(20*pow(250, tri), 0.707, 0) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
highpass2_jump_test = no.noise : wa.highpass2(20*pow(250, sq), 0.707, 0) with { P = int(ma.SR/10); sq = ba.period(2*P) < P; };
References
(wa.)bandpass2
Standard second-order bandpass filter. Frequencies outside the given range of frequencies are attenuated, the frequencies inside it pass through.
Usage
_ : bandpass2(f0, Q, dtune) : _
Where:
f0: cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495Q: the quality factordtune: detuning of the frequency in cents
Test
wa = library("webaudio.lib");
os = library("oscillators.lib");
ba = library("basics.lib");
ma = library("maths.lib");
no = library("noises.lib");
bandpass2_test = os.tosc(440) : wa.bandpass2(1000, 1, 0);
bandpass2_slider_test = os.tosc(440) : wa.bandpass2(hslider("bandpass2:f0", 1000, 20, 20000, 1), hslider("bandpass2:Q", 1, 0.1, 20, 0.001), hslider("bandpass2:dtune", 0, -1200, 1200, 1));
bandpass2_modulated_test = no.noise : wa.bandpass2(20*pow(250, tri), 1, 0) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
bandpass2_jump_test = no.noise : wa.bandpass2(20*pow(250, sq), 1, 0) with { P = int(ma.SR/10); sq = ba.period(2*P) < P; };
References
(wa.)notch2
Standard notch filter, also called a band-stop or band-rejection filter. It is the opposite of a bandpass filter: frequencies outside the give range of frequencies pass through, frequencies inside it are attenuated.
Usage
_ : notch2(f0, Q, dtune) : _
Where:
f0: cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495Q: the quality factordtune: detuning of the frequency in cents
Test
wa = library("webaudio.lib");
os = library("oscillators.lib");
ba = library("basics.lib");
ma = library("maths.lib");
no = library("noises.lib");
notch2_test = os.tosc(440) : wa.notch2(1000, 1, 0);
notch2_slider_test = os.tosc(440) : wa.notch2(hslider("notch2:f0", 1000, 20, 20000, 1), hslider("notch2:Q", 1, 0.1, 20, 0.001), hslider("notch2:dtune", 0, -1200, 1200, 1));
notch2_modulated_test = no.noise : wa.notch2(20*pow(250, tri), 1, 0) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
notch2_jump_test = no.noise : wa.notch2(20*pow(250, sq), 1, 0) with { P = int(ma.SR/10); sq = ba.period(2*P) < P; };
References
(wa.)allpass2
Standard second-order allpass filter. It lets all frequencies through, but changes the phase-relationship between the various frequencies.
Usage
_ : allpass2(f0, Q, dtune) : _
Where:
f0: cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495Q: the quality factordtune: detuning of the frequency in cents
Test
wa = library("webaudio.lib");
os = library("oscillators.lib");
ba = library("basics.lib");
ma = library("maths.lib");
no = library("noises.lib");
allpass2_test = os.tosc(440) : wa.allpass2(1000, 1, 0);
allpass2_slider_test = os.tosc(440) : wa.allpass2(hslider("allpass2:f0", 1000, 20, 20000, 1), hslider("allpass2:Q", 1, 0.1, 20, 0.001), hslider("allpass2:dtune", 0, -1200, 1200, 1));
allpass2_modulated_test = no.noise : wa.allpass2(20*pow(250, tri), 1, 0) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
allpass2_jump_test = no.noise : wa.allpass2(20*pow(250, sq), 1, 0) with { P = int(ma.SR/10); sq = ba.period(2*P) < P; };
References
(wa.)peaking2
Frequencies inside the range get a boost or an attenuation, frequencies outside it are unchanged.
Usage
_ : peaking2(f0, gain, Q, dtune) : _
Where:
f0: cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495gain: the gain in dBQ: the quality factordtune: detuning of the frequency in cents
Test
wa = library("webaudio.lib");
os = library("oscillators.lib");
ba = library("basics.lib");
ma = library("maths.lib");
no = library("noises.lib");
peaking2_test = os.tosc(440) : wa.peaking2(1000, 3, 1, 0);
peaking2_slider_test = os.tosc(440) : wa.peaking2(hslider("peaking2:f0", 1000, 20, 20000, 1), hslider("peaking2:gain", 3, -24, 24, 0.1), hslider("peaking2:Q", 1, 0.1, 20, 0.001), hslider("peaking2:dtune", 0, -1200, 1200, 1));
peaking2_modulated_test = no.noise : wa.peaking2(20*pow(250, tri), 3, 1, 0) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
peaking2_jump_test = no.noise : wa.peaking2(20*pow(250, sq), 3, 1, 0) with { P = int(ma.SR/10); sq = ba.period(2*P) < P; };
References
(wa.)lowshelf2
Standard second-order lowshelf filter. Frequencies lower than the frequency get a boost, or an attenuation, frequencies over it are unchanged.
Usage
_ : lowshelf2(f0, gain, dtune) : _
Where:
f0: cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495gain: the gain in dBdtune: detuning of the frequency in cents
Test
wa = library("webaudio.lib");
os = library("oscillators.lib");
ba = library("basics.lib");
ma = library("maths.lib");
no = library("noises.lib");
lowshelf2_test = os.tosc(440) : wa.lowshelf2(500, 6, 0);
lowshelf2_slider_test = os.tosc(440) : wa.lowshelf2(hslider("lowshelf2:f0", 500, 20, 20000, 1), hslider("lowshelf2:gain", 6, -24, 24, 0.1), hslider("lowshelf2:dtune", 0, -1200, 1200, 1));
lowshelf2_modulated_test = no.noise : wa.lowshelf2(20*pow(250, tri), 6, 0) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
lowshelf2_jump_test = no.noise : wa.lowshelf2(20*pow(250, sq), 6, 0) with { P = int(ma.SR/10); sq = ba.period(2*P) < P; };
References
(wa.)highshelf2
Standard second-order highshelf filter. Frequencies higher than the frequency get a boost or an attenuation, frequencies lower than it are unchanged.
Usage
_ : highshelf2(f0, gain, dtune) : _
Where:
f0: cutoff frequency in Hz, at least 0.005 times the sample rate, at most 0.495gain: the gain in dBdtune: detuning of the frequency in cents
Test
wa = library("webaudio.lib");
os = library("oscillators.lib");
ba = library("basics.lib");
ma = library("maths.lib");
no = library("noises.lib");
highshelf2_test = os.tosc(440) : wa.highshelf2(2000, -6, 0);
highshelf2_slider_test = os.tosc(440) : wa.highshelf2(hslider("highshelf2:f0", 2000, 20, 20000, 1), hslider("highshelf2:gain", -6, -24, 24, 0.1), hslider("highshelf2:dtune", 0, -1200, 1200, 1));
highshelf2_modulated_test = no.noise : wa.highshelf2(20*pow(250, tri), -6, 0) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
highshelf2_jump_test = no.noise : wa.highshelf2(20*pow(250, sq), -6, 0) with { P = int(ma.SR/10); sq = ba.period(2*P) < P; };