UPDATE (01-18-2009): Some of you may have noticed that the Flash SWF was missing if you were viewing this post via its permalink. It’s fixed now. Sorry ’bout my screw-up.
UPDATE #2 (08-23-2009): Just realized that the text file with the sample ActionScript class was called “BasicAnalysis.txt”. It should’ve been called “BasicAnalyzer.txt”, which you’d need to rename to “BasicAnalyzer.as”. The link has been updated below.
Most of my traffic comes from searches for Actionscript 3 spectrum analysis. Because of this, I decided to post an updated example that you can use to conquer your
SoundMixer.computeSpectrum() woes. I realized that my original post had source code that wasn’t entirely transparent — and thus, probably not as useful as it could be.
This new example shows how you can draw a series of columns (using basic
Graphics.drawRect() code) and how you can do something a little more interesting. I wanted to include both so that people learning from this code don’t get too confused.
Download the source: BasicAnalyzer.txt (you’ll want to rename the extension to
Things you might gain from referencing this class:
- A better understanding of what’s going on with the ByteArray that
- A means for averaging the values in the ByteArray so that you don’t have to represent all 256 values of each stereo channel
- A simple example for doing something a little more “interesting” with the float values in the ByteArray
The source is fully commented, so feel free to snag it for your own use.
You can either make this class your FLA’s document class if you want to try it out, or you can add it as a child of any DisplayObject. Though, there’s some positioning based on
stage.stageHeight in there, so your mileage may vary. If you do make it your document class, the Flash movie you’re seeing is 560px by 400px.
Also if you’re interested, the music is “Venus Morning” off an album called “Talk Strange: A Beat Tape Inspired by Bjork” by Dert. Click on his name for the download. Highly recommended.
UPDATE #3 I created a ZIP file with the code and an FLA demonstrating how to use it. You can grab that package here: BasicAnalyzer.zip. It was created using Flash CS4, but should be able to be opened using Flash CS3.