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

サウンド波形 SoundMixer.computeSpectrum

音源はこちらから頂きました。http://www.mp3.com/free-music/
/**
 * Copyright hacker_szoe51ih ( http://wonderfl.net/user/hacker_szoe51ih )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/qMzl
 */

/*
音源はこちらから頂きました。http://www.mp3.com/free-music/
*/

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.JointStyle;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.system.*;
    import flash.geom.Matrix;
    import flash.media.Sound;
    import flash.media.SoundLoaderContext;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    
    
     [SWF(width=465,height=465,backgroundColor=0x0,frameRate=45)]
    public class Main extends Sprite
    {
        
        public var pass:String = "http://custom-base.oops.jp/demo/wonderfl/sound/jamesfigurine-allwaychina-erlendoye.mp3";
        public var sound1:Sound;
        public var soundCh:SoundChannel;
        public var channelLength:Number = 256;
        
        public var nomalSoundSp:Sprite;
        public var fftSoundSp:Sprite;
        public var byteArray:ByteArray;
        public var byteFFTArray:ByteArray;
        
        public var onoff:Boolean = true;
        
        public var leftCol:uint = 0xffffff;
        
        public var W:Number = stage.stageWidth;
        public var H:Number = stage.stageHeight;
        
        
        public function Main() {
            
                init();
            
            }
            
            public function init():void {
                Security.loadPolicyFile("http://custom-base.oops.jp/crossdomain.xml");
                byteArray = new ByteArray();
                byteFFTArray = new ByteArray();
                
                //通常の波形データを表示する用
                nomalSoundSp = new Sprite();
                nomalSoundSp.x = W/2-256/2;
                nomalSoundSp.y = H / 4 + 30;
                addChild(nomalSoundSp);
                
                //FFTされた波形データを表示する用
                fftSoundSp = new Sprite();
                fftSoundSp.x = W/2-256/2;
                fftSoundSp.y = H / 4*3+40;
                addChild(fftSoundSp);
                
                //soundを読み込む
                sound1 = new Sound();
                var context:SoundLoaderContext = new SoundLoaderContext();
                sound1.addEventListener(Event.COMPLETE, loadComplete);
                sound1.load(new URLRequest(pass),context);                
            }
            
            public function loadComplete(e:Event):void {
                soundCh = sound1.play();
                soundCh.addEventListener(Event.SOUND_COMPLETE, soundComplete);
                stage.addEventListener(Event.ENTER_FRAME, myEnterFrame);
                stage.addEventListener(MouseEvent.CLICK, onClick);
            }
            
            public function myEnterFrame(e:Event):void {
                writeSoundData();
            }
            
            public function writeSoundData():void {
                SoundMixer.computeSpectrum(byteArray);//通常の波形データを表示する用
                SoundMixer.computeSpectrum(byteFFTArray, true);//FFTされた波形データを表示する用
                createImage();
            }
            
            public function createImage():void {
                //描画初期化
                nomalSoundSp.graphics.clear();
                nomalSoundSp.graphics.lineStyle(1,leftCol);
                nomalSoundSp.graphics.moveTo(-1, 0);
                
                fftSoundSp.graphics.clear();
                fftSoundSp.graphics.lineStyle(1, leftCol);
                fftSoundSp.graphics.beginFill(0xffffff);
                fftSoundSp.graphics.moveTo(0, 0);
                
                //パラメーターセット
                var i:int;
                var moveY:Number = 0;
                
                for (i = 0; i < channelLength;i++ ) {
                    moveY = byteArray.readFloat() * H / 4;//byteArrayから浮動小数値をreadFloatで取得してmoveYに入れる
                    nomalSoundSp.graphics.lineTo(i, moveY);
                }
                
                var moveY2:Number = 0;
                
                for (i = 0; i < channelLength;i++ ) {
                    moveY2 = byteFFTArray.readFloat() * H / 4;//byteArrayから浮動小数値をreadFloatで取得してmoveYに入れる
                    fftSoundSp.graphics.lineTo(i, -moveY2);
                }
                fftSoundSp.graphics.lineTo(i, 0);
                fftSoundSp.graphics.endFill();
            }
            
            //サウンドを全て再生したら終了
            public function soundComplete(e:Event=null):void {
                stage.removeEventListener(Event.ENTER_FRAME, myEnterFrame);
                soundCh.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
            }
            
            //on off
            public function onClick(e:MouseEvent):void {
                if (onoff) {
                    soundCh.stop();
                    onoff = false;
                }else {
                    soundCh = sound1.play();
                    onoff = true;
                }
            }
            
        
    }
    
    
}