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: Simple instrument

this was also 'forked' from 
http://www.hulstkamp.com/2008/09/12/sound-in-flash-10-beta-generating-waveforms-timbre-and-pitch/175
Get Adobe Flash player
by Thy 28 Jun 2010
/**
 * Copyright Thy ( http://wonderfl.net/user/Thy )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/19Tn
 */

// forked from k__'s Simple instrument
// this was also 'forked' from 
// http://www.hulstkamp.com/2008/09/12/sound-in-flash-10-beta-generating-waveforms-timbre-and-pitch/175

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    
    /**
     * ...
     * @author Thi
     */
    public class Main extends Sprite 
    {
        
        // propriedades
        private var w:Number // largura do stage
        private var h:Number // altura do stage
        
        //
        private var numX:uint = 12 // quantas notas terá em 1 oitava
        private var numY:uint = 8 // quantas oitavas terá
        
        // mouse cursor
        private var cursor:Shape
        private var cursorW:Number // largura do cursor
        private var cursorH:Number // altura do cursor
        private var ix:uint // index X do mouse
        private var iy:uint // index Y do mouse
        
        //
        private var som:Som
        private var som_vector:Vector.<Som>
        
        // temp text
        private var TF:TextField
        private var format:TextFormat
        private var back_bitmap:Bitmap
        private var back_data:BitmapData
        
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //
            w = stage.stageWidth
            h = stage.stageHeight
            
            // draw cursor
            cursor = new Shape()
            cursor.graphics.lineStyle(1, 0)
            cursorW = w / numX
            cursorH = h / numY
            cursor.graphics.drawRect(0, 0, cursorW, cursorH)
            
            // draw background
            back_data = new BitmapData(w, h, false, 0xFFFFFF)
            back_bitmap = new Bitmap(back_data, "auto", true)
            format = new TextFormat("Arial", 16)
            TF = new TextField()
            TF.defaultTextFormat = format
            TF.autoSize = TextFieldAutoSize.LEFT
            stage.addChild(TF)
            TF.y = cursorH * 5 + cursorH * .3
            TF.x = cursorW*.3
            TF.text = "A";
            back_data.draw(stage)
            TF.text = "B"
            TF.x = cursorW * 2 + cursorW*.3
            back_data.draw(stage)
            TF.text = "C"
            TF.x = cursorW * 3 + cursorW*.3
            back_data.draw(stage)
            TF.text = "D"
            TF.x = cursorW * 5 + cursorW*.3
            back_data.draw(stage)
            TF.text = "E"
            TF.x = cursorW * 7 + cursorW*.3
            back_data.draw(stage)
            TF.text = "F"
            TF.x = cursorW * 8 + cursorW*.3
            back_data.draw(stage)
            TF.text = "G"
            TF.x = cursorW * 10 + cursorW*.3
            back_data.draw(stage)
            //
            format = null
            stage.removeChild(TF)
            TF = null
            
            // display List
            stage.addChild(back_bitmap)
            stage.addChild(cursor)
            
            // soms storage
            som_vector = new Vector.<Som>()
            
            // listeners
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mv) // ajustar pos do mouse
            stage.addEventListener(MouseEvent.CLICK, cl)
            //
            stage.addEventListener(Event.ENTER_FRAME, kill)
        }
        
        private function mv(e:MouseEvent):void
        {    
            cursor.x = ((mouseX / cursorW) >> 0)  * cursorW
            cursor.y = ((mouseY / cursorH) >> 0)  * cursorH
        }
        
        private function cl(e:MouseEvent):void
        {
            ix = (mouseX / cursorW) >> 0
            iy = (mouseY / cursorH) >> 0
            //
            som = new Som(numX, numY, ix, iy)
            som_vector.push(som)
        }
        
        private function kill(e:Event):void
        {
            var i:int = 0
            while (i < som_vector.length)
            {
                if (som_vector[i].dead)
                {
                    som_vector[i] = null
                    som_vector.splice(i, 1)
                    --i
                }
                ++i
            }
            
        }
        
    }
    
}



//package  
//{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    /**
     * ...
     * @author Thi
     */
    /*public*/ class Som extends EventDispatcher
    {
        
        // sound
        private var som:Sound
        private var ch:SoundChannel
        // index & sound stuff
        private var numX:uint
        private var numY:uint
        private var ix:uint
        private var iy:uint
        //
        private var pitch:Number // pitch O.O'
        private var di:uint // i don't know O.O'
        private var freq:Number // frequência O.O'
        
        // loop process
        private var i:uint // loop
        private var value:Number // number
        // when the sound stops
        public var dead:Boolean
        
        
        public function Som(numX:uint,numY:uint,ix:uint, iy:uint) 
        {
            this.numX = numX
            this.numY = numY
            this.ix = ix
            this.iy = iy
            pitch = ix + (numY - iy) * numX
            di = numX
            freq = Math.pow(2, pitch / di) * 55
            trace(ix, iy, numX, numY, freq)
            //
            som = new Sound()
            som.addEventListener(SampleDataEvent.SAMPLE_DATA, data)
            //
            ch = som.play()
        }
        
        private var multiplier:Number = .15
        private var sample_rate:int = 44100
        private var two_pi:Number = 2 * Math.PI
        private var two_pi_over_sample_rate:Number = two_pi / sample_rate
        
        private function data(e:SampleDataEvent):void
        {
            i = 0
            while (i < 8192)
            {
                value = Math.sin( (e.position + i) * two_pi_over_sample_rate * freq )
                e.data.writeFloat(value * multiplier)
                e.data.writeFloat(value * multiplier)
                ++i
            }
            // kill
            som.removeEventListener(SampleDataEvent.SAMPLE_DATA, data)
            dead = true
        }
        
    }

//}