Forum Topic: AS3 :: Compute Spectrum

(2,917 views • 5 replies)

This topic is 1 page long.

<< < > >>
None

ssjskipp

Reply To Post Reply & Quote

Posted at: 6/7/07 10:06 PM

ssjskipp LIGHT LEVEL 15

Sign-Up: 10/16/03

Posts: 860

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.

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

UnknownFear

Reply To Post Reply & Quote

Posted at: 6/8/07 08:29 PM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,912

Interesting. I don't have Flash 9, but the effect looks pretty cool.

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


NewWorldProductions LIGHT LEVEL 08

Sign-Up: 01/11/07

Posts: 906

yeah, man, you lost me at How it works/How you use it.

If it's an auto-code that doesn't require a symbol to work, can I ask for the script?


None

trig1

Reply To Post Reply & Quote

Posted at: 6/9/07 09:55 AM

trig1 NEUTRAL LEVEL 15

Sign-Up: 10/04/05

Posts: 2,107

Why on earth did you use lineTo and not bitmap data? Bitmap data renders at quarter of the time, and works just as well.

Anyways, the actuall tutorial was kinda rushed, and didnt look easy on the eye (no bold, no italics, no titles) . No offence, but I might make a slightly better tutorial on it later :P

BBS Signature

None

ssjskipp

Reply To Post Reply & Quote

Posted at: 6/9/07 11:48 AM

ssjskipp LIGHT LEVEL 15

Sign-Up: 10/16/03

Posts: 860

Please do! I just found that no one had made one before, so I posted a quick example. And I figured more people would understand the API lineTo than BitmapDate (given if they're trying to use computeSpectrum, they should know BitmapData anyway)

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

st1k

Reply To Post Reply & Quote

Posted at: 6/24/07 04:58 PM

st1k DARK LEVEL 18

Sign-Up: 02/13/07

Posts: 1,530

wow i so dont get this.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 10:33 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!