flash on 2010-8-21

by m0ose
♥0 | Line 122 | Modified 2010-08-21 05:29:06 | MIT License
play

ActionScript3 source code

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

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import org.osmf.layout.AbsoluteLayoutFacet;
            
            public var length:int = 64;
            public var freq_divisions:int = 60;
            
            public var min_freq:int = 200 ;
            public var max_freq:int = 2000 ;
            
            
            public var seq_img:BitmapData = new BitmapData( length, freq_divisions, false, 0x000000);
            
            
            public static const SAMPLING_RATE:int = 44100;
            public static const TWO_PI:Number = 2*Math.PI;
            
            public var tone:Sound;
            public var tonechannel:SoundChannel = new SoundChannel();
            public var position:int = 0;

            
            public function clear_img():void
            {
                seq_img.fillRect( seq_img.rect, 0x000000);
            }
            public function stopTone():void {
                
                tone.removeEventListener(SampleDataEvent.SAMPLE_DATA, generateSineTone);
                stage.removeChild( stage.getChildByName( "_bar"));
                
            }
            
            public function startTone():void {
                
                clear_img();
                // draw :)
                var s:Shape = new Shape();
                s.graphics.lineStyle( 1, 0xffffff);
                s.graphics.drawCircle( 10,5,3);
                s.graphics.drawCircle( 10, 15, 3);
                s.graphics.drawEllipse(20, 10, 3,10);
                seq_img.draw(s);
                //..draw :)
                
                tone = new Sound();
                
                init();//intit graphics
                
                tone.addEventListener(SampleDataEvent.SAMPLE_DATA, generateSineTone);
                
                tonechannel = tone.play()
                
                var scr_refresher:Timer = new Timer( 1000 / 50);
                scr_refresher.addEventListener(TimerEvent.TIMER, refreshScreen);
                scr_refresher.start();
            }
            
            private function frequency( y:int):int
            {
                return Math.round( max_freq - (  y * (max_freq - min_freq) / (freq_divisions + 1) ) );
            }
            
            //public var latency:int = 0;
            
            public function generateSineTone(e:SampleDataEvent):void {
                
                //latency = ((e.data.position / 44.1) - tonechannel.position);
                var amplitude:Number = 0.0;
                var PI2_O_SR:Number = (2*Math.PI / SAMPLING_RATE);
                
                //
                //PRE FILL ARRAY WITH PIXEL COLORS
                //          FOR SPEED.
                var col:Array = new Array( seq_img.height);
                for( var f1:int=0; f1 < seq_img.height; f1++)
                {
                    if( seq_img.getPixel( position , f1) != 0x000000)
                    {
                        col[f1] = 1;    
                    }
                    else
                    {
                        col[f1] = 0;
                    }
                }
                
                // MAKE THE SIN WAVE
                var samples_n:int = 8192;
                
                for(var i:int=0;i< samples_n ;i++)
                {
                    for( var f:int=0; f < col.length; f++)
                    {
                        if( col[f] == 1)
                        {
                            amplitude += Math.sin( (i + e.position) * frequency(f) * PI2_O_SR ) / (freq_divisions );            
                        }
                    }
                    e.data.writeFloat( amplitude  );//left
                    e.data.writeFloat( amplitude  );//right
                    
                
                }

                
                position++ ;
                if( position >= seq_img.width){
                    position = 0;
                }
                
            }
    
            //
            //
            //
            //DRAWING

            private var drawing:Boolean = false;
            
            public function init():void
            {
                _img.source= new Bitmap( seq_img);
                
                _img.scaleX = stage.width / seq_img.width;
                _img.scaleY = stage.height / seq_img.height;
                var bar:Shape = new Shape();
                bar.graphics.lineStyle( 5, 0xff0000, 0.8);
                bar.graphics.drawRect( 0, 0 , 6, seq_img.height * _img.scaleY + 10 );
                
                stage.addChild( bar).name = "_bar";
                
            }
            
            
            public function startDraw(e:MouseEvent):void
            {
                drawing = true;
                draw( e);
            }
            public function stopDraw( e:MouseEvent):void
            {
                drawing = false;
            }
            public function draw( e:MouseEvent):void
            {
                if( drawing)
                {
                    //_log.text += " draw " + e.localX + "," + e.localY;
                    seq_img.setPixel( Math.round(e.localX)  , Math.round(e.localY) , 0xffffff );
                    refreshScreen();
                    //_img.source = new Bitmap( seq_img);
                }
            }
            public function refreshScreen( e:TimerEvent = null):void
            {
                var dispImg:BitmapData = seq_img.clone();
                
                stage.getChildByName("_bar").x =  (((position - 4) + seq_img.width ) % seq_img.width) * _img.scaleX ;
                
        
                
            }
            
            
            
        ]]>
    </fx:Script>
    <mx:Image x="0" y="0" id="_img" mouseDown="startDraw(event)"  mouseMove="draw(event)" mouseUp="stopDraw(event)"/>
    <s:Button x="115" y="382" label="start" click="startTone()"/>
    <s:Button x="193" y="382" label="stop" click="stopTone()"/>

    <s:TextArea x="118" y="413" id="_log" visible="false"/>
    <s:Button x="292" y="382" label="clear()" click="clear_img()"/>
</s:Application>