BB.AudioSequencer
The Web Audio API exposes access to the audio subsystem’s hardware clock ( the “audio clock” via .currentTime ). This is used for precisely scheduling parameters and events, much more precise than the JavaScript clock ( ie. Date.now(), setTimeout() ). However, once scheduled audio parameters and events can not be modified ( ex. you can’t change the tempo or pitch when something has already been scheduled... even if it hasn't started playing ). the BB.AudioSequencer is a collaboration between the audio clock and JavaScript clock based on Chris Wilson’s article, A Tale of Two Clocks - Scheduling Web Audio with Precision which solves this problem.
Constructor
BB.AudioSequencer
(
-
config
)
-
config
Parameters:
-
config
ObjectA config object to initialize the Sequencer, use keys "whole", "half", "quarter", "sixth", "eighth" and "sixteenth" to schedule events at those times in a measure additional (optional) config parameters include:
{
multitrack: false, // play only once sample at a given beat
noteResolution: 1, // play only 8th notes (see below)
scheduleAheadTime: 0.2 // schedule 200ms ahead (see below)
tempo: 150, // 150 beats per minute
}
Example:
the BB.AudioSequencer only handles scheduling ( it doesn't create any AudioNodes ), but it does require a BB.Audio.context because it uses the context.currentTime to property schedule events
BB.Audio.init();
// create AudioSequencer ( with optional parameters )
// assuming drum is an instanceof BB.AudioSampler
var track = new BB.AudioSequencer({
tempo: 140, // in bpm
whole: function( time ){
drum.play('kick', time );
},
quarter: function( time ){
drum.play('snare', time );
},
sixteenth: function( time ){
drum.play('hat', time );
}
});
view basic BB.AudioSequencer example
Properties
bars
Number
default:1
how many measures per sequence
current16thNote
Number
returns the current note
currentBar
Number
current measure being played
isPlaying
Boolean
default:false
whether or not sequencer is playing
multitrack
Boolean
default:true
whether or not to play more than one sample at a given beat
noteResolution
Number
default:0
0: play all 16th notes, 1: play only 8th notes, 2: play only quarter notes
scheduleAheadTime
Number
default:0.1
how far ahead to schedule the audio (seconds), adjust for sweet spot ( smaller the better/tighter, but the buggier/more demanding)
tempo
Number
default:120
tempo in beats per minute
Methods
nextNote
()
protected
advance current note and time by a 16th note
scheduleNote
()
protected
schedules appropriate note based on noteResolution && beatNumber ( ie current16thNote )
toggle
(
-
[type]
)
-
[type]
toggles play/stop or play/pause
Parameters:
-
[type]
String optionaltoggles play/pause instead of default play/stop
Example:
// toggles start/stop (ie. starts from beginning each time)
track.toggle();
// toggles play/pause (ie. starts from where last puased )
track.toggle("pause");
update
()
advances to the next note ( when it's time )
Example:
// in update loop
if(track.isPlaying) track.update();