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

fft of sin wave sound

Get Adobe Flash player
by milchreis 05 Jul 2012
/**
 * Copyright milchreis ( http://wonderfl.net/user/milchreis )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/gECa
 */

package 
{
    import flash.utils.ByteArray;
    import flash.display.AVM1Movie;
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundMixer;
    import flash.events.SampleDataEvent;
    import flash.events.Event;
    
    public class FlashTest extends Sprite 
    {
        private var snd:Sound;
        private var ba:ByteArray;
        public function FlashTest() 
        {
            ba = new ByteArray(); 
                 
            snd = new Sound();
            snd.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
            snd.play();
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(e:Event):void
        {
            graphics.clear();
            
            SoundMixer.computeSpectrum(ba, false);
            drawByteArray(0xFF0000);       
            
            SoundMixer.computeSpectrum(ba, true);
            drawByteArray(0x00FF00);
            
        }
        
        private function drawByteArray(color:uint):void
        {
            graphics.lineStyle(1,color);
            graphics.moveTo(0, stage.stageHeight/2 * (1 - ba.readFloat()));
            for (var i:uint = 1; i < 256; ++i) 
            {
                graphics.lineTo(stage.stageWidth/256*i, stage.stageHeight/2 *(1 - ba.readFloat()));
            }
        }


        
        private function onSampleData(e:SampleDataEvent):void
        {
            for (var c:int=0; c<8192; c++) 
            {
                e.data.writeFloat(Math.sin((Number(c+e.position)/Math.PI/2))*0.25);
                e.data.writeFloat(Math.sin((Number(c+e.position)/Math.PI/2))*0.25);
            }
        }

    }
}