Sound_step3 サウンドヴィジュアライザ
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.system.Security;
public class Main extends Sprite
{
private var _channel:SoundChannel;
private var _byteArray:ByteArray = new ByteArray;
private const CHANNEL_LENGTH:int = 256;
private var _barArray:Array = [];
public function Main():void
{
Security.loadPolicyFile("http://www.muraken.biz/crossdomain.xml");
var _bar:Shape;
var _offsetX:int = Math.floor((stage.stageWidth - CHANNEL_LENGTH) / 2);
for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
_bar = new Shape;
_bar.graphics.beginFill(0);
_bar.graphics.drawRect(0, -1, 1, 1);
_bar.graphics.endFill();
addChild(_bar);
_bar.y = stage.stageHeight;
_bar.x = i+_offsetX;
_barArray[i] = _bar;
}
var _url:String = "http://www.muraken.biz/wonderfl/test.mp3";
var _req:URLRequest = new URLRequest(_url);
var _snd:Sound = new Sound;
_snd.load(_req);
_channel = _snd.play(0, int.MAX_VALUE);
addEventListener(Event.ENTER_FRAME, _update);
}
private function _update(e:Event):void
{
SoundMixer.computeSpectrum(_byteArray, true);
var i:int;
var l:Number, r:Number, lr:Number;
var _bar:Shape;
for (i = 0; i < CHANNEL_LENGTH; i++) {
_byteArray.position = i * 4;
l = _byteArray.readFloat();
_byteArray.position = i * 4 + 1024;
r = _byteArray.readFloat();
lr = l > r ? l : r;
_bar = _barArray[i];
_bar.height = 300 * lr;
}
}
}
}