the guide™
bytebeat started in september 2011, when a programmer viznut posted a video called "experimental music from very short c programs" and wrote about it in a blog post, algorithmic symphonies from one line of code. the idea was that a single line of code, run 8000 times a second, could make actual music. people online started sharing their own one-liners, and that's how bytebeat became a thing.
this guide will explain how bytebeat works one concept at a time, and walk you through making a short song with drums, a melody and chords! this guide should only take about an hour. if you don't understand anything, try searching it up, or ask someone in the Hack Club Slack for help.
you'll be able to play and edit every example in the guide! these orange boxes are small snippets of code; make sure you're continuing to make your own version with the editor as you go along the guide.
are you doing this on your own or with your club?
here's what your song might sound like:
01 sound
to play sounds, a speaker moves back and forth, and digital audio tells it how to move through list of numbers called samples.
in bytebeat, you'll write a formula that makes those samples. the computer counts a number t that starts at 0 and goes up by one 8,000 times a second. each
time your formula runs, that answer the computer gets is what the speaker will play. the simplest formula is just t:
however, that doesn't sound that good...
computers store numbers as bits, which are just 0s and 1s. 8 bits together make a byte, and a byte can only hold the numbers 0 to 255. bytebeat keeps just one byte of your answer (hence the name), so anything past 255 wraps back around to 0, like a clock going from 12 back to 1.
so as t counts up, the output climbs from 0 to 255, drops back to 0 and climbs
again:
this type of shape is called a sawtooth wave, because it looks like the teeth of a saw. you just heard it as a low buzz.
one climb takes 256 samples, so it happens 8000 / 256 = 31.25 times a second. this means that the frequency of the note, measured in hertz (hz), is 31.25.
go to the editor and see how t sounds.
02 pitch
pitch is how high or low a sound is. the faster the speaker moves back and forth, the higher it sounds.
to raise the pitch, make the wave repeat faster. t * 2 climbs twice as fast as t, so it wraps around twice as often and wil play a higher note:
8000 samples a second × 2 ÷ 256 = 62.5 hz ≈ the note B1
as the frequency is 31.25 hz times the number, every time you double the number, the note will go up one octave. try changing the 2 to a 4, then 8, then 16 while it plays.
values in between will accordingly give notes that are higgher. t * 3 plays a fifth above t * 2.
the sawtooth is one example waveform. the short sequence & 128 keeps a single bit of the result, so the
output jumps between 0 and 128 with nothing in between. this is what's called a square wave, when a note
is kept with only hollower sound, like an old game console.
your code will run left to right, so * runs before &, and t * 4 & 128 means (t * 4) & 128. & 64 which will make a smaller jump, so it's quieter.
you'll need that later, when several sounds play at once.
change your project to a quiet square wave. 6 × 31.25 = 187.5 hz, which will be the lowest note of your melody.
03 rhythm
rhythm needs something that changes much slower than the sound. >> wil shift
the bits of a number to the right, which divides by a power of two and drops the
remainder. t >> 11 is t divided by 2048, so it goes up by one
every 2048 samples(which is about a quarter of a second). that's what's called a beat counter.
& 1 keeps the lowest bit, so t >> 11 & 1 goes 0, 1, 0,
1, one step per beat. multiply a sound by it and the sound turns off and on:
for a pattern, write the beats as a binary number. each 1 is a hit and each 0 is a rest,
read from right to left. t >> 11 & 7 counts 0 to 7, and shifting the
pattern by that much picks out one bit:
you can write more than one line. the lines above the last one set variables, and the last line is the sound.
the last line starts with hit and not ( on purpose. javascript
joins a line that starts with ( or [ onto the line above it, so (t * 8 & 128) * hit would break. if you need a bracket first, end the line
above with ;.
since drums don't always need to be on, they'll hit once and fade out. ~t flips every bit of t, so ~t >> 4 & 127 counts down from 127 to 0, once
per beat. multiply by it and each beat starts loud and fades:
the >> 7 at the end divides by 128 to bring the result back under 256.
a kick drum is the same idea with a very low note, fading over half a second. for a hi-hat,
use noise: random() gives a different number every sample, which sounds like
hiss. adding two sounds together plays both. replace your project with this:
04 melody
a melody is a list of notes that changes over time. put the numbers in an array and use a counter to pick one:
t >> 12 & 3 goes 0, 1, 2, 3 and repeats, moving on every half second. [4, 5, 6, 8][...] picks the number at that position.
4, 5, 6 and 8 sound good together, but that's only four notes. for more, use a scale. these numbers are the major scale (do re mi fa so la ti do) as whole-number ratios. do is 24 and the do an octave up is 48:
this plays the scale going up. >> 2 divides by 4 so the notes aren't too
high.
now write a tune as positions in the scale, where 0 is the first note and 7 is the last.
add it to your project, and change the numbers in tune to make it yours:
05 chords
a chord is a few notes played at once. you already know how: add them. keep each one small so the total stays under 256. if it goes over, it wraps around and crackles.
4, 5 and 6 make a major chord. try 10, 12 and 15 for a minor one, which sounds sadder.
songs move between chords. this picks a root note every two seconds and builds a chord on it. the three parts are root ÷ 8, root × 5 ÷ 32 and root × 3 ÷ 16. that's the same 4 : 5 : 6 shape, an octave under the melody:
the roots are F♯, B, F♯ and C♯. add the chords to your project:
the parts add up to at most 64 + 93 + 63 + 15 = 235, which stays under 256.
06 combine
you have all the parts. now arrange them so the song changes over time. a comparison gives 0
when it's false and 1 when it's true. t >> 16 > 0 is false for the
first 8 seconds and true after that, so multiplying the lead by it brings the melody in at
8 seconds:
that's the whole song. some ways to make it your own:
- change
tuneandrootto write your own parts. - use a binary pattern from step 3 so the hat only plays on some beats.
- use
t >> 17 & 1to switch between two tunes every 16 seconds. - change
>> 2in the lead to>> 1to move it up an octave.
08 submit
finished your song and exported it? you're ready to submit!