Time for action – creating AudioSequencer
Let's create our AudioSequencer
object in a file called audioSequencer.js
. We'll start by defining a few variables:
function AudioSequencer() { var _events = [], _playbackRate = 1, _playing = false, eventHandler = undefined, timeoutID = 0;
First we define an _events
array to hold all of the music events to play. The _playbackRate
variable controls how fast the song plays. A value of 1
will be at normal speed, less than 1
slower, and more than 1
faster. The _playing
variable is set to true
while a song is playing. eventHandler
will be set to a function that gets called when an event is fired, and timeoutID
will contain the handle returned from setTimeout()
in case the user stops the game and we need to cancel the timeout.
Now let's define some public property methods. The first is events()
. It is used to get or set the
_events
array:
this.events = function(newEvents) { if (newEvents...