Plucked strings and struck bells
A game score in Web Audio
Pikefall's music is not a recording. It is written as patterns and played by instruments synthesised in the browser, and it follows the battle: quieter while you build, fuller when a wave arrives, a layer of tension as the enemy gets close, and almost silence when you are down to your last two lives.
Don't fight the medium
A fantasy game suggests an orchestra, and a synthesised string section is the one sound everybody has heard done properly ten thousand times. It cannot survive being done approximately. So the score is a consort instead: a plucked string, a struck bell, a drone and a frame drum. Each of those is something synthesis is genuinely good at, and none of them is an imitation of something better. It also suits the game, which is a march across a map under coats of arms, not an epic.
Three instruments
The string is Karplus-Strong: a burst of noise fed through a short delay line with a gentle filter in the loop, which rings like a plucked string at the pitch the delay sets. The catch is that a delay of a whole number of samples tunes to the nearest whole sample, and the filter adds a fraction of its own. Measured, our first version was 2 to 9 cents flat, by a different amount in every register, so each register was a slightly different instrument beating against the drone. Interpolating the delay and subtracting the filter's own delay puts every note within a cent.
The bell is FM synthesis with inharmonic partials, which is what says "struck metal". Its modulation fades faster than its volume, so the strike is bright and the tail is nearly pure.
The drum is a membrane rather than a kick: a few inharmonic modes under an eight-millisecond slap of noise, with a small bend in pitch. A large bend smeared the modes into a tom.
None of these is built note by note. In Web Audio the expensive thing is creating nodes, not the arithmetic, so each instrument is rendered once into a buffer at a few pitches, and a note is one buffer playback and one gain. That is cheaper than the plain oscillator it replaced.
Music that never has to end
Ordinary harmony is built from cadences: it sets up an arrival and then arrives. A battle has no idea when it will end, so every cadence a loop writes is a promise it cannot keep. The score is modal instead, cycling through four chords with no dominant, so it can turn over forever without owing a resolution. Each region of the campaign plays the same score in a different mode, which is a bigger change to the ear than it sounds on paper.
Two small numbers stop it sounding like a sequencer: an accent on the strong beats, and up to seven milliseconds of timing scatter. Both come from a hash of the bar and the beat rather than a random generator, so the same battle performs the same way every time.
A peak is not a loudness
The mix looked balanced on paper, with the drone and the plucked tune at the same level. Measured, the sustained layers were nearly six times louder than the tune at the busiest moment. A drone sounds for the whole bar at full level; a plucked note is a spike that decays. Equal peaks are nowhere near equal loudness. The tune is now the reference and the bed sits under it.
When the measuring tool is wrong
To hear the score without playing, we wrote a renderer that runs the game's real audio code through a small offline implementation of Web Audio and writes a WAV file. Its first renders sounded like noise, and the cause was the renderer, not the music:
- A one-sample click. The clock advanced by adding 0.025 repeatedly, so "now" landed a hair after the sample it meant, and a note's envelope started one sample late with its volume still at full.
- A 40 Hz comb. A volume glide restarted from the wrong value forty times a second, on every layer.
- A reverb 690 times too loud, from dividing the impulse's energy by the sample rate instead of its length.
The lesson cost two batches of bad renders: when the measurement is the thing you listen to, check it against a control first. A pure sine, a chord and white noise, measured through the tool and through the browser's own analyser, are what settled it.
Play a battle with sound on to hear it.