BB.AudioNoise
A module for creating differnt kinds of noise ( defined as any nonconventional and/or mathematically calculated sound buffer )
Index
Methods
- BB.AudioNoise constructor
- connect
- disconnect
- makeNoise
- noiseOff
- noiseOn
- setGain
Constructor
BB.AudioNoise
(
-
config
)
-
config
Parameters:
-
config
Objectthis can either be a string (type of noise "white","pink","brown","perlin" ), or a custom function ( see example further down ) or a config object to initialize the Noise can contain the following:
{
context: BB.Audio.context[2], // choose specific context
connect: fft, // overide default destination
volume: 0.5, // technically master "gain" (expolential multiplier)
duraton: 2, // in seconds (corresponds to length of buffer)
channels: 1, // channels in buffer
type: "pink", // "white","pink","brown","perlin" or custom function
}
Example:
in the example below instantiating the BB.AudioNoise creates a GainNode ( essentially the Noise's output ) connected to the default BB.Audio.context ( ie. AudioDestination )
everytime noise is played, for example: white.makeNoise()
, a corresponding SourceNode with the appropriate sound buffer (the initialized noise data) is created and connected to the Noise's master GainNode
BB.Audio.init();
var white = new BB.AudioNoise('white');
white.makeNoise();// plays noise
var now = BB.Audio.context.currentTime;
white.makeNoise( 8, 0.5, now+3 ); // plays noise for 8 seconds, at half volume, 3 seconds from now
// example with config object
var brown = new BB.AudioNoise({
volume: 0.75,
type: "brown"
});
// example with custom function type
var noisey = new BB.AudioNoise(function(){
var frameCount = this.ctx.sampleRate * this.duration;
for (var ch = 0; ch < this.channels; ch++) {
var data = this.buffer.getChannelData( ch );
for (var i = 0; i < frameCount; i++) {
data[i] = Math.random() * 2 - 1;
};
};
});
view basic BB.AudioNoise example
Properties
buffer
AudioBuffer
the buffer object
channels
Number
default:2
number of channels
ctx
AudioContext
default:BB.Audio.context
the Audio Context this derived from
duration
Number
default:1
buffer length in seconds
gain
GainNode
private
the "output" gain node ( use .volume, .setGain() to interface with this )
node
AudioNode
the source node
type
String
default:"white"
"white","pink","brown" or "custom"
volume
Number
default:1
the master volume (of output gain node)
Methods
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 )
makeNoise
(
-
[duration]
-
[velocity]
-
[schedule]
)
-
[duration]
-
[velocity]
-
[schedule]
plays noise for a specified period of time
Parameters:
-
[duration]
Number optionalhow long to play the noise (in seconds)
-
[velocity]
Number optionalhow loud (scalar value applied to master volume)
-
[schedule]
Number optionalwhen to play it
Example:
BB.Audio.init();
var whitenoise = new BB.AudioNoise({
type: "white",
volume: 0.75
});
// plays for a quarter second, at twice the initial 0.75 volume ( ie. 1.5 )
whitenoise.makeNoise( 0.25, 2 );
// plays for 1 second at the original volume (0.75), will start playing 5 seconds from now
var now = BB.Audio.context.currentTime;
whitenoise.makeNoise( 1, 1, now+5 );
noiseOff
()
stops playing the noise
Example:
BB.Audio.init();
var whitenoise = new BB.AudioNoise(); // default white noise
whitenoise.noiseOn();
whitenoise.noiseOff();
noiseOn
(
-
[schedule]
)
-
[schedule]
starts playing the noise
Parameters:
-
[schedule]
Number optionalwhen to play
Example:
BB.Audio.init();
var whitenoise = new BB.AudioNoise(); // default white noise
whitenoise.noiseOn();
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