Archive for September 20th, 2007

AS3 Papervision + Sound Visualizer + Line3D + Tweener - Part 2 FF07

So here’s my Flash Forward inspired piece!

Once again used Andy’s great Line3D class and gave it a Flash Forward 07 twist.

package com.hydrotik {

        import flash.events.Event;
        import flash.display.*;
        import flash.net.URLRequest;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundMixer;
        import flash.utils.ByteArray;
        import caurina.transitions.Tweener;
        import org.papervision3d.core.proto.*;
        import org.papervision3d.core.geom.*;
        import org.papervision3d.scenes.*;
        import org.papervision3d.cameras.*;
        import org.papervision3d.objects.*;
        import org.papervision3d.materials.*;

        public class Hydro3D extends Sprite {
               
                private static const LOOP:String = “../flashassets/mp3/jens_buchert.mp3″;
               
                private const STAGE_WIDTH              :Number = 600;
                private const STAGE_HEIGHT            :Number = 300;
                private const ANIMATION_TIME    :Number = 1;
                private const ANIMATION_TYPE    :String = “easeoutquad”;
                private const MULT                        :int = 10;
                private var controlPoints              :Array;
                private var itemArray         :Array;
                private var trailArray      :Array;
                private var currTrailArray            :Array;
                private var lineArray         :Array;
                private var count                            :int = 0;
                private var container       :Sprite;
                private var scene           :MovieScene3D;
                private var camera          :Camera3D;
                private var sc          :SoundChannel;
                private var s               :Sound;
                private var lines                        :Line3D;

                public function Hydro3D():void {

                        controlPoints = [];
                        itemArray = [];
                        trailArray = [];
                        currTrailArray = [];
                        lineArray = [];
                       
                        s = new Sound();
                        s.load(new URLRequest(LOOP));
                        sc = s.play(0, 1000);
                       
                        //container
                        container = new Sprite();
                        container.cacheAsBitmap = true;
                        addChild( container );
                        container.x = STAGE_WIDTH/2;
                        container.y = STAGE_HEIGHT/2;
                       
                        //scene
                        scene = new MovieScene3D( container );
                       
                        //camera
                        camera = new Camera3D();
                        camera.z = -200;
                       
                        addEventListener( Event.ENTER_FRAME, onEnterFrame );
                }
               
               
                private function onEnterFrame( event: Event ):void {
                        camera.x +=((container.mouseX*10) - camera.x) * 0.005;
                        camera.y +=((container.mouseY*10) - camera.y) * 0.005;

                        var bytes:ByteArray = new ByteArray();
                        SoundMixer.computeSpectrum(bytes, false, 0);
                       
                        var amp:Number = ((sc.leftPeak + sc.rightPeak)/2);
                       
                        lineArray[count] = new Line3D([new Vertex3D(-128, 0, -(count * MULT))], rgbToHex(count/4, count/6, count), 1, 1);
                        scene.addChild(lineArray[count]);
                       
                        for(var i:int = 0; i < 256; i++){
                                var v:Vertex3D = new Vertex3D((i*2) - 128, bytes.readFloat() * 300, -(count * MULT));
                                lineArray[count].addVertex(v);
                        }
                       
                        Tweener.addTween(lineArray[count], {
                                                alpha:0,
                                                time:ANIMATION_TIME,
                                                transition:ANIMATION_TYPE,
                                                onComplete:removeLine,
                                                onCompleteParams:[lineArray[count]]
                                        });
                       
                        scene.renderCamera( camera );
                        count = (count < 50) ? count + 1 : 0;
                };
               
                private function removeLine(l:Line3D = null):void{
                        scene.removeChild(l);
                }
               
                public function rgbToHex(uR:uint, uG:uint, uB:uint):int{
            var uColor:uint;
            uColor =  (uR & 255) << 16;
            uColor += (uG & 255) << 8;
            uColor += (uB & 255);
            return uColor;
        }

        }
}

I didn’t do anything with the amplitude but I left it in there if you decide you want to play with it. It’s somewhat processor intensive. I wish external mp3’s would loop. You’d think Adobe would have addressed that by now. Because of that, you’d want to attach it from your library, but I think it’s ok for testing. Helps you find a good loop that gives you good results when playing with a visualizer. Just overwrite the mp3 file or switch the path.

Once again the loop I’m using:

Jens Buchert
The music is the song “Mélange Eléctrique” from Jens Buchert’s album Spa Lounge. Great downtempo album.


Lines3D Part 2 Source

Thursday, September 20th, 2007

FF - Day 2

Today was more to my liking, which is why I’m posting only once today. Was kept busy and enjoyed all of the seminars I attended. I’m not going to get into too much detail yet as I was working on a Flash Forward inspired Papervision/Sound visualizer. Check back any minute for that.

Thursday, September 20th, 2007