it wirks only if run it in the Flex. if copy : image, track and swf to
another place visualisation doesn't work. Why?

package
{
        import away3d.cameras.HoverCamera3D;
        import away3d.containers.View3D;
        import away3d.containers.Scene3D;
        import away3d.materials.BitmapMaterial;
        import away3d.materials.ColorMaterial;
        import away3d.primitives.Cube;
        import away3d.primitives.RegularPolygon;
        import away3d.primitives.Trident;
        import away3d.core.utils.Cast;

        import flash.display.Bitmap;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.filters.BlurFilter;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundMixer;
        import flash.net.URLRequest;
        import flash.utils.ByteArray;
        import flash.utils.Timer;

        [SWF(width="1024", height="768", frameRate="30",
backgroundColor="#ffffff")]
        public class music extends Sprite
        {
                private var cam:HoverCamera3D;
                private var lastKey:uint;
                private var keyIsDown:Boolean = false;
                private var view:View3D;

                private var numCubes:Number=16; // Set depending on speed of
computer. adjust amplitude accordingly
                private var amplitude:Number=70;
                private var cubes:Array;
                private var cubesMirrored:Array;
                private var cubeColors:Array;
                private var mp3:Sound;
                private var soundBytes:ByteArray;
                private var soundPlaying:Boolean=false;
                private var soundValues:Array;
                private var pausePosition:int=0;
                private var channel:SoundChannel;
                private var highlightFilter:Array;
                private var maxLev:Number=0;

                private var t:Timer;

                private var url:String='track.mp3';
                private var scene:Scene3D;

                //camera----------------
                private var lastMouseX:Number;
                private var lastMouseY:Number;
                private var lastPanAngle:Number;
                private var lastTiltAngle:Number;
                private var move:Boolean = false;
                //camera----------------

                [Embed (source="01.jpg")]
                private var greyTexture:Class;

                public function music()
                {
                        scene=new Scene3D();

                        // init objects
                        cubes=new Array();
                        cubesMirrored=new Array();
                        cubeColors=new Array();
                        soundBytes=new ByteArray();
                        soundValues=new Array();

                        // setup and load mp3
                        mp3=new Sound(new URLRequest(url));
                        //mp3.load(new URLRequest("02 Bear Trap.mp3")); // 
Note: you will
have to change this to point to one of your own mp3 files
                        channel=new SoundChannel();

                        // prefill arrays with zero's
                        for(var p:int=0; p<numCubes; p++){
                                soundValues.push(0);
                                cubeColors.push(0);
                        }

                        // create a "hovering" camera
                        cam=new HoverCamera3D();
                        cam.z=-1000; // make sure the camera is positioned away 
from the
default 0,0,0 coordinate
                        cam.panangle=20;
                        cam.tiltangle=20;
                        cam.targetpanangle=20;
                        cam.targettiltangle=20;
                        cam.mintiltangle=-90;
                        cam.zoom=16;

                        // create a viewport
                        view = new View3D({scene:scene, camera:cam, 
x:stage.stageWidth/2,
y:stage.stageHeight/2});
                        addChild(view);

                        // Show the axis
                        var axis:Trident=new Trident(150);
                        view.scene.addChild(axis);

                        // Create and place the cubes
                        var i:int; var zPos:int;
                        for(i=0; i<numCubes; i++){
                                // position left/right channle cubes differently
                                if(i<(numCubes/2)){
                                        zPos = 15;
                                } else {
                                        zPos = -15;
                                }

                                // add positive value cubes
                                var 
xPos:Number=(i%(numCubes/2)*30)-(((numCubes/2)*30)/2)+15;
                                var cubeMaterial:ColorMaterial=new 
ColorMaterial(0xffffff);
                                var cube:Cube=new 
Cube({material:cubeMaterial,width:20,height:
1,depth:20,x:xPos,z:zPos,y:5,ownCanvas:true});
                                view.scene.addChild(cube);
                                cubes[i]=cube;

                                // add the "reflection" cubes
                                var cubeMaterialM:ColorMaterial=new 
ColorMaterial(0xffffff);
                                var cubeM:Cube=new 
Cube({material:cubeMaterialM,width:20,height:
1,depth:20,x:xPos,z:zPos,y:-5,alpha:1,ownCanvas:true});
                                view.scene.addChild(cubeM);
                                cubesMirrored[i]=cubeM;
                        }

                        // Add a textured plane
                        var planeMat:BitmapMaterial=new 
BitmapMaterial(Cast.bitmap
(greyTexture), {alpha:0.5});
                        var plane:RegularPolygon = new 
RegularPolygon({material:planeMat,
radius:500, sides:4, ownCanvas:true});
                        plane.ownCanvas=true;
                        view.scene.addChild(plane);

                        // Create a filter for blurring cubes that get new 
values
                        highlightFilter=new Array(new BlurFilter());

                        // update the view so the cover has something to display
                        cam.hover();
                        view.render();

                        // listen for key events and run every frame
                        addEventListener(Event.ENTER_FRAME, onEnterFrame);

                        // listen for events from the Cover
                        addEventListener(Event.ACTIVATE, playSound);
                        addEventListener(Event.DEACTIVATE, stopSound);

                        //camera--------------
                        stage.addEventListener(MouseEvent.MOUSE_DOWN, 
MouseDown);
                        stage.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
                        //camera--------------
                }

                private function onEnterFrame(e:Event):void
                {
                                // Grab and set up sound values
                                SoundMixer.computeSpectrum(soundBytes,true);
                                var tempValue:Number=0;
                                var newValues:Array=new Array();
                                var 
valuesToMerge:Number=Math.floor(512/numCubes);
                                var i:uint;

                                // grab volume averages from 512 samples for 
use with X cubes
                                for (i=0; i<512; i++) {
                                        // read out levels from byteArray
                                        var lev:Number = soundBytes.readFloat() 
* amplitude;
                                        tempValue += lev;
                                        // create average so we only get 
"numCubes" of values
                                        if(i%valuesToMerge == valuesToMerge-1){
                                                var avg:Number = 
tempValue/numCubes;
                                                newValues.push(avg);
                                                tempValue = 0;
                                        }
                                }

                                // update cubes
                                var l:int = newValues.length;
                                var newSoundValues:Array = new Array();
                                for (i = 0; i < l; i++) {
                                        var currentCube:Cube = cubes[i] as Cube;
                                        var currentCubeMirror:Cube = 
cubesMirrored[i] as Cube;
                                        // if the values is higher than the 
previous one, go white
                                        if(newValues[i] > soundValues[i]){
                                                newSoundValues[i] = 
newValues[i];
                                                cubeColors[i] = 128;
                                                currentCube.filters = 
highlightFilter;
                                                currentCubeMirror.filters = 
highlightFilter;
                                        } else { // If lower, just keep fading
                                                var dampedValue:Number = 
soundValues[i]-3;
                                                if(dampedValue<1){ dampedValue 
= 1; }
                                                cubeColors[i] -= 10;
                                                newSoundValues[i] = dampedValue;
                                                currentCube.filters = null;
                                                currentCubeMirror.filters = 
null;
                                        }
                                        // Adjust cube and mirror cube height
                                        currentCube.height = newSoundValues[i];
                                        currentCube.y = newSoundValues[i]/2;
                                        currentCubeMirror.height = 
newSoundValues[i];
                                        currentCubeMirror.y = 
newSoundValues[i]/2*-1;

                                        // Adjust color
                                        var adjustedColor:uint = cubeColors[i];
                                        var colString:String;
                                        var colStringMirror:String;
                                        var increase:Number = 1.7; // how much 
whiter shall the
"reflected" cubes be
                                        if(i<l/2){ // First X cubes are blue
                                                colString = 
"0x"+ensureTwoDigits(adjustedColor)+ensureTwoDigits
(adjustedColor)+"ff";
                                                colStringMirror = 
"0x"+ensureTwoDigits(adjustedColor*increase)
+ensureTwoDigits(adjustedColor*increase)+"ff";
                                        } else { // Next X cubes are red
                                                colString = 
"0xff"+ensureTwoDigits(adjustedColor)+ensureTwoDigits
(adjustedColor);
                                                colStringMirror = 
"0xff"+ensureTwoDigits(adjustedColor*increase)
+ensureTwoDigits(adjustedColor*increase);
                                        }

                                        // Read out and update the material
                                        var col:ColorMaterial = 
currentCube.material as ColorMaterial;
                                        var colM:ColorMaterial = 
currentCubeMirror.material as
ColorMaterial;
                                        col.color = uint(colString);
                                        colM.color = uint(colStringMirror);
                                }

                                // update soundValues with new values
                                soundValues = newSoundValues.concat();

                                // rotate the camera around the cubes
                                //cam.targetpanangle += .5;

                                //camera------------------
                                var cameraSpeed:Number = 0.3; // Approximately 
same speed as mouse
movement.
                                if (move)
                                {
                                        cam.targetpanangle = 
cameraSpeed*(stage.mouseX - lastMouseX) +
lastPanAngle;
                                        cam.targettiltangle = 
cameraSpeed*(stage.mouseY - lastMouseY) +
lastTiltAngle;
                                }
                                //camera------------------

                                // render the view
                                cam.hover();
                                view.render();
                }

                // Clamp and adjust HEX values. Make sure the HEX has an initial
zero (if required)
                private function ensureTwoDigits(n:uint):String{
                        var s:String;
                        if(n > 255){ n = 255;}
                        if(n < 1){ n = 1;}
                        if(n < 16){
                                s = "0"+n.toString(16);
                        } else {
                                s = n.toString(16);
                        }
                        return s;
                }

                // Sound events triggered by the cover
                private function playSound(e:Event):void
                {
                        if(!soundPlaying){
                                soundPlaying = true;
                                channel = mp3.play(pausePosition);
                                if( 
!channel.hasEventListener(Event.SOUND_COMPLETE) ){
                                        
channel.addEventListener(Event.SOUND_COMPLETE,
soundCompleteHandler);
                                }
                        }
                }

                private function stopSound(e:Event):void
                {
                        pausePosition = channel.position;
                        channel.stop();
                        soundPlaying = false;
                }

                // restart the mp3 when finished playing
                private function soundCompleteHandler(e:Event):void
                {
                        pausePosition = 0;
                        soundPlaying = false;
                        playSound(e);
                }

                //camera-------------
                private function MouseDown(event:MouseEvent):void
                {
                        lastPanAngle = cam.targetpanangle;
                        lastTiltAngle = cam.targettiltangle;
                        lastMouseX = stage.mouseX;
                        lastMouseY = stage.mouseY;
                        move = true;
                }

                private function MouseUp(event:MouseEvent):void
                {
                        move = false;
                }
                //camera-------------
        }
}

Reply via email to