AS3 :: Main
:(you know you love it)
Well, I decided to make a REALLY quick tutorial on how to use computeSpectrum!
You can view the SWF here *56kb*: Denvish [let it load]
(There's just code in FLA, no need to host)
*Note* Compute Spectrum is STRICTLY AS3! There is no AS2 function.
Source code is at the BOTTOM of the tutorial.
Okay, first an explaination of what SoundMixer.computeSpectrum(); does:
If you take the time to look at Flash's help, they have a text-book defination.
But, for you lazy people, it basically takes a 'screen shot' of the current sound (that's right, not the ENTIRE sound file, just what's on 'now'), and outputs the data to a ByteArray, 256 values for left channel, 256 values for right.
How it works/how to use it:
When you use SoundMixer.computeSpectrum(); you need to pass a ByteArray for the function to output the data to.
IE:
var mySpectrum:ByteArray = new ByteArray();
SoundMixer.computeSpectrum(mySpectrum);
Now, since it's a ByteArray, we need to read the data differently to use it. To do this, we use readFloat(); (for this example -- there are many more read (as well as write) functions, for a list, go search Flash's Help (under Action Script 3.0) for ByteArray)
And do use this, we simply do mySpectrum.readFloat();
Now, computeSpectrum will return a number, from -1 to 1. So, let's start by doing this:
for (var r=0; r<256; r++){
trace("[Right Channel - "+r+"] "+mySpectrum.readFloat();
}
for (var l=256; l<512; l++){
trace("[Left Channel - "+l+"] "+mySpectrum.readFloat();
}
Make sure to have a sound playing when you run this.
To attach sounds with AS3:
Same idea as attaching MovieClips from the library. Give it a class name, and just do:
var myNewSound:ClassName = new ClassName();
myNewSound.play();
In my source, there's comments explaining everything. Copy+paste if you must.
Source Code (paste to frame):
var song:SoundExample = new SoundExample();//Attach sound form the library
songChannel = song.play();//Start playing the sound by attaching it to a song Channel
function loop(event:Event) {//Loop the sound
songChannel = song.play();
}
songChannel.addEventListener(Event.SOUND_COMP LETE, loop); //When the channel completes
var mc:Sprite = new Sprite();//Create the API sprite
stage.addChild(mc);//Add the sprite to the stage
function computeMixer(event:Event) {//Event Handler (ENTER_FRAME)
var mySpectrum:ByteArray = new ByteArray();//Create a new ByteArray
/*
The computeSpectrum takes a ByteArray, so that's the sort of variable we need to create
*/
SoundMixer.computeSpectrum(mySpectrum);//Run the computeSpectrum function, outputting to mySpectrum
mc.graphics.clear();//Clear the sprite's API
//Left Channel
mc.graphics.lineStyle(1, 0x00FF00, 1);//Set the lineStyle
mc.graphics.moveTo(-1, 100);//Move it to the start position
for (var r=0; r<256; r++) {//For function for LEFT CHANNEL
/*
We run a For loop to cycle through the Byte entries of the newly created ByteArray
0 - 255 is Left channel (first 256 values)
256 - 513 is Right channel (last 256 values)
*/
mc.graphics.lineTo(r, 100+(mySpectrum.readFloat()*100));//Line to the current part of the spectrum
/*
The reason I did 100+, was because mySpectrum.readFloat() returns a number form -1 to 1. So that doens't help
Whe I want to display the data, now does it?
So, by adding 100, I move the start point to a y of 100
And by MULTIPLYING by 100, I make the waves be...bigger.
Also, I use 'r' for the x value (since it's going from 0 -> 255);
*/
}//end for
//Right Channel
mc.graphics.lineStyle(1, 0x0000FF, 1);//Set new Linestlye (for right channel)
for (var l=256; l<512; l++) {//For function for RIGHT CHANNEL
mc.graphics.lineTo(l, 100+(mySpectrum.readFloat()*100));//Line to the current part of the spectrum
}//end for
}//end EventHandler function
stage.addEventListener(Event.ENTER_FRAME, computeMixer);//Add the computeMixer event to stage's eventListener's
//AS3's way of adding events
Good luck with your sound...playing...with...flash...ness.