class WhiteNoiseGenerator : Object {
	SDL.AudioSpec desired;
	SDL.AudioSpec obtained;
	Rand rand = new Rand();

	public static void main(){
		new WhiteNoiseGenerator();
		GLib.Thread.usleep(3000000);
	}

	public WhiteNoiseGenerator(){
		desired = SDL.AudioSpec(){
			freq = 44100,
			format = SDL.AudioFormat.S16SYS,
			channels = 2,
			samples = 512,
			callback = write_cb,
			userdata = this.ref()
		};
		SDL.init();
		SDL.Audio.open(desired, out obtained);
		SDL.Audio.pause(0);
	}

	void write_cb(uint8[] stream){
		int16[] noise = new int16[stream.length / sizeof(int16)];
		for(int i = 0; i < noise.length; i++)
			noise[i] = (int16) (rand.int_range(int16.MIN, int16.MAX));
		SDL.Audio.mix(stream, (uint8[]) noise, stream.length, 128);
	}
}
