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: sound.extract()

www.takasumi-nagai.com/soundfiles/sound001.mp3";
左サンプリングデータ
右サンプリングデータ
サンプリング長
loadSound();
ダイヤル設定 
 表示位置
sound.close();
e.sound.close();
I/O エラー
データロード進捗率
 データロード完了
再生開始

画面準備 
データテーブル準備
オーディオ再生
毎フレーム処理
再生位置ライン

sampleLEFT
sampleRIGHT

リピート再生
波形描画の工程
画面クリア
W = Stage.width
画面全体更新
画面クリア
画面一部更新
var rect:Rectangle = new Rectangle(p2,0,W-p2,H);
var point:Point = new Point(0,0);
disp.copyPixels( disp, rect, point );
var rect2:Rectangle = new Rectangle(p2,0,W-p2,H);
disp.fillRect(rect2, 0x00ffff);//画面クリア
p = prev;
 0 → stage.width
high
low
high
low
scale / pixel
波のピーク
振り幅が小さい時

Btnクラス

カラーパターン
 White
Black
txt.embedFonts = true;
txt.antiAliasType = AntiAliasType.ADVANCED;
ホイールインターフェース
カラー設定
ホイールの色
つまみの色
影の色
つまみの色(ロールオーバー時)
つまみの色(操作不可)
プロパティ
つまみの角度
影
値
回転数
描画サイズ
最大値
小数点以下を求める場合はtrue
コンストラクタ
ホイールのつまみ部分
描画
位置関係
ホイール描画
つまみの描画
操作エリア
イベント:ロールオーバー
イベント:ロールアウト
イベント:マウスダウン
イベント:マウスアップ
操作完了
ダイヤル操作
イベント発生
ダイヤルの角度から値を決定
回転数
小数点以下の扱い
操作受付の状態 true:可 false:不可
操作可
操作不可
状態更新
描画1
描画2
外縁
内縁
イベント
Get Adobe Flash player
by tepe 21 Feb 2011
/**
 * Copyright tepe ( http://wonderfl.net/user/tepe )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/fupe
 */

// forked from matacat's sound.extract()
package
{
    import com.bit101.components.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.media.*;
    import flash.net.*;
    import flash.text.*;
    import flash.utils.*;
    
    [SWF(width = 465, height = 465, frameRate = 60)]
    public class Main extends Sprite {
        
        private const W:int  = stage.stageWidth;
        private const H:int  = stage.stageHeight >> 3;
        private const CL:int = H;
        private const CR:int = H * 3;
        
        private var sound:Sound = new Sound();
        private var sndch:SoundChannel;
        
        private var color:ColorTransform = new ColorTransform();
        
        private var waveL:Vector.<Number>;//左サンプリングデータ
        private var waveR:Vector.<Number>;//右サンプリングデータ
        private var length:uint;//サンプリング長
        
        private var disp:BitmapData = new BitmapData(W, H << 2);
        private var rect:Rectangle  = new Rectangle(0, 0, 1, 0);
        private var scale:int       = 1;
        
        private var wheel:Wheel = new Wheel();
        private var btn:Btn;
        private var file:FileReference = new FileReference();
        private var loader:MP3FileReferenceLoader = new MP3FileReferenceLoader();
        private var panel:Sprite = new Sprite();
        
        public function Main():void
        {
            //loadSound();
            
            panel.graphics.beginFill(0x000000,0.7); 
            panel.graphics.drawRect(0,350,W,100); 
            panel.graphics.endFill(); 
            addChild(panel);      
            
            //ダイヤル設定 
            wheel.x = 50;    wheel.y = 400;// 表示位置
            panel.addChild(wheel);
            wheel.max = 300;
            wheel.value = 50;
            wheel.addEventListener(CompoEvent.CHANGE, change);
            
            btn = new Btn();
            btn.init({type:1,width:60});
            btn.x = 150;
            btn.y = 400; 
            btn.text = "select File";
            panel.addChild(btn); 
            btn.addEventListener(MouseEvent.CLICK,function():void{
                loader.addEventListener(MP3SoundEvent.COMPLETE,onComplete);
                file.addEventListener(Event.SELECT,onSelect);               
                file.browse( [new FileFilter("mp3 File","*.mp3")] ); 
                
            });
            
        }
        
        private function onSelect(e:Event):void{
            
            loader.getSound(file); 
        }
        private function onComplete(e:MP3SoundEvent):void{
            sound = e.sound;
            playSound(); 
            loader.removeEventListener(Event.COMPLETE,onComplete); 
            file.removeEventListener(Event.SELECT,onSelect);
        }

        
        private function change(evt:CompoEvent):void {
            if(wheel.turn <0)scale = 1;
            else scale = (wheel.turn*wheel.max) + wheel.value + 1; 
     
        }
        
        private function loadSound():void
        {
            var tf:TextField = new TextField();
            tf.x        = stage.stageWidth - tf.width >> 1;
            tf.y        = stage.stageHeight - tf.height >> 1;
            tf.autoSize = TextFieldAutoSize.CENTER;
            tf.text     = "loading...";
            addChild(tf);
            
            // I/O エラー
            sound.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void
            {
                tf.text = e.text.replace(/ /g, "\n");
            });
            
            // データロード進捗率
            sound.addEventListener(ProgressEvent.PROGRESS, function(e:ProgressEvent):void
            {
                tf.text = "loading... " + (e.bytesLoaded / e.bytesTotal * 100).toFixed(0) + "%";
            });
            
            // データロード完了
            sound.addEventListener(Event.COMPLETE, function(e:Event):void
            {
                removeChild(tf);
                playSound();//再生開始
            });
            
            //sound.load(new URLRequest(URL), new SoundLoaderContext(1000, true));
        }
        
        //
        private function playSound():void
        {
            initDisp();//画面準備 
            initTable();//データテーブル準備
            
            sndch = sound.play();//オーディオ再生
            sndch.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);//毎フレーム処理
        }
        
        private function initDisp():void
        {
            
            //再生位置ライン
            graphics.lineStyle(3, 0x000080, 0.5);
            graphics.moveTo(W >> 1, 0);
            graphics.lineTo(W >> 1, H << 2);
     
            
            stage.addChildAt(new Bitmap(disp), 0);
        }
        
        //
        private function initTable():void
        {
            var bytes:ByteArray = new ByteArray();
            sound.extract(bytes, sound.length * 44.1);
            
            length = bytes.length >> 3;
            waveL  = new Vector.<Number>(length, true);
            waveR  = new Vector.<Number>(length, true);
            
            bytes.position = 0;
            for (var i:int = 0; i < length; i++) {
                waveL[i] = bytes.readFloat();//sampleLEFT
                waveR[i] = bytes.readFloat();//sampleRIGHT
            }
        }
        
        //
        private function onSoundComplete(e:Event):void
        {
            //リピート再生
            sndch.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
            sndch = sound.play();
            sndch.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
        }
        
        private var prev:Number = 0;
        //波形描画の工程
        private function onEnterFrame(e:Event):void
        {
            disp.lock();
            disp.fillRect(disp.rect, 0xffffff);//画面クリア
            //W = Stage.width
            var p:Number = sndch.position * 44.1 - (scale * W >> 1);
            var p2:Number;
            var col:ColorTransform =new ColorTransform();
            disp.lock();
            if(prev < p){//画面全体更新
                disp.fillRect(disp.rect, 0xffffff);//画面クリア
            }
            else{//画面一部更新
                p2 = (prev-p)/scale;
               // var rect:Rectangle = new Rectangle(p2,0,W-p2,H);
                //var point:Point = new Point(0,0);
                //disp.copyPixels( disp, rect, point );
                //var rect2:Rectangle = new Rectangle(p2,0,W-p2,H);
                //disp.fillRect(rect2, 0x00ffff);//画面クリア
                //p = prev;

            }
            
            
            for (var i:int = 0; i < W; i++, p += scale) {// 0 → stage.width
                var uL:Number = -1,//high
                    dL:Number =  1,//low
                    uR:Number = -1,//high
                    dR:Number =  1,//low
                    n:Number;
                
                for (var j:int = 0; j < scale; j++) {// scale / pixel
                    var k:int = p + j;
                    
                    //波のピーク
                    if (0<= k && k < length) {
                        n = waveL[k];
                        if (uL < n) uL = n;
                        if (n < dL) dL = n;
                        
                        n = waveR[k];
                        if (uR < n) uR = n;
                        if (n < dR) dR = n;
                        
                    } else {
                        uL = uR = dL = dR = 0;
                    }
                }
                
                rect.x = i;
                
                uL = uL * H + CL;
                dL = dL * H + CL;
                n  = uL - dL;
                
                if (n <= 1) {//振り幅が小さい時
                    col.color = 0x010066ff;
                    //col.redOffset = n*10;
                    //col.greenOffset = -n*10;
                    disp.setPixel32(i, uL, col.color);//
                } else {
                    col.color = 0xff00ff00;
                    //col.redOffset = n*10;
                    //col.greenOffset = -n*10;
                    col.alphaOffset = -n*30;
                    rect.y      = dL;
                    rect.height = n;
                    disp.fillRect(rect, 0xff00ff00); 
                }
                
                uR = uR * H + CR;
                dR = dR * H + CR;
                n  = uR - dR;
                if (n <= 1) {
                    col.color = 0x00006666;
                    col.redOffset = n*10;
                    col.greenOffset = -n*10;
                    disp.setPixel32(i, uR, col.color);//
                } else {
                    col.color = 0xdd006666;
                    col.redOffset = n*10;
                    col.greenOffset = -n*10;
                    rect.y      = dR;
                    rect.height = n;
                    disp.fillRect(rect, col.color); 
                }
            }
            prev = p;
            disp.unlock();
        }
    }
}

//////////////////////////////////////////////////
// Btnクラス
//////////////////////////////////////////////////


import flash.display.*;
import flash.text.*;
import flash.filters.GlowFilter;
import flash.events.MouseEvent;

class Btn extends Sprite {
    public var id:uint;
    private var shade:Shape;
    private var bottom:Shape;
    private var light:Shape;
    private var base:Shape;
    private var txt:TextField;
    private var label:String = "";
    private static var fontType:String = "_ゴシック";
    private var _width:uint = 60;
    private static var _height:uint = 20;
    private static var corner:uint = 5;
    private var type:uint = 1;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private static var upColor:uint = 0x666666;
    private static var overColor:uint = 0x333333;
    private static var offColor:uint = 0x999999;
    private static var gColor:uint = 0x00ff00;
    private var blueGlow:GlowFilter;
    private var shadeGlow:GlowFilter;
    private var _clicked:Boolean = false;
    private var _enabled:Boolean = true;

    public function Btn() {
    }

    public function init(option:Object):void {
        if (option.id != undefined) id = option.id;
        if (option.label != undefined) label = option.label;
        if (option.width != undefined) _width = option.width;
        if (option.type != undefined) type = option.type;
        draw();
    }
    private function draw():void {
        
        switch (type) {//カラーパターン
        case 1 :// White
            bColor = 0xFFFFFF;
            sColor = 0x000000;
            upColor = 0x666666;
            overColor = 0x333333;
            offColor = 0x999999;
            break;
        case 2 :// Black
            bColor = 0x000000;
            sColor = 0xFFFFFF;
            upColor = 0x666666;
            overColor = 0x999999;
            offColor = 0x333333;
            break;
        }
        
        blueGlow = new GlowFilter(gColor, 0.6, 5, 5, 2, 3, false, true);
        shadeGlow = new GlowFilter(sColor, 0.3, 4, 4, 2, 3, false, true);
        shade = new Shape();
        bottom = new Shape();
        light = new Shape();
        base = new Shape();
        txt = new TextField();
        addChild(shade);
        addChild(bottom);
        addChild(light);
        addChild(base);
        addChild(txt);
        createBase(shade, _width, _height, corner, sColor);
        shade.filters = [shadeGlow];
        createBase(bottom, _width, _height, corner, sColor, 0.3);
        createBase(light, _width, _height, corner, gColor);
        light.filters = [blueGlow];
        createBase(base, _width, _height, corner, bColor);
        txt.x = -_width*0.5;
        txt.y = -_height*0.5;
        txt.width = _width;
        txt.height = _height - 1;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        //txt.embedFonts = true;
        //txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = 12;
        tf.align = TextFormatAlign.CENTER;
        txt.defaultTextFormat = tf;
        txt.text = label;
        enabled = true;
        mouseChildren = false;
    }
    
    
    public function get text():String{
        return txt.text;
    }
    public function set text(str:String):void{
        txt.text = str;
    }


    
    private function rollOver(evt:MouseEvent):void {
        _over();
    }
    private function rollOut(evt:MouseEvent):void {
        _up();
    }
    private function press(evt:MouseEvent):void {
        _down();
    }
    private function release(evt:MouseEvent):void {
        _up();
    }
    private function click(evt:MouseEvent):void {
    }
    private function _up():void {
        txt.y = -_height*0.5;
        txt.textColor = upColor;
        base.y = -1;
        light.visible = false;
        light.y = -1;
    }
    private function _over():void {
        txt.y = -_height*0.5;
        txt.textColor = overColor;
        base.y = -1;
        light.visible = true;
        light.y = -1;
    }
    private function _down():void {
        txt.y = -_height*0.5 + 1;
        txt.textColor = overColor;
        base.y = 0;
        light.visible = true;
        light.y = 0;
    }
    private function _off():void {
        txt.y = -_height*0.5 + 1;
        txt.textColor = offColor;
        base.y = 0;
        light.visible = false;
        light.y = 0;
    }
    public function get clicked():Boolean {
        return _clicked;
    }
    public function set clicked(param:Boolean):void {
        _clicked = param;
        enabled = !_clicked;
        if (_clicked) {
            _down();
        } else {
            _up();
        }
    }
    public function get enabled():Boolean {
        return _enabled;
    }
    public function set enabled(param:Boolean):void {
        _enabled = param;
        buttonMode = _enabled;
        mouseEnabled = _enabled;
        useHandCursor = _enabled;
        if (_enabled) {
            _up();
            addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
            addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
            addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
            addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
            addEventListener(MouseEvent.CLICK, click, false, 0, true);
        } else {
            _off();
            removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
            removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
            removeEventListener(MouseEvent.MOUSE_DOWN, press);
            removeEventListener(MouseEvent.MOUSE_UP, release);
            removeEventListener(MouseEvent.CLICK, click);
        }
    }
    private function createBase(target:Shape, w:uint, h:uint, c:uint, color:uint, alpha:Number = 1):void {
        target.graphics.beginFill(color, alpha);
        target.graphics.drawRoundRect(-w*0.5, -h*0.5, w, h, c*2);
        target.graphics.endFill();
    }

}


import flash.display.Sprite;
import flash.display.Shape;
import flash.filters.DropShadowFilter;
import flash.geom.ColorTransform;
import flash.events.Event;
import flash.events.MouseEvent;


//ホイールインターフェース
class Wheel extends Sprite {
    private var base:Sprite;
    private var thumb:Sprite;
    private var point:Shape;
    private var hit:Shape;
    private var zeroPos:Shape;
    //カラー設定
    public var bColor:uint = 0xFFFFFF;//ホイールの色
    private var cColor:uint = 0x999999;//つまみの色
    private var sColor:uint = 0x000000;//影の色
    private var pColor:uint = 0x666666;//つまみの色(ロールオーバー時)
    private var offColor:uint = 0xCCCCCC;//つまみの色(操作不可)
    private var cColorTrans:ColorTransform;
    private var pColorTrans:ColorTransform;
    private var offColorTrans:ColorTransform;
    //プロパティ
    
    private var zero:Number = 0;
    private var angle:Number = 0;//つまみの角度
    private var shade:DropShadowFilter;//影
    private var initValue:Number = 0;//値
    public var value:Number = 0;
    public var turn:int = 0;//回転数
    private var _enabled:Boolean = true;
    public var size:uint = 30;//描画サイズ
    public var max:Number = 100;//最大値
    public var dec:Boolean = false;//小数点以下を求める場合はtrue

    //コンストラクタ
    public function Wheel() {
        shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 2, 3, false, false);
        cColorTrans = new ColorTransform();
        cColorTrans.color = cColor;
        pColorTrans = new ColorTransform();
        pColorTrans.color = pColor;
        offColorTrans = new ColorTransform();
        offColorTrans.color = offColor;
        
        base = new Sprite();
        thumb = new Sprite();
        point = new Shape();
        hit = new Shape();
        
        addChild(base);
        base.addChild(thumb);
        thumb.addChild(point);//ホイールのつまみ部分
        thumb.addChild(hit);
        addChild(thumb);

        draw();
    }

    //描画
    private function draw():void {
        //位置関係
        base.x = thumb.x = 0;
        base.y = thumb.y = 0;
        createDonut(base, size+size/3, size-size/3);//ホイール描画
        base.filters = [shade];
        point.x = size;
        createCircle(point, size/6, bColor, 1);//つまみの描画
        hit.x = size;
        createCircle(hit, size/3, bColor, 0);//操作エリア
        reset();
        _up();
        enabled = true;
        thumb.mouseChildren = false;
    }
    
    //イベント:ロールオーバー
    private function rollOver(evt:MouseEvent):void {
        _over();
    }
    
    //イベント:ロールアウト
    private function rollOut(evt:MouseEvent):void {
        _up();
    }
    
    //イベント:マウスダウン
    private function press(evt:MouseEvent):void {
        _down();
        thumb.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
        stage.addEventListener(MouseEvent.MOUSE_UP, releaseOutside, false, 0, true);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, change, false, 0, true);
        stage.addEventListener(Event.MOUSE_LEAVE, leave, false, 0, true);
    }
    
    //イベント:マウスアップ
    private function release(evt:MouseEvent):void {
        _up();
        checkValue();
        var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
        dispatchEvent(e);
        thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
        stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
        stage.removeEventListener(Event.MOUSE_LEAVE, leave);
    }
    
    private function releaseOutside(evt:MouseEvent):void {
        _up();
        checkValue();
        var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
        dispatchEvent(e);
        thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
        stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
        stage.removeEventListener(Event.MOUSE_LEAVE, leave);
    }
    
    //操作完了
    private function leave(evt:Event):void {
        _up();
        checkValue();
        var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
        dispatchEvent(e);
        thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
        stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
        stage.removeEventListener(Event.MOUSE_LEAVE, leave);
    }
    
    
    private function _up():void {
        point.transform.colorTransform = cColorTrans;
    }
    private function _over():void {
        point.transform.colorTransform = pColorTrans;
    }
    private function _down():void {
        point.transform.colorTransform = pColorTrans;
    }
    private function _off():void {
        point.transform.colorTransform = offColorTrans;
    }
    
    //ダイヤル操作
    private function change(evt:MouseEvent):void {
        _down();
        thumb.rotation = Math.atan2(base.mouseY, base.mouseX)/Math.PI*180;
        evt.updateAfterEvent();
        checkValue();
        var e:CompoEvent = new CompoEvent(CompoEvent.CHANGE, value);
        dispatchEvent(e);//イベント発生
    }
    
    //ダイヤルの角度から値を決定
    private function checkValue():void {
        var prev:Number = value;
        value = (((thumb.rotation+450-angle)%360)/360)*max;
        //回転数
        if(prev < max/2){
            if(max - max/3 < value)turn--;
        }
        else{
            if(value < max/3)turn++;
        }

 
        if(dec == false)value = Math.round(value);//小数点以下の扱い
    }
    
    
    //操作受付の状態 true:可 false:不可
    public function get enabled():Boolean {
        return _enabled;
    }
    
    public function set enabled(param:Boolean):void {
        _enabled = param;
        if (!_enabled) _off();
        thumb.buttonMode = _enabled;
        thumb.mouseEnabled = _enabled;
        thumb.useHandCursor = _enabled;
        
        if (_enabled) {//操作可
            thumb.addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
            thumb.addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
            thumb.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
        } else {//操作不可
            thumb.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
            thumb.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
            thumb.removeEventListener(MouseEvent.MOUSE_DOWN, press);
        }
    }
    
    //状態更新
    public function reset():void {
        value = initValue;
        thumb.rotation = value - 90 + zero;
    }
    
    //描画1
    private function createCircle(target:Shape, r:uint, color:uint, alpha:Number):void {
        target.graphics.beginFill(color, alpha);
        target.graphics.drawCircle(0, 0, r);
        target.graphics.endFill();
    }
    
    //描画2
    private function createDonut(target:Sprite, outer:uint, inner:uint):void {
        target.graphics.beginFill(bColor);
        target.graphics.drawCircle(0, 0, outer);//外縁
        target.graphics.drawCircle(0, 0, inner);//内縁
        target.graphics.endFill();
    }

}

//イベント
import flash.events.Event;
class CompoEvent extends Event {
    public static const SELECT:String = "select";
    public static const CHANGE:String = "change";
    public var value:*;

    public function CompoEvent(type:String, value:*) {
        super(type);
        this.value = value;
    }

    public override function clone():Event {
        return new CompoEvent(type, value);
    }

}


import flash.display.*;
import flash.events.*;
import flash.media.Sound;
import flash.net.FileReference;
import flash.utils.ByteArray;
import flash.utils.Endian;
    
[Event(name="complete", type="MP3SoundEvent")]
class MP3FileReferenceLoader extends EventDispatcher{
        private var mp3Parser:MP3Parser;
        
        public function MP3FileReferenceLoader(){
            mp3Parser=new MP3Parser();
            mp3Parser.addEventListener(Event.COMPLETE,parserCompleteHandler);
            
        }

        public function getSound(fr:FileReference):void    {
            
            mp3Parser.loadFileRef(fr);
        }
        private function parserCompleteHandler(ev:Event):void{
            var parser:MP3Parser=ev.currentTarget as MP3Parser;
            generateSound(parser);
        }
        private function generateSound(mp3Source:MP3Parser):Boolean{
            var swfBytes:ByteArray=new ByteArray();
            swfBytes.endian=Endian.LITTLE_ENDIAN;
            for(var i:uint=0;i<SoundClassSwfByteCode.soundClassSwfBytes1.length;++i){
                swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes1[i]);
            }
            var swfSizePosition:uint=swfBytes.position;
            swfBytes.writeInt(0); //swf size will go here
            for(i=0;i<SoundClassSwfByteCode.soundClassSwfBytes2.length;++i){
                swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes2[i]);
            }
            var audioSizePosition:uint=swfBytes.position;
            swfBytes.writeInt(0); //audiodatasize+7 to go here
            swfBytes.writeByte(1);
            swfBytes.writeByte(0);
            mp3Source.writeSwfFormatByte(swfBytes);
            
            var sampleSizePosition:uint=swfBytes.position;
            swfBytes.writeInt(0); //number of samples goes here
            
            swfBytes.writeByte(0); //seeksamples
            swfBytes.writeByte(0);
                        
            var frameCount:uint=0;
            
            var byteCount:uint=0; //this includes the seeksamples written earlier
                        
            for(;;){
            
                var seg:ByteArraySegment=mp3Source.getNextFrame();
                if(seg==null)break;
                swfBytes.writeBytes(seg.byteArray,seg.start,seg.length);
                byteCount+=seg.length;
                frameCount++;
            }
            if(byteCount==0){
                return false;
            }
            byteCount+=2;

            var currentPos:uint=swfBytes.position;
            swfBytes.position=audioSizePosition;
            swfBytes.writeInt(byteCount+7);
            swfBytes.position=sampleSizePosition;
            swfBytes.writeInt(frameCount*1152);
            swfBytes.position=currentPos;
            for(i=0;i<SoundClassSwfByteCode.soundClassSwfBytes3.length;++i){
                swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes3[i]);
            }
            swfBytes.position=swfSizePosition;
            swfBytes.writeInt(swfBytes.length);
            swfBytes.position=0;
            var swfBytesLoader:Loader=new Loader();
            swfBytesLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfCreated);
            swfBytesLoader.loadBytes(swfBytes);
            return true;
        }
        private function swfCreated(ev:Event):void{

            var loaderInfo:LoaderInfo=ev.currentTarget as LoaderInfo;
            var soundClass:Class=loaderInfo.applicationDomain.getDefinition("SoundClass") as Class;
            var sound:Sound=new soundClass();
            dispatchEvent(new MP3SoundEvent(MP3SoundEvent.COMPLETE,sound));
            
        }
}

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

class MP3SoundEvent extends Event{

        public var sound:Sound;
        
        public static const COMPLETE:String="complete";
        public function MP3SoundEvent(type:String, sound:Sound, bubbles:Boolean=false, cancelable:Boolean=false){
            super(type, bubbles, cancelable);
            this.sound=sound;
        }
        
}


import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.net.FileReference;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
    
[Event(name="complete", type="flash.events.Event")]
class MP3Parser extends EventDispatcher{
        private var mp3Data:ByteArray;
        private var loader:URLLoader;
        private var currentPosition:uint;
        private var sampleRate:uint;
        private var channels:uint;
        private var version:uint;
        private static var bitRates:Array=[-1,32,40,48,56,64,80,96,112,128,160,192,224,256,320,-1,-1,8,16,24,32,40,48,56,64,80,96,112,128,144,160,-1];
        private static var versions:Array=[2.5,-1,2,1];
        private static var samplingRates:Array=[44100,48000,32000];
        public function MP3Parser(){
            
            loader=new URLLoader();
            loader.dataFormat=URLLoaderDataFormat.BINARY;
            loader.addEventListener(Event.COMPLETE,loaderCompleteHandler);
            
            
        }
        internal function load(url:String):void{
            var req:URLRequest=new URLRequest(url);
            loader.load(req);
        }
        internal function loadFileRef(fileRef:FileReference):void{
            fileRef.addEventListener(Event.COMPLETE,loaderCompleteHandler);
            fileRef.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
            //fileRef.addEventListener(Event.COMPLETE,loaderCompleteHandler);
            fileRef.load();
        }
        private function errorHandler(ev:IOErrorEvent):void{
            trace("error\n"+ev.text);
        }
        private function loaderCompleteHandler(ev:Event):void{
            mp3Data=ev.currentTarget.data as ByteArray;
            currentPosition=getFirstHeaderPosition();
            dispatchEvent(ev);
        }
        private function getFirstHeaderPosition():uint{
            mp3Data.position=0;
            
            
            while(mp3Data.position<mp3Data.length){
                var readPosition:uint=mp3Data.position;
                var str:String=mp3Data.readMultiByte(3,"us-ascii");
                
                
                if(str=="ID3"){
                    mp3Data.position+=3;
                    var b3:int=(mp3Data.readByte()&0x7F)<<21;
                    var b2:int=(mp3Data.readByte()&0x7F)<<14;
                    var b1:int=(mp3Data.readByte()&0x7F)<<7;
                    var b0:int=mp3Data.readByte()&0x7F;
                    var headerLength:int=b0+b1+b2+b3;
                    var newPosition:int=mp3Data.position+headerLength;
                    trace("Found id3v2 header, length "+headerLength.toString(16)+" bytes. Moving to "+newPosition.toString(16));
                    mp3Data.position=newPosition;
                    readPosition=newPosition;
                }
                else{
                    mp3Data.position=readPosition;
                }
                
                var val:uint=mp3Data.readInt();
                
                if(isValidHeader(val)){
                    parseHeader(val);
                    mp3Data.position=readPosition+getFrameSize(val);
                    if(isValidHeader(mp3Data.readInt())){
                        return readPosition;
                    }
                    
                }

            }
            throw(new Error("Could not locate first header. This isn't an MP3 file"));
        }
        internal function getNextFrame():ByteArraySegment{
            mp3Data.position=currentPosition;
            var headerByte:uint;
            var frameSize:uint;    
            while(true){
                if(currentPosition>(mp3Data.length-4)){
                    trace("passed eof");
                    return null;
                }
                headerByte=mp3Data.readInt();
                if(isValidHeader(headerByte)){
                    frameSize=getFrameSize(headerByte);
                    if(frameSize!=0xffffffff){
                        break;
                    }
                }
                currentPosition=mp3Data.position;
                
            }

            mp3Data.position=currentPosition;
            
            if((currentPosition+frameSize)>mp3Data.length){
                return null;
            }
            
            currentPosition+=frameSize;
            return new ByteArraySegment(mp3Data,mp3Data.position,frameSize);
        }
        internal function writeSwfFormatByte(byteArray:ByteArray):void{
            var sampleRateIndex:uint=4-(44100/sampleRate);
            byteArray.writeByte((2<<4)+(sampleRateIndex<<2)+(1<<1)+(channels-1));
        }
        private function parseHeader(headerBytes:uint):void{
            var channelMode:uint=getModeIndex(headerBytes);
            version=getVersionIndex(headerBytes);
            var samplingRate:uint=getFrequencyIndex(headerBytes);
            channels=(channelMode>2)?1:2;
            var actualVersion:Number=versions[version];
            var samplingRates:Array=[44100,48000,32000];
            sampleRate=samplingRates[samplingRate];
            switch(actualVersion){
                case 2:
                    sampleRate/=2;
                    break;
                case 2.5:
                    sampleRate/=4;
            }
            
        }
        private function getFrameSize(headerBytes:uint):uint{
            
            
            var version:uint=getVersionIndex(headerBytes);
            var bitRate:uint=getBitrateIndex(headerBytes);
            var samplingRate:uint=getFrequencyIndex(headerBytes);
            var padding:uint=getPaddingBit(headerBytes);
            var channelMode:uint=getModeIndex(headerBytes);
            var actualVersion:Number=versions[version];
            var sampleRate:uint=samplingRates[samplingRate];
            if(sampleRate!=this.sampleRate||this.version!=version){
                return 0xffffffff;
            }
            switch(actualVersion){
                case 2:
                    sampleRate/=2;
                    break;
                case 2.5:
                    sampleRate/=4;
            }
            var bitRatesYIndex:uint=((actualVersion==1)?0:1)*bitRates.length/2;
            var actualBitRate:uint=bitRates[bitRatesYIndex+bitRate]*1000;            
            var frameLength:uint=(((actualVersion==1?144:72)*actualBitRate)/sampleRate)+padding;
            return frameLength;
            
        }
        
         private function isValidHeader(headerBits:uint):Boolean{
            return (((getFrameSync(headerBits)      & 2047)==2047) &&
                    ((getVersionIndex(headerBits)   &    3)!=   1) &&
                    ((getLayerIndex(headerBits)     &    3)!=   0) && 
                    ((getBitrateIndex(headerBits)   &   15)!=   0) &&
                    ((getBitrateIndex(headerBits)   &   15)!=  15) &&
                    ((getFrequencyIndex(headerBits) &    3)!=   3) &&
                    ((getEmphasisIndex(headerBits)  &    3)!=   2)    );
        }
    
        private function getFrameSync(headerBits:uint):uint{
            return uint((headerBits>>21) & 2047); 
        }
    
        private function getVersionIndex(headerBits:uint):uint{ 
            return uint((headerBits>>19) & 3);  
        }
    
        private function getLayerIndex(headerBits:uint):uint{ 
            return uint((headerBits>>17) & 3);  
        }
    
        private function getBitrateIndex(headerBits:uint):uint{ 
            return uint((headerBits>>12) & 15); 
        }
    
        private function getFrequencyIndex(headerBits:uint):uint{ 
            return uint((headerBits>>10) & 3);  
        }
    
        private function getPaddingBit(headerBits:uint):uint{ 
            return uint((headerBits>>9) & 1);  
        }
    
        private function getModeIndex(headerBits:uint):uint{ 
            return uint((headerBits>>6) & 3);  
        }
    
        private function getEmphasisIndex(headerBits:uint):uint{ 
            return uint(headerBits & 3);  
        }
        
}

import flash.utils.ByteArray;
    
class ByteArraySegment    {
        public var start:uint;
        public var length:uint;
        public var byteArray:ByteArray;
        public function ByteArraySegment(ba:ByteArray,start:uint,length:uint){
            byteArray=ba;
            this.start=start;
            this.length=length;
        }
}


    
class SoundClassSwfByteCode{
    
        internal static const silentMp3Frame:Array=
        [
            0xFF , 0xFA , 0x92 , 0x40 , 0x78 , 0x05 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00,
            0x4B , 0x80 , 0x00 , 0x00 , 0x08 , 0x00 , 0x00 , 0x09 , 0x70 , 0x00 , 0x00,
            0x01 , 0x00 , 0x00 , 0x01 , 0x2E , 0x00 , 0x00 , 0x00 , 0x20 , 0x00 , 0x00,
            0x25 , 0xC0 , 0x00 , 0x00 , 0x04 , 0xB0 , 0x04 , 0xB1 , 0x00 , 0x06 , 0xBA,
            0xA8 , 0x22 , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF,
            0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF
        ]
        
        internal static const soundClassSwfBytes1:Array = [ 0x46 , 0x57 , 0x53 , 0x09 ];
        
        internal static const soundClassSwfBytes2:Array =
        [    
            0x78 , 0x00 , 0x05 , 0x5F , 0x00 , 0x00 , 0x0F , 0xA0 , 
            0x00 , 0x00 , 0x0C , 0x01 , 0x00 , 0x44 , 0x11 , 0x08 , 
            0x00 , 0x00 , 0x00 , 0x43 , 0x02 , 0xFF , 0xFF , 0xFF , 
            0xBF , 0x15 , 0x0B , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 
            0x53 , 0x63 , 0x65 , 0x6E , 0x65 , 0x20 , 0x31 , 0x00 , 
            0x00 , 0xBF , 0x14 , 0xC8 , 0x00 , 0x00 , 0x00 , 0x00 , 
            0x00 , 0x00 , 0x00 , 0x00 , 0x10 , 0x00 , 0x2E , 0x00 , 
            0x00 , 0x00 , 0x00 , 0x08 , 0x0A , 0x53 , 0x6F , 0x75 , 
            0x6E , 0x64 , 0x43 , 0x6C , 0x61 , 0x73 , 0x73 , 0x00 , 
            0x0B , 0x66 , 0x6C , 0x61 , 0x73 , 0x68 , 0x2E , 0x6D , 
            0x65 , 0x64 , 0x69 , 0x61 , 0x05 , 0x53 , 0x6F , 0x75 , 
            0x6E , 0x64 , 0x06 , 0x4F , 0x62 , 0x6A , 0x65 , 0x63 , 
            0x74 , 0x0F , 0x45 , 0x76 , 0x65 , 0x6E , 0x74 , 0x44 , 
            0x69 , 0x73 , 0x70 , 0x61 , 0x74 , 0x63 , 0x68 , 0x65 , 
            0x72 , 0x0C , 0x66 , 0x6C , 0x61 , 0x73 , 0x68 , 0x2E , 
            0x65 , 0x76 , 0x65 , 0x6E , 0x74 , 0x73 , 0x06 , 0x05 , 
            0x01 , 0x16 , 0x02 , 0x16 , 0x03 , 0x18 , 0x01 , 0x16 , 
            0x07 , 0x00 , 0x05 , 0x07 , 0x02 , 0x01 , 0x07 , 0x03 , 
            0x04 , 0x07 , 0x02 , 0x05 , 0x07 , 0x05 , 0x06 , 0x03 , 
            0x00 , 0x00 , 0x02 , 0x00 , 0x00 , 0x00 , 0x02 , 0x00 , 
            0x00 , 0x00 , 0x02 , 0x00 , 0x00 , 0x01 , 0x01 , 0x02 , 
            0x08 , 0x04 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 , 
            0x02 , 0x01 , 0x01 , 0x04 , 0x01 , 0x00 , 0x03 , 0x00 , 
            0x01 , 0x01 , 0x05 , 0x06 , 0x03 , 0xD0 , 0x30 , 0x47 , 
            0x00 , 0x00 , 0x01 , 0x01 , 0x01 , 0x06 , 0x07 , 0x06 , 
            0xD0 , 0x30 , 0xD0 , 0x49 , 0x00 , 0x47 , 0x00 , 0x00 , 
            0x02 , 0x02 , 0x01 , 0x01 , 0x05 , 0x1F , 0xD0 , 0x30 , 
            0x65 , 0x00 , 0x5D , 0x03 , 0x66 , 0x03 , 0x30 , 0x5D , 
            0x04 , 0x66 , 0x04 , 0x30 , 0x5D , 0x02 , 0x66 , 0x02 , 
            0x30 , 0x5D , 0x02 , 0x66 , 0x02 , 0x58 , 0x00 , 0x1D , 
            0x1D , 0x1D , 0x68 , 0x01 , 0x47 , 0x00 , 0x00 , 0xBF , 
            0x03 
        ];
        
        internal static const soundClassSwfBytes3:Array=
        [ 
            0x3F , 0x13 , 0x0F , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 
            0x01 , 0x00 , 0x53 , 0x6F , 0x75 , 0x6E , 0x64 , 0x43 , 
            0x6C , 0x61 , 0x73 , 0x73 , 0x00 , 0x44 , 0x0B , 0x0F , 
            0x00 , 0x00 , 0x00 , 0x40 , 0x00 , 0x00 , 0x00 
        ];

}