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.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.*;
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();
mySound.addEventListener(EventType.COMPLETE,completeListener);
mySound.addEventListener(IOErrorEventType.IO_ERROR,ioErrorListener);

var id:uint=flash.util.setInterval(progressListener,100);
mySound.load(request);
myChannel=mySound.play();

mySprite=new Sprite();
mySprite.graphics.beginFill(0x666666);
mySprite.graphics.drawRect(0,0,200,20);
mySprite.graphics.endFill();
addChild(mySprite);

mySprite2=new Sprite();
mySprite2.graphics.beginFill(0x666666);
mySprite2.graphics.drawRect(0,30,200,20);
mySprite2.graphics.endFill();
addChild(mySprite2);

equalizer=new Sprite();
addChild(equalizer);

myButton=new Button();
myButton.label="hallo";
myButton.x=100;
myButton.y=50;
addChild(myButton);

}

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);
mySprite.scaleX=myChannel.leftPeak;
mySprite2.scaleX=myChannel.rightPeak;
flash.media.Sound.computeSpectrum(spectrum,false,2);
equalizer.graphics.clear();
equalizer.graphics.beginFill(0x666666);
equalizer.graphics.moveTo(0,200);
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: Franto.com 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:
    http://www.blixtsystems.com/index.html#ss=pid&pid=20&page=blog

  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:)
    Latcho

    package {
    //import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    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();
    snd.load(sndFile);
    spectrum=new ByteArray();
    osci=new Sprite();
    addChild(osci);
    snd.play();
    flash.util.setInterval(udpdateSpectrum,50);
    }

    private function udpdateSpectrum(){
    flash.media.Sound.computeSpectrum(spectrum,false,1);
    osci.graphics.clear();
    osci.graphics.beginFill(0x123456);
    osci.graphics.moveTo(0,150);
    osci.graphics.lineTo(0,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){
    osci.graphics.lineTo( Math.round(i/2.5) , 150-(spectrum.readFloat()*100) );
    }
    osci.graphics.lineTo(Math.round(i/2.5),150);
    osci.graphics.endFill()
    }
    }
    }

  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. http://gunofason.com/flash/as3/spectFun.html

    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.