// forked from wonderfl_external_media's Sound_step3 サウンドヴィジュアライザ
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.filters.BlurFilter;
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;
[SWF(width=465,height=465,backgroundColor=0xFFFFFF,frameRate=30)]
public class Main extends Sprite
{
private var _channel:SoundChannel;
private var _byteArray:ByteArray = new ByteArray;
private const LEVEL:int = 4;
private const CHANNEL_LENGTH:int = 256;
private var _circleArray:Array = []
private var _thresholds:Array = [0,20,30,80,100,180,200,256]
private var _ratios:Array = [120,80,120,120]
private var _bmp:Bitmap = new Bitmap();
private var _bmd:BitmapData = new BitmapData(465, 465,true,0);
public function Main():void
{
var _circle:Shape;
Security.loadPolicyFile("http://www.muraken.biz/crossdomain.xml");
for (var i:int = 0; i < LEVEL; i++)
{
_circle = new Shape;
_circle.graphics.beginFill(0);
_circle.graphics.drawCircle(0,0,50);
_circle.graphics.endFill();
addChild(_circle);
_circle.x = 50 + stage.stageWidth/LEVEL * i;
_circle.y = stage.stageHeight/2;
_circleArray[i] = _circle;
}
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);
_bmp.bitmapData = _bmd;
addEventListener(Event.ENTER_FRAME, _update);
}
private function _update(e:Event):void
{
var _circle:Shape;
var sumlr:Array = [0,0,0,0]
SoundMixer.computeSpectrum(_byteArray, true);
var i:int, max_ch:int;
var l:Number, r:Number, lr:Number;
for (i = _thresholds[0]; i < _thresholds[1]; i++) {
_byteArray.position = i * 4;
l = _byteArray.readFloat();
_byteArray.position = i * 4 + 1024;
r = _byteArray.readFloat();
sumlr[0] += l > r ? l : r;
}
for (i = _thresholds[2]; i < _thresholds[3]; i++) {
_byteArray.position = i * 4;
l = _byteArray.readFloat();
_byteArray.position = i * 4 + 1024;
r = _byteArray.readFloat();
sumlr[1] += l > r ? l : r;
}
for (i = _thresholds[4] = 128; i < _thresholds[5]; i++) {
_byteArray.position = i * 4;
l = _byteArray.readFloat();
_byteArray.position = i * 4 + 1024;
r = _byteArray.readFloat();
sumlr[2] += l > r ? l : r;
}
for (i = _thresholds[6]; i < _thresholds[7]; i++) {
_byteArray.position = i * 4;
l = _byteArray.readFloat();
_byteArray.position = i * 4 + 1024;
r = _byteArray.readFloat();
sumlr[3] += l > r ? l : r;
}
for (i = 0; i < LEVEL; i++) {
_circle = _circleArray[i];
_circle.width = sumlr[i]/64*_ratios[i]+50;
_circle.height = sumlr[i]/64*_ratios[i]+50;
}
//var level:int = (LEVEL - max_ch / (CHANNEL_LENGTH / LEVEL));
_bmd.draw(_circle);
_bmd.applyFilter(_bmd,_bmd.rect,_bmd.rect.topLeft,new BlurFilter(4,4));
}
}
}