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 ...

Get HTML5 Web Application Development By Example Beginner's guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.