Random Track Sequencer Effect

This effect is different from others in the book in that it has no interface! There's nothing to see, but plenty to hear. We're aiming to overlay random tracks to create a unique sound?something that's a little different from what a user might normally hear. The composition contains four ten-second loops comprising a random selection from the following categories:

  • Background: a selection of five background bed tracks
  • Drums: a selection of five drum tracks
  • Guitar: a selection of five guitar loops
  • Overlay: a selection of different overlays

As the movie loads, a random selection is made from each category. The tracks are then combined to create one of 625 possible soundtracks.

That's enough of the introduction?let's get cracking! To edit this effect, locate random.fla in the code archive.

Setting the Scene
  1. Create a new Flash document that's 200 pixels wide and 200 pixels high. Modify the frame rate to 24 fps (Modify > Document?).
  2. Rename Layer1 as Actions.
  3. Select File > Import > Import to Library? and select the following MP3 files from the code archive: Background1.mp3, Background2.mp3, Background3.mp3, Background4.mp3, Background5.mp3, Drum1.mp3, Drum2.mp3, Drum3.mp3, Drum4.mp3, Drum5.mp3, Guitar1.mp3, Guitar2.mp3, Guitar3.mp3, Guitar4.mp3, Guitar5.mp3, Overlay1.mp3, Overlay2.mp3, Overlay3.mp3, Overlay4.mp3, and Overlay5.mp3.
  4. Select these sound clips within the Library Panel, then right-click and select Linkage?. Check the Export for ActionScript checkbox, accept the provided identifier (remove the .mp3 extension if you're using Flash MX 2004), and click OK.
  5. That's all the setting up we need to do for this example. As we're not adding an interface or allowing any user interaction, no other elements need to be included, apart from the ActionScript to produce the sound composition.

    Adding the ActionScript

  6. Select the first frame of the Actions layer and add the following code to the Actions Panel:
  7. Example 5.8. random.fla Actions : 1

    function randomBetween (a, b)
    {
            return Math.min (a, b) + random (Math.abs (a - b) + 1);
    }
    MovieClip.prototype.PlayQueue = function (clip)
    {
            var orchestra = new Sound (this);
            orchestra.attachSound (clip);
            orchestra.start ();
    };
    function PlayClips ()
    {
            var background = "background" + randomBetween (1, 5);
            var drums = "drum" + randomBetween (1, 5);
            var guitar = "guitar" + randomBetween (1, 5);
            var overlay = "overlay" + randomBetween (1, 5);
            var clips = new Array (background, drums, guitar, overlay);
            for (var i = 0; i < clips.length; i++)
            {
                    var mc = createEmptyMovieClip ("SoundHolder" + i, i);
                    mc.PlayQueue (clips[i]);
            }
    }
    PlayClips ();

     

    That's a fairly concise piece of code. Preview the movie and skip the code walkthrough, or read on for the nitty-gritty details?the choice is yours!

    Let's look at the PlayClips function to see how it all fits together. For each of the tracks, we create a variable (e.g. drums) that we fill with one of the linkage identifiers for the sound clips in the library, using the randomBetween function to generate a random number between one and five. We then combine these variables into an array of sound clips:

     

    var clips = new Array (background, drums, guitar, overlay);

    From here, we use a for loop to create an empty movie clip for each element of the array. These clips play the selected sound clips at random. This is accomplished by calling the PlayQueue movie clip method (which we'll examine in a moment) and passing the sound clip linkage identifier as a parameter.

     

    for (var i = 0; i < clips.length; i++)
    {
            var mc = createEmptyMovieClip ("SoundHolder" + i, i);
            mc.PlayQueue (clips[i]);
    }

    We call the PlayQueue method for each of the new movie clips created by the PlayClips function. This embeds a sound clip into each of these movie clips, then plays it.

    MovieClip.prototype.PlayQueue = function (clip)
    {
            var orchestra = new Sound (this);
            orchestra.attachSound (clip);
            orchestra.start ();
    };

     

    Because the sound clips are created in quick succession at runtime, the effect is one of a single composite track with a background, plus drum, guitar, and overlay sections.

  8. Save and preview your document.

You may notice that every time you play the movie, you get a different composition. Some work very well, while others sound fairly clunky! It's all about experimentation and seeing what's possible.