In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Rainbow audio time domain graph v0.2

Originally written by NME a.k.a Anthony R Pace

Just playing with colour to get a rainbow-ish effect.
Get Adobe Flash player
by NME 16 May 2012
  • Related works: 1
  • Talk

    tes21090 at 07 Sep 2012 22:17
    feedback problem
    tes21090 at 07 Sep 2012 22:17
    feedback problem
    NME at 30 Jul 2013 09:18
    feedback problem? I'm not really sure how you could be having a feedback problem, since it doesn't play the audio back, and only displays the audio. I haven't heard complaints from anyone else, but if you really are having an issue, tell me more so I can look into it?

    Tags

    Embed
/**
 * Copyright NME ( http://wonderfl.net/user/NME )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9nN6
 */

// forked from NME's forked from: basic bmp audio graph v0.1
// forked from NME's basic bmp audio graph v0.1
//Originally written by NME a.k.a Anthony R Pace 
package {
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;
    import flash.events.SampleDataEvent;
    import flash.media.Microphone;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.Sprite;
    public class BMPAudioGraph extends Sprite {
        public var  bmpHeight:int = stage.stageHeight,//The height of the graph
                    smplHeight:int = 60,//The height of the Rect that will visually represent the position and height of the sample
                    bmp:Bitmap = new Bitmap(new BitmapData(512,bmpHeight,false,0x333333)), //the bitmap that will be used to represent the time domain
                    mic:Microphone = Microphone.getMicrophone(), //the default microphone if available
                    n:int, // the sample number
                    sn:Number,  //sample value at n, or s[n] 
                    A:Number = ((bmpHeight-smplHeight)/2),//'A'  for amplitude factor. It is also the remainder of space that you will be able to lift/drop the sampleBMP 
                    sampleBMD:BitmapData = new BitmapData(1,smplHeight,false,0x000000),
                    pt:Point = new Point(0,0),
                    b:BitmapData = sampleBMD,//at first it's the sample image bmd, and then it'll be a reference to the main bmd
                    zbdDrawn:Boolean = true,
                    zbd:BitmapData;
        public function makeWave(ba:ByteArray):void{ //sample data event handler
            ba.position = 0;
            b = bmp.bitmapData;
            b.lock();
            b.fillRect(b.rect,0x333333); 
            //hmm... I am thinking that maybe next time I will decrease the sample frequency in hz, 
            //'cause if I do, I can do a form of smooth interpolation using bezier curves...
            //as it stands I think it looks nice, but pretty darn choppy. 
            for (n = 0;n<512;++n){
               sn = ba.readFloat();//reads 4 bytes = 32 bit floating point sample value
               pt.x=n;
               pt.y = A-sn*A;//in this case I am using the Amplitude as not only a factor, but also the offset for the zero line.
               b.copyPixels(sampleBMD,sampleBMD.rect,pt);
            }
            b.unlock();
        }
        public function sdeh(e:SampleDataEvent):void{
            if (mic.activityLevel <20){
                if(!zbdDrawn){
                    zbdDrawn = true;
                    b = bmp.bitmapData;
                    b.lock();
                    b.fillRect(b.rect,0x333333);
                    pt.x = 0;
                    pt.y = A;
                    b.copyPixels(zbd,zbd.rect,pt);
                    b.unlock();
                  }
            }else{
                zbdDrawn = false;
                makeWave(e.data);}
            }
        public function BMPAudioGraph() {
            //first make the image that will represent a sample
            var shneg6_6pt5:uint =(smplHeight-6)/6.5;
            var lastY:uint=0;
            //probably should have turned this into a loop
            b.fillRect(new Rectangle(0,lastY+=1,1,shneg6_6pt5),0xE52019); //start this at 1, because we want a bit of black at the top
            b.setPixel(0,lastY+=shneg6_6pt5,0x000000);
            b.fillRect(new Rectangle(0,lastY+=1,1,shneg6_6pt5),0xFF6600);
            b.setPixel(0,lastY+=shneg6_6pt5,0x000000); 
            b.fillRect(new Rectangle(0,lastY+=1,1,shneg6_6pt5),0xFFA500);
            b.setPixel(0,lastY+=shneg6_6pt5,0x000000);
            b.fillRect(new Rectangle(0,lastY+=1,1,shneg6_6pt5),0xD4FF00);
            b.setPixel(0,lastY+=shneg6_6pt5,0x000000);
            b.fillRect(new Rectangle(0,lastY+=1,1,shneg6_6pt5),0x00FFA1);
            b.setPixel(0,lastY+=shneg6_6pt5,0x000000);
            b.fillRect(new Rectangle(0,lastY+=1,1,shneg6_6pt5),0x2644D8);
            //b.setPixel(0,lastY+=shneg6_6pt5,0x000000);
           // b.fillRect(new Rectangle(0,lastY+=1,1,shneg6_6pt5),0x8465FF);
            b.fillRect(new Rectangle(0,lastY+=shneg6_6pt5,1,smplHeight-lastY),0x000000);       
            stage.addChild(bmp);//place the main bitmap on the display list to make it visible
            var ba:ByteArray = new ByteArray();
            for(var i:uint=0;i!=2048;++i){
                ba.writeByte(0);
            }
            makeWave(ba);
            zbd = new BitmapData(512,smplHeight,false,0x000000); 
            zbd.copyPixels(bmp.bitmapData, new Rectangle(0,A,512,smplHeight), new Point(0,0));
            mic.rate = 11;// sets the sample frequency to 11025hz
            mic.setSilenceLevel(0);//setting this to 0 makes it so you hear all activity... you should notice noise in the line.
            mic.addEventListener(SampleDataEvent.SAMPLE_DATA,sdeh); // call the sdeh function if the mic hears audio 
}}}