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

sound test, random

...
@author Thi
Get Adobe Flash player
by Thy 28 Jul 2010
/**
 * Copyright Thy ( http://wonderfl.net/user/Thy )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/neIQ
 */

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.geom.ColorTransform;
    import flash.geom.Rectangle;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.utils.ByteArray;
    
    /**
     * ...
     * @author Thi
     */
    public class Main extends Sprite 
    {
        
        // piano drawing
        private var piano_data:BitmapData
        private var piano_bitmap:Bitmap
        private var piano_shape:Shape
        
        // piano proprieties
        private var piano_num:int = 37 // num notes
        private var piano_first_pos:int = 1 // dó = 1; sí = 12
        private var piano_A4_hz:Number = 440 // A4 frequence
        private var piano_A4_pos:int = 22 // A4 position
        private var piano_key_width:Number = 465 / piano_num
        
        // notes proprieties
        private var freqs:Vector.<Number>
        
        // loops
        private var i:int, j:int, k:int, l:int
        
        // sound
        private var sound:Sound
        private var channel:SoundChannel
        private var sample:Number
        private var position:Number // sound playing position
        //
        private var spec_shape:Shape // sound spectrum
        private var ba:ByteArray
        //
        private var note_list:Vector.<Number> // list of notes bein played
        private var volume_list:Vector.<Number> // list of notes volumes bein played
        
        // sound physics
        private var prop_rate:Number = 44100
        private var small_pi:Number = 2 * Math.PI / prop_rate
        private var small_volume:Number // just (1 / num of notes playing) 
        
        // particle
        private var part_num:int = 40
        private var part_list:Vector.<Part>
        private var part:Part
        // 
        private var part_bitmap:Bitmap
        private var part_bitmapdata:BitmapData
        private var part_col:ColorTransform
        private var part_rect:Rectangle
        
        
        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);
            
            // draw piano
            draw_piano()
            
            // note positions and proprieties
            notes_freqs()
            
            // sound
            sound = new Sound()
            sound.addEventListener(SampleDataEvent.SAMPLE_DATA, sample_data)
            channel = sound.play()
            
            // spectrum
            spec_init()
            this.addEventListener(Event.ENTER_FRAME, spec_update)
            
            // init_particles
            part_init()
            
            //
            addChild(part_bitmap)
            addChild(spec_shape)
            addChild(piano_bitmap)
            
            // enter frame
            this.addEventListener(Event.ENTER_FRAME, ef)
        }
        
        private function draw_piano():void
        {
            
            piano_shape = new Shape()
            piano_shape.graphics.lineStyle(1, 0xFFFFFF)
            piano_shape.graphics.moveTo(0, 1)
            piano_shape.graphics.lineTo(465, 1)    
            
            i = -1
            while (++i < piano_num)
            {
                piano_shape.graphics.moveTo(i * piano_key_width, 0)
                piano_shape.graphics.lineTo(i * piano_key_width, 4)
            }
            
            piano_data = new BitmapData(465, 4, true, 0)
            piano_data.draw(piano_shape)
            piano_shape.graphics.clear()
            piano_shape = null
            piano_bitmap = new Bitmap(piano_data, "auto", true)
            piano_bitmap.y = 465 - 10
        }
        
        
        private function notes_freqs():void
        {
            freqs = new Vector.<Number>(piano_num+1,true)
            i = -1
            while (++i < piano_num)
            {
                freqs[i] = Math.pow(2, (i-piano_A4_pos)/12) * piano_A4_hz * small_pi // this small pi is (2pi/44100)
            }
            note_list = new Vector.<Number>()
            volume_list = new Vector.<Number>()
        }
        
        
        private function sample_data(e:SampleDataEvent):void
        {
            l = note_list.length
            small_volume = 1/l
            position = e.position
            
            i = -1
            while (++i < 8192) // 2048, 8192
            {
                sample = 0
                j = -1
                while (++j < l)
                {
                    sample += Math.sin ((i+position) * note_list[int(j)]) * small_volume * volume_list[int(j)]
                }
                
                e.data.writeFloat(sample)
                e.data.writeFloat(sample)
            }
        }
        
        
        private function spec_init():void
        {
            ba = new ByteArray
            spec_shape = new Shape()
        }
        
        
        private function spec_update(e:Event):void
        {
            SoundMixer.computeSpectrum(ba, false)
            spec_shape.graphics.clear()
            spec_shape.graphics.lineStyle(1,0xFFFFFF)
            i = -1
            spec_shape.graphics.moveTo(0,230)
            while (++i < 256)
            {
                spec_shape.graphics.lineTo(i*1.81640625,230 - ba.readFloat()*40)
            }
        }
        
        private function part_init():void
        {
            part_list = new Vector.<Part>(part_num, true)
            i = 0
            part_list[i] = new Part(Math.random()*465, 0, Math.random() - .5, Math.random()*.5, Math.random()*0xFFFFFF)
            while (++i < part_num)
            {
                part_list[i] = new Part(Math.random() * 465, 0, Math.random() - .5, Math.random() * .5,Math.random()*0xFFFFFF)
                part_list[i-1].next = part_list[i]
            }
            //
            part_bitmapdata = new BitmapData(465,465,false, 0)
            part_bitmap = new Bitmap(part_bitmapdata, "auto", true)
            part_col = new ColorTransform(Math.random() * .3 + .9, Math.random() * .3 + .9, Math.random() * .3 + .9, .999)
            part_rect = new Rectangle(0, 0, 465, 465)
        }
        
        private function part_update():void
        {
            part_bitmapdata.lock()
            part_bitmapdata.colorTransform(part_rect, part_col)
            part = part_list[0]
            part.x += part.vx
            part.y += (part.vy += .2)
            if (part.x < 0)
            {
                part.x = 0
                part.vx *= -1
            } else if (part.x > 465)
            {
                part.x = 465
                part.vx *= -1
            }
            if (part.y > 455)
            {
                part.y = 455
                note_list.push(freqs[(part.x / piano_key_width) >> 0])
                volume_list.push(1)
                part.vy = - Math.random() * 10 - .2
                part.vx = Math.random() * 2 -1
            }
            part_bitmapdata.setPixel(part.x, part.y, part.c)
            
            while (part = part.next)
            {
                part.x += part.vx
                part.y += (part.vy += .2)
                if (part.x < 0)
                {
                    part.x = 0
                    part.vx *= -1
                } else if (part.x > 465)
                {
                    part.x = 465
                    part.vx *= -1
                }
                if (part.y > 455)
                {
                    part.y = 455
                    note_list.push(freqs[(part.x / piano_key_width) >> 0])
                    volume_list.push(1)
                    part.vy = - Math.random() * 10 - .2
                    part.vx = Math.random() * 2 -1
                }
                part_bitmapdata.setPixel(part.x, part.y, part.c)
            }
            part_bitmapdata.unlock()
        }
        
        private function ef(e:Event):void
        {
            part_update()
            list_update()
        }
        
        private function list_update():void
        {
            i = -1
            l = note_list.length
            trace(l)
            while (++i < l)
            {
                volume_list[i] -= .04
                if (volume_list[i] <= 0)
                {
                    note_list.splice(i,1)
                    volume_list.splice(i, 1)
                    --i
                    --l
                }
            }
        }
    }
    
}

//package  
//{
    /**
     * ...
     * @author Thi
     */
    /*public*/ class Part
    {
        
        public var x:Number, y:Number, vx:Number, vy:Number, c:uint
        public var next:Part
        
        public function Part(x:Number,y:Number,vx:Number,vy:Number, c:uint) 
        {
            this.x = x
            this.y = y
            this.vx = vx
            this.vy = vy
            this.c = c
        }
        
    }

//}