-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBasic.as
103 lines (88 loc) · 2.6 KB
/
Basic.as
1
package { import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.events.SampleDataEvent; import flash.media.Microphone; import flash.media.Sound; import flash.media.SoundMixer; import flash.utils.ByteArray; /** * Simple Microphone test for Flash Player 10.1 * @author Devon O. */ [SWF(width='550', height='450', backgroundColor='#000000', frameRate='40')] public class Basic extends Sprite { // sound private var _soundBytes:ByteArray = new ByteArray(); private var _micBytes:ByteArray; private var _micSound:Sound; private var _lines:Vector.<Shape> = new Vector.<Shape>(512, true); private var _ctr:int; public function Basic():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(event:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); initEqualizer(); initMic(); initSound(); addEventListener(Event.ENTER_FRAME, drawLines); } private function initEqualizer():void { var holder:Sprite = new Sprite(); for (var i:int = 0; i < 512; i++) { var line:Shape = new Shape(); with(line.graphics) { beginFill(0xFFFFFF); drawRect(0, -300, 1, 300); endFill(); } line.x = i; line.scaleY = 0; holder.addChild(line); _lines[i] = line; } holder.y = 400; holder.x = stage.stageWidth * .5 - holder.width * .5; addChild(holder); } private function initMic():void { var mic:Microphone = Microphone.getMicrophone(); if ( mic ) { mic.setLoopBack(false); mic.rate = 44; mic.gain = 60; mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler); } else { // no mic } } private function micSampleDataHandler(event:SampleDataEvent) :void { _micBytes = event.data; _micSound.play(); } private function initSound():void { _micSound = new Sound(); _micSound.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler); } private function soundSampleDataHandler(event:SampleDataEvent):void { for (var i:int = 0; i < 8192 && _micBytes.bytesAvailable > 0; i++) { var sample:Number = _micBytes.readFloat(); event.data.writeFloat(sample); event.data.writeFloat(sample); } } private function drawLines(event:Event):void { SoundMixer.computeSpectrum(_soundBytes, true); if (_soundBytes.bytesAvailable) { _ctr = 0; while (++_ctr < 512) { _lines[_ctr].scaleY = _soundBytes.readFloat(); } } } }}