Create a Flash 5 Remixer

What You Need: You must have the Flash 5 authoring application, the flash 5 player and the included sound tracks to complete this tutorial.

by Steve Hayden Porter
URL:
http://archive.devx.com/dhtml/articles/hp072701/hp072701-1.asp

Flash 5 is more than a movie and sound player—it's an application platform. Using a little ingenuity and some ActionScript, you can build a fully-functioning remixer that runs in any Flash-enabled browser A "remixer" is an audio application that allows users to control the volume and pan (the right or left position of sound) of a multi-track recording. A remixer typically has a user interface similar to an audio mixer console, which includes volume sliders, pan sliders, and mute buttons (see figure 1). Users can rearrange or "remix" audio tracks by changing the volume and pan of different tracks at different points in time.


Figure 1. Example of a typical Audio Mixer Console

Try your hand at remixing with the demo Flash remixer. Launch remixer

It's now possible to create a remixer using the Flash 5 player, because Flash 5 supports separate volume and pan for up to 8 sounds playing simultaneously. These features and the fact that Flash is ubiquitous, make it an attractive technology for developing audio applications.

In this article, I'll show you how to build a simple Flash remixer. To get the greatest benefit from this article, you should be familiar with the Flash 5 authoring environment, ECMA compliant ActionScript and basic digital audio concepts.

The example remixer console has one "channel strip", containing a volume slider, pan slider and a mute button, for each track in your song and a master control strip for master volume, pan and mute. Sliders and toggle buttons on the strips, control the audio tracks.

Create a Volume Slider
A slider consists of three parts: a button representing the handle, a movie clip to hold the button, and a graphic representing the slider "groove". A user drags the slider up or down its groove to change volume.

To create a slider you must constrain the drag space of a movie clip. A movie clip's registration point, with the cross hair icon typically in the center of the clip, represents its current x and y position on the stage. By passing coordinates to the startDrag(( ) method you define a rectangle within which a user can drag the movie clip.

The constrained area of your volume slider will be a vertical line 1 pixel wide and 100 pixels high, which corresponds to the Flash player's volume range of 0 - 100. The default position of your slider is the top of the slider "groove" representing maximum volume. I am using the default value of 100 to simplify the slider building process. Later on, when you are more comfortable with the process you can change the default setting of your volume slider.


Figure 2. Constrained Drag Space for Volume Slider

1. Create a button representing your slider handle and place it within movie clip.

2. Add the following ActionScript to the button:

   on (press) {
	   startDrag("", false, left, top, right, bottom);
	   dragging = true;
   }

   on(release, releaseOutside) {
	   stopDrag( );
	   dragging = false;
   }

3.	Select the movie clip and add the following Actions:

   onClipEvent(load) {
	   top = Math.round(_y); // default position
	   left = Math.round(_x);
	   right = left; // prevent horizontal drag
	   bottom = top + 100 //100 pixels below top
   }
4. Place the slider movie clip on the stage and name it for its corresponding audio track, such as 'drumVolSlider".

5. Create a slider "groove" graphic on the main timeline and align the top of the slider movie clip with the top of the groove. The groove should be 100 pixels high plus the height of your slider handle to compensate for the slider's centered registration point.

Create a Pan Slider
A user drags the slider to the right or left to change pan. The default position of your pan slider is the middle of the groove, representing the center point between left and right speakers.

Create a slider following the previous steps, however in Step 3 use the following ActionScript code:

   onClipEvent(load) {
	   top = Math.round(_y);
	   left = Math.round(_x) - 10; // default handle position
	    is middle of 20 px slider range
   right = Math.round(_x) + 10; // 
   bottom = top; // prevent vertical drag.
   center = Math.round(_x); // default position
   }
I suggest constraining the drag space of your pan slider to a width of 20 pixels so it will easily fit within your channel strip layout. For step 5, your slider groove graphic should be the length of the drag space, 20 pixels, plus the width of the slider handle.


Figure 3. Constrained Drag Space for Pan Slider

Create a Mute Toggle Button
The final element in your channel strip is the mute toggle button. When "muted" a track is silenced regardless of changes to the volume slider, when "unmuted" volume control returns to the volume slider.

Start your remixer with all tracks but one muted so users can build a song. For buttons that will be "muted" at the beginning, frame one of your movie clip represents the "muted" state.

1. Create a button with the following ActionScript attached:

   on(release) {
	   play( );  // change frames when clicked
   }
2. Create a two-frame movie clip with three layers: button, mute graphic and actions.

3. Place the button in frames one and two of the button layer.

4. Create the "muted" state graphic and place it in frame one only of the "mute graphic" layer.

5. Add the following ActionScript code to frame one of the actions layer:

6.
   stop( ); 
   muted = true; 
7. Add similar ActionScript code to frame two:

8.
   stop( ); 
   muted = false;
9. Place the mute button movie clip on the stage and name it for its corresponding audio track, such as 'drumMuteBtn".


Figure 4. Mute Toggle Button defaults to "muted" state

For buttons that will be "unmuted" at the beginning, frame two of your movie clip represents the "muted" state. To make the default state of a mute button "unmuted" you must move the mute graphic to frame two and switch the ActionScript from steps 5 and 6.

Build your Remixer Console
First, you will create a channel strip for each audio track. To assemble a channel strip, place the volume slider on top, the pan slider in the middle and the mute button on the bottom. Place each channel strip side by side using the audio mixer screen shot (see figure 1) as an example. The master control strip should be furthest to the right.

You must also create a "transport", which is a set of controls consisting of play and stop buttons and a dynamic text box, with the variable name "mixerstate", that shows the remixer's play state or load progress. Later, you will add the code to play and stop your remixer and display its state.

Finally you should include a title, credits and copyright statement. Place your transport controls and title information on the main timeline.

Add Audio Tracks
After designing the console, you are ready to add audio tracks. Download the sample tracks in WAVE (Windows) or AIFF (MacOS) formats. For each audio track, you will create a two-frame movie clip with a stop( ) action in frame one that prevents the clip from starting automatically, and a sound in frame two. Set the sound to "event" sync and loop = 999 in the Sound Panel (see Figure 5).


Figure 5. Structure of Audio Track Movie Clip

Next, you will create a movie clip named "sounds" to aid simultaneous playback of your tracks. The sounds movie clip contains your audio tracks, a silent stream sound to improve synchronization and ActionScript code to play your audio tracks simultaneously.

1) Attach each audio movie clip to frame one of the sounds movie clip.

2) Name each movie clip according to the audio track it contains, such as "drumtrackmc".

3) Attach a stop action in frame one that prevents the clip from starting automatically.

4) Attach a sound to frame two of the sounds movie clip.

5) Select the sound and open the Sound Edit window. Drag the envelope handles of both channels to zero and minimize the wave form length by dragging the Time Out Control to the left. (see Figure 6).


Figure 6. Set Volumes to 0 and Minimize waveform in Sound Edit Window


Figure 7. Set sound to stream sync

6) Loop the "silent" stream sound so that the wave form appears on the timeline from frame two through frame four. (see figure 7).
A silent stream sound requires the Flash player to maintain a constant frame rate, which helps syncronization. In frame three attach the ActionScript below.
   // loop through all movie clip objects attached to this movie clip
   for(name in this)
   {
		   // play each sound track movie clip
		   this[name].gotoAndPlay(2); 
   }
The preceding code, combined with the "silent" stream sound will play your tracks simultaneously (see Figure 8).


Figure 8. Structure of Sounds Movie Clip

Attach the sounds movie clip to the audio section of the main timeline. Create a loop in frames 6 and 7 to show a message in your "mixerstate" dynamic text box while audio loads. Use the following diagram as an example of your main timeline's structure. (see Figure 9).


Figure 9. Structure of Main Timeline

Attach the following ActionScript to the main timeline in Frame 6:
   // if audio in frame 8 is loaded exit loop.
   ifFrameLoaded(8) { 
   gotoAndPlay("objects");
   mixerstate = "Audio Loaded"; 
   }
Finally, attach the following ActionScript to the main timeline in Frame 7:
   gotoAndPlay(_currentframe - 1 );  // loop
   mixerstate = "Loading Audio"; // show loading message
Flash will loop these frames until all audio tracks are loaded, after which the playhead moves on to the "objects" section.

Wiring Your Console for Sound The final step is to "wire" the console to control audio using Flash 5 Sound Objects. You create one Sound Object for each track in your remixer and a global Sound Object for the master control strip (see Figure 10).


Figure 10. Create Sound Objects in frame 14

1. In the "objects" section, create a variable for each track and assign it to the Sound constructor function. Name the variable according to the audio track it represents.

2. Pass your sound's movie clip target path to the Sound constructor function. This connects your audio track to your Sound Object.

For example, if you have a Sound Object named "drumTrack" that controls the sound, "drumtrack.wav" in the movie clip, "drumtrackmc", you would write:

   mySoundObject = new Sound(target path to movie clip containing a  sound)

   drumTrack = new Sound(_root.sounds.drumtrackmc);
If you do not specify a target path, a Sound Object will control volume and panning for all sounds in the player, which is useful when you want to create a master volume control.
   global = new Sound( );
You use the Sound Object methods in your console elements to change volume and pan. The setVolume( ) method has a range between 0 (silence) and 100 (maximum volume). The setPan( ) method has a range from -100 (left speaker) to 100 (right speaker). The following examples demonstrate the setVolume( ) and setPan( ) methods.
	   mySoundObject.method(value)

   drumTrack.setVolume(0); // mutes drumtrack.wav.
   drumTrack.setVolume(100); // maximum volume for drumtrack.wav.
   global.setVolume(0); // mutes all sound.

   drumTrack.setPan(-100); // drumtrack.wav plays from the left speaker
   drumTrack.setPan(100); // plays from the right speaker
   drumTrack.setPan(0); // plays equally from both speakers
Use the code templates below, to add Sound Object code to your console elements. Replace "mySoundObject" with your own Sound Object and "correspondingmutebtn" or "correspondingslider" with the instance name of the appropriate element in this channel strip. The elements of the master "control" strip will control the "global" Sound Object.

Volume Slider
   onClipEvent(enterFrame) {
	   // convert _y into a number between 0 and 100.
   position = Math.round(100 - (_y - top));  

   // only change volume when user is dragging this movie clip or 
   // corresponding mute button is unselected.
   if (dragging && !_root.correspondingmutebtn.muted) {
		   _root.mySoundObject.setVolume(position);
	   }
   }
Pan Slider
   onClipEvent(enterFrame) {
	   if(dragging){
   // convert _x into a number between -100 and 100.
   position = Math.round((_x - center) * 10);
		   _root. mySoundObject .setPan(position);
	   }
   }
	
Mute Button defaults to muted state
   In frame one: _root.mySoundObject.setVolume(0);
   In frame two: _ root.mySoundObject.setVolume
   (_root.correspondingslider.position);
Mute Button defaults to unmated state
   In frame one: _ root.mySoundObject.setVolume
   (_root.correspondingslider.position);
   In frame two: _root.mySoundObject.setVolume(0);
Play Button
   on(release){
	   // disable the play button if the mixerstate 
	   has the substring "Load" 
   // because audio is loading.
	   if(_root.mixerstate.indexOf("Load") == -1){
			   stopAllSounds( ); // prevent event sync layering
			   sounds.gotoAndPlay(2);
			   mixerstate = "Playing";
		   }
	   }
Stop Button
	   on(release){
   if(_root.mixerstate.indexOf("Load") == -1){
			   stopAllSounds( );
			   mixerstate = "Stopped";
		   }
   }
Finally, in the "ready" section of the main timeline you will and add a stop action and set the "mixerstate" variable to "Ready". This indicates that your remixer is ready for use. You will also need to initialize the volume and pan of each Sound Object based upon the current configuration of your mixer console.
   // intialize volume and pan for track 1, do this for each track
   if (track1MuteButtonName.muted) {volume =  0} else 
   {volume = track1VolumeSliderName.position}
   track1SoundObjectName.setVolume(volume);
   track1SoundObjectName.setPan(track1panSlider.position);
Your main timeline should look like figure 11.


Figure 11. Initialize Mixer in Frame 20

Congratulations, you just created a Flash remixer! Keep in mind that web applications work best when there is excellent content being "applied". Consider creating your remixer for "content" rather than "features". There are plenty of feature filled "dance" remixers out there already. Try to do something new, such as creating music educational tools, using music styles other than dance music or experimenting with different types of user interfaces and redefine this emerging audio application genre!



Hayden Porter is a Web developer and musician with a special interest in Interactive Sound for the Web. He is a frequent contributor both as writer and forum moderator for www.sonify.org and maintains a Web design business, www.aviarts.com, in Akron Ohio.Reach him at Email address: hayden@aviarts.com.




Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers