ピー音ジェネレータ

by taiga
♥0 | Line 51 | Modified 2009-06-03 15:57:00 | MIT License
play

ActionScript3 source code

/**
 * Copyright taiga ( http://wonderfl.net/user/taiga )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/biCy
 */

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<!---
ピー音ジェネレータ
@author taiga
試作品
再生時間を設定して、MP3 化して保存できるようにしたいかな…と。
-->
    <mx:Script>
        <![CDATA[
            public static const T_PI          :Number = Math.PI * 2;
            public static const SAMPLING_RATE :Number = 44100;
            protected var sound               :Sound;
            protected var soundChannel        :SoundChannel;
            protected var phase               :Number;
            protected override function createChildren():void {
                super.createChildren();
                phase = 0;
                sound = new Sound();
                sound.addEventListener(SampleDataEvent.SAMPLE_DATA, sineWaveGenerator);
                playButton.enabled = true;
            }
            protected function sineWaveGenerator(event:SampleDataEvent):void {
                var i           :int;
                var phaseAngle_ :Number;
                var sample_     :Number;
                for (i = 0; i < 8192; i++) {
                    phase += 1000 / SAMPLING_RATE;
                    phaseAngle_ = phase * T_PI;
                    sample_ = Math.sin(phaseAngle_) / 10;
                    event.data.writeFloat(sample_);
                    event.data.writeFloat(sample_);
                }
            }
            public function onPlay():void {
                soundChannel = sound.play();
                playButton.enabled = false;
                stopButton.enabled = true;
            }
            public function onStop():void {
                soundChannel.stop();
                playButton.enabled = true;
                stopButton.enabled = false;
            }
        ]]>
    </mx:Script>
    <!--- 再生ボタン -->
    <mx:Button id="playButton" label="play()" click="onPlay()" enabled="false" />
    <!--- 停止ボタン -->
    <mx:Button id="stopButton" label="stop()" click="onStop()" enabled="false" />
</mx:Application>