BB.AudioFX
A module for creating filter, reverb and delay effects ( abstracts the WebAudio API BiquadFilterNode, ConvolverNode and DelayNode ), for more advanced audio effects see the BB.AFX base Audio Effects module
Index
Methods
Constructor
BB.AudioFX
(
-
type
-
config
)
-
type
-
config
Parameters:
-
type
Stringthe type of effect you want to create ( filter, reverb and delay)
-
config
ObjectA config object to initialize the effect ( include examples for diff effects )
Example:
when using 'filter' type
in the example bellow the drum sampler is connected to the AudioFX ( in this case a 'filter' ), and the AudioFX is connected to the default BB.Audio destination
BB.Audio.init();
var filt = new BB.AudioFX('filter');
var drum = new BB.AudioSampler({
connect: filt,
kick: 'audio/kick.ogg',
snare: 'audio/snare.ogg'
});
BB.AudioFX can also take an option config object. when using the 'filter' type, a config object can include the following:
var filt = new BB.AudioFX('filter',{
connect: fft,
type: "lowpass",
frequency: 880,
Q: 8,
fgain: 5,
});
filter types include "lowpass" (default), "highpass", "bandpass", "lowshelf", "highshelf", "peaking", "notch" and "allpass"
check out the BiquadFilterNode documenation for more details on filter types and the properties ( frequency, Q, gain)
view basic BB.AudioFX 'filter' example
when using 'reverb' type
the AudioFX ( 'reverb' ), can be used just like the 'filter' example above. there's two ways to create "reverb" FX, either algorithmically ( default ) or by pasing it paths to impulse file[s] ( see conolution reverb on wikipedia)
// algorithmically calculate convolution's impulse buffer
var reverb = new BB.AudioFX('reverb');
// customize algorithmically generated impulse buffer
var reverb = new BB.AudioFX('reverb',{
duration: 2,
decay: 4.3,
reverse:true
});
// generate convolution buffer from impulse file
var reverb = new BB.AudioFX('reverb',{
paths: ['audio/impulse.wav']
});
check out the ConvolutionNode documenation for more info
view basic BB.AudioFX 'reverb' example
when using 'delay' type
the AudioFX ( 'delay' ), can be used just like the 'filter' example above.
var delay = new BB.AudioFX('delay');
// with optional config
var delay = new BB.AudioFX('delay',{
max: 5, // max delay time
time: 1 // current delay time
});
// modify delay time afterwards
delay.time = 3.2;
check out the DelayNode documenation for more info
view basic BB.AudioFX 'delay' example
Properties
ctx
AudioContext
default:BB.Audio.context
the Audio Context this derived from
dry
Number
default:0
the dry channel gain/volume
fgain
Number
fgain value when type is 'filter'
frequency
Number
frequency value when type is 'filter'
gain
GainNode
private
the "output" gain node ( use .volume, .setGain() to interface with this )
Q
Number
Q value when type is 'filter'
time
Number
default:0
the delay time (up to max specified)
volume
Number
default:1
the master volume (of output gain node)
wet
Number
default:1
the wet channel gain/volume
Methods
calcFrequencyResponse
(
-
length
)
-
length
"filter" type only.
calculate the frequency response for a length-specified list of audible frequencies ( can be used to draw a curve representing the filter )
Parameters:
-
length
Numberthe length of the frequency/response arrays
view basic BB.AudioFX 'filter' example
var freqRes = filt.calcFrequencyResponse( canvas.width );
// maths via: http://webaudioapi.com/samples/frequency-response/
var dbScale = Math.round(canvas.height/4);
var dbScale2 = Math.round(canvas.height/12.5);
var pixelsPerDb = (0.5 * canvas.height) / dbScale;
ctx.beginPath();
for (var i = 0; i < canvas.width; ++i) {
var mr = freqRes.magResponse[i];
var dbResponse = dbScale2 * Math.log(mr) / Math.LN10;
var x = i;
var y = (0.5 * canvas.height) - pixelsPerDb * dbResponse;
if ( i == 0 ) ctx.moveTo( x, y );
else ctx.lineTo( x, y );
}
ctx.stroke();
connect
(
-
destination
-
output
-
input
)
-
destination
-
output
-
input
connects the Noise to a particular AudioNode or AudioDestinationNode
Parameters:
-
destination
AudioNodethe AudioNode or AudioDestinationNode to connect to
-
output
Numberwhich output of the the Noise do you want to connect to the destination
-
input
Numberwhich input of the destination you want to connect the Noise to
Example:
BB.Audio.init();
var node = new BB.AudioBase({
volume: 0.75,
});
node.connect( exampleNode );
// connected to both default BB.Audio.context && exampleNode
// so if exampleNode is also connected to BB.Audio.context by default,
// ...then you've got node connected to BB.Audio.context twice
...which looks like this ( where the first Gain is the Noise and the second is the exampleNode )
disconnect
(
-
destination
-
output
-
input
)
-
destination
-
output
-
input
diconnects the Noise from the node it's connected to
Parameters:
-
destination
AudioNodewhat it's connected to
-
output
Numberthe particular output number
-
input
Numberthe particular input number
BB.Audio.init();
var node = new BB.AudioBase({
volume: 0.75,
});
node.disconnect(); // disconnected from default BB.Audio.context
node.connect( exampleNode ); // connected to exampleNode only
...which looks like this ( where the first Gain is the node and the second is the exampleNode )
setDryGain
(
-
num
-
ramp
)
-
num
-
ramp
set's the dry gain ( && adjust the wet gain accordingly, so that they total to 1 )
Parameters:
-
num
Numbera float value, 1 being the default volume, below 1 decreses the volume, above one pushes the gain
-
ramp
Numbervalue in seconds for how quickly/slowly to ramp to the new value (num) specified
Example:
BB.Audio.init();
var fx = new BB.AudioFX('filter');
var noise = new BB.AudioNoise({
connect: fx
});
fx.setDryGain( 0.75, 2 ); // raises dry level from 0 - 0.75 over 2 seconds (wet level drops to 0.25)
// if no ramp value is needed, you could alternatively do
fx.dry.volume = 0.75; // immediately jumps to 0.75 (and wet to 0.25)
setGain
(
-
num
-
ramp
)
-
num
-
ramp
sets the gain level of the node ( in a sense, master volume control )
Parameters:
-
num
Numbera float value, 1 being the default volume, below 1 decreses the volume, above one pushes the gain
-
ramp
Numbervalue in seconds for how quickly/slowly to ramp to the new value (num) specified
Example:
BB.Audio.init();
var node = new BB.AudioBase({
volume: 0.75
});
node.setGain( 0.25, 2 ); // lower's volume from 0.75 to 0.25 in 2 seconds
// if no ramp value is needed, you could alternatively do
node.volume = 0.5; // immediately jumps from 0.25 to 0.5
setWetGain
(
-
num
-
ramp
)
-
num
-
ramp
set's the wet gain ( && adjust the dry gain accordingly, so that they total to 1 )
Parameters:
-
num
Numbera float value, 1 being the default volume, below 1 decreses the volume, above one pushes the gain
-
ramp
Numbervalue in seconds for how quickly/slowly to ramp to the new value (num) specified
Example:
BB.Audio.init();
var fx = new BB.AudioFX('filter');
var noise = new BB.AudioNoise({
connect: fx
});
fx.setWetGain( 0.15, 2 ); // drops wet level from 1 - 0.15 over 2 seconds (dry level rises to 0.85)
// if no ramp value is needed, you could alternatively do
fx.wet.volume = 0.15; // immediately jumps to 0.15 (and dry to 0.85)
useImpuse
(
-
index
)
-
index
"reverb" type only.
when using impulse files, you can use this method to switch between the different files initially loaded in the 'paths' when the AudioFX 'reverb' was instantiated
Parameters:
-
index
Numberof impulse.buffers to be used
view basic BB.AudioFX 'reverb' example
var reverb = new BB.AudioFX('reverb',{
paths:[
'audio/giant_hall.wav',
'audio/small_room.wav',
'audio/telephone.wav'
]
});
// by default 'audio/giant_hall.wav' (or reverb.impulse.buffers[0]) is used
// below we switch to small_room.wav (or reverb.impulse.buffers[1])
reverb.useImpulse(1);