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

Sound

Get Adobe Flash player
by XELF 21 Dec 2008
package {
    import flash.display.*;
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.*;

    public class Main extends Sprite {        
        private var sound:Sound = new Sound();
        public var parameters:Parameters = new Parameters();
        public var wave:WaveForm = new Wave();
        //public var wave:WaveForm = new SineWave();
        public var channel:SoundChannel;

        public function Main() {
            sound.addEventListener(SampleDataEvent.SAMPLE_DATA,
                generateSamples);
            channel = sound.play();
        }
        
        private function generateSamples(e:SampleDataEvent):void {
            wave.Generate(e, parameters);
        }
    }
}

{
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;

    class Parameters {
        public var Phase:Number = 0;
        public var BaseFrequency:Number = 440;
        public var Frequency:Number = 440;
        public var SamplesPerSecond:Number = 44100;
        public var Amplitude:Number = 0.3;
        public var Samples:uint = 8192;
        public var Time:Number = 0;
        private var Step:Number;
        private var TimeStep:Number;

        public function Parameters() {
            Setup();
        }
        public function Setup():void {
            Step = Frequency / SamplesPerSecond;
            TimeStep = 1 / SamplesPerSecond;
        }
        public function Update():void {
            Phase += Step;
            Phase %= 1;
            Time += TimeStep;
        }
    }

    interface WaveForm {
        function Generate(e:SampleDataEvent,
           p:Parameters):void;
    }

    class Wave implements WaveForm {
        public function Generate(e:SampleDataEvent,
            p:Parameters):void {
            for (var i:uint = 0; i < p.Samples; i++) {
p.Frequency=p.BaseFrequency+Math.sin(p.Time*Math.PI*9.2)*10;
p.Amplitude*=0.99998;
if (p.Time >= 0.5) { p.Time = 0; p.Amplitude = 0.3;
p.BaseFrequency = 220 * Math.pow(2, ((int)(36*Math.random()))*(1.0/12));
 }
p.Setup();
                var sample:Number;
                sample = Math.sin(p.Phase * (Math.PI * 2))
                    * p.Amplitude;
                sample += Math.sin(p.Phase * (Math.PI * (2.9 + 23 / (1+40*p.Time))))
                    * p.Amplitude*0.6;
                sample += Math.sin(p.Phase * (Math.PI * (4.9 + 4 / (1+3*p.Time))))
                    * p.Amplitude*0.4;
                if (sample < -1) sample = -1;
                else if (sample > 1) sample = 1;
                e.data.writeFloat(sample);
                e.data.writeFloat(sample);
                p.Update();
            }
        }
    }

    class SineWave implements WaveForm {
        public function Generate(e:SampleDataEvent,
            p:Parameters):void {
            for (var i:uint = 0; i < p.Samples; i++) {
                var sample:Number;
                sample = Math.sin(p.Phase * (Math.PI * 2))
                    * p.Amplitude;
                if (sample < -1) sample = -1;
                else if (sample > 1) sample = 1;
                e.data.writeFloat(sample);
                e.data.writeFloat(-sample);
                p.Update();
            }
        }
    }
}