Making music in the browser with no sound files
How Cleffy Bird makes every note and sound effect from Web Audio oscillators.
Cleffy Bird is a little game that teaches children (my girls) to read notes on a staff. Cleffy flies toward a cloud and has to name the note correctly to fly through the gap in the clouds. Answer wrong and it bonks into the cloud. Sound matters a lot here. The user should hear the note they have read, and a right answer should feel like a reward.
Every sound in the game is made by code while it runs. There are no audio files and no audio library, just about 230 lines on top of the browser’s Web Audio API. Here’s how it works, and the iPhone quirk that took the most effort to get right.
Why not just use sound files?
- Nothing to download. No MP3s to fetch or wait for... Sound is ready the moment the game is.
- Any pitch for free. The game needs every note across two clefs. Instead of recording each one, I just calculate it.
- Exact timing. Web Audio schedules sounds on its own clock, so a tune keeps its rhythm even when the game is busy drawing.
- Cheap to play with. When I added a voice for the bonus levels, it was a few lines of code.
Science: From note name to pitch
Western tuning has a neat formula. Take A above middle C as 440 Hz. Every semitone up multiplies the frequency by the twelfth root of two. (docs) So every note becomes a number:
function frequency(letter, octave) {
const midi = 12 * (octave + 1) + SEMITONES[letter];
return 440 * Math.pow(2, (midi - 69) / 12);
}Every note in the game carries its frequency, so when the bird lands on a G, the game just asks the synth to play that number.
One building block: a tone
Every sound in the game, from a piano note to the bonk, is made from one small function. It creates an oscillator, which makes a steady wave at some pitch. Then it runs that through a gain node, which controls how loud the wave is over time:
osc.frequency.setValueAtTime(freq, start);
if (endFreq) osc.frequency.exponentialRampToValueAtTime(endFreq, start + dur);
gain.gain.setValueAtTime(0.0001, start);
gain.gain.exponentialRampToValueAtTime(volume, start + attack); // quick fade in
gain.gain.exponentialRampToValueAtTime(0.0001, start + dur); // slow fade out
osc.start(start);
osc.stop(start + dur + 0.05);A few things I learned building this:
- Fade in, even a tiny bit. An oscillator that starts at full volume makes an audible click. A fade of 5–10 milliseconds removes it.
- Exponential fades sound natural. Our ears hear loudness on a log scale, so an exponential fade sounds like a real note dying away. The catch: an exponential ramp can’t reach zero, so it fades to
0.0001. - Oscillators are single-use. Each sound makes new nodes, plays them once, stops them on schedule, and lets the browser clean up. There’s no cleanup code to write.
- All sounds go through one shared volume control. A single master gain at 0.6 sits in front of the speakers, so stacked sounds don’t distort.
Making it sound like an instrument
A single wave sounds like a test tone. Two waves start to sound like an instrument. The “piano” voice is a triangle wave at the note’s pitch, plus a much quieter sine wave an octave up that dies away in under half the time. That fading top note is what makes it sound struck rather than like a beep.
The “chip” voice for the bonus levels uses the same idea with square waves, cut shorter. Square waves are much louder than triangles at the same setting, so the chip voice plays at about a third of the volume.
Sound effects are just pitch sweeps
Once you have a tone that can slide in pitch, sound effects come almost for free. A rising slide sounds happy. A falling one sounds like a bump.
- Flap: a sine wave sliding up from 420 to 820 Hz.
- Crash: a sine wave dropping from 190 to 55 Hz, with a low square-wave thud underneath.
- Coin: two quick, high square blips, borrowed from every platformer ever.
- Combo and fanfare: quick rising runs of notes. The fanfare is C–E–G–C with the last note held.
Playing a tune in rhythm
Some levels are real tunes, and at the end you can play the whole thing back. My first instinct was a setTimeout per note. The problem is that timers run on the main thread, which is also drawing the game, so the rhythm wobbles. Web Audio has a better option: you can schedule every note up front against the audio clock.
let t = ctx.currentTime + 0.05;
for (const note of notes) {
if (note.freq !== null) playVoice(note.freq, t, ring(note.seconds));
t += note.seconds; // rests just move the clock forward
}
return t - ctx.currentTime + 0.3; // how long it lasts, for the UIThe game only uses a timer to know when to switch off the “playing” state on the button, and that timer has no effect on the sound. Each note also rings about 15% longer than its written length. That way phrases join up smoothly instead of sounding choppy.