forked from: Microphone

by windy
♥0 | Line 41 | Modified 2011-04-13 00:50:27 | MIT License
play

ActionScript3 source code

/**
 * Copyright windy ( http://wonderfl.net/user/windy )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1jyh
 */

// forked from only3km's Microphone
package {
    import flash.text.TextField;
    import flash.display.Graphics;
    import flash.media.Microphone;
    import flash.utils.ByteArray;
    import flash.display.Sprite;
    import flash.events.SampleDataEvent;
   
    public class FlashTest extends Sprite {
        public function FlashTest() {
            var tf:TextField = new TextField();
            addChild(tf);
            // write as3 code here..
            var soundBytes:ByteArray = new ByteArray();
            var mic:Microphone = Microphone.getMicrophone(); 
            mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler); 
             
            const PLOT_HEIGHT:int = 224;
            
            var sprite:Sprite = new Sprite();
            addChild(sprite);
            
            function micSampleDataHandler(event:SampleDataEvent):void 
            { 
                var g:Graphics = sprite.graphics;
                g.clear();
                
                g.lineStyle(0, 0xFFCC00, 1);
                g.beginFill(0xFFCC00, .5);
                g.moveTo(0, PLOT_HEIGHT);
                
                var n:Number;
                var i:int = 0;
                
                while(event.data.bytesAvailable) 
                { 
                    n = (event.data.readFloat() * PLOT_HEIGHT);
                    g.lineTo(i, PLOT_HEIGHT - n);
                    i++;
                } 
                g.lineTo(i, PLOT_HEIGHT);
                g.lineTo(0, PLOT_HEIGHT);
                g.endFill();
                
                tf.text = i.toString();
                
                sprite.width = 400;
            }
        }
    }
}