Bytebeat: Algorithmic Symphonies

A couple of days ago I first encountered “bytebeat”. This is a new hype revolving algorithmic music and sounds. The basic idea is this:

main(t) {
  for(t=0;;t++)
    putchar( t * (((t>>12) | (t>>8) ) & ( 63 & (t>>4))));
}

This simple loop has one variable, t (time). And every iteration we use t to calculate some output. Next, we take the output and pipe it into 8-bit 8-kHz PCM channel (for example /dev/audio!).

A couple of examples in a YouTube video:
<iframe type="text/html" width="640" height="390" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen src="http://www.youtube.com/embed/tCRPUv8V22o"></iframe>

In Java you could do the following:

import javax.sound.sampled.*;

public class AudioPump {

	public static void main(String[] args) throws Exception {

		AudioFormat format = new AudioFormat(8000f, 8, 1, false, false);
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
		SourceDataLine soundLine = (SourceDataLine)AudioSystem.getLine(info);
		soundLine.open(format, 32000);
		soundLine.start();

		byte[] buffer = new byte[8];
		int t = 0;

		while(true) {
			for(int n = 0; n < buffer.length; n++) {
				buffer[n] = (byte)f(t++);
			}
			soundLine.write(buffer, 0, buffer.length);
		}
	}

	private static int f(int t) {
		return (t>>7|t|t>>6)*10+4*(t&t>>13|t>>6);
	}
	//return (t*(t>>5|t>>8))>>(t>>16);
	//return t*(((t>>12)|(t>>8))&(63&(t>>4)));
}

These simple one-liners are able to produce amazing sounds and music!

It becomes even easier to play around with bytebeat if you use one of the many Javascript implementations.
Here is one (including the best song I’ve created): http://t.co/9oognysS

The computer/synthetic sounds are also good for dubstep.

For more information on this subject:
- http://canonical.org/~kragen/bytebeat/
- http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html