-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPitchDetection.as
270 lines (218 loc) · 6.54 KB
/
PitchDetection.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 PitchDetection 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 PitchDetection():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; var res:Array = new Array(); while (++_ctr < 512) { _lines[_ctr].scaleY = _soundBytes.readFloat(); if (_lines[_ctr].scaleY>0.9) { //trace(_lines[_ctr].scaleY, _ctr); res.push(_ctr); } } var sum:int; for (var i:int =0; i<res.length; i++) { sum+=res[i]; } var avg:Number = (sum/res.length); if (avg>0) { trace("AVG: ", avg, (avg*4)); pitchBlock.y = 800-(avg*4); } } } }}/*package { import flash.system.Security; import flash.system.SecurityPanel; import flash.display.Sprite; import flash.events.Event; import flash.events.SampleDataEvent; import flash.media.Microphone; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundMixer; import flash.utils.ByteArray; public class PitchDetection extends Sprite { private var _mic:Microphone; private var _micBytes:ByteArray; private var _micSound:Sound; private var _soundChannel:SoundChannel; private var _datas:Number; public function PitchDetection() { _mic = Microphone.getMicrophone(); _mic.rate = 44; _mic.setSilenceLevel(0); _mic.setUseEchoSuppression(true); _mic.addEventListener(SampleDataEvent.SAMPLE_DATA, _micSampleDataHandler); _micSound = new Sound(); _micSound.addEventListener(SampleDataEvent.SAMPLE_DATA, _soundSampleDataHandler); } public function _micSampleDataHandler(e:SampleDataEvent):void { output_txt.appendText("in mic handler"); _micBytes = e.data; while (_micBytes.bytesAvailable) { _datas = _micBytes.readFloat(); } _micSound.play(); } public function _soundSampleDataHandler(e:SampleDataEvent):void { output_txt.appendText("in sound handler"); for (var i:int = 0; i < 2048; ++i) { e.data.writeFloat(_datas); e.data.writeFloat(_datas); } } public function outPutData(event:Event):void { SoundMixer.computeSpectrum(_soundBytes, true); if (_soundBytes.bytesAvailable) { _ctr = 0; while (++_ctr < 512) { } } } }}*//*package { import flash.system.Security; import flash.system.SecurityPanel; 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; public class PitchDetection extends Sprite { private var _soundBytes:ByteArray = new ByteArray(); private var _micBytes:ByteArray; private var _micSound:Sound; private var _ctr:int; public function PitchDetection() { initMic(); initSound(); addEventListener(Event.ENTER_FRAME, outPutData); } private function initMic():void { var mic:Microphone = Microphone.getMicrophone(); Security.showSettings(SecurityPanel.MICROPHONE); if ( mic ) { output_txt.appendText(" has mic "); mic.setLoopBack(true); mic.rate = 44; mic.gain = 60; mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler); } else { // no mic } } private function micSampleDataHandler(event:SampleDataEvent):void { output_txt.appendText(", has mic "); _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 outPutData(event:Event):void { SoundMixer.computeSpectrum(_soundBytes, true); if (_soundBytes.bytesAvailable) { _ctr = 0; while (++_ctr < 512) { } } } }}*/