flash on 2013-8-1

by tepe
♥0 | Line 61 | Modified 2013-08-06 12:39:09 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;
    import flash.media.*;
    import flash.utils.*;
    public class FlashTest extends Sprite {
        private var src_obj:Sound;
        public function FlashTest() {
            // write as3 code here..
            var s:Sprite = new Sprite();
            s.graphics.beginFill(0);
            s.graphics.drawRect(0,0,110,110);
            s.graphics.endFill();
            addChild(s);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,onkey);
            stage.addEventListener(KeyboardEvent.KEY_UP,offKey);
            src_obj = new Sound(url);
            src_obj.addEventListener(Event.COMPLETE,SoundCompleteFunc);
            
            //src_obj.load();
        }
        private var onF:Boolean;
        private function onkey(e:KeyboardEvent):void{
            if(onF==false){
                onF = true;
                ch = src_obj.play(0.1,1);
            }
            //ch = out_obj.play(0);
            //ch = src_obj.play();
        }
        private function offKey(e:KeyboardEvent):void{
            if(onF==true)onF=false;
        }


        // パラメータ
        private var sampling:int = 2048;        // 1度に転送するサンプリング数

        // ソース用サウンドオブジェクト作成
        private var url : URLRequest = new URLRequest("http://192.168.11.7/sound/a.mp3");
        
        // 出力用サウンドオブジェクト
        private var out_obj:Sound = null;
        private var ch:SoundChannel = null;
        
        

        private function SoundCompleteFunc(event:Event):void{
            // 出力用サウンドオブジェクト作成
            out_obj = new Sound();
            // 新しいオーディオデータ要求時に呼び出されるイベント
            out_obj.addEventListener(SampleDataEvent.SAMPLE_DATA, SampleDataFunc);
            // 再生開始
            //channel = out_obj.play();
        }
        
        private function play():void{
            var i:int;
            var data:Number;
            

        }



        // サウンドデータ要求時に呼び出されるイベント
        private function SampleDataFunc(event:SampleDataEvent):void{
            var i:int;
            // ソース用と出力用のバッファ
            var src_buffer:ByteArray = new ByteArray();
            var out_buffer:ByteArray = event.data;
    
            // サウンドデータを取得
            var get_sampling:int = src_obj.extract(src_buffer,sampling);

            // サウンドバッファが足りない場合は空白を埋める
            for(i=get_sampling;i < sampling;i++){
                src_buffer.writeFloat(0.0);
                src_buffer.writeFloat(0.0);
            }
            

            // サウンドデータを出力バッファにコピー
            var data:Number;
            src_buffer.position = 0;
            for(i=0;i < sampling;i++){
                // 左チャンネル
                data = src_buffer.readFloat();
                out_buffer.writeFloat(data);    
                // 右チャンネル
                data = src_buffer.readFloat();
                out_buffer.writeFloat(data);
            }
        }
    }
}