Browse DevX
Sign up for e-mail newsletters from DevX


Create Sound Synchronization Magic in Flash, Part 2 : Page 2

Remember the speaker cabinets from the '70s, with lights that throbbed to the music? Using a powerful third-party utility for Flash, you can create games and animations that use rapid audio synchronization to drive assets with sound instead of programming.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Input File Formats
FlashAmp supports compressed or uncompressed AIFF and WAV files, as well as SWA (Shockwave Audio) and MP3 files. Both 16- and 8-bit sound files are supported, as well as the most common sample rates: 44.100, 22.050, 11.025, and 7.418 kHz. If you are using MP3 files, they must be encoded using Constant Bit Rate encoding (CBR), and stereo files must use normal stereo mode. Neither Variable Bit Rate encoding (VBR) nor Joint Stereo formats are supported at this time.

Output File Formats
FlashAmp can output data in three ways. First, it can display the data within FlashAmp so you can copy it and paste it into Flash. This is handy if your sound and data are both internal within Flash. Second, it can output the data as a text file formatted for the loadVariables() ActionScript method. This method loads data as name/value pairs of text strings, so FlashAmp will format its output this way to make it compatible. Within your scripts, you can then process the data and convert the strings into usable data types. This is useful when you wish to dynamically load data from a remote server. Third, FlashAmp can output the data as a text file formatted for the #include ActionScript compiler directive. This is the best way to handle external data and/or sounds when you do not need to load data from a remote server. The file is, essentially, an external ActionScript file, so it is easy to read, easy to update, and doesn't clutter up your Flash scripting panel with code you are not likely to review repeatedly.

Internal vs. External Sounds
As you can see by the output file format information, FlashAmp data is compatible with the use of internal or external sounds in your Flash file. Internal sounds can appear in a timeline, or be used from the library using the Flash sound object and the attachSound() method. External MP3 files can be loaded using the Flash sound object and the loadSound() method. Each approach has its pros and cons, so I'll discuss them briefly with some code examples.

Internal Sounds, Using the Timeline
This is the old school approach of simply importing a sound and adding it to a timeline frame span using the property inspector. Making certain the sound is set to Stream, sync will be maintained and you can use the timeline's _currentframe property to reference the FlashAmp array's correct index. Remembering that ActionScript uses a zero-based array, the syntax we're familiar with from our previous discussions is based on a sound starting in frame 1. Looking at this sample line of script, you see we take the current frame of 1, subtract 1, and end up with array index 0, as we should.

this._alpha = amplitude[_root._currentframe-1]

This same technique can be used if you don't start the sound in frame 1. For example, if you start the sound in frame 20, the script would look like this:

this._alpha = amplitude[_root._currentframe-20]

The benefits of using internal timeline sounds are that everything is internal (making it easier to distribute files without concerns about pathnames), and you can start and stop the sound without losing sync. If you place the sound in the root timeline, the Stream setting will allow the sound to play during download. (You can still use a timeline in a MovieClip, but the MovieClip will need to download fully before the sound can begin.) The disadvantages of using timeline-based sounds are that keeping everything internal makes it more difficult to make changes to the sound (the sound must be updated or re-imported), you can't switch sounds dynamically, and you can't use sound object properties such as volume and pan.

Internal Sounds, Using the Flash Sound Object
This approach involves attaching an imported sound to a Flash sound object using the attachSound() method and its Linkage name, and starting the sound playback when you need it. You can use a _currentframe property or a variable to walk through the FlashAmp array.

The benefits of using the sound object with internal sounds are that you have full access to the sound object properties (such as volume and pan) and, as long as other sounds with Linkage names exist in the file's Library, you can dynamically switch sounds. The disadvantages include having to update or re-import sounds if edits are made, but also that sounds controlled by the sound object must fully download before playing.

External Sounds, Using the Flash Sound Object
Using external MP3 sounds requires essentially the same Flash approach as using internal sounds with the Sound object, but it's even simpler. Instead of importing the sound and assigning a Linkage name, you simply use the loadSound() method and supply a pathname to the file. Everything else remains the same on the sound playback side, including access to volume and pan through the Sound object. However, there's one wrinkle when using the FlashAmp array.

Because the sound is external, you'll need to use the position property of the Sound object the way you normally use a timeline's current frame. Sound.position is measured in milliseconds, so the goal is to arrive at a frame rate equivalent value. This is accomplished by dividing the current Sound.position millisecond value by 1000, and multiplying it by your frame rate.

For example, using a 12 fps file and a sound's elapsed time of 1 second (when the Sound.position value would be 1000), the equation would be 1000/1000*12. This equals 12, which is where you should be at one second in a 12 fps movie. Using a fraction of a second, however, will yield a decimal value. At one-tenth of a second, the equation would be (100/1000) * 12, which would yield 1.2. However, you don't typically want decimal values for these situations, so round the number to an integer. It helps to round down, when dealing with sound values, because that way you're always sure to get zero values during silences. So, instead of using Math.round(), use Math.floor(), which rounds down to the nearest integer even if the value to the right of the decimal point is .5 or higher. An entire script might look like this:

onClipEvent (load) { // sets up a sound object faTrack = new Sound(); // load the external sound faTrack.loadSound("mySound.mp3", true); // play the sound faTrack.start(); } onClipEvent (enterFrame) { // get the current frame equivalent of an external // sound by dividing the current playback position // of the sound by 1000, multiply by Flash frame // rate, and round down myFrame = Math.floor((this.faTrack.position/1000)*fps) // retrieve corresponding value from the FlashAmp array this._alpha = amplitude[myFrame] };

The benefits of using external sounds include access to the Sound object properties and methods, a greater ease in switching sounds on the fly, and the convenience of simply replacing the sound file if edits are required. The downside is that Flash has some problems tracking the sound position of external sounds if you stop and start playback. Sync will be maintained if the sound is played straight through, but if the sound is stopped and started again from the beginning, Flash will not automatically reset the Sound.position value and you will not be able to maintain sync with the FlashAmp array. The workaround is to simply void the Sound object when the sound is stopped and create a new Sound object when you want to play the sound again.

Much, Much More
There are many more creative, impressive examples of FlashAmp projects on the Marmalade Multimedia Web site. The Related Resources section of this article (see left column) includes links to these examples, as well other sources of additional information. Whether you are an artist or a programmer, I hope this look at Flash sound synchronization has inspired you to use sound in creative ways. Send me links to your efforts!

Rich Shupe is president of FMA, a full-service multimedia development and training facility based in New York. He is a regular Flash columnist for DevX and coauthor of the upcoming "Flash 8: Projects for Learning Animation and Interactivity" and the upcoming "Learning ActionScript 3.0," (both published by O'Reilly). FMA develops web and disc-based products for clients including McGraw-Hill, Phillips, 20th Century Fox, and Nickelodeon, and trains digital media professionals in Flash, ActionScript, HTML, JavaScript, and more.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date