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: FP10.1用 マイク録音&再生

マウスダウンの間に録音、放すと再生
Get Adobe Flash player
by taka_milk 26 Nov 2009
    Embed
/**
 * Copyright taka_milk ( http://wonderfl.net/user/taka_milk )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dc9P
 */

// forked from keim_at_Si's forked from: FP10.1用 マイク録音&再生
// forked from keno42's FP10.1用 マイク録音&再生
// マウスダウンの間に録音、放すと再生
package  
{
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.SampleDataEvent;
    import flash.media.Microphone;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.text.TextField;
    import flash.utils.ByteArray;
    import org.si.utils.FFT;
    public class VoiceChat extends Sprite
    {
        private var recording:Sprite = new Sprite();
        private var mic:Microphone;
        public function VoiceChat() 
        {
            addChild(recording);
            mic = Microphone.getMicrophone();
            mic.rate = 44;
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
            _initFFT();
            mic.addEventListener(SampleDataEvent.SAMPLE_DATA, onRecord);
        }
        private function onRecord(e:SampleDataEvent):void {
            _execFFT(e.data);
            recording.graphics.clear();
            recording.graphics.lineStyle(0);
            recording.graphics.moveTo( 0, 100-form[i]*20 );
            for ( var i:int = 1; i < 256; i ++ ) {
                recording.graphics.lineTo( i*2, 100-form[i]*20 );
            }
        }
        private function onDown(e:MouseEvent):void{}
        private function onUp(e:MouseEvent):void{}

        private var src:Vector.<Number> = new Vector.<Number>(2048);
        private var spec:Vector.<Number> = new Vector.<Number>(1024);
        private var form:Vector.<Number> = new Vector.<Number>(256);
        private var hann:Vector.<Number> = new Vector.<Number>(2048);
        private var fft:FFT = new FFT(2048);
        private function _initFFT() : void {
            for (var i:int=0; i<2048; i++) hann[i]=0.5-Math.cos(i*0.00306796)*0.5;
        }
        private function _execFFT(data:ByteArray) : void {
            var i:int, j:int, m:Number, n:Number;
            data.position = 0;
            for (i=0; i<2048; i++) src[i]=data.readFloat()*hann[i];
            fft.setData(src).calcDCT().getIntensity(spec);
            // 44100/2048*(256+4)=5.6kHz
            for (i=0; i<256; i++) {
                form[i] = Math.log(spec[i+4]);
            }
        }
    }   
}