Bytebeats in C and Python (generative symphonies from extremely small programs)

In October 2011, viznut posted about his initial experiment with “bytebeats,” or extremely short C programs using one line of math to make generative music. He followed up with a much more in-depth article describing the mathematics and musical theory behind these operations. The concept is simple, and the results are beautiful and diverse. The involved math uses bitwise operations and bit shifts (as well as general operations) based on an ever-increasing integer in an infinite loop. The result is printed to the standard output as an ASCII character, resulting in a never-ending chain of chars that are piped through the sound card and, thus, interpreted as audio.


There are several websites allowing you to compose formulas and listen to their output, such as this awesome site. But this post is aimed at those of you whom would like to try this out for yourselves, on your own machines. If you’re using Mac OS X, you’ll quickly learn that there is no /dev/audio (or /dev/dsp) to pipe through. Interestingly, my Linux install is missing these as well (Windows users can pipe through Cygwin, or so I hear). Instead, you should install SoX (Sound eXchange), a command line tool for audio conversions, audio playback, and recording. Most importantly, SoX has the ability to pipe raw data through the default audio device. It’s a pretty nifty tool (though its argument interface is a bit crowded).

Using C is as simple as it gets in terms of writing the code, although using a Python script simplifies things since you don’t need to recompile every time you make a change. This comes at the cost of extra lines, however, so stick with C if you want to write one-line symphonies.

The following code blocks have “PLACE MATH HERE” comments showing where to put your symphony’s formula. Once you’re ready to run the program, you’ll pipe it through sox with the following command:

-r is the sampling rate, here 8000 Hz
-b is bits, here 8
-c is channels, here 1
-t is filetype, here raw
-s is signed integer encoding
is pipe input (or redirect output)
-d is default audio device

Using C

Set up a source file, bytebeat.c perhaps, with the following code:

In the terminal, type the following commands to compile your program and pipe it through SoX:

Using Python

Set up a script, perhaps, with the following code:

Where C apparently uses an implicit modulus on its putchar() function so that numbers larger than 255 will still produce an ASCII character, Python is stricter. It will throw a ValueError if the chr() function is given too large a value. Thus, the % 256 is needed above. If you’re using Python 3.0, ignore the __future__ import. Also, if you don’t want to use that for some reason, you’ll have to import sys and use sys.stdout.write(chr(int( without the end=”” argument to achieve the same result.

In the terminal, type the following command to run your script and pipe it through SoX:

Some songs

Here are some formulas I’ve come up with that I find particularly interesting or nice. If you don’t want to produce these on your own sound card, paste them into the aforementioned JavaScript-fueled site to test them. Please share yours!

One Response to “Bytebeats in C and Python (generative symphonies from extremely small programs)”

  1. Maaan I couldn’t pipe successfully into sox. So cool you actually ported the thing to python :) Much appreciated

Leave a Reply