Learn music by changing code: a free course
Five short lessons, and no account needed. Each lesson shows a short track, then the same track with one thing changed. Press Listen on both and hear what the change does. You learn to read the code by hearing it.
The track you start with
Every lesson starts from this track. It has three parts, one per line: drums, bass and keys. The last line plays them together.
setcpm(25) // 100 BPM
const drums = s("bd ~ sd ~, [~ hh]*2") .gain(.5)
const bass = note("<c2 a1 f1 g1>") .s("sawtooth") .lpf(600) .gain(.4)
const keys = note("<[c3,e3,g3] [a2,c3,e3] [f2,a2,c3] [g2,b2,d3]>") .s("piano") .gain(.4)
stack(drums, bass, keys)Lesson 1: faster or slower
The first line sets the tempo. setcpm(25) means 25 bars a minute; with four beats a bar, that is 100 beats a minute. Change 25 to 32:
setcpm(32) // 128 BPM: the only change
const drums = s("bd ~ sd ~, [~ hh]*2") .gain(.5)
const bass = note("<c2 a1 f1 g1>") .s("sawtooth") .lpf(600) .gain(.4)
const keys = note("<[c3,e3,g3] [a2,c3,e3] [f2,a2,c3] [g2,b2,d3]>") .s("piano") .gain(.4)
stack(drums, bass, keys)Lesson 2: busier drums
The text in quotes is the rhythm. Each word is a step in one bar. [~ hh]*2 plays a hi-hat twice a bar; hh*8 plays it eight times:
setcpm(25) // 100 BPM
// the hi-hats: eight a bar instead of two
const drums = s("bd ~ sd ~, hh*8") .gain(.5)
const bass = note("<c2 a1 f1 g1>") .s("sawtooth") .lpf(600) .gain(.4)
const keys = note("<[c3,e3,g3] [a2,c3,e3] [f2,a2,c3] [g2,b2,d3]>") .s("piano") .gain(.4)
stack(drums, bass, keys)Lesson 3: new notes
The bass plays one note a bar: C, A, F, G. Put two notes in brackets and the bar splits between them. Here each note jumps up an octave (c2 to c3) and back:
setcpm(25) // 100 BPM
// the bass: each note jumps up an octave and back
const drums = s("bd ~ sd ~, [~ hh]*2") .gain(.5)
const bass = note("<[c2 c3]*2 [a1 a2]*2 [f1 f2]*2 [g1 g2]*2>") .s("sawtooth") .lpf(600) .gain(.4)
const keys = note("<[c3,e3,g3] [a2,c3,e3] [f2,a2,c3] [g2,b2,d3]>") .s("piano") .gain(.4)
stack(drums, bass, keys)Lesson 4: another instrument
.s("piano") chooses who plays the chords. Write another name, and the same chords move to it:
setcpm(25) // 100 BPM
// the chords: on a marimba instead of a piano
const drums = s("bd ~ sd ~, [~ hh]*2") .gain(.5)
const bass = note("<c2 a1 f1 g1>") .s("sawtooth") .lpf(600) .gain(.4)
const keys = note("<[c3,e3,g3] [a2,c3,e3] [f2,a2,c3] [g2,b2,d3]>") .s("marimba") .gain(.4)
stack(drums, bass, keys)Lesson 5: darker or brighter
lpf is a filter: it cuts the high sounds above its number. A low number sounds dark and muffled; a high one sounds bright. Change 600 to 2500:
setcpm(25) // 100 BPM
// the bass filter: 600 became 2500, so it sounds brighter
const drums = s("bd ~ sd ~, [~ hh]*2") .gain(.5)
const bass = note("<c2 a1 f1 g1>") .s("sawtooth") .lpf(2500) .gain(.4)
const keys = note("<[c3,e3,g3] [a2,c3,e3] [f2,a2,c3] [g2,b2,d3]>") .s("piano") .gain(.4)
stack(drums, bass, keys)Now ask the chat for a change
You changed a tempo, a rhythm, notes, a sound and a filter. That is most of what a change is. In Shawarma you do not have to write it: type the change in words, like "faster, with busier hi-hats", and the chat changes those lines while the track plays.
Make a track and ask for a change. Your first track is free, with no sign-up. When you want to go further, the tutorial and the drum patterns guide take the code further.
Try it in Shawarma
Shawarma is a music editor where every track is code. Your track keeps playing; type a change in the chat and hear it at once. Undo brings the earlier version back. Or edit the code yourself, as in this guide. The editor is free, with 5 free changes in the chat every week; Pro starts with 7 days free.