Sound spectrum code

Here is some code for playing with the sound spectrum/peak in flash player 8.5.
package {

import flash.display.MovieClip;
import flash.util.trace;
import flash.util.ByteArray;
import flash.util.SetIntervalTimer;
import flash.display.Sprite;
import mx.controls.Button;

public class AsOnly extends MovieClip {

private var mySound:Sound;
private var myChannel:SoundChannel;
private var mySprite:Sprite;
private var mySprite2:Sprite;
private var equalizer:Sprite;
private var spectrum:ByteArray;
private var myButton:Button;

public function AsOnly() {
var request:URLRequest=new URLRequest("mySound.mp3");
spectrum=new ByteArray();
mySound=new Sound();

var id:uint=flash.util.setInterval(progressListener,100);

mySprite=new Sprite();;,0,200,20);;

mySprite2=new Sprite();;,30,200,20);;

equalizer=new Sprite();

myButton=new Button();


private function completeListener(event:Event){
trace("event: "+event.type);

private function ioErrorListener(event:Event){
trace("error: "+event.type);

private function progressListener(){
//trace("left Peak: "+myChannel.leftPeak+" rightPeak: "+myChannel.rightPeak);
for(var i:uint=0;i
So far i really love flex 2.0 / fp 8.5. If flash 8 was the greatest release ever for MM what is this?
Microsoft may have something coming with Sparkle but Flex is allready here!

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest

21 thoughts on “Sound spectrum code

  1. I’ve been so hoping for this feature to appear :)
    Thanks a lot for the sample code.

    It doesn’t show anything like a proper spectrum though :(
    I tried setting FFT to true and that doesn’t help either.
    I’m not sure how to access the items in the byteArray, but it seems that it’s not correct to simply use the array access brackets.
    Neither readDouble or readFloat seems to give me correct values either though…

  2. Jon B: You don’t need the MXML. Just add the code as a new class (you’ll have to modify it some due to WordPress messing it up). Right click the class in the navigator and choose Application Management > Set as default application. Compile (CTRL + B). And check out AsOnly.html in your bin-folder.

    Cred to the autuhor of this, real cool stuff. :)

  3. Pingback: JanuMedia :: Sound Spectrum on Flash Player 8.5 :: October :: 2005

  4. Pingback: flash blog of betaruce»Blog Archive » Links to samples/tutorials for AS3

  5. Pingback: Flash blog » Collected links to ActionScript 3.0 examples

  6. Ok….I played some more with it and it’s indeed readFloat you should use.
    That will read the next 4 bytes from the position in the byteArray and give you the correct float value.
    At least I get a perfect waveform display using that, but to get a spectrum graph you need to set FFTMode to true.
    But in my tests I just get a irregular mess when doing that :(
    You can check my examples here:

  7. Pingback: » ActionScript 3.0 Stuff

  8. I read that the FFT=true works faulty !
    So how can we get a real FFT spectrum, the one above… try to find the base?

    4 now I keep it limited to a simple scope:)

    package {
    //import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.util.ByteArray;
    import flash.util.SetIntervalTimer;
    import flash.util.trace;

    public class AsOnly extends Sprite {
    private var snd:Sound;
    private var chn:SoundChannel;
    private var osci:Sprite;
    private var spectrum:ByteArray;

    public function AsOnly() {
    var sndFile:URLRequest=new URLRequest(“–darkness.MP3”);
    snd=new Sound();
    spectrum=new ByteArray();
    osci=new Sprite();

    private function udpdateSpectrum(){,false,1);;;,150);,0);
    for(var i:uint=0;i

  9. and we continue from that last line… replace the LESSTHANSIGN
    continued from above…

    for(var i:uint=0;i LESSTHANSIGN 1024;i+=4){ Math.round(i/2.5) , 150-(spectrum.readFloat()*100) );

  10. latcho, your example only reads one channel. the macromedia documentation says “The size of the ByteArray object created is fixed to 512 floating point values” however if you return the length it returns 2048. So by using just 1024 you are just displaying the left side of the spectrum. i also made a example using fft and the values it returns are quite buggy.

    careful, it crashes the ax plugin but runs in the rest.

  11. Pingback: CODE: XT » Blog Archive » Who wants MIDI in the Flash Player?

  12. Pingback: Action Script 3.0 « Juan Anzaldo

Comments are closed.