forked from: forked from: NoiseMaker
// forked from shinseki09's forked from: NoiseMaker
// forked from kappaLab's NoiseMaker
package
{
import flash.display.Sprite
import flash.display.Shape
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.events.SampleDataEvent;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.utils.Timer;
[SWF(backgroundColor=0x000000, frameRate=60)]
public class NoiseMaker extends Sprite
{
private var monitor:Vector.<Shape> = new Vector.<Shape>()
public function NoiseMaker()
{
for (var i:int = 0; i < 256; i++)
{
var s:Shape = new Shape()
s.graphics.beginFill(0xFFFFFF * Math.random());
s.graphics.drawCircle(0, 0, 200)
//s.x = Math.sin(i) * (stage.stageWidth/2) + stage.stageWidth/2;
//s.y = Math.cos(i) * stage.stageHeight/2 + stage.stageHeight/2;
s.x = stage.stageWidth * i / 256
s.y = stage.stageHeight/2
monitor.push(s)
addChild(s)
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
filters = [new BlurFilter(5, 5)]
var count:int = 0
function setVol():void
{
count++
if (count > 240) {
count = 1
}
if (count < 20) return;
if (count % 20 == 0) {
volB = 2
}
if (count % 20 == 1
|| count % 20 == 2
|| count % 20 == 3
|| count % 20 == 4
|| count % 20 == 5
|| count % 20 == 6) {
if(Math.random() > .5)
volB2 = 2
}
if (count < 40) return;
if (count % 40 == 0) {
volP = 1
}
if (count % 40 == 5) {
volP2 = 1
volN = 1
}
if (count % 40 == 5
|| count % 40 == 6
|| count % 40 == 7
|| count % 40 == 8
|| count % 40 == 9
|| count % 40 == 10) {
if(Math.random() > .5)
volN = 2
}
if (count < 120) return;
if (count % 120 == 0) {
volK2 = 1
}
if (count % 120 == 11
|| count % 120 == 12
|| count % 120 == 13
|| count % 120 == 14
|| count % 120 == 15
|| count % 120 == 16) {
if(Math.random() > .3)
volK = 1
}
if (count % 120 == 11
|| count % 120 == 12
|| count % 120 == 13
|| count % 120 == 14
|| count % 120 == 15
|| count % 120 == 16) {
if(Math.random() > .1)
volK3 = 1
}
if (count % 120 == 14
|| count % 120 == 15
|| count % 120 == 16
|| count % 120 == 17
|| count % 120 == 18
|| count % 120 == 19) {
if(Math.random() > .1)
volK2 = 1
volP = 1
}
}
var volB:Number = 0
var volB2:Number = 0
var volK:Number = 0
var volR:Number = 0
var volP:Number = 0
var volP2:Number = 0
var volN:Number = 0
var volN2:Number = 0
var volK2:Number = 0
var volK3:Number = 0
var mySound:Sound = new Sound();
function gen(e:SampleDataEvent):void {
setVol()
for ( var c:int = 0; c < 2048; c++ ) {
var i:Number = Number(c + e.position)
var arr:Vector.<Number> = new Vector.<Number>()
var bass:Number = 0;
volB += (0 - volB) * .0001;
bass = sine(i,60);
bass *= volB
arr.push(bass)
var bass2:Number = 0;
volB2 += (0 - volB2) * .0008;
bass2 = sine(i,20) ;
bass2 *= volB2;
arr.push(bass2)
var kick:Number = 0;
volK += (0 - volK) * .0001;
kick = triangle(i, 60);
kick = (kick < .1)?0:kick;
kick *= (volK > .5)?1:volK;
arr.push(kick)
var kick2:Number = 0;
volK2 += (0 - volK2) * .0008;
kick2 = square(i, .5) + square(i-1, 5) + noise();
kick2 *= volK2
arr.push(kick2)
var kick3:Number = 0;
volK3 += (0 - volK3) * .0006;
kick3 = triangle(i, 2) + square(i,1)
kick3 *= volK3
arr.push(kick3)
var piano:Number = 0;
volP += (0 - volP) * .0001;
piano = sine(i,2.5)
piano *= volP
arr.push(piano)
var piano2:Number = 0;
volP2 += (0 - volP2) * .0001;
piano2 = sine(i,2.75)
piano2 *= volP2
arr.push(piano2)
var nis:Number = 0
volN += (0 - volN) * .0005;
nis = noise()
nis *= volN;
arr.push(nis)
var nis2:Number = 0
volN2 += (0 - volN2) * .001 ;
nis2 = noise() + square(i,.5)
nis2 *= volN2;
arr.push(nis2)
var smpl:Number = 0
for each (var p:Number in arr) {
smpl += p
}
e.data.writeFloat(smpl);
e.data.writeFloat(smpl);
}
}
mySound.addEventListener(SampleDataEvent.SAMPLE_DATA,gen);
mySound.play();
}
private function onEnterFrame(e:Event):void
{
var bytes:ByteArray = new ByteArray();
SoundMixer.computeSpectrum(bytes, false, 0);
if (bytes.length == 1) return
//trace(bytes.readFloat())
bytes.position = 0
for (var i:int = 0; i < 256 ; i++ ){
var sample:Number = bytes.readFloat()
monitor[i].scaleX = sample * 1
monitor[i].scaleY = sample * 1
}
}
private function sine(i:Number,f:Number):Number
{
var smpl:Number = Math.sin(i / Math.PI / f);
return smpl
}
private function square(i:Number, f:Number):Number
{
var smpl:Number = Math.sin(i/Math.PI/f);
smpl = (smpl > 0)?1: -1;
return smpl
}
private function saw(i:Number,f:Number):Number
{
f *= 25
var smpl:Number = 1 - (i % f) / f * 2;
return smpl
}
private function triangle(i:Number,f:Number):Number
{
f *= 25
var smpl:Number =Math.abs(((i) % f) / f - .5) * 4 - 1;
return smpl
}
private function noise():Number
{
return Math.random() * 2 - 1;
}
}
}