Bytebeat

What is bytebeat?

Bytebeat is music generated from short programs. Specifically, these programs generate PCM audio as a function of time. The first explanation I ever read of bytebeat was this web page, and every page I've seen mention bytebeat links back to it too, so I'll carry on the tradition.

To summarise that page, a bytebeat program is a piece of code, which when put in a loop that increments a value t, generates a piece of music. Below is Crowd, one of the first pieces of bytebeat music discovered.

((t<<1)^((t<<1)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7 Crowd by Kragen, CC-BY

How is audio represented on computers?

As a list of numbers, just like everything else. The most common way of representing a waveform on a computer is called Linear Pulse Code Modulation, where the list of numbers are called samples and they represent discrete amplitude levels. The samples are spaced evenly in time.

The most common audio sampling encoding is signed integer 16-bit 44.1kHz, meaning that each sample lasts 1/44100 seconds (about 22 microseconds), and is an integer between -32768 and 32767. Larger bitdepths are typically represented with samples as floats between -1 and 1, allowing the signal to be described independently of the quantization step. The size of the quantization step is 0.5**(n-1) where n is the bitdepth (0.000030517578125 for 16-bit).

The typical encoding used in bytebeat is unsigned integer 8-bit 8kHz, i.e. each sample is a value between 0 and 255, and is played for 1/8000th of a second (125 microseconds). This encoding is used because it is the default encoding used by aout on Linux, as it was the standard encoding when PCM sound cards first came to market.

Helper Functions

The Math object is included, so sin calls Math.sin, cos calls Math.cos, etc.

In C, it is possible to parse a character as an integer. Some bytebeat composers use this to index into a string to iterate through a list of integers in a compact syntax. For example, in C, "HEADACHE"[t%8] would produce the numbers [72, 69, 65, 68, 65, 67, 72, 69] repeatedly, the ASCII character codes for the letters "HEADACHE".

To imitate this behaviour in JavaScript, the function int works as Math.floor for numbers, and as (x,i) => x.charCodeAt(i) for strings. For example the C code "HEADACHE"[(t>>11)%8] could be written as int("HEADACHE",(t>>11)%8).

int("HEADACHEGOLDFISH",(tt>>10)%(8+(8&((tt>>14)|(tt>>15)))))*(2**(n=2+((tt>>15-(3&tt>>18))%4))/3**n*n*t&~7&0x1e70>>((tt>>16)%8)) Headache Goldfish by Stellartux, CC-BY