I made a game! It's called Riddim Riot, and you can play it in your browser right now, on a laptop or on your phone.
It's a top-down, twin-stick rhythm shooter. Every gun is a dubstep bass synth, and every shot lands on the beat. So the way you fight writes the track.
It started as a design brief with one question: is firing bass on the beat fun? I thought it would be, but I had no idea how much of the work would be about time. Not game time, music time. Getting one wobble gun to fire on the beat was the easy part. Keeping everything else on that beat, on every device, is what the rest of this post is about.
One clock for everything
A game usually runs on its frame loop: every frame, move things a little and draw them. A song runs on a clock that counts bars and beats. If those two keep their own time, they drift apart, and a rhythm game falls apart with them.
So Riddim Riot has one clock, the Tone.js Transport. It sends a tick every sixteenth note, carrying its place in the bar and the exact time that step will play. The drums schedule their hits from it. A gun queues your press and fires on the next slot of its rhythm. Enemies hop one tile on the beat, and waves spawn on bar lines. Nothing in the game uses a timer or counts frames for anything musical.
That fixes the drift. But it left the visuals early.
Drawing at the time you hear
Audio in the browser is scheduled ahead. The time the audio thread is working on is not the time coming out of your speakers: there is the output latency between them. In Chrome on a Mac's built-in output, I measured 22 milliseconds. On Bluetooth headphones, it can be a few hundred.
If the flash on the kick follows the audio clock, it fires before you hear the kick. So the game reads the clock in two ways. New sounds are scheduled from the audio clock. Everything you see is drawn at the time you hear, worked out from getOutputTimestamp():
const projected = timestamp.contextTime + (wallNow - timestamp.performanceTime) / 1000; return Math.min(projected, audioNow); The timestamp says which audio time was playing at which moment on the page's own clock. Add the time that has passed since, and you know what is playing now. It is never later than the audio clock, and if the browser has no timestamp yet, the game falls back to the audio clock.
One catch: Tone runs on a wrapper around the AudioContext, and the wrapper has no getOutputTimestamp(). So the game reads it from the native context underneath.
I measured it over five minutes of play. 431 kicks landed exactly on the grid, the drift was 0.012 ms a minute, and every kick lit up within a frame of when it was heard. The on-beat bonus is judged on the heard time too, so a press that sounds right counts as right.
A gun is a synth
The weapons are my favourite part. They're rolled from parts, a bit like Borderlands, but every part sets a gameplay stat and a sound together. The manufacturer sets the damage and the waveform. The oscillator is the kind of gun, like a wobble or a beam. The filter cutoff is the range, and the resonance tightens the spread.
The LFO rate is the fun one, because it is also the fire rhythm. A 1/16 gun fires every sixteenth note and wobbles at that rate. A 1/4 gun fires on every beat. Slower guns hit harder, so every rate does about the same damage in a bar, and the slowest ones burst on a hit:
export const LFO_RATES = { '1/2': { division: '1/2', damage: 3.4, size: 1.8, blast: { radius: 150, share: 0.8, knockback: 64 } }, '1/4': { division: '1/4', damage: 1.8, size: 1.35, blast: { radius: 96, share: 0.7, knockback: 50 } }, '1/8': { division: '1/8', damage: 1, size: 1, blast: null }, '1/16': { division: '1/16', damage: 0.55, size: 1, blast: null }, } A magazine is always one bar, and the reload is a fill: a click on the offbeat, then a rumble on the next beat. Your loadout of three guns is an instrument rack, and switching between them is how you play it.
Rolling sounds from parts had one problem I didn't expect. The parts change the loudness in ways that are hard to predict. Drive and echo, for example, push it up differently per engine, and my first rolls were 17 dB apart. Some guns were whispers, others were walls. Instead of a formula, the game now measures. When you pick up a gun, it renders one bar of held fire offline, in about 35 ms, and sets the gain that brings it to -20 dB RMS. Every gun now sits at the same level, whatever the parts.
The loadout menu shows each gun as a row of a step sequencer, lit where it fires. You can listen to new loot over the current bar of the track before you swap it in.
Then I tried it on a phone
The audio crackled, and it got worse the longer you played. On a phone, it couldn't keep up at all.
The first cause was Tone's instruments. They create new audio nodes for every note, and those were leaking thousands of nodes a minute. So I built every voice once, from sources that run all the time, and opened and closed them with envelopes. Playing a level now creates zero new nodes.
That stopped the crackle over time. The phones needed more: the problem there was how many nodes there were at all. Tone's classes are convenient, but each one is built from many browser nodes under the hood. A level ran about 1,350 of them. I rebuilt the voices from the browser's own nodes, one browser node for each node in the design, and copied over the curves and waveshapes Tone uses so they would sound the same. A level now runs about 260.
To check that "sounds the same" was true, I rendered every voice offline in both versions and compared them. The weapons match to within 0.01 dB. And rendering 20 seconds of a whole level offline went from 3.5 seconds to 0.9 in Chromium.
A level is a song
Each level is an arena and a song, both generated from a seed. You step into the circle in the middle and it fills over a bar. When it is full, the next bar line slams: the drums come in and the song starts.
The song has a fixed shape: an intro, a breakdown, a build-up, two drops, a bridge, a third drop and an outro. The waves follow it: sparse in the intro, bigger and faster in the drops. The third drop opens with the boss. Once the boss is down and the outro plays, the circle opens again as the exit.
The seed also picks the key and a chord progression from a set of dark loops. A pad and a few backing layers play over those chords, and the riffs of your guns move onto each chord too, so whatever you fire stays in key.
The Firefox surprise
One last one. To let you listen to loot, the menu pauses the game by suspending its AudioContext and plays the preview on its own. The obvious way to do that is a second AudioContext. In Firefox on desktop, that second context runs, its clock moves, and you hear nothing: a suspended context on the page keeps every other context silent.
Media elements still play in that state. So the preview loop and every weapon preview are now rendered offline into WAV files and played through <audio> elements.
Play it
There's a leaderboard, see if you can set some highscore! Turn your sound on.
- Play: https://riddimriot.com




