Series
Create a Live Coding Music App
Build an app in which code makes music: a clock, drums and synths from the Web Audio API alone, a small language for rhythm with its own parser, and an editor whose code changes the song while it plays.
Eighteen parts that end in loopcode, a music app for the browser in which a song is a few lines of code, and changing the code changes the music without a gap. You start with a single beep and a clock that keeps time, build a drum machine and synths out of nothing but oscillators and noise, and write a small language for rhythm, with a tokenizer, a parser and patterns that are functions of time. Then comes the editor: its code builds the song, mistakes are caught and shown at their line, and effects, LFOs and your own samples follow. At the end the notes light up in the code as they play, the editor suggests what to type, and a link carries a song to someone else. No audio library and no framework: TypeScript, Vite, the Web Audio API and CodeMirror. loopcode is a small relative of wicktone, which adds a canvas of nodes to the same idea. Every part ends with the app as it stands, playable right on the page.
Quest line
- A First SoundStart loopcode, a live coding music app for the browser: set up a Vite project with TypeScript, get to know the Web Audio API as a graph of nodes, and play a first note with an oscillator and an envelope that keeps it from clicking.25 min
- A Clock That Keeps TimeJavaScript's timers are too sloppy for music. Build a clock that counts in bars, schedules notes ahead of time on the exact clock of the audio context, and keeps its place when the tempo changes. A metronome lets you hear it.25 min
- Drums From Oscillators and NoiseBuild a drum machine without a single recording: a kick from a sine with a falling pitch, a snare and a clap from filtered noise, hi-hats from six square waves, and an open hi-hat that a closed one cuts off. Then play a beat written as sixteen steps of x and dots.30 min
- Patterns Are Functions of TimeReplace the beat of x and dots with patterns: functions that are asked for a stretch of time and answer with the events that begin in it. Six small functions combine them into beats, and a beat can be swapped while it plays without losing the time.35 min
- A Small Language for RhythmWrite beats as text: bd [~ bd] sd ~ instead of nested function calls. A tokenizer, a parser and a compiler turn the text into the patterns of part 4, a mistake is marked at its place under the input, and a broken pattern leaves the last good one playing.40 min
- Speed, Length and ChanceSeven additions to the mini-notation: * and / for speed, @ and _ for length, ! for repeats, ? for chance and (3,8) for euclidean rhythms. Steps get a weight, random numbers become a function of time, and an algorithm spreads hits evenly over a bar.40 min
- Choices, Polymeter and Changing SpeedsFinish the mini-notation: a choice between several sequences that is made anew in every cycle, layers of different length that drift against each other, and speeds that change from cycle to cycle. The part ends with a reference of every symbol.20 min
- A Synth for Bass and LeadBuild a mono synth for bass lines and leads: an oscillator, a low-pass filter and a gain that run the whole time, and notes that only move their settings. Note names become MIDI numbers, and two tags in the notation add accents and slides.30 min
- Code That Makes MusicThe inputs make way for an editor. Its code describes a song through a small API that is built from a schema, running the code gives a patch of plain data, and an engine turns the patch into units that play. CodeMirror and two keys complete the page.45 min
- Change It While It PlaysTurn the app into a live coding app: the engine compares every run of the code with what is playing, keeps the units that are still there, creates the new ones and fades out the rest. The music changes without a gap.20 min
- When the Code Is WrongCatch every mistake the code can make: options are checked against the schema with a "did you mean", an error is marked at its line in the editor, and a worker with a timeout ends code that never ends. The music plays on through all of it.40 min
- Chords and PadsBuild a poly synth that plays several notes at once: every note gets two detuned oscillators and an envelope of its own with attack, decay, sustain and release. Chords are written in the notation, and the new kind of unit takes a class, a schema entry and one line in the engine.30 min
- Filter, Delay, Reverb and DistortionPut effects between the instruments and the speakers: a filter, a delay whose echoes follow the tempo, a reverb made of decaying noise and a distortion that bends the wave. A limiter in front of the speakers catches what gets too loud.35 min
- Movement With LFOsLet slow waves turn the knobs: an LFO moves the cutoff, the pitch or the volume of another unit. It is a cable into an audio parameter, and its wave is worked out from the cycle, so it stays locked to the bars.30 min
- Your Own SoundsAdd a sampler that plays recordings at the pitch of every note. Audio files are decoded, kept in IndexedDB and listed in a bar above the code, a bell is rendered with an OfflineAudioContext, and files can be dropped on the page.40 min
- See What You HearMake the music visible: every step of a pattern lights up in the editor at the moment its note is heard, and a small display shows the wave and the spectrum of the sound. On the way you meet the decorations of CodeMirror and the AnalyserNode.35 min
- An Editor That HelpsTeach the editor the words of the app: suggestions for functions, options, values and drum names while you type, and an explanation for the word under the mouse. All of it comes from the schema that already builds the API, and the syntax tree says where the cursor is.30 min
- Save and ShareKeep the song across reloads with localStorage, and pack it into a link that needs no server. Code that arrives with a link is shown, but not run until you press Play. Then the series ends: what you built in 18 parts, and where to take it from here.30 min