# Bytebeat

Bytebeat is a procedural chiptune/8bit style music generated by a short expression in a programming language; it was discovered/highlighted in 2011 by Viznut (author of countercomplex blog) and others, and the technique capable of producing quite impressive music by single-line code has since caught the attention of many programmers, especially in the demoscene. There has even been a [paper](https://arxiv.org/abs/1112.1368) written about bytebeat. Bytebeat can produce music similar (though typically a bit simpler) to that created e.g. with music trackers but with a lot less complexity, storage size and usually also less effort. The techniques can also be used to produce non-musical sounds, for example a car engine in a racing game etc. There are now communities, such as https://dollchan.net, who experiment, investigate and create at times very advanced bytebeat.

Some advantages of bytebeat music are:

- Ability to create something that sounds like music very quickly, easily, without requiring skills of music composition.
- Taking negligible space to store. This is however a form of space-time tradeoff and therefore comes at the price of requiring more computational power -- the power required is also very small but may play a role when using the most primitive of computers.
- Taking negligible amount of code to program, removing bloat.
- NOT requiring special music hardware such as oscillator chips etc., all that's needed is a CPU. Again there is a catch in putting more stress on the CPU -- which is no issue even with weak computers nowadays, but may present some trouble for the most primitive computers (which may potentially be one reason for why bytebeat wasn't discovered and used in the earliest computers).
- Copyright may be inapplicable or much more difficult to enforce as we're only dealing with trivially simple snippets of code, i.e. this form of art may be more free as in freedom.
- ...

This is a beautiful hack for LRS/suckless programmers because it takes quite a tiny amount of code, space and effort to produce nice music, e.g. for games (done e.g. by Anarch).

8bit samples corresponding to `unsigned char` are typically used with bytebeat (but it's not a necessity). The formulas take advantage of wrap-around overflows occurring when a variable is constantly incremented in loop, which when played as as sound will create a rhythmical pattern, i.e. the value keeps going up and upon reaching the maximum value jumps back to minimum, creating a saw-shaped waveform of a fixed frequency. This is built upon by using operators such as bit shifts, addition and multiplication, but also other bitwise operators and more complex functions to create more interesting effects.

Bytebeat also looks very cool when rendered as an image (outputting pixels instead of audio samples). Oftentimes we see fractal patterns in it.

By now some have mastered the art of bytebeat and have taken it to the level at which we can produce tracks sounding like completely legitimate music. Making bytebeat manually is simple in that it can be done quickly and easily, but difficult by the fact that we're dealing with a chaotic and therefore unpredictable system, so finding a good sounding formula is very much a trial and error effort. Nonetheless it is possible to tame the system to a degree, for instance by composing libraries of patterns and advanced bytebeat editors or by using genetic programming and/or bruteforce to simply search for a formula that will best approximate a real audio track. This way people have produced bytebeat formulas playing Rickroll etc. As one would expect such formulas are considerably more complex than the simple, manually created bytebeat -- sometimes taking several lines of code as opposed to just several characters -- but even so it is incredible that a whole music track that would otherwise take hundreds of kilobytes to store can be compressed this way into a bunch of instructions taking orders of magnitude less storage space.

## How To

Quick experiments with bytebeat can be performed with online tools that are easy to find on the web, these usually use JavaScript.

Nevertheless, traditionally we use C for bytebeat. We simply create a loop with a *time* variable (`i`) and inside the loop body we create our bytebeat expression with the variable to compute a char that we output.

A simple "workflow" for bytebeat "development" can be set up as follows. Firstly write a C program:

```
#include <stdio.h>

int main(void)
{
  for (unsigned int i = 0; i < 10000; ++i)
    putchar(
      (i >> 4) + (i * i) % 64 // < bytebeat formula here
    );

  return 0;
}
```

Now compile the program and play its output e.g. like this:

```
gcc program.c && ./a.out | aplay
```

Now we can just start experimenting and invent new music by fiddling with the formula indicated by the comment.

General tips/tricks and observations are these:

- Outputting the variable `i` creates a periodical saw-shaped beat, **multiplication/division decreases/increases the speed, addition/subtraction shifts the phase backward/forward**.
- Squaring (and other powers) create a **wah-wah effect**.
- Crazier patterns can be achieved by **using the variable in places of numerical constants**, e.g. `i << ((i / 512) % 8)` (shifting by a value that depends on the variable).
- Modulo (`%`) increases the frequency and **decreases volume** (limits the wave peak).
- So called **Sierpinski harmonies** are often used melodic expressions of the form `i*N & i >> M`.
- Bitwise and (`&`) can add distortion (create steps in the wave).
- A **macro structure** of the song (silent/louds parts, verse/chorus, ...) can be achieved by combining multiple patterns with some low-frequency pattern, e.g. this alternates a slower and faster beat: `int cond = (i & 0x8000) == 0;`, `cond * (i / 16) + !cond * (i / 32)`
- **Extra variables** can add more complexity (e.g. precompute some variable `a` which will subsequently be used multiple times in the final formula).
-  It may be possible to create something more impressive by further incorporating non-bytebeat techniques such as FIR filters.
- ...

## Copyright

It is not exactly clear whether, how and to what extent copyright can apply to bytebeat: on one hand we have a short formula that's uncopyrightable (just like mathematical formulas), on the other hand we have music, an artistic expression. Many authors of bytebeat "release" their creations under free licenses such as CC-BY-SA, but such licenses are of course not applicable if copyright can't even arise.

We believe copyright doesn't and SHOULDN'T apply to bytebeat. To ensure this, it is possibly good to stick CC0 to any released bytebeat just in case.

## Examples

A super-simple example can be just a simple:

- `i * 2`

The following more complex examples come from the LRS game Anarch (these are legally safe even in case copyright can apply to bytebeat as Anarch is released under CC0):

- distortion guitar rhythmical beat: `~((((i >> ((i >> 2) % 32)) | (i >> ((i >> 5) % 32))) & 0x12) << 1) | (i >> 11)`
- electronic/techno: `((0x47 >> ((i >> 9) % 32)) & (i >> (i % 32))) | (0x57 >> ((i >> 7) % 32)) | (0x06 >> ((i >> ((((i * 11) >> 14) & 0x0e) % 32)) % 32))`
- main theme, uses an extra variable: `(((i) & 65536) ? (a & (((i * 2) >> 16) & 0x09)) : ~a)`, where `uint32_t a = ((i >> 7) | (i >> 9) | (~i << 1) | i)`

Here is an ASCII visualization of the first track:

```
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
M'''''''''''''''M'''''''''''''''k'M'''''''''''''k'M'''''''''''''
c;xxxxxxxxxxxxxxc;cxxxxxxxxxxxxx,;xxxxxxxxxxxxxx,;cxxxxxxxxxxxxx
. ;.:;;;;;;;;;;;. ;.:;;;;;;;;;;;0 . :;;;;;;;;;;;0 . :;;;;;;;;;;;
okokokkkkkkkkkkklklkokkkkkkkkkkkokokokkkkkkkkkkklklkokkkkkkkkkkk
xkX0000000000000ckX0000000000000xkXK000000000000ckXK000000000000
oMKMKMMMMMMMMMMMlMkMKMMMMMMMMMMMoMKMKMMMMMMMMMMMlMkMKMMMMMMMMMMM
xK00KWWWWWWWWWWWcKX0KWWWWWWWWWWWxK0KKWWWWWWWWWWWcKXKKWWWWWWWWWWW
xMWMWMWWWWWWWWWWx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