Hack 55 Turn Low-Bandwidth Monaural Sounds into Stereo Sounds

figs/beginner.gif figs/hack55.gif

Create stereo sounds without excessive bandwidth by splitting monaural sounds into two channels.

Flash will play any sound as a two-channel signal. It will play a monaural (mono) sound by sending the same signal to both speakers and play a stereo signal by sending the left channel to one speaker and the right channel to the other. You can change the volume of these two sound channels via a sound envelope (accessible via the Edit button in the Properties panel) or via ActionScript (using Sound.setVolume( )). You can also change the percentage of each channel sent to each speaker [Hack #60] using Sound.setPan( ) or Sound.setTransform( ).

To turn a low-bandwidth mono sound into stereo using a sound envelope, start by importing the sound into Flash using FileImportImport to Library. You can tell if the sound file you just imported is mono or stereo by the number of waveforms that appear for the sound file in the Library (one for mono, two for stereo).

In the Library, right-click (Windows) or figs/command.gif-click (Mac) on the sound file you imported, and select Properties from the pop-up menu. The Sound Properties dialog box appears. Change the Compression option to MP3, as shown in Figure 7-9.

Figure 7-9. Sound export settings in the Sound Properties dialog box

You should almost always choose MP3 compression because it gives by far the best filesize versus sound fidelity trade-off. The main disadvantage of MP3 format is that it requires decompression at runtime, which can tax slower computers. If bandwidth is not an issue (i.e., for offline applications) and you're supporting slower computers, you might use RAW format because it requires no runtime decompression and therefore uses less processing power.

You can export the sound as either stereo or mono. When using MP3 compression above 20 Kbps, choose stereo because MP3 compresses stereo signal information well. If you drop the bit rate below 20 Kbps, the stereo option isn't supported. Low bit rates are suitable for most UI sounds and can reduce the total download size of your sound assets to nearly zero, but it would still be nice to have stereo while being able to select 16 Kbps.

You can obtain stereo at low bit rates by adding an envelope. Select a keyframe in the timeline. In the Properties panel, select a sound file from the Sound drop-down list. Make sure the Sync parameters are set to Event, Repeat, and 1, as shown in Figure 7-10.

Figure 7-10. Sound sync settings

Click the Edit button to bring up the Edit Envelope window shown in Figure 7-11.

Figure 7-11. The Edit Envelope window

The two panels' waveforms in the window represent the two sound channels, and the graph lines represent volume. If the two channels have the same volume envelope shape (which is the default), a mono sound will be played through your speakers as the original mono sound. By making the two graphs dissimilar, the volume in each channel is different from the other channel. This creates a stereo output because the signal to each speaker is now unique.

The sound envelope supports up to eight control points (the draggable squares). To create a new control point, click on any part of the envelope that does not already have a control point. To delete a control point, drag it off the pane.

Many sites use ActionScript to control their sound rather than using timeline-attached sound. To use the previous technique with sounds attached at runtime with MovieClip.attachSound( ), create a sound-holder movie clip whose keyframes have the sounds attached to them. For each keyframe with sound attached to it, also place a stop( ) action on the same frame. Also place a stop( ) action on the first frame, as shown in Figure 7-12.

Figure 7-12. A sound-holder movie clip and stop( ) actions in the timeline

The ActionScript to control your sound should look something like this (assuming you've set the sound-holder clip's instance name to soundHolder using the Properties panel):

mySound = new Sound(soundHolder);  // Define the Sound object

soundHolder.gotoAndPlay("sound1"); // Start the sound

mySound.stop( );                    // Stop the sound

Using volume envelopes on a sound file allows you to turn a single mono sound into several stereo sounds. Rather than using several lower-quality UI sounds, you can use one high-quality UI sound but use volume envelopes to turn the sound into several different stereo sounds.