forked from: forked from: soundtest11
/**
* Copyright aobyrne ( http://wonderfl.net/user/aobyrne )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fqY4
*/
// forked from gaina's soundtest11
package
{
import com.bit101.components.CheckBox;
import com.bit101.components.Label;
import com.bit101.components.RadioButton;
import com.bit101.components.VBox;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.media.SoundMixer;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import frocessing.color.ColorHSV;
/**
* ...
* @author nagai
* sound by AlainMikuni
*
*/
[SWF(width="465",height="465",frameRate=30)]
public class SoundTest11 extends Sprite
{
private const ROW:int = 16;
private const COL:int = 16;
private const RADIUS:int = 5;
private var original_rasio:Number = 0;
private var rasio:Number = 0;
private var FFTMode:Boolean = true;
private var w:Number;
private var h:Number;
private var sound:Sound;
private var obj_array_l:Array = [];
private var obj_array_r:Array = [];
private var position_array_l:Array = [];
private var label:Label;
private var blendModeCurrent:String=BlendMode.DIFFERENCE;
private var isColor:Boolean = true;
public function SoundTest11():void
{
//Wonderfl.capture_delay(10);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
w = stage.stageWidth;
h = stage.stageHeight;
original_rasio = w / 16;
PlaySound("http://www.takasumi-nagai.com/soundfiles/sound004.mp3");
}
private function PlaySound(url:String):void
{
sound = new Sound();
var _context:SoundLoaderContext = new SoundLoaderContext(1000, true);
sound.addEventListener(Event.COMPLETE, SoundLoadeComplete);
sound.load(new URLRequest(url), _context);
}
private function SoundLoadeComplete(e:Event):void
{
obj_array_l = Draw();
obj_array_r = Draw();
position_array_l = PointPosition(obj_array_l);
blendModeRadioButtons();
addEventListener(Event.ENTER_FRAME, loop);
sound.play(0, 10, new SoundTransform(0.7, 0));
stage.addEventListener(Event.RESIZE, ResizeEventHandler);
//stage.addEventListener(MouseEvent.MOUSE_UP, FFTModeChanger);
}
private function loop(e:Event):void
{
var _byte:ByteArray = new ByteArray();
SoundMixer.computeSpectrum(_byte, FFTMode);
for (var n:int = 0; n < 2; n++)
{
for (var i:int = 0; i < 256; i++)
{
var data:Number = _byte.readFloat();
var p:Number = data * 2;
var _point:Point = position_array_l[i] as Point;
if (n==0)
{
var _sp:Sprite = obj_array_l[i] as Sprite;
_sp.blendMode = blendModeCurrent;
if (FFTMode)
{
_sp.x += p;
_sp.y += p;
if (data == 0)
{
_sp.x += (_point.x - _sp.x) / 10;
_sp.y += (_point.y - _sp.y) / 10;
_sp.scaleX += (1 - _sp.scaleX) / 10;
_sp.scaleY += (1 - _sp.scaleY) / 10;
}
}
else
{
_sp.scaleX += data;
_sp.scaleY += data;
_sp.x += (_point.x - _sp.x) / 10;
_sp.y += (_point.y - _sp.y) / 10;
if (data < 0.2 && data > -0.2)
{
_sp.scaleX += (1 - _sp.scaleX) / 10;
_sp.scaleY += (1 - _sp.scaleY) / 10;
}
}
}
else if (n==1)
{
var _sp_r:Sprite = obj_array_r[i] as Sprite;
_sp_r.blendMode = blendModeCurrent;
if (FFTMode)
{
_sp_r.scaleX += p;
_sp_r.scaleY += p;
_sp_r.x += (_point.x - _sp_r.x) / 15;
_sp_r.y += (_point.y - _sp_r.y) / 15;
if (data <0.2)
{
_sp_r.scaleX += (1 - _sp_r.scaleX) / 10;
_sp_r.scaleY += (1 - _sp_r.scaleY) / 10;
}
}
else
{
_sp_r.scaleX += data * rasio;
_sp_r.scaleY += data * rasio;
_sp_r.x += (_point.x - _sp_r.x) / 15;
_sp_r.y += (_point.y - _sp_r.y) / 15;
if (data <0.2)
{
_sp_r.scaleX += (1 - _sp_r.scaleX) / 5;
_sp_r.scaleY += (1 - _sp_r.scaleY) / 5;
}
}
}
}
}
}
/*
private function FFTModeChanger(e:MouseEvent):void
{
if (FFTMode)
{
FFTMode = false;
}
else
{
FFTMode = true;
}
}
*/
private function ResizeEventHandler(e:Event):void
{
w = stage.stageWidth;
h = stage.stageHeight;
rasio = (w / 16) / original_rasio;
for (var i:int = 0; i < position_array_l.length; i++)
{
var _point:Point = position_array_l[i] as Point;
_point.x = Math.floor((w / 16 / 2) + w / 16 * (i % COL));
_point.y = Math.floor((h / 16 / 2) + h / 16 * (Math.floor(i / ROW)));
}
}
private function PointPosition(array:Array):Array
{
var _array:Array = [];
for (var i:int = 0; i < array.length; i++)
{
var _obj:Object = array[i] as Object;
var _point:Point = new Point(_obj.x, _obj.y);
_array.push(_point);
}
return _array;
}
private function Draw():Array
{
var _array:Array = [];
var colorHSV:ColorHSV = new ColorHSV;
for (var i:int = 0; i < ROW ; i++)
{
for(var j:int = 0; j < COL; j++)
{
colorHSV.h =0.9* (i * ROW + j) * 360 / 256;
trace( "(i*ROW+j) : " + (i*ROW+j) );
var _sp:Sprite = new Sprite();
_sp.graphics.beginFill(colorHSV.value);
_sp.graphics.drawCircle(0, 0, RADIUS);
_sp.graphics.endFill();
_sp.x = 15 + j * (w - 15) / COL;
_sp.y = 15 + i * (h - 15) / ROW;
//_sp.blendMode = "invert";
addChild(_sp);
_array.push(_sp);
}
}
return _array;
}
private function Reset(obj_array:Array):void
{
var colorHSV:ColorHSV = new ColorHSV;
for (var i:int = 0; i < ROW ; i++)
{
for(var j:int = 0; j < COL; j++)
{
colorHSV.h =0.9* (i * ROW + j) * 360 / 256;
trace( "(i*ROW+j) : " + (i * ROW + j) );
var j1:int = i * COL + j;
var _sp:Sprite = obj_array[j1];
_sp.graphics.clear();
_sp.graphics.beginFill(isColor?colorHSV.value:0);
_sp.graphics.drawCircle(0, 0, RADIUS);
_sp.graphics.endFill();
_sp.blendMode = blendModeCurrent;
}
}
}
public function blendModeRadioButtons() :void
{
var blendmodes:Array= [ BlendMode.ADD, BlendMode.ALPHA, BlendMode.DARKEN, BlendMode.DIFFERENCE, BlendMode.ERASE, BlendMode.HARDLIGHT, BlendMode.LAYER, BlendMode.INVERT, BlendMode.LIGHTEN, BlendMode.MULTIPLY, BlendMode.NORMAL, BlendMode.OVERLAY, BlendMode.SCREEN, BlendMode.SHADER, BlendMode.SUBTRACT];
var vbox2:VBox = new VBox(this, 0, 0);
for each (var item:String in blendmodes)
{
new RadioButton(vbox2,0,0,item,false,onRadioButton)
}
label = new Label(vbox2, 0, 0);
label.scaleX = label.scaleY = 10;
label.text = blendModeCurrent;
new CheckBox(vbox2, 0, 0, 'color', onColor).selected=isColor;
new CheckBox(vbox2, 0, 0, 'fft', onfft).selected=FFTMode;
}
private function onfft(e:Event):void
{
var checkBox:CheckBox = e.target as CheckBox;
FFTMode = checkBox.selected;
}
private function onColor(e:Event):void
{
var checkBox:CheckBox = e.target as CheckBox;
isColor = checkBox.selected;
Reset(obj_array_l);
Reset(obj_array_r);
}
private function onRadioButton(e:Event):void
{
var radioButton:RadioButton = e.target as RadioButton;
label.text = radioButton.label;
blendModeCurrent = radioButton.label;
}
}
}