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!

Comments

21 responses to “Sound spectrum code”

  1. Peter Avatar

    I think this will be the greatest release ever for Adobe 😉

  2. Jon B Avatar

    So how I put this into Flex Builder and try it? I’ve never used Flex and don’t know what to put in the MXML

  3. Dominik Avatar

    Line is incomplete

    for(var i:uint=0;i

    //trace(spectrum[i]);

    give me a hint, please

  4. Dominik Avatar

    i think it is

    for (var i : uint = 0; i

  5. Dominik Avatar

    oh, WordPress is wrong

    for(var i:uint=0;i “less than” spectrum.length;i++)

    thank you for this sample

  6. Leo Bergman Avatar

    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…

  7. Erik Avatar
    Erik

    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. 🙂

  8. […] you can try the code that people at RichApps.de wrote here Comments » The URI to TrackBack this entry is: http://janumedia.blogsome.com/2 […]

  9. […] urce code) ActionScript 3.0 Sound >> 2 sites Sound Spectrum Visualizer Andre Michelle Sound Spectrum Example (source code) ActionScript 3.0 Tutorials >> 10 sites Actionscript 3 – Fi […]

  10. Leo Bergman Avatar

    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

  11. […] reading over here. Tinic has a PNG Encoder over here. And some Sound spectrum code over here at RichApps.
    Entry Filed under: ActionScript 3.0

    Leave a Comment

    […]

  12. David Avatar
    David

    I can’t see a button
    import mx.controls.Button;

  13. latcho Avatar

    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

  14. latcho Avatar

    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()
    }
    }
    }

  15. latcho Avatar

    and by the way, the documentation from macromedia is also faulty:
    use biteArray.readfloat to get the right values.

    check this http://blixt.gotdns.com/blixt/?ss=pid&pid=20&page=blog&menu=blog&lang=en#ss=bc&bc=AS3&page=blog

  16. gunofason Avatar

    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.

  17. Bryan Ledford Avatar
    Bryan Ledford

    The length of the byteArray is 2048 bytes, but a 32 bit integer requres 4 bytes so 2048/4 = 512 32 bit integer values.

    With FFT set to false (which it needs to be right now since FFT doesn’t work yet) you get values from -1 to 1. Plotting that will display the raw waveform.

    Example:
    http://www.bryanledford.com/experiments/BitmapSpectrum.html
    -right-click for source.

  18. Bart Avatar
    Bart

    Is it possible to use this spectrum class just in flash 8 professional?

  19. […] _______________________________________ Robert A. Colvin <RColvin@kaplan.edu>to Flashcoders http://www.richapps.de/?p=23 check it out..;) _______________________________________________ hank williams <hank777@gmail.com>to Flashcoders […]

  20. […] Sobre sonido esta este snippet de codigo en richapps […]