Articles Archive
Articles Search
Director Wiki
 

Lengthening sounds with internal loops

May 23, 2000
by Gary Rosenzweig

One problem that developers faced before Director 8 was the inability to do anything fancy with sounds. A problem that I always faced was that sounds had to be a fixed length. So something like a rocket engine or ray gun had to have a well-defined time period.

One way we got around this was to use a looping sound, then fade in and then out at the proper time. However, sound designers hate this because they can't have a nice start and end to the sound.

Director 8's new Sound Lingo allows you to set loop points in a sound, and then the number of loops to perform. So, for instance, if a sound is 8 seconds long, you can set it to play the first 2 seconds, loop between second 2 and 6, and then play the last 2 seconds. If you design the sound right, you can then have variable-length sounds. The first section begins the sound, the middle section repeats according to the length of the sound, and the final section ends the sound.

To play a sound normally, you can use syntax like this:

on mouseDown me
  sound(1).play(member("sample"))
end

However, to add modifications like internal loops, you need to pass a property list in. Here is a script that will loop the middle section of a sound 3 times:

on mouseDown me
  sound(1).play([#member: member("sample"), #loopStartTime: 721, #loopEndTime: 1221, #loopCount: 3])
end

This script starts the loop at .721 seconds, ends it at 1.221 seconds, and loops this section 3 times. When the sound plays, it begins by playing from 0.0 to 0.721 seconds, then loops between .721 and 1.221 seconds three times, and then plays the remainder of the loop.

So, by using the same sound member, we can get a sound of a different length. We can even make the length of the sound depend on the user's actions. Here is a script that starts the sound, loops the middle section until the user lifts up the mouse button, and then finishes the sound.

on mouseDown me
  sound(1).play([#member: member("sample"), #loopStartTime: 721, #loopEndTime: 1221, #loopCount: 0])
end

on mouseUp me
  sound(1).breakLoop()
end

on mouseUpOutside me
  mouseUp(me)
end

Note that the #loopCount is set to 0. This tells the sound object to loop the section forever. Then, we use the breakLoop() command to tell the sound to finish its current loop and then continue on to play the rest of the sound.

Try out these three scripts. Keep in mind that the real magic here is that they all use the same sound member. In Director 7, you would have had to use two different sound members for the first two buttons, and the third button would have just been plain impossible.

Director 8 download for Mac or Windows.

Since this Sound Lingo is new, it still has some bugs. When I tried this same script on a sound half the length of the one I used in the movie above, it didn't work. The breakLoop() command simply halted the sound. I have a feeling that it has trouble dealing with very small sounds.

Gary Rosenzweig's latest book is "Advanced Lingo for Games." In it, you can find the source code for more than 20 complete games. More information about the book can be found at http://clevermedia.com/resources/bookstore/book4.html. It can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.