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

画像のWAVファイル化

画像をオシロスコープ用の音声信号に変換します。
使用例 http://www.youtube.com/watch?v=1h8QF0g4kPw
Get Adobe Flash player
by TheCoolMuseum 10 Dec 2009
/**
 * Copyright TheCoolMuseum ( http://wonderfl.net/user/TheCoolMuseum )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/q0HQ
 */

// forked from TheCoolMuseum's ローカルファイルの読み込み・保存
// 画像をオシロスコープ用の音声信号に変換します。
// 使用例 http://www.youtube.com/watch?v=1h8QF0g4kPw

package {
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.ByteArray;
    import flash.utils.Endian;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.net.FileReference;
    import flash.net.FileFilter;
    
    public class FlashTest extends Sprite {
        private var fileLoader:FileReference;
        private var fileSaver:FileReference;
        private var imageLoader:Loader;
        private var wavBuffer:ByteArray;
        
        public function makeBtn(name:String):TextField{
           var btn:TextField = new TextField();
            btn.text = name;
            btn.autoSize = TextFieldAutoSize.LEFT;
            btn.border = true;
            btn.selectable = false;
            return btn;
        } 
        public function FlashTest() {
            var loadBtn:TextField = makeBtn("LOAD");
            addChild(loadBtn);
            loadBtn.addEventListener(MouseEvent.CLICK, onClickLoadBtn);

            var saveBtn:TextField = makeBtn("SAVE");
            saveBtn.y = 32;
            addChild(saveBtn);
            saveBtn.addEventListener(MouseEvent.CLICK, onClickSaveBtn);
        }
        private function onClickLoadBtn(e:Event):void{
            load();
        }
        private function load():void{
            var fileFilter:FileFilter = new FileFilter("Images (.png .jpg .gif)", "*.png;*.jpg;*.gif");

           	fileLoader = new FileReference();
        	fileLoader.addEventListener(Event.SELECT, onSelectLoadFile);
        	fileLoader.addEventListener(Event.COMPLETE, onCompleteLoadFile);
        	
        	fileLoader.browse([fileFilter]);
        }
        private function onSelectLoadFile(e:Event):void{
            fileLoader.load();
        }
        private function onCompleteLoadFile(e:Event):void{
            imageLoader = new Loader();
            imageLoader.loadBytes(fileLoader.data);
            imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadImage);
        }
        private function onCompleteLoadImage(e:Event):void{
            var width:int = imageLoader.width;
            var height:int = imageLoader.height;
            var bitmapData:BitmapData = new BitmapData(width, height);
            bitmapData.draw(imageLoader);
            
            var dataBuffer:ByteArray = new ByteArray();
            dataBuffer.endian = Endian.LITTLE_ENDIAN
            for(var v:int=0; v<height; v++){
                for(var u:int=0; u<width; u++){
                    var color:uint = bitmapData.getPixel(u, v);
                    var c:Number = (color&0xff) + ((color>>8)&0xff) + ((color>>16)&0xff);
                    if(c>384){
                        var l:int = 32768-int(v/height*65535);
                        var r:int = int(u/width*65535)-32768;
                        dataBuffer.writeShort(l); 
                        dataBuffer.writeShort(r); 
                    }
                }
            }
            var fmtBuffer:ByteArray = new ByteArray();
            fmtBuffer.endian = Endian.LITTLE_ENDIAN
            fmtBuffer.writeShort(1); //PCM
            fmtBuffer.writeShort(2); //Stereo
            fmtBuffer.writeUnsignedInt(44100); //SampleRate
            fmtBuffer.writeUnsignedInt(44100*4); //DataRate
            fmtBuffer.writeShort(4); //4Byte = 16bit*Stereo
            fmtBuffer.writeShort(16); //16bit

            var loopBuffer:ByteArray = new ByteArray();
            loopBuffer.endian = Endian.LITTLE_ENDIAN
            loopBuffer.writeUnsignedInt(0); //Manufacturer
            loopBuffer.writeUnsignedInt(0); //Product
            loopBuffer.writeUnsignedInt(0); //Sample Period
            loopBuffer.writeUnsignedInt(0x3C); //MIDI Unity Note
            loopBuffer.writeUnsignedInt(0); //MIDI Pitch Fraction
            loopBuffer.writeUnsignedInt(0); //SMPTE Format
            loopBuffer.writeUnsignedInt(0); //SMPTE Offset
            loopBuffer.writeUnsignedInt(1); //Num Sample Loops
            loopBuffer.writeUnsignedInt(0); //Sampler Data
            loopBuffer.writeUnsignedInt(0); //Cue Point ID
            loopBuffer.writeUnsignedInt(0); //Type
            loopBuffer.writeUnsignedInt(0); //Start
            loopBuffer.writeUnsignedInt(dataBuffer.length/4); //End
            loopBuffer.writeUnsignedInt(0); //Fraction
            loopBuffer.writeUnsignedInt(0); //Play Count

            wavBuffer = new ByteArray();
            wavBuffer.endian = Endian.LITTLE_ENDIAN

            wavBuffer.writeUTFBytes("RIFF")	;
            wavBuffer.writeUnsignedInt(36 + fmtBuffer.length + dataBuffer.length + loopBuffer.length);
            
            wavBuffer.writeUTFBytes("WAVE")	;
            
            wavBuffer.writeUTFBytes("fmt ")	;
            wavBuffer.writeUnsignedInt(fmtBuffer.length);
            wavBuffer.writeBytes(fmtBuffer);
            
            wavBuffer.writeUTFBytes("data")	;
            wavBuffer.writeUnsignedInt(dataBuffer.length);
            wavBuffer.writeBytes(dataBuffer);
            
            wavBuffer.writeUTFBytes("smpl")	;
            wavBuffer.writeUnsignedInt(loopBuffer.length);
            wavBuffer.writeBytes(loopBuffer);
            
        }
        private function onClickSaveBtn(e:Event):void{
            save();
        }
        private function save():void{
           	fileSaver = new FileReference();
           	fileSaver.save(wavBuffer, fileLoader.name.split(".")[0]+".wav");
        }
    }
}