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

forked from: forked from: basic bmp audio graph v0.1

Originally written by NME a.k.a Anthony R Pace - anthony.pace@utoronto.ca
Get Adobe Flash player
by wh0 02 Jan 2012
/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/utlE
 */

// forked from NME's forked from: basic bmp audio graph v0.1
//Originally written by NME a.k.a Anthony R Pace - anthony.pace@utoronto.ca 
package {
    import flash.geom.Rectangle; 
    import flash.utils.ByteArray;
    import flash.events.Event;
    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 bmp:Bitmap = new Bitmap(new BitmapData(512,500,false,0x006600)), //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] 
        bah:ByteArray, //Byte Array  that Holds the sample data
        A:Number = 250,//amount to normailize for visual representation, or 'A'  for amplitude factor.  
        rect:Rectangle = new Rectangle(0, 0, 1, 20); //for drawing each data point
        public function sdeh(e:SampleDataEvent):void{ //sample data event handler
            bah = e.data;
            bah.position = 0;
            bmp.bitmapData.lock();
            bmp.bitmapData.fillRect(bmp.bitmapData.rect, 0x3CD8FF);
            for (n = 0;n<512;++n){
               sn = bah.readFloat();//reads 4 bytes = 32 bit floating point sample value
               rect.x = n;
               rect.y = 240-sn*A;
               bmp.bitmapData.fillRect(rect,0xd7f7ff);
            }
            bmp.bitmapData.unlock();
        }
        public function BMPAudioGraph() {
            stage.addChild(bmp);//make the bitmap visible on the display list
            mic.rate = 11;// sets N, 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
        }
    }
}