Flash 9 Sound Spectrum!

If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old “analyze in AE” method is now a thing of the past. The new SoundMixer.computeSpectrum() method reads audio data at runtime allowing you to create audio-driven animations. Click on the image below to see my first experiment. This only took 10 minutes! Get ready to see some crazy stuff from the Flash community. Source is below as well.

Flash Player 9 required!

[as]var s:Sound = new Sound();
var sc:SoundChannel;
var ba:ByteArray = new ByteArray();
var array:Array;

s.load(new URLRequest(“mix.mp3″));
sc = s.play(0,1000);

this.addEventListener(Event.ENTER_FRAME, spectrum);

var a:Number = 0;

function spectrum(event:Event)
{
a = 0;
graphics.clear();
SoundMixer.computeSpectrum(ba,true,0);
for(var i=0; i < 256; i=i+8)
{
a = ba.readFloat();
var num:Number = a*360;
graphics.lineStyle(num/15,0x0066FF|(num << 8));
graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2,i);
}

} [/as]

Lee


Commentary

  1. Francois says:

    very cool!!
    I can’t wait to play with this. How do you compile/publish this script?
    Thanks.
    F

  2. Otto says:

    Cool, it came out today and you are already working with it at an advanced level in my eyes. you think can you make a tutorial on GotoAndLearn About this?

    -Otto

  3. Paul Neave says:

    Superb! Nice choice of music too…

  4. James Lyon says:

    Yeah, very nice mix.

    The new API rocks!!!

  5. Peter says:

    I can’t recreate your example. Did you build this using Flex or Flash9 AS3 Preview?

  6. josh says:

    that is much easier. can you do an update tut on it. I think I got how it works but just in case some of us are missing something witht the AS3 sintax

  7. Lee Brimelow says:

    Yes I am using the Flash 9 preview to compile this. What errors are you getting?

  8. iBrent says:

    I got an error about sandbox security, somethin’ or other. I think it’s connected to the fact that the actionscript file is pointing to a file that is a relative path:

    6. s.load(new URLRequest(“mix.mp3″));

    I was able to input the actionscript into my alpha version of Flash 9 and change the url to an mp3 on my computer and it worked great.

    Cool stuff man!

  9. Otto says:

    oooh! oooh! I have an idea! how about you do the same thing you did on GotoAndLearn, but instead of using after effects, use this. I will love to do something like that ( I dont undrerstand a single thing about as3 syntax).

  10. Sean Foushee says:

    This will be the greatest upgrade to Flash, and the way Adobe is moving forward with the Flash API and Flex is awe inspiring.

  11. Harry says:

    Still you cannot use the soundMixer for dynamically created sound objects, right? I mean is it possible to have this effect on an mp3 player where the user points and listens to any mp3 off the net?

  12. Jake Hawkes says:

    I am not sure I am as stoked about the release of the new player. It seems that there are many demands on viewers to date and I am concerned that flash 9 is going to have a slower adoption rate unless some really big sites adopt it right away with detection and installation. I have released a few videos that require flash 8 and I have witnessed many email from people hearing only my music. I have sneaker detection and installation but on some machines it requres a reboot…so I think this is really starting to be a bit of a problem.

  13. Lee, you are great!

    With AS3 we dont need to parse sound Spectrum. computeSpectrum works really perfect. There is a version that i did with flash 8 in my website with your tut; http://www.ozguraltay.com //coming soon :) Now i can update it…

    @Jake Hawkes; you are right about the flash detection. I am using the Macromedia’s flash detection kit
    Windows machines dont need reboot, just opening a new browser should be fine. But as you say, to push people to DL the latest version of flash sometimes is a nightmare…

  14. jippo says:

    Very very nice. I’m looking forward to having a play.

    Was hoping to make some sort of game which uses beat detection, then you have to press certain keys, but it doesnt *look* like you can analyse sound before you play it, unless i’m missing something…

  15. jippo says:

    Also Lee, am I right in thinking that your example is only using the first 1/8th of the spectrum?

    trying to get my head around all this ByteArray stuff!

  16. hlgao says:

    Very good ,I have successed to run it !

  17. KC says:

    VERY COOL !

    Thanks Lee.

  18. hi

    Very good example. but found prob. with FLASH 9 .i m useing the same above code but i got problem when loding and playing the mp3 songs. The last 10 to 15% portion of mp3 file is not getting loaded into memory/buffer so that the song is not playing completly .Song is stop before the end length(duration) of mp3. As a result Waveform is not generated completly

    if any idea to solve this prob. pls help me. its very urgent to m

  19. Daniel says:

    Cool man, I’ve constructed a xml playlist around the as3code, it works great, now I have a jukebox with vis, Thanks Lee.

    Greets, Daniel, The Netherlands

  20. lx says:

    that’s so cool!
    I like this
    It’s won’t be real in as2
    I can’t wait to make one

  21. John says:

    Where do we get this so called as3 update cause im not finding it?

  22. Susie says:

    I am going to try it. Flash 8 is too much FUN!!!!!!!!!

    I HAVE A PROBLEM THO. I built an MP3 Player and it runs perfect on my machine, as soon as I upload to the web it doesn’t produce any sound, I tried it on a couple of browsers but no luck. Any advice??????
    THanks, I like this

  23. Sammi says:

    I am to having this problem were the player does not play the sound completetly. In my case it actually does play until end if the sound was not interrupted while playing. If I do a pause/resume while playing then the sound does not finish and I do not get a SOUND_COMPLETE event from the channel and channel.position never reaches snd.length.

    Any ideas?

  24. Josh says:

    When I put this code into Flash 9 Public Alpha, I get an error message that says “Failure to initialize Java Runtime Environment, you may need to reinstall Flash.”

    O_o what’s that all about?

  25. chad says:

    Get an error when I try the code. Down loaded the flash 9 alpha. Here is the error.
    namespace internal.
    var sc:SoundChannel;
    ReferenceError: Error #1065: Variable Timeline0_82462e9c65e6af488b1f2418cf2ceca is not defined.

  26. LEE says:

    I think the Flash Player update killed this…

  27. Thomas says:

    I did also get this error:

    When I put this code into Flash 9 Public Alpha, I get an error message that says “Failure to initialize Java Runtime Environment, you may need to reinstall Flash.”

    WHY!!!

  28. Jordan says:

    Flash 9 Public Alpha is incompatible with Intel Macs, which is likely why you got “Failure to initialize Java Runtime Environment, you may need to reinstall Flash.”

    It only runs on a 10.4 Mac as far as I know.

  29. Brett says:

    I’m running on OS 10.4.8 and I see the “Failure to initialize Java Runtime Environment…” I’m on an Intel Macbook Pro, so maybe that has something to do with it.

  30. r-a-n-d-o-m says:

    great !
    I made this 4 years ago using flashamp, but know that you can do it realtime is awsome

    http://www.r-a-n-d-o-m.com/combustion.html (looks a bit the same)

  31. Dan says:

    I was wondering if we could do the same for sound captured from the Mic. Couldn’t find a way yet. Anyone?

  32. Espen Christensen says:

    Yeah, im wondering the same. I want to make an audio visualizer that captures audio from my Audio Input, and visualizes it live. Is this possible? If, does anyone know of any examples, sources, tutorials?

  33. Mayank says:

    make sure u use action script 3 and compile using flash player 9. Note that the variable sc is not necessarily needed. the variable a contains the peak!

  34. Chris says:

    Yo for all us noobs out there I hacked it for flash compiling

    package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.URLRequest;
    import flash.media.*;
    import flash.utils.*;

    public class Visualizer extends Sprite {

    private var s:Sound = new Sound();
    private var sc:SoundChannel = new SoundChannel();
    private var ba:ByteArray = new ByteArray();
    private var array:Array;
    public var a:Number = 0;

    public function Visualizer() {
    s.load(new URLRequest(“Marie Antoinette Soundtrack – 05 – Jynweythek Ylow.mp3″));
    sc = s.play(0,1000);
    this.addEventListener(Event.ENTER_FRAME, spectrum);
    }

    function spectrum(event:Event):void {
    a = 0;
    graphics.clear();
    SoundMixer.computeSpectrum(ba,true,0);
    for(var i=0; i

  35. Chris says:

    for(var i=0; i

  36. Sam says:

    Chris – is this a hack for actionscript 2.0? Looks very useful, but it seems to be cut short….. Or does the code just continue as in the initial example…?

    Thanks in advance

  37. Bahc says:

    Brilliant!

    any ideas how to use that on a video stream?
    any way of capturing the soundchannel on a video, or is there any alternative.

    thx

  38. Iain says:

    Lee, will there ever be a change to the behaviour of Flash Player, so that computeSpectrum and soundmixer work on a per player instance basis? At the moment, the computespectrum function throws errors if you have youtube (for example) open in another tab. This post explains the bug/feature:

    http://www.soledadpenades.com/2007/09/24/security-sandbox-violation-how-come/

  39. Joe says:

    Do you know why this demo doesnt display in Firefox? I even opened the swf directly in the browser, and nothing!

  40. Joe says:

    Nevermind. I just read the post above. That was my problem.

  41. Alex says:

    Thanks! Ive been looking for something like this.

    http://www.alyse-designs.com/

    I made a few adjustments, You can spend hours playing with this.

  42. Spider says:

    I have a question: Howa I can show whole spectrum of mp3 file. It could not be showing dinamicly but when I load mp3 file it shows me spectrum of whole file. How I can do that.

    P.S This same effect like I need to do is when we drag mp3 file onto flash form and it showns in library with full spectrum.

  43. jono says:

    new to AS3…

    having trouble, it says “label must be simple identifier”
    in the line graphic.lineStyle

    help anyone.

  44. monali says:

    This is not related to this site but i wanna to ask a query. I am designing a new bloging site. The design which i have made is too simple and i m not satisfied with it. Will u plz suggest the goo design for that.

  45. noj says:

    flash player 10 lets you access the soundTransform of the microphone. with dynamic sound Generation API, i can see its gona get really really crazy

  46. simone says:

    Hi!Nice work really!
    There is a possibility to associate jpg images, ui would like to build a soundspectrum who works with images taken from flickr, is it possible?
    Thanks in advance ;)

  47. simone says:

    i try to explain me better. I would like to associate pictures and music and create an interection amongst them.

  48. corbanb says:

    stumbled on this trying to work though an issue I’m having. posted the code here for easier copy and paste.

  49. Ivica says:

    it doesn’t work if a youtube player is in another tab

  50. Roberto says:

    Hi there,
    Is it possible to use 2 different SoundMixer.computeSpectrum() analysis with 2 different sounds (.mp3)?

    Thanks

  51. biohazard says:

    hi friends …..i need a videoSpectrum….this exists?

    I want to show the waves produced by the sound of a video..

    please i need help ok —thnks

  52. Umesh says:

    I have a soundlcloud player mixer play some mixes, what I want to do was make a spectrum analyser for a mix player such as sound cloud. Is there a way to ready the sound output of a player like sound cloud, what I mean is can a spectrum analsyer just GET whatever sound is being played and use that???

  1. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  2. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  3. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  4. [FLASH]Vespers linked here

    [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  5. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  6. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  7. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  8. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  9. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  10. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

  11. [... If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old "analyze in AE" method is now a thing ...]

Leave a Comment