TODAY, CREATE.

January 17th, 2009
prev next

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 .as).

Things you might gain from referencing this class:

  • A better understanding of what’s going on with the ByteArray that SoundMixer.computeSpectrum() writes to
  • 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.

14 Responses to Another computeSpectrum() Example

  1. gaien says:

    ouaaaaaaaa what is it the song ?
    i’m french your player is very good

  2. Mike Creighton says:

    gaien, the song is listed at the bottom of the post: “Venus Morning” by Dert. You can download the album here .

  3. gaien says:

    Ha thanks you very mutch siorry for my english is very low and poor.

    i dont see in the txt by writted you but thx thx thx ^^ what not inset a flash player in you flash spectrum ? for play the next track no is good idea no ?

  4. erik says:

    I don’t see anything :(

  5. erik says:

    this is great, thank you very much for the source.

    cheers

  6. eugemjj says:

    awesome

  7. Al says:

    good job. Like the song and great example.

  8. bob says:

    I’ve used FLEX 3.0 Pro and AS 3.0 on and off over the last year but I’ve been sick on and off also and not as involved as I’d like to have been.

    Can you give some simple explicit steps to what you mean by making your class a child of a stage object in Flash CS3?

    I have a horizontally animated oscilloscope animation that’s of sort of a random nature. What I’d like to do is take what your using for the vertical equalizer, mirror them across a horizontal center line, and use them as masks to show only that part of the oscilloscope animation that is relevant.

    Obviously the bars above and below the centerline I’m having an alpha fade so it’s not just a cut off of the underlying animation, but I cannot even get past the “packages cannot be nested” or importing. That’s what I mean by AS 3.0 involvement.

    I’ve been busy getting my health back and doing projects in AS 2.0

    Thanks - great stuff (the music too).

  9. bob says:

    I’m doing a simple use of making the action script file the documents class file but getting this very odd error,

    intrinsic class flash.display.BitmapData

  10. Mike Creighton says:

    @bob:

    Regarding some of the questions you have regarding “making your class a child of a stage object in Flash CS3″: the source code I attached to this blog post is just a Sprite — one of the core DisplayObject classes in ActionScript 3. I’d highly recommend getting acquainted with this new shift in thinking since you’re coming from an ActionScript 2 background. Adobe has a good intro article here:

    http://www.adobe.com/devnet/flash/quickstart/display_list_programming_as3/

    If you’re not familiar with this aspect of AS3 yet, then my guess is that your experience with Flex Builder 3 revolves more around MXML components, which is definitely different.

    If you want to try compiling this example in Flash CS3 (or CS4), you can follow these steps:

    1. Create a new FLA file (ActionScript 3.0) and save it somewhere on your hard drive.

    2. Grab an mp3 file from your music collection and put it in the same folder as your FLA. Rename the file to example.mp3.

    3. Download my source code and rename the file from BasicAnalysis.txt to BasicAnalyzer.as. Put that file in the same folder as the FLA you created. Don’t put it in a subdirectory. It’s needs to be right next to your FLA.

    4. Back to the FLA in Flash. Click on the first frame on the root timeline, and open up the Actions panel.

    5. Input the following code in the Actions panel:

    var myAnalyzer : BasicAnalyzer = new BasicAnalyzer();
    addChild(myAnalyzer);
    

    6. Test the movie (Command / Control + Enter) and the example should work.

    Now regarding the BitmapData error you’re getting — I really have no clue about that. The example source code doesn’t use the BitmapData class anywhere in it, so I don’t know why it’s trying to reference it.

  11. Rt says:

    Mike, I’ve been real busy with other things, but I wanted to get back to this. I’m familiar with the ActionScript 3.0 programming model in FLEX pretty well and have been involved in the GUMBO beta test. MXML is something I mastered when FLEX first came out.

    I do agree though, that I want to get this working in my Flash CS3 for Mac OS X 10.5.7 (I’ve not been able to afford as of yet the $899 to upgrade my Master Suite to CS4).

    Anyway, I’ve performed the actions above periodically once in a while and it’s always the same thing, I get this error for the CS3 created ActionScript 3.0 flash file, with BasicAnalysis.as in the same directory and an example.mp3 also in the same directory with the example.swf (I’ve tried different .fla names, doesn’t seem to make a difference)….this is the error,

    1 var myAnalyzer : BasicAnalyzer = new BasicAnalyzer();
    2 addChild(myAnalyzer);

    For Line 1 above (which obviously is not in the code itself) I get 2 errors from the BasicAnalysis.txt file,

    Error #1 Description:
    1046: Type was not found or was not a compile-time constant: BasicAnalyzer.

    Error #1 Source:
    var myAnalyzer : BasicAnalyzer = new BasicAnalyzer();

    Error #2 Description:
    1180: Call to a possibly undefined method BasicAnalyzer.

    Error #2 Source:
    var myAnalyzer : BasicAnalyzer = new BasicAnalyzer();

    I also downloaded just tonight a fresh copy of your BasicAnalyzer.txt which of course I renamed as BasicAnalyzer.as

    Thanks for any help.

  12. Mike Creighton says:

    @Rt

    Are you sure the BasicAnalyzer.as file is in the same folder as the FLA you created? It doesn’t matter if you’re using Flash CS3 or Flash CS4.

  13. Rt says:

    Yes, it is in the same folder. Always has been. I’m using Flash CS3 on Mac OS X 10.5.7

    Not sure if that matters or not.

    I keep thinking that when a reply is published I’ll get a notification, Btw, that’s why there’s so much time between responses.

    I’ll check back more often. I love your example, but it still doesn’t work for me.

  14. Mike Creighton says:

    @Rt

    I just created a ZIP file with an FLA, sample MP3, and the BasicAnalyzer.as file. It was created using Flash CS4, but I saved it as a Flash CS3 FLA so you should be able to open it.

    You can download it here: BasicAnalyzer.zip.

Any thoughts?