mp3波形器

as3可以把声音解析成二进制,并加以分析。结合pv3d,做了个例子~

 

package
{
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    
    import org.papervision3d.lights.PointLight3D;
    import org.papervision3d.materials.shadematerials.PhongMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.Cube;
    import org.papervision3d.view.BasicView;
    
    [SWF(backgroundColor=0x000000)]
    public class Sound3D extends BasicView
    {
        private var universe:DisplayObject3D;
        private var cubesArray:Array;
        
        private var bgm:Sound;
        private var bgmChannel:SoundChannel;
        private var ba:ByteArray;
        private var num:Number;
        private var myBlockArray:Array;
        private var smallBlockArray:Array;
        private var ready:Boolean=false;
        public function Sound3D()
        {
            init3D();            
        }
        private function init3D():void
        {
            cubesArray=[];
            smallBlockArray=[];
            universe=new DisplayObject3D();
            //camera.z=500;
            camera.zoom=80;
            scene.addChild(universe);
            var light:PointLight3D=new PointLight3D();
            for(var i:uint=0;i<20;i++)
            {
                var mt:PhongMaterial=new PhongMaterial(light,0xff0000,0x220000,3);
                var ml:MaterialsList=new MaterialsList({all:mt });
                var cube:Cube=new Cube(ml,30,30,200);
                
                var cubeSmall:Cube=new Cube(ml,30,30,10);
                
                //cube.x=15*i-30*15/2;
                cubeSmall.rotationY=cube.rotationY=18*i;
    
                cube.moveForward(200);
                cubeSmall.moveForward(200);
                cubeSmall.y=250;
                
                var cubeHolder:DisplayObject3D=new DisplayObject3D();
                cube.y=100;
                cubeHolder.addChild(cube);
                cubesArray.push(cubeHolder);
                smallBlockArray.push(cubeSmall);
                
                universe.addChild(cubeHolder);
                universe.addChild(cubeSmall);
                
                
            }
            ready=true;
            initSound();
            startRendering();
        }
        private function initSound():void
        {
            bgm = new Sound();
            ba = new ByteArray();
            bgm.load(new URLRequest('../lib/where-is-the-love.mp3'));
            bgmChannel = bgm.play();    
        }
        override protected function onRenderTick(event:Event=null):void
        {
            super.onRenderTick();
            universe.rotationY=viewport.containerSprite.mouseX/10;
            universe.y=viewport.containerSprite.mouseY;
            
            drawFunction();
            
        }

        private function drawFunction():void
        {
            //获取音谱信息
            SoundMixer.computeSpectrum(ba, true, 0);
            trace(ba.length);
            if(ready)
            {
                for (var i:uint = 0; i < 20; i++)
                {
                    if(cubesArray[i].scaleY<.1)
                    {
                        cubesArray[i].alpha=0;
                    }
                    else
                    {
                        cubesArray[i].alpha=.5;
                    }
                    cubesArray[i].scaleY = ba.readFloat();
                    if(cubesArray[i].scaleY*200>smallBlockArray[i].y)
                    {
                        smallBlockArray[i].y=cubesArray[i].scaleY*200;
                    }
                    smallBlockArray[i].y-=10;
                    if(smallBlockArray[i].y<5)
                    {
                        smallBlockArray[i].y=5;
                    }
                    
                }
            }
        }
    }
}

 

下载:

另存为... 后缀名改为.swf

posted on 2009-09-30 16:37  jiahuafu  阅读(350)  评论(0编辑  收藏  举报

导航