Sunday, December 12, 2010


I’ve put together a couple of examples using the new audio capabilities of Actionscript 3 combined with thePapervision3D engine. I am creating a 3D carousel of cube primitives and am setting their scaleY values according to the frequency spectrum that I get back from the MP3 file. The first example shows the cubes in wireframe mode so you can see what is happening. The second example uses a bitmap texture to create a cool effect that I think matches the music really well. The code for the first example is below as well. Just click on the images to see the examples and also make sure that you Flash Player 9.
[as]import org.papervision3d.scenes.*;
import org.papervision3d.cameras.*;
import org.papervision3d.objects.*;
import org.papervision3d.materials.*;

var so:Sound = new Sound();
var sc:SoundChannel;
var ba:ByteArray = new ByteArray();
var array:Array;

so.load(new URLRequest(“amon4.mp3″));
sc = so.play(0,1000);

var sp:Sprite = new Sprite();
this.addChild(sp);
sp.x = 320;
sp.y = 240;

var radius:int = 2000;
var s:MovieScene3D = new MovieScene3D(sp);
var cam:Camera3D = new Camera3D();

var pl:Plane = new Plane();
pl.visible = false;
s.addChild(pl);
cam.target = pl;
cam.z = -3500;

var ang:Number = 360/15;
for(var i:int=0; i<15; i++)
{
var bam:WireframeMaterial = new WireframeMaterial(0xFFFFFF*Math.random());
bam.oneSide = false;
var p:Cube = new Cube(bam);
p.name = “i”+i;
p.scaleY = 5;
s.addChild(p);
p.z = Math.sin((ang*i)*Math.PI/180) * radius;
p.x = Math.cos((ang*i)*Math.PI/180) * radius;
}

this.addEventListener(Event.ENTER_FRAME, render);
var yOff:Number = 0;
var angleZ:Number = 0;
var a:Number = 0;
function render(e:Event)
{
SoundMixer.computeSpectrum(ba,true,0);
for(var i=0; i < 256; i=i+8)
{
a = ba.readFloat()*10;
if(Math.round(i/8)<15) s.getChildByName(“i”+ Math.round(i/8).toString()).scaleY = (a/15)*6;
}

cam.y = (this.mouseY – 240)*8;
angleZ += (this.mouseX – 320)*0.0001;
cam.z = Math.sin(angleZ) * 3000;
cam.x = Math.cos(angleZ) * 3000;

s.renderCamera(cam);
}
s.renderCamera(cam);[/as]

No comments:

Post a Comment