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 kinkuma_design's 位相で変わるのこぎり波とか
package  
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;

    public class Main extends Sprite
    {
        public static const SINE:String = "sine";
        public static const SAW:String = "saw";
        public static const TRIANGLE:String = "triangle";
        public static const TRIANGLE2:String = "triangle2";
        public static const SQUARE:String = "square";
        public static const NOISE:String = "noise";
        public static const PI2:Number = Math.PI * 2;
        public static const SAMPLE_RATE:uint = 44100;
        
        private var sound:Sound;
        private var bufferSize:uint; //2048~8192
        private var amp:Number; //音量
        public var phase:Number; //位相
        public var frequency:Number; //周波数 Hz
        public var waveShape:String; //波形
        
        public function Main() 
        {
            init();
        }
        
        private function init():void
        {
            bufferSize = 2048;
            phase = 0;

            //この辺変えてみてください
            amp = 0.1;
            waveShape = TRIANGLE2;
            frequency = 440;
            //--------------------           

            sound = new Sound();
            sound.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleDataEventHd);
            sound.play();
        }
        
        private function sampleDataEventHd(e:SampleDataEvent):void 
        {
            var i:uint;
            var sample:Number;
            for(i = 0; i < bufferSize; i++)
            {
                sample = getSample(i) * amp;
                if(sample > 1)
                {
                    sample = 1;
                }
                else if(sample < -1)
                {
                    sample = -1;
                }
                e.data.writeFloat(sample);
                e.data.writeFloat(sample);
                addPhase();
            }
        }
        
        private function getSample(i:uint):Number
        {
            var myT:uint = 35;
            var sample:Number;
            switch(waveShape)
            {
                //サイン波
                case SINE:
                    sample = Math.sin(phase);
                break;
                
                //のこぎり波
                case SAW:
                    sample = - phase / Math.PI + 1; 
                break;
                
                //三角波
                case TRIANGLE:
                    sample = (phase < Math.PI) ? -2 / Math.PI * phase + 1 : 2 / Math.PI * phase - 3; 
                break;
                
                //矩形波
                case SQUARE:
                    sample = (phase < Math.PI) ? -1 : 1;
                break;
                
                //ノイズ
                case NOISE:
                    sample = Math.random() * 2 - 1;
                break;
                case TRIANGLE2:
                    if( i%(myT*2) >= myT ){
		        sample = i%myT;
		    }else{
		        sample = myT- i%myT;
		    }
                break;
                
                default: 
                    sample = 0;
                break;
            }
            return sample;
        }
        
        private function addPhase():void
        {
            var dPhase:Number = PI2 * frequency / SAMPLE_RATE;
            phase += dPhase;
            if(phase > PI2)
            {
                phase = phase % PI2;
            }
        }
    }
}