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: SoundFork

Get Adobe Flash player
by jtnystrom 17 Jul 2009
/**
 * Copyright jtnystrom ( http://wonderfl.net/user/jtnystrom )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/hMCF
 */

// forked from murderdeathkitty's SoundFork
// forked from murderdeathkitty's flash on 2009-7-17
package {
    import flash.display.*;
    import flash.events.*;
    import flash.media.*;
    import flash.utils.*;
    public class SoundFork extends Sprite {
        
        private var sound: Sound;
        private var channel: SoundChannel
        
        private var lastVal: Number;
        private var lastPitch: Number;
           
        public function SoundFork(): void
        {
            addEventListener(Event.ADDED_TO_STAGE, start);
            addEventListener(Event.REMOVED_FROM_STAGE, stop);
            lastVal = Math.random();
            lastPitch = 0.1;
        
        }
        public function start(e: Event = null): void
        {
            sound = new Sound();
            sound.addEventListener(SampleDataEvent.SAMPLE_DATA, generate);
            channel = sound.play();
        }
        public function stop(e: Event = null): void
        {
            if(channel != null){
                channel.stop();
                channel = null;
            }
        }
        
        //replace this with your own sound!!!
        //output is 44100 hz, stereo. values between -1 and 1
        private function generate(e: SampleDataEvent): void
        {
            var data: ByteArray = e.data;
            var i: Number;
            var output: Number;
            
            lastPitch = lastPitch + (Math.random() - 0.5) * 0.02;
            for(i = 0; i < 2048; ++i){
                lastVal = lastVal + (Math.random() -0.5) * lastPitch;
                if (lastVal<-1)
                {
                    lastVal = lastVal * 0.9;
                }
                if (lastVal > 1)
                {
                    lastVal = lastVal * 0.9;
                }
                    
                output = lastVal;
                data.writeFloat(output);//left
                data.writeFloat(output);//right
            }
        }
    }
}