Line Visualizer
音をグラフィックで表現する練習その2。
ファンがすごい回る。
/**
* Copyright su8erlemon ( http://wonderfl.net/user/su8erlemon )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/xjg6
*/
package {
import flash.display.Shape;
import flash.display.Stage;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.geom.PerspectiveProjection;
import flash.geom.Point;
import flash.display.StageQuality;
import flash.events.MouseEvent;
import frocessing.color.ColorHSV;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.display.BlendMode;
import frocessing.color.ColorHSL;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.media.Sound;
import flash.utils.ByteArray;
import flash.net.URLRequest;
import flash.media.SoundMixer;
import flash.media.SoundChannel;
import flash.system.Security;
import flash.filters.ColorMatrixFilter;
[SWF(width="465", height="465", frameRate="60", backgroundColor="0x000000")]
public class Main extends MovieClip{
private const SW:int = 465;
private const SH:int = 465;
private var st:Sprite = new Sprite();
private var rl:Number = 0;
private var circles:Array = [];
private var lines:Array = [];
private var sound:Sound = new Sound();
private var spectBytes:ByteArray = new ByteArray();
private var spectrum:Vector.<Number> = new Vector.<Number>(512, true);
private var spectrumAverage:Number = 0.0;
private var oldX:Number;
private var oldY:Number;
private var shake:int=1;
private var tension:Number=0.5;
public function Main() {
Security.loadPolicyFile('http://su8erlemon.com/crossdomain.xml');
sound.addEventListener(Event.COMPLETE, soundLoadCompleteHandler);
sound.load(new URLRequest('http://su8erlemon.com/lab/music/music3.mp3'));
}
private function soundLoadCompleteHandler(e:Event):void{
sound.removeEventListener(Event.COMPLETE, soundLoadCompleteHandler);
init();
}
private function init():void{
var bl:Shape = new Shape();
bl.graphics.beginFill(0x000000,1);
bl.graphics.drawRect(0,0,465,465);
bl.graphics.endFill();
addChild(bl);
st.graphics.beginFill(0x00ff00,0);
st.graphics.drawRect(-100,-100,200,200);
st.graphics.endFill();
st.x = SW/2;
st.y = SH/2;
addChild(st);
addEventListener(Event.ENTER_FRAME,loop);
var sc:SoundChannel = sound.play();
sc.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
private function soundCompleteHandler(e:Event):void{
var sc:SoundChannel = e.target as SoundChannel;
sc.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
sc.stop();
sc = sound.play();
sc.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
private function loop(e:Event):void{
computeSpectrum();
rl += tension-spectrumAverage*0.6;
tension += (1 - (tension + spectrumAverage*1.5 ))/10;
var www:int = int(spectrumAverage*80);
var line:Shape = new Shape();
line.graphics.lineStyle(1,0xffffff);
line.graphics.moveTo(oldX,oldY);
var ww:int = (150 + int(shake)*www/2 - Math.random() * www/4)*Math.sin(rl*0.58);
var hh:int = (150 + int(shake)*www/2 - Math.random() * www/4)*Math.cos(rl*0.58);
var circle:Shape = new Shape();
circle.graphics.beginFill(0xffffff,1);
circle.graphics.drawCircle(ww,hh,3);
circle.graphics.endFill();
circles.push(circle);
st.addChild(circle);
line.graphics.lineTo(ww,hh);
lines.push(line);
st.addChild(line);
oldX = ww;
oldY = hh;
var ll:int = circles.length;
for(var i:uint = 0;i<ll;i++){
circles[i].alpha += (0-circles[i].alpha)*(0.4 - spectrumAverage*0.25);
lines[i].alpha += (0-lines[i].alpha)*(0.4 - spectrumAverage*0.25);
if(circles[i].alpha <= 0){
if(circles[i].parent!=null)circles[i].parent.removeChild(circles[i]);
if(lines[i].parent!=null)lines[i].parent.removeChild(lines[i]);
}
}
shake = shake * -1;
}
private function computeSpectrum():void{
var bytes:ByteArray = spectBytes;
bytes.position = 0;
SoundMixer.computeSpectrum(bytes, false);
var total:Number = 0;
var plus:Number = 0;
var pc:uint = 0.0001;
var minus:Number = 0;
var mc:uint = 0.0001;
var numbers:Vector.<Number> = spectrum;
for (var i:uint = 0; i < 512; ++i) {
var n:Number = bytes.readFloat() * 2.0;
total += Math.abs(n);
if (n > 0) {
plus += n;
++pc;
}
else if (n < 0) {
minus += n;
++mc;
}
numbers[i] = n;
}
spectrumAverage = total / 512.0 * 2;
}
}
}