# Write the prettiest bytebeat composition

Bytebeat is a style of music one can compose by writing a simple C program that's output is piped to aplay or /dev/dsp.

main(t){for(;;t++)putchar(((t<<1)^((t<<1)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7);}


There is a good deal of information on the bytebeat site, a javascript implementation, and more demos and example compositions in this thread.

Very simple rules : Try to write a pretty sounding composition. Most up votes wins since that's obviously subjective, although not that subjective considering the usual results.

• Useful blog article Jan 24 '12 at 6:28
• Are you holding this to strict bytebeat (i.e. no buffers)? Jan 24 '12 at 12:43
• Why is it restricted to C? Jan 24 '12 at 15:04
• There isn't afaik any intrinsic reason to restrict using buffers or other languages, but usually bytebeat means that particular C loop with the output defined by a formula. I voted up Joey Adams answer even thought rand() is non-standard. Jan 24 '12 at 15:51
• While I like this idea, the FAQ calls for an objective winning criteria (it's too easy to get trapped in calculations or ASCII art). Can we look for something better defined than "pretty"? Jan 24 '12 at 16:44

(Signed 16-bit little endian, 8000Hz mono (--format=S16_LE))

## Music

Much better than before! (although it's quite long)

main(t){for(;;t++)putchar(((7&(((t>>17)+1)>>2)+((t>>10)&1+2*(t>>18&1))*(("23468643"[7&t>>12]-48)+(3&t>>11))+((3&t>>17)>0)*(3&t>>9)*!(1&t>>10)*(((2+t>>10&3)^(2+t>>11&3))))*t*"@06+"[3&t>>15]/32));}

(You can listen this at here)

I wrote this, but even I don't know how some part works, like >0 and (especially) the first 7&.

Change for loop to for(;!(t>>22);t++)... to listen it 'once'. I don't know whether it "loops" exactly the same way, however.

## Melody (base of above music)

I love this melody I made (C-G-A-F ftw), but it's too 'plain'...

main(t){for(;;t++)putchar(((t>>10)&1)*(t*("23468643"[7&t>>12]-48)+t*(3&t>>11))*"@06+"[3&t>>15]/32);}

## Simple music (which I made before)

main(t){for(;;t++)putchar(t*(3&t>>11)+(t&t>>11)*4*!((t>>11)%3));}

• If you click the above bytebeat link, you'll see a sox command line to convert it to a .wav file, and an odd compressor command line. Jan 24 '12 at 15:57
• +1 Amazing! A few bits (e.g. 1:30 - 1:40) sound kind of "stuttering", but then again, much of the very charm of bytebeat is in these odd and unconventional rhythms and melodies, and the overall effect is excellent. (Ps. I accidentally played this at 8 bits per sample, and I sounds surprisingly good that way too. Sort of like playing a 45 rpm vinyl at 33.3 rpm.) Jan 25 '12 at 19:14

The ruler function in C minor:

#include <math.h>
#include <stdio.h>

#define PI 3.14159265358979323846

#define step(freq, n) ((freq) * pow(2, (n) / 12.0))
#define note(n)       step(440, n)
#define MIDDLE_C      note(-9)

int count_zeros(unsigned int n)
{
int count = 0;
for (; (n & 1) == 0; n >>= 1)
count++;
return count;
}

int minor_note(int note)
{
int octave = note / 7;
int scale[] = {0, 2, 3, 5, 7, 8, 10};

note %= 7;
if (note < 0) {
note += 7;
octave--;
}

return scale[note] + octave*12;
}

int main(void) {
double t = 0.0;
double freq = MIDDLE_C * 2;
double step = PI * 2 / 8192;
int n = 0;
int i = 0;

for (i = 1;; t += step, i++) {
if (i == 1024) {
i = 0;
n++;
freq = step(MIDDLE_C, minor_note(count_zeros(n)));
}

putchar(sin(t * freq) * 50.0 + 128.0);
}

return 0;
}

• Nice melody, but to be a "composition" this IMO needs something more... Jan 25 '12 at 19:20
main(t){for(;;t+=(t%6)?1:2)putchar((((t<<t^(t>>8))|(t<<7))*((t<<t&(t>>12))|(t<<10))));}

• Very classic bytebeat, if that's not an oxymoron. +1 from me. Jan 25 '12 at 19:27

Emphasising "beat" over "byte":

#include<math.h>

double s(double,double);double r(double,double);double d(double);double f(double);

char bytebeat(int t){return (d(f(t/4000.)/3) + 1) * 63;}
double f(double t){
double sn=s(1./2,t-1); sn*=(sn*sn);
return 3*s(1./4,1/s(1,t))+3*s(4,1/sn)/2+s(4,1/(sn*sn*sn*sn*sn))/4
+2*s(55+18.3*r(1./2,t),t)+s(110+s(5,t)/4000,t)*s(1,t)+s(220+110*r(1,t)+55*r(3,t),t)/5
+s(880+440*r(1./2,t)-220*r(1,t)+110*r(2,t)+s(5,t)/4000,t)
*(2+s(1760+438*r(3./2,t)-1234*r(2,t)+423*r(5,t),t))/9
+s(s(1,t)+s(1./2,t)+s(1./4,t)+s(1./8,t),t)*s(s(1,t)+s(1./2,t)+s(1./4,t)+s(1./8,t)+1,t)
+r(264+11*r(1./20,t),t)*s(1./20,t);
}
double s(double f,double t){return d(sin(f*3.14159265*(t+999)));}
double r(double f,double t){return s(f,t)<0;}
double d(double a){return tanh(a+a*a/4);}

main(t){for(;;++t)putchar(bytebeat(t));}


To be used at 8 kHz, uint8 mono. Sounds best over decently bass-capable loudspeakers.

main(){for(;;)putchar(rand());}


Sounds like the ocean ;-)

• More like a really busy railway crossing to me. :) Jan 25 '12 at 19:23
• Protip: if it sounds like a continuous tone, your RNG is broke. Jan 25 '12 at 23:15
• easier way: \$ cat /dev/urandom | aplay May 25 '14 at 1:46
• @B1KMusic much more simple: aplay /dev/urandom May 1 '15 at 7:04

Combined melody and harmony:

r=3,
r=3,
m=(t*(t>>12|t>>13|t>>14|t>>15|t>>16|t>>17|t>>18))&63,

h= ((t&t>>7&t>>6)|t*5&t>>8-c^t*6&t>>9-c|t*7&t>>12-c^t*9&t>>11-c^t*11&t>>22^t*19&t>>20^t*14&t>>20|t*23&t>>15-c|t*12&t>>9|t*30&t>>30|t>>5|t>>4)-31, m|h