BB.AudioTone
A module for creating oscillating tones ( periodic wave forms ) along with music theory utilities
Index
Methods
- BB.AudioTone constructor
- connect
- createScale
- disconnect
- freq
- makeChord
- makeNote
- note
- noteOff
- noteOn
- setGain
Constructor
BB.AudioTone
(
-
config
)
-
config
Parameters:
-
config
ObjectA config object to initialize the Tone, 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)
type: "custom", // "sine", "square", "sawtooth", "triangle", "custom"
wave: [ 0, 1, 0, 0.5, 0, 0.25, 0, 0.125 ] // only for "custom" type
}
see the waveshaper for an example of how the wave property works. the "wave" property abstracts the WebAudio API's createPeriodicWave method a bit. the Array passed above starts with 0 followed by the fundamental's amplitude value, followed by how ever many subsequent harmonics you choose. Alternatively, you can also pass an object with a "real" and "imag" (imaginary) Float32Array, for example:
{
context: BB.Audio.context[2], // choose specific context
type: "custom",
wave: {
imag: new Float32Array([0,1,0,0.5,0,0.25,0,0.125]),
real: new Float32Array(8)
}
}
Example:
in the example below instantiating the BB.AudioTone creates a GainNode ( essentially the Tone's output ) connected to the default BB.Audio.context ( ie. AudioDestination )
everytime an individual tone is played, for example: O.makeNote("C#")
, a corresponding OscillatorNode with it's own GainNode is created and connected to the Tone's master GainNode ( the image below is an example of the graph when two notes are played )
BB.Audio.init();
var O = new BB.AudioTone({
volume: 0.75,
type: "square"
});
O.makeNote( 440 );
O.makeNote( 880 );
view basic BB.AudioTone example
Properties
ctx
AudioContext
default:BB.Audio.context
the Audio Context this derived from
gain
GainNode
private
the "output" gain node ( use .volume, .setGain() to interface with this )
type
String
the type of wave
volume
Number
default:1
the master volume (of output gain node)
wave
Object
default:null
the wave Array/Object
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 )
createScale
(
-
scale
-
root
-
[tuning]
)
Array
-
scale
-
root
-
[tuning]
generates a musical scale ( array of related frequency values ) from a root note.
the notes are stored in an array in the .scales
property (
object ) and can be accessed by querying the key ( name ) of the scale type
you generated like so: .scales.major
, which
will return an array of notes ( frequency number values in Hz )
Parameters:
-
scale
Stringname of the scale type you want to generate. can be either major, naturalminor, harmonicminor, melodicminor, majorpentatonic, minorpentatonic, blues, minorblues, majorblues, augmented, diminished, phrygiandominant, dorian, phrygian, lydian, mixolydian, locrian, jazzmelodicminor, dorianb2, lydianaugmented, lydianb7, mixolydianb13, locrian#2, superlocrian, wholehalfdiminished, halwholediminished, enigmatic, doubleharmonic, hungarianminor, persian, arabian, japanese, egyptian, hirajoshi, nickfunk1, nickfunk2
-
root
Numberthe fundamental/root note of the scale ( the "A" in an "A major" ) can be either a number (frequency in Hz) or a note string ( "A", "A#", "B", etc )
-
[tuning]
String optionalthe kind of tuning (temperment) you would like to use to derive the scale with, can be either "equal" (default) or "just"
Returns: Array
the newly created array
Example:
BB.Audio.init();
var O = new BB.AudioTone({ volume: 0.5 });
function scheduler( freq, tuning, timeout ) {
return function() {
setTimeout(function(){
O.makeNote({
type: "sine",
tuning: tuning,
frequency: freq,
duration: 0.25,
attack: 0.1,
release:0.5
});
},timeout);
}
}
function playScale( scale, root, tuning ){
O.createScale( scale, root ); // this line generates the scale
for (var i = 0; i < O.scales[scale].length; i++) {
var freq = O.scales[scale][i];
scheduler( freq, tuning, i*500 )();
};
}
playScale( "major", 220, "just" );
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 )
freq
(
-
root
-
semitones
-
[tuning]
)
Number
-
root
-
semitones
-
[tuning]
utility method which takes a root frequency and a number of semitones and returns the frequency of the "note" the specified number of semitones away from the root frequency
Parameters:
-
root
Numberthe root frequency ( in Hz )
-
semitones
Numberthe number of semitones away from the root you need to know the frequency of ( negative values are lower pitches then root, positive values are higher pitches )
-
[tuning]
String optionalthe kind of temperment to base the transposition on ( eitehr "equal" for equatempered tuning or "just" for pure/harmoniic tuning )
Returns: Number
a frequency value in Hz
Example:
BB.Audio.init();
var O = new BB.AudioTone();
O.makeNote( O.freq( 440, 5 ) ); // plays a D,587.33Hz ( 5 semitones up from A,440Hz )
makeChord
(
-
config
)
-
config
plays (starts and stops) a chord ( based on "just" or "equal" temperments ) from any of the following chord types: maj, min, dim, 7, min7, maj7, sus4, 7sus4, 6, min6, aug, 7-5, 7+5, min7-5, min/maj7, maj7+5, maj7-5, 9, min9, maj9, 7+9, 7-9, 7+9-5, 6/9, 9+5, 9-5, min9-5, 11, min11, 11-9, 13, min13, maj13, add9, minadd9, sus2, 5,
Parameters:
-
config
Objectobject with any of the optional properties listed in the example below
Example:
BB.Audio.init();
var O = new BB.AudioTone({ volume: 0.5 });
O.makeChord({
frequency: 220,
type: "min7", // plays a minor 7th chord
tuning: "just", // ...based on "just" (pure/harmonic) tuning rather the default eqaul tempered tuning
velocity: 0.75, // 75% of 0.5 ( ie. O's master gain )
attack: 1, // fade in for 1 second
sustain: 2, // hold note for 2 more seconds
release: 2, // fade out for 2 more seconds
schedule: BB.Audio.context.currentTime + 5, // play 5 seconds from now
});
makeNote
(
-
config
-
velocity
-
sustain
)
-
config
-
velocity
-
sustain
plays (starts and stops) a tone
Parameters:
-
config
Objectthe first value can either be a config object or a note ( in either Hz or string format )
-
velocity
Numberthe gain value ( relative to the Tone's master valume ) of this particular note
-
sustain
Numberthe amount of time ( in seconds ) to sustain ( play ) the note
Example:
BB.Audio.init();
var O = new BB.AudioTone({ volume: 0.5 });
O.makeNote("A"); // plays a 440 Hz sine wave
O.makeNote( 444, 0.5, 3 ); // plays a 444 Hz sine wave at half volume for three seconds
O.makeNote({
frequency: 220,
velocity: 0.75, // 75% of 0.5 ( ie. O's master gain )
attack: 1, // fade in for 1 second
sustain: 2, // hold note for 2 more seconds
release: 2, // fade out for 2 more seconds
schedule: BB.Audio.context.currentTime + 5, // play 5 seconds from now
});
note
(
-
root
-
octave
-
[frequency]
)
Number
-
root
-
octave
-
[frequency]
utility method which takes a root note (String) and a number of semitones and returns the frequency of the "note" the specified number of semitones away from the root note
Parameters:
-
root
Numberthe root note ( "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" where "A" is 440 )
-
octave
Numberthe number of octaves away from the root you need to know the frequency of ( negative values are lower pitches then root, positive values are higher pitches )
-
[frequency]
Number optionalalternative "A" frequency ( overrides default of 440 )
Returns: Number
a frequency value in Hz
Example:
BB.Audio.init();
var O = new BB.AudioTone();
O.makeNote( O.note( "A", 1 ) ); // plays an A,880Hz ( 1 octave up from A,440Hz )
noteOff
(
-
frequency
-
[release]
)
-
frequency
-
[release]
stops playing a tone
Parameters:
-
frequency
Numbera value in Hz of the note you want to stop playing
-
[release]
Number optionalthe time it takes for the note to fade out ( in seconds, default 0 )
Example:
BB.Audio.init();
var O = new BB.AudioTone({ volume: 0.5 });
O.noteOn( 440, 1, 0.5 );
setTimeout(function(){ O.noteOff(440); },1000);
noteOn
(
-
frequency
-
[velocity]
-
[attack]
-
[schedule]
)
-
frequency
-
[velocity]
-
[attack]
-
[schedule]
starts playing a tone
Parameters:
-
frequency
Numbera value in Hz of the frequency you want to play
-
[velocity]
Number optionalthe volume/gain relative to the master volume/gain of this particular note ( default 1 )
-
[attack]
Number optionalthe time it takes for the note to fade in ( in seconds, default 0.05 )
-
[schedule]
Number optionalif you want to schedule the note for later ( rather than play immediately ), in seconds ( relative to BB.Audio.context.currentTime )
Example:
BB.Audio.init();
var O = new BB.AudioTone({ volume: 0.5 });
O.ntoeOn( 440, 1, 0.5 ); // will stay on, until noteOff(440) is executed
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