[AS3] How to plug your microphone with a SoundSpectrum in Flash Player 10.1
For a personal experimentation, i wanted to have a look at the new features of the flash player 10.1, and more precisely concerning the microphone. Before 10.1, the flash player providen’t data for the microphone, but times are changing, for the happiness of every flash developper
.
Stop twaddling, and start coding!
Of course, for testing it you will need the flash player 10.1 and its SWC in order to compile it.
You can found them on the Adobe Labs.
var _soundBytes:ByteArray = new ByteArray(); var _micBytes:ByteArray; var son:Sound; var sc:SoundChannel; var pow:int=0; var myBar:Sprite; init(); function init(event:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); myBar=new Sprite; initMic(); initSound(); addEventListener(Event.ENTER_FRAME, drawLines); } 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 } } function micSampleDataHandler(event:SampleDataEvent):void { _micBytes=event.data; sc=son.play(); } function initSound():void { son = new Sound(); son.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler); } 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); } } function drawLines(e:Event):void{ SoundMixer.computeSpectrum(_soundBytes, true); myBar.graphics.clear(); myBar.graphics.lineStyle(2,0xabc241); for (var i=0; i<256; i++) { pow=_soundBytes.readFloat()*200; pow=Math.abs(pow); myBar.graphics.drawRect(i*2, 0, 2, pow); addChild(myBar); } }
Hope you liked it! Any comments are welcome







