# Digital Computer Music with Bitwise Operators

## Bytebeat Music

Bytebeat music is a new kind of mathematical experimental extreme minimalist music using binary logical operators (binary arithmetic).

Very small equations can produce melodies and rhythms.

b

It is an interesting discovery in Computer Music made by (or shared by) Viznut (Ville-Matias Heikkilä). Here we create some of these sounds using JavaScript audio.

Those sounds use binary operators in equations to create music and sound effects.

Complex sound waves can be generated by combining time variable (t) and bitwise operation (logical AND, OR, SHIFT,...) to create patterns and structures that can be converted to sound to create beats, effects and a new kind of electronic music. Some of these sounds effects are similar to video game music, chiptune (chip music), Sawtooth wave, 8bit music synths and the demo scene assembly-generated music and tunes.

Here are some of my first "compositions" while playing with the equations:

http://bit.do/bitmusic1

Equations:

Left audio channel: t*((t>>24|t>>44)&51&t>>1)

Right audio channel: t*((t>>24|t>>44)&15&t>>1)

Sample frequency: 22050 Khz

http://bit.do/bitmusic2

Left: t*((t>>12|t>>31)&51&t>>6)

Right: t*((t>>12|t>>31)&51&t>>5)

Sample frequency: 22050 Khz

http://bit.do/bitmusic3

Left speaker: (t/3)*((t>>12|t>>31)&31&t>>4)&68

Right speaker: (t/4)*((t>>12|t>>31)&31&t>>5)&68

Sample frequency: 44100 Khz

http://bit.do/bitmusic4

Left: (t/4)*(((t/2)>>12|t>>13)&15&t>>5)&180

Right: (t/3)*(((t/2)>>12|t>>13)&15&t>>4)&180

http://bit.do/bitmusic5

Left: (t/2)*((t>>12)&(t/2))&90

Right: (t/3)*((t>>12)&(t))&90

Sample frequency: 44100 Khz

http://bit.do/bitmusic6

Left: ((t/4)*((t>>12)&(t/2))&60)

Right: ((t/3)*((t>>12)^(t/2))&60)

44100 Khz

http://bit.do/bitmusic7

(((t/4)>>2)|(((t/2)&(16384))?(t/4)<<2:(t/2)))

(((t/4)>>3)|(((t/2)&(16384))?(t/2)<<2:(t/2)))

44100 Khz

http://bit.do/bitmusic8 : sin(t*(sin(t*0.01)*0.0005))*63

http://bit.do/bitmusic8b : sin(t*(sin(t*0.003)*0.00015))*63

http://bit.do/bitmusic8c : sum of two sin waves

Mono

44100 Khz

This one does not have any bitwise operators, only sine curve to generate the sound.

http://bit.do/bitmusic10

Left: (3000/(t&(2100+t/4100))&1)*35 - (3000/(t&2100)&1)*35

Right: (3000/(t&(2200+t/2000))&1)*35 - (3000/(t&2200)&1)*35

44.1 Khz

http://bit.do/bitmusic11

Left: (3000/(t&(t>>10))&1)*35

Right: (3000/(t&(t>>11))&1)*35

44.1 Khz

http://bit.do/bitmusic12 (with drums!)

Left: (3000/(t&(t>>10))&1)*35+(3000/(t&16383)&1)*35

Right: (3000/(t&(t>>11))&1)*35+(3000/(t&21383)&1)*35

44.1 Khz

http://bit.do/bitmusic13

Left: (3000/(t&(t<<10))&1)*35

Right: (3000/(t&(t>>10))&1)*35

44.1 Khz

Variation of this:

http://bit.do/bitmusic13d

http://bit.do/bitmusic13e

http://bit.do/bitmusic13f

http://bit.do/bitmusic13g

http://bit.do/bitmusic13h

http://bit.do/bitmusic13i

http://bit.do/bitmusic13j

http://bit.do/bitmusic13k

http://bit.do/bitmusic13b

Improved complete version: http://bit.do/bitmusic-x1

http://bit.do/bitmusic14

http://bit.do/bitmusic15 (fade-laser effect)

http://bit.do/bitmusic15b (fade-laser effect. This second version is better because of the "fade in effect" in the beginning to reduce starting noise)

http://bit.do/bitmusic16

Melody in two parts (3 seconds each), using the ternary operator:

`t<(44100*3) ? (t/4)*((t<<1|t>>4)&13&t>>12)&58 // First part`

: (t/6)*((t<<1|t>>4)&13&t>>12)&58 // Play after 3 sec.

http://bit.do/bitmusic17

Simple demo of two seconds audio.

Second 1 is a pure "t" note, second 2 is "t/2" (half frequency). Using ternary operator (*condition true ? then : else*).

t<44100 ? (t) : (t/2)

44100 Khz

http://bit.do/bitmusic18

Simple demo of a pure note of 440 Hz (pitch standard) using sine wave:

sin(440*2*3.14*t/44100)*128

sin(frequency * 2 * PI * t/sample_rate) * Amplitude

http://bit.do/bitmusic19

sin(880*2*3.14*t/44100)*128 * Math.exp(-2*t/44100)

Same as above, but with "fade effect" created by decreasing amplitude using exponential function. Sound fades in about 2 seconds.

http://bit.do/bitmusic19b

Same as above, but with inverse fade effect (volume increases) using "1-exp" function, so it will start as "0" and end as "1" as time increases: (...)*(1-Math.exp(-0.6*t/44100))

http://bit.do/bitmusic20

cos(2*3.14*220*(t/44100)+20*cos(2*3.14*11*(t/44100)))*128*Math.exp(-4*t/44100)

Water drop sound effect with echo. Change exp from -4 to -14 to increase fade effect and hear only one drop with no echo. Does not use bitwise operators, only plain cosine wave functions.

http://bit.do/bitmusic-whitenoise

White noise (random number generator between 0 and 1, times 256 that is amplitude):

Math.random(1)*256

http://bit.do/bitmusic21

Based on the equation from Viznut's Countercomplex blog.

Left: (t*1&t>>7|t*3&t>>7|t*2&t/1024)

Right: (t*2&t>>7|t*4&t>>7|t*3&t/1024)

http://bit.do/bitmusic21b

Improved version from above. Faster (>>6 instead of >>7) and more bass (t*0.5 instead of t*2).

Left: (t*3&t>>7|t*1&t>>6|(t*0.5)&t/1024)

Right: ((t*1)&t>>7|t*2&t>>6|t*3&t/1024)

http://bit.do/bitmusic22

Three tones, one after another.

( ((t*3)&t/256) | ((t*2)&t/512) | ((t*1)&t/1024) )

http://bit.do/bitmusic22b

One tone:

((t*1)&t/512)

http://bit.do/bitmusic23

Left: ((t/5.5125)*1000>>14) | (t/5.5125)*2000>>10

Right: ((t/5.5125)*500>>15) | (t/5.5125)*1000>>10

(Bass base, repetitive sound)

http://bit.do/bitmusic23b

((t)*100>>14) | (t)*800>>12

((t)*100>>15) | (t)*800>>11

http://bit.do/bitmusic24

Left: ((t/2) & (t)>>6 | (t*3&t>>7) |(t*2)&t/1024 ) * "8421"[t>>16&3]/2

Right: (t*(t/2) & (t)>>6 | (t*3&t>>7) |(t*2)&t/1024 ) * "8421"[t>>16&3]/2

http://bit.do/bitmusic25

Left: (((2*t/ ("5434"[(t/2)>>16&3]) )) & (t/2)>>6 | ((t/2)*2&(t/2)>>7) |((t/2)*2)&(t/2)/1024 ) * "8421"[(t/2)>>16&3]/ 2

Right: (((2*t/ ("5434"[(t/2)>>16&3]) )) & (t/2)>>6 | ((t/2)*2&(t/2)>>7) |((t/2)*2)&(t/2)/1024 ) * "8421"[(t/2)>>16&3]

http://bit.do/bitmusic-sierpinski

Sierpinski Triangle Music

t << (8%((t*1)&t/256))

http://bit.do/bitmusic-sierpinski2

Improved version of the Sierpinski Triangle music:

127%(t/16) >> (8 % ((t*1)&t/256))

http://bit.do/bitmusic-sierpinski3

Stereo version of Sierpinski music:

Left: 127%(t/16) >> (8 % ((t*2)&t/256))

Right: 127%(t/16) >> (8 % ((t*3)&t/256))

`http://bit.do/bitmusic9`

This one was created by "mu6k" (a demo scene hacker) and is one of the most interesting that I've seen. Great rhythm with drums beat, bass, harmonic tones. Let's try to decode part of it:

(3e3/(y=t&16383)&1)*35 + // first part

(x=t*"6689"[t>>16&3]/24&127)*y/4e4 + // second part

((t>>8^t>>10|t>>14|x)&63) // third part

The full equation creates some variables (x and y) that are used later. The three parts are added (with the "+" operator) so they are played together as a single composition.

**The Drum**

The first part is the beat (electronic drum):

(3000/(t&16383)&1)*35 (play)

Change the &16383 to other values to test other beat sounds, like this:

(3000/(t&21440)&1)*35 (play) or (3000/(t&2600)&1)*35 (play)

**Tonal melody**

The second part contains the melody (four notes are represented by the numbers 6,6,8,9) and can be isolated as a single equation that will play the four notes (44.1 Khz):

(t*"6689"[t>>13&3]/24&127) (play)

I changed from "t>>16" to "t>>13" because this seems to mean the note duration. It will play twice as fast when you subtract one number. So "t>>13" will play 2 times faster than "t>>14".

A more intuitive way to write would be:

(t*"6689"[(t>>13)%4]/24&127) // "%4" means 4 notes.

Changing it a bit (from "&127" to "&32"), it will sound more "pure", because it seems to be a filter:

(t*"6689"[t>>13&3]/24&32) (play)

Make a pulse effect to each note:

(t*"6689"[t>>15&3]/24&127) * (t&8191)/12000 (play)

And the third part of the equation uses this melody (encoded in the "x" variable) and adds some effects to it.

Mega Laser War:

http://bit.do/laser-war

Left: (600000/(t*t/280000&16383)&1)*8

Right: (600000/(t*t/380000&16383)&1)*8

Space Engine:

http://bit.do/spaceengine

Left: (70000/(t*t*t/540000000&16383)&1)*10

Right: (70000/(t*t*t/340000000&16383)&1)*10

Riff duo:

Left: t*(t>>12)&(1)*t

Right: t*(t>>12)&(2)*t

Riff octaves:

http://bit.do/riff-octaves

Left: t*(t>>12)&(1)*t

Right: t*(t>>12)&(2 * parseInt(t/(44100*2)) )*t

Fading bass:

http://bit.do/fading-bass

Left: (t&t%252)^t*0.5 - (t>>12)

Right: (t&t%254)^t*1 - (t>>11)

Blinking Bells:

http://bit.do/blinkingbells

Left: 63&t*(t>>12) * (1+( t>> 14) )

Right: 63&t*(t>>12) * (1+( t>> 13) )

I also wrote a PERL script to create sound waves that can be output to the audio speaker (/dev/audio): http://perl.postbit.com/playing-with-audio-in-perl.html

(Equations by Rodrigo Siqueira)

Summary of bitwise operators in Javascript:

Bitwise AND: a & b

Bitwise OR: a | b

Bitwise XOR: a ^ b

Bitwise NOT: ~ a (invert the bits)

Left shift: a << b

Right shift: a >> b

More information:

**Algorithmic symphonies from one line of code - how and why:**

http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html

Audio visualization and sounds of many of those formulas:

**Some deep analysis of one-line music programs:**

http://countercomplex.blogspot.com/2011/10/some-deep-analysis-of-one-line-music.html

**Forum to post and share formulas:**

http://pouet.net/topic.php?which=8357&com=25&which=8357&com=25&page=1

**Music formula collection:**

http://pelulamu.net/countercomplex/music_formula_collection.txt

**Hacker News (Ycombinator) discussion:**

http://news.ycombinator.com/item?id=3063359

The future:

Produce generative music using pentatonic scale, because pentatonic scale always sounds good and it is natural (see Bobby McFarrin hacks your brain with music TED video and listen to Minor Pentatonic Scale examples (from the "Color Your Major Chords Using Minor Pentatonic Scales").