SoundMixer.computeSpectrumとByteArray (5)
//////////////////////////////////////////////////////////////////////////////
SoundMixer.computeSpectrumとByteArray (5)
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/edPl
*/
////////////////////////////////////////////////////////////////////////////////
// SoundMixer.computeSpectrumとByteArray (5)
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.net.URLRequest;
import frocessing.color.ColorHSV;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var sound:Sound;
private var channel:SoundChannel;
private static var soundPath:String = "http://www.takasumi-nagai.com/soundfiles/sound001.mp3";
private var byteArray:ByteArray;
private var canvas:Shape;
private static var channels:uint = 256;
private var color:ColorHSV;
private static var factors:uint = 32;
private static var directions:Array = [-1, 1];
private static var colors:Array = [200, 120];
public function Main() {
Wonderfl.capture_delay(10);
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
canvas = new Shape();
addChild(canvas);
canvas.x = 232;
canvas.y = 425;
//
sound = new Sound();
sound.addEventListener(Event.COMPLETE, complete, false, 0, true);
sound.load(new URLRequest(soundPath), new SoundLoaderContext(10, true));
byteArray = new ByteArray();
color = new ColorHSV(0);
}
private function complete(evt:Event):void {
evt.target.removeEventListener(Event.COMPLETE, complete);
start();
}
private function start():void {
channel = sound.play(0, 5);
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
private function update(evt:Event):void {
SoundMixer.computeSpectrum(byteArray, true, factors);
canvas.graphics.clear();
for (var c:uint = 0; c < 2; c++) {
var direction:int = directions[c];
color.h = colors[c];
for (var n:uint = 0; n < channels; n++) {
var p:Number = byteArray.readFloat();
color.s = 1 - 0.6*n/256;
color.v = p*1.5;
canvas.graphics.lineStyle(0, color.value);
canvas.graphics.moveTo(0, -n*1.5);
canvas.graphics.lineTo(p*200*direction, -n*1.5);
}
}
}
}
}