Manipulating Sounds in AS3 With the Standing Wave 3 Library by Preview

Tutorial DetailsProgram: Flash ProfessionalDifficulty: IntermediateEstimated Completion Time: 2h
Join to Access

In this tutorial we will be exploring the AS3 sound manipulation library Standing Wave 3. We will create a sinewave piano, use a single MP3 to play many different pitches, make a chord and scale out of a single MP3, mix sounds together, and write the mixed sound to a ByteArray as .wav data.


Step 1: Explaining Standing Wave 3

The Standing Wave 3 library is a subset of the library that was used to create the very popular online music scoring application at noteflight.com. The library takes advantage of the dynamic sound generation that came along with Flash Player 10, and uses the newly created Alchemy extension to the Flash Player, which allows you to compile C++ code to run on the open source ActionScript Virtual Machine (AVM2).


Step 2: Create New Flash Project

Go to File > New and choose “ActionScript 3.0″ with the following properties.

  • W:600
  • H:600
  • Background Color:#CCCCCC

Save this file as “StandingWave.fla”


Step 3: Create “Main.as”

Go to File > New and choose “Actionscript 3.0 Class”.

Save this file as “Main.as” and set it as the Document Class.


Step 4: Adding the Library

You will need to grab a copy of the Standing Wave Library at github.

Once you have extracted the .zip file, you will need to copy the com folder (which is located in the standingwave > sw3 > src directory) into your project directory.

Next you have to add awave.swc, which is located in the standingwave > alchemy directory, to your project. Go to File > Actionscript Settings, then click on the “Library Path” tab to add the swc. If you’re not sure how to do this see the tutorial on how to use an external library in your Flash projects.


Step 5: Coding the Constructor

Inside “Main.as” add the following code.

package{
	//Class extends MovieClip
	import flash.display.MovieClip;
	//Needed for our Buttons and Complete Events
	import flash.events.MouseEvent;
	import flash.events.Event;
    //Needed for standing Wave Library
	import flash.media.Sound;
	import com.noteflight.standingwave3.elements.*;
	import com.noteflight.standingwave3.filters.*;
	import com.noteflight.standingwave3.formats.*;
	import com.noteflight.standingwave3.generators.*;
	import com.noteflight.standingwave3.modulation.*;
	import com.noteflight.standingwave3.output.*;
	import com.noteflight.standingwave3.performance.*;
	import com.noteflight.standingwave3.sources.*;
	import com.noteflight.standingwave3.utils.*;
	public class Main extends MovieClip{

		public function Main(){

		}

	}//Close the class
}//Close the package

Here we import the classes we will need throughout the tutorial and set up our Main() constructor function.


Step 6: Note Frequencies

To be able to effectively take advantage of the Standing Wave library, we need a reference to note frequencies. You can find a nice chart on the www.phy.mtu.edu website.

Don’t even worry about trying to memorize these, or having to constantly refer to this chart; we will create a nice “NoteUtils” class along the way that will reference all the notes shown in this chart.


Step 7: Audio Sources

Standing wave makes use of Audio Sources. These can be produced in a number of ways: by extracting sound from an sound file, using an existing array of values to construct a sound, or generating sound from mathematical functions at runtime.

A good video showing some basic sound manipulation using flashes dynamic sound generation is this one on Lee Brimelow’s GotoAndLearn.com website.

Some of the Audio Sources available are:

  • SineSource – allows you to generate sine waves
  • LoopSource – allows you to loop another source.
  • SamplerSource – provides a convenient way to perform complex sample wavetable playback functionality. It has flexible start and loop points, and accepts pitch modulations.

All Audio Sources implement the IAudioSource Interface.

...and that's the end of the preview!

Unlock the full tutorial by becoming a member of Tuts+ Premium

Including...

  • 886 Exclusive Tutorials

  • Course Library

  • 35 Top-selling eBooks

  • New Content Weekly

Take the Tour or Join Tuts+ Premium

If you've already got a Premium account, just Sign in to Access