pinknoise
package {
import flash.display.Sprite;
import flash.events.SampleDataEvent;
import flash.media.Sound;
public class PinkNoise extends Sprite {
private var pn:PinkNumber;
private var snd:Sound;
public function PinkNoise() {
init();
}
private function init():void {
snd = new Sound();
snd.addEventListener(SampleDataEvent.SAMPLE_DATA, pinknoiseGenerator);
pn = new PinkNumber(100);
snd.play();
}
private function pinknoiseGenerator(e:SampleDataEvent):void {
for(var c:uint = 0; c < 8192; c++) {
e.data.writeFloat(pn.nextValue*0.01);
e.data.writeFloat(pn.nextValue*0.01);
}
}
}
}
class PinkNumber {
/* via
* http://www.firstpr.com.au/dsp/pink-noise/
*/
private var max_key:int;
private var key:int;
private var white_values:Array;
private var range:uint;
public function PinkNumber(range:int = 128) {
max_key = 0x1f;
this.range = range;
key = 0;
white_values = new Array();
for(var i:uint = 0; i < 5; i++) {
var white_value:Number = Math.random() * 32767 % (range/5);
white_values.push(white_value);
}
}
public function get nextValue():int {
var last_key:int = key;
var sum:uint;
key++;
if(key > max_key) {
key = 0;
}
var diff:int = last_key ^ key;
sum = 0;
for(var i:uint = 0; i < 5; i++) {
if(diff & (1 << i)) {
white_values[i] = Math.random() * 32767 % (range/5);
}
sum += white_values[i];
}
return sum;
}
}