Created
January 30, 2009 06:55
-
-
Save brapse/54963 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package enell | |
{ | |
import caurina.transitions.Tweener; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
public class Mountain extends Sprite | |
{ | |
//positiong | |
public var initX:int; | |
public var initY:int; | |
private var ranNum: Number; | |
//speed | |
private var order:Number; | |
//color | |
private var initColor: Number = 0x1c335e; | |
private var inMotion:Boolean; | |
private var xPrime: Number; | |
private var yPrime: Number; | |
private var radius:Number; | |
public var count: int; | |
public var inc:int = 1; | |
//parameters | |
public var delay:Number; | |
public var length:Number; | |
public function Mountain(order:Number, initX:int, initY:int,radius:int = 500):void | |
{ | |
this.order = order + 1; | |
this.initX = initX; | |
this.initY = initY; | |
this.ranNum = Math.random() //make this object unique | |
this.x = initX; | |
this.y = initY; | |
this.radius = radius; | |
this.count = (-1*radius) + (order*10); | |
//trace("init count:" + count); | |
//init parameters | |
this.delay = ranNum; | |
this.length = 1- ranNum; | |
addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
createUi(10); | |
//this.alpha = 0; | |
this.inMotion = true; | |
Tweener.addTween(this, {alpha: 1, time: 1+ranNum, onComplete: stopMotion}); | |
} | |
private function createUi(size:Number):void | |
{ | |
graphics.beginFill(initColor|(order*10<<8)); | |
graphics.drawRect(initX,initY,size,size+4); | |
//graphics.drawCircle(initX, initY, size); | |
graphics.endFill(); | |
} | |
private function onEnterFrame(evt:Event):void | |
{ | |
//shiftMove(); | |
} | |
private function increment():void | |
{ | |
if(count < -radius) | |
{ | |
inc = 1; | |
}else if(count > radius){ | |
inc = -1; | |
} | |
count += inc*10; | |
} | |
public function startMotion():void | |
{ | |
generatePrimes(); | |
this.inMotion = true; | |
x = initX; | |
y = initY; | |
graphics.moveTo(initX, initY); | |
graphics.lineTo(xPrime,yPrime); | |
Tweener.addTween(this,{x:xPrime,y:yPrime, time:length, | |
delay: delay, transition: "linear" , | |
onComplete: nextMotion, onCompleteParams:[this]}); | |
} | |
public function nextMotion(clip:Mountain): void | |
{ | |
if(clip.inMotion) | |
{ | |
//trace("transition from (" + this.initX + ", " + this.initY + ") -> (" + this.xPrime + ", " + this.yPrime + ")"); | |
//return to orgin; | |
x = initX; | |
y = initY; | |
alpha = 0; | |
generatePrimes(); //willset xPrime and yPrime | |
Tweener.addTween(clip,{x:xPrime,y:yPrime, time:1+length, transition: "linear", | |
alpha: 1,rotation: ranNum* 100, | |
onComplete: nextMotion, onCompleteParams:[clip]}); | |
} | |
} | |
public function shootMove(time:int=1, _alpha:int = 1, rotation:int=0):void | |
{ | |
if(!this.inMotion) | |
{ | |
this.inMotion = true; | |
x = initX; | |
y = initY; | |
//this.alpha = 0; | |
generatePrimes(); | |
Tweener.addTween(this,{x:xPrime,y:yPrime, time:time, transition: "linear", | |
alpha: _alpha, rotation: rotation, | |
onComplete: stopMotion}); | |
trace("shootMove Complete!"); | |
} | |
} | |
public function bolt(amount:Number=5, time:int=1):void | |
{ | |
trace("bolting"); | |
this.scaleX = 1; | |
this.scaleY = 1; | |
Tweener.addTween(this, { | |
scaleX:amount, scaleY: amount, time:time }); | |
} | |
//Produce next x and y variables | |
public function generatePrimes(): void | |
{ | |
//increment(); | |
xPrime = count; | |
yPrime = radius - Math.abs(xPrime); | |
} | |
public function stopMotion():void | |
{ | |
this.inMotion = false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Experiments with flash visualization | |
Faurier transforms used to analyse a sound file | |
and move some squares around. | |
requires sound.mp3 file | |
*/ | |
package { | |
import enell.Mountain; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.geom.Matrix; | |
import flash.media.Sound; | |
import flash.media.SoundChannel; | |
import flash.media.SoundMixer; | |
import flash.net.URLRequest; | |
import flash.utils.ByteArray; | |
import flash.system.Security; | |
Security.allowInsecureDomain(true); | |
[SWF(backgroundColor="#000000", width="500", height="500") ] | |
public class cinviz extends Sprite | |
{ | |
private var sound:Sound; | |
private var channel:SoundChannel; | |
private var spc:Number; | |
private var mod:Number; | |
private var sp:Sprite = new Sprite(); | |
private var gr:Sprite = new Sprite(); | |
private var count:Number = 0; | |
private var mat:Matrix = new Matrix; | |
private var mountains:Array = new Array(); | |
//computed values | |
private var mean:Number; | |
private var mode:Number; | |
private var stdD:Number; | |
public function cinviz() | |
{ | |
//stage.align = StageAlign.LEFT; | |
sound = new Sound(new URLRequest("sound.mp3")); | |
channel = sound.play(); | |
for(var i:int;i < 2; i++) | |
{ | |
var mount:Mountain = new Mountain(i, 100, 100 ); | |
mountains.push(mount); | |
addChild(mount); | |
} | |
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
} | |
private function getRandom( minVal:Number, maxVal:Number):Number{ | |
return minVal + Math.random() * (maxVal - minVal); | |
} | |
public function onEnterFrame(e:Event):void | |
{ | |
var spectrum:ByteArray = new ByteArray(); | |
var mod:Number; | |
SoundMixer.computeSpectrum(spectrum, true); | |
count++; | |
if(count > 2){ | |
computeBa(spectrum); | |
for(var i:int=0;i < mountains.length; i++) | |
{ | |
spectrum.position=i; | |
mod = spectrum.readFloat(); | |
//mountains[i].initX = (mountains[i].initX + mod) % 250; | |
//mountains[i].initY = (mountains[i].initY + mod) % 250; | |
mountains[i].shootMove(); | |
mountains[i].bolt(); | |
//mountains[i].length = mod; | |
} | |
} | |
} | |
//produces mean, mode and standard deviation | |
public function computeBa(ba:ByteArray): void | |
{ | |
var cur:Number; | |
var sum:Number = 0; | |
var seq:Number = 0; | |
var min:Number; | |
var max:Number; | |
for(var i:int = 0; i < 256;i++) | |
{ | |
cur = ba[i]; | |
sum += cur; | |
if(i == 0 || cur < min) | |
min = cur; | |
if(i == 0 || cur > max) | |
max = cur; | |
} | |
this.mean = sum/256; | |
for(var k:int = 0; k < 256;k++) | |
{ | |
cur = ba[i]; | |
seq += Math.pow(mean - cur,2); | |
} | |
this.stdD = Math.sqrt(seq/256); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment