SoundMixer.computeSpectrumとByteArray (3)
//////////////////////////////////////////////////////////////////////////////
SoundMixer.computeSpectrumとByteArray (3)
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/xVGw
*/
////////////////////////////////////////////////////////////////////////////////
// SoundMixer.computeSpectrumとByteArray (3)
////////////////////////////////////////////////////////////////////////////////
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="#FFFFFF", 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 unit:Number = 360/256;
private static var factors:uint = 2;
public function Main() {
Wonderfl.capture_delay(11);
init();
}
private function init():void {
graphics.beginFill(0x333333);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
graphics.beginFill(0x000000);
graphics.drawRect(94, 60, 128, 300);
graphics.endFill();
graphics.beginFill(0x000000);
graphics.drawRect(242, 60, 128, 300);
graphics.endFill();
canvas = new Shape();
addChild(canvas);
canvas.x = 94;
canvas.y = 60;
var labelL:Label = new Label(100, 12);
addChild(labelL);
labelL.x = 158 - 50;
labelL.y = 400;
labelL.text = "left";
labelL.textColor = 0xCCCCCC;
var labelR:Label = new Label(100, 12);
addChild(labelR);
labelR.x = 306 - 50;
labelR.y = 400;
labelR.text = "right";
labelR.textColor = 0xCCCCCC;
//
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++) {
for (var n:uint = 0; n < channels; n++) {
var p:Number = byteArray.readFloat();
color.h = unit*n;
canvas.graphics.lineStyle(0, color.value);
canvas.graphics.moveTo(148*c + n/2, 300);
canvas.graphics.lineTo(148*c + n/2, 300 - p*300);
}
}
}
}
}
//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Label extends Sprite {
private var txt:TextField;
private static var fontType:String = "Arial";
private var txtWidth:uint;
private var fontSize:uint;
public function Label(w:uint, s:uint) {
txtWidth = w;
fontSize = s;
draw();
}
private function draw():void {
txt = new TextField();
txt.width = txtWidth;
addChild(txt);
txt.autoSize = TextFieldAutoSize.CENTER;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = fontSize;
tf.align = TextFormatAlign.CENTER;
txt.defaultTextFormat = tf;
}
public function set text(param:String):void {
txt.text = param;
}
public function set textColor(param:uint):void {
txt.textColor = param;
}
}