Sagar Web Games

Home › Devlog

Music without audio files: how our games compose their own soundtracks

2026-11-29 · Sagar Studio

Open almost any of our games and you will hear a soundtrack. None of it is a recording. Each tune is composed by a program on your own device, while the game boots, and then played in a loop. The sound effects are made the same way. (One game we imported from elsewhere, Coin Flip, brings its own recorded audio; every other game uses the engine described here.)

Why not just use audio files?

A recorded song is a download, and we have 200 games. We keep the first page of the site small and load each game only when you open it, so adding a few megabytes of music per game would work against that. The second reason is rights: the engine uses plain maths and synthesised sound, so there are no samples to license. The third is variety. Because a song is built from a recipe, every game can have its own tune instead of sharing a handful of stock loops.

From a few numbers to a song

A game describes its music with a handful of values: a tempo, a root note in hertz, four chord positions, a seed number and an energy level from 0 to 1. A small build step looks up the style that game belongs to, adds a number made from a hash of the game's id, and hands everything to the composer. The composer returns two audio buffers, each one loop long.

The song is normally 16 bars, split into an A section and a B section. If the tempo is slow enough that 16 bars would run past about 44 seconds, it is 8 bars instead. Every instrument is synthesised: oscillators shaped by an attack, decay, sustain and release envelope, with extra partials or vibrato for plucked, bowed and blown sounds. Drums are built from swept sine waves and filtered noise. Reverb and echo are computed in code too.

Styles are palettes, and melodies are grown

There are 36 styles, from chiptune, lo-fi, jazz, synthwave and drum and bass to orchestral, folk and space, plus Indian styles such as raga, carnatic, bhangra and sufi. A style is a recipe of allowed choices: a tempo range, which scales (modes or ragas) it may use, how much swing it has, which instruments can play the lead, the chords and the bass, and a few rhythm patterns written as 16-character strings, where an x marks a note and a dot marks a rest.

The game's seed and id then pick from that palette: the lead instrument, the chord rhythm, the bass pattern, the drum groove, the drum fill, the scale, the chord progression, the song form (which two-bar phrases repeat and where) and whether there is a breakdown bar. The function that works all this out is deterministic, so a game always sounds the same, but two games in the same style make different choices and sound clearly different.

The melody is built from two-bar motifs. Each step moves mostly by one or two scale notes, with an occasional leap, and a contour (an arch, a rise, a fall or a wave) nudges the line in a direction so it does not wander. Notes on strong beats land on chord tones, which is what makes a tune sound settled. Three motifs are made, plus a varied copy of the first, and the form decides the order. The very last note resolves to the home note so the loop can start again without sounding unfinished. In the Indian styles the lead can slide into a note or add a quick grace note, and scales can skip notes according to the raga's rules.

Two layers that follow the game

The first buffer, the calm layer, holds the harmony, bass, drone, melody and ambience, and plays all the time, including on the title screen. The second, the drive layer, holds drums, an arpeggio and a counter-melody. Its volume follows the game's intensity, which the game sets as the action heats up, so the music thickens during a fast moment and thins out again over a few seconds.

Both loops are mono buffers at 22,050 samples per second. Anything that rings past the end of the loop, such as an echo or reverb tail, is written back onto the start, so the seam between the last sample and the first has no click. Composing runs in a background Web Worker while the game boots, so the screen does not freeze, and falls back to the main thread where workers are not available. The effects bank is synthesised too, 26 short sounds such as clicks, lasers, coins and fanfares.

How a game gets its style

A file called the music map lists every game and its style, chosen by theme: games set in rain get the monsoon style, forts and tower defence get heroic, crypts get spooky, card tables get a casino feel. Our LiDAR wall games for events get high-energy styles. A game may also name its own style in its own settings, and that wins over the map. Haveli Escape, for example, is mapped to monsoon, and Table Tennis Darbar to racing.

What the checks verify

The music check runs with our tests. For every style, in both a major and a minor key, it renders the song and fails if any sample is not a number, if the peak goes above 0.98, if the average loudness falls outside a sensible range, if the jump at the loop seam is bigger than the song's own sample-to-sample movement, or if a loop is shorter than 12 or longer than 45 seconds.

It then checks the catalogue: every game must have a style, no style may hold more than 12 games, and two neighbouring games of the same genre may not share a style. To prove that games differ, it composes pairs of games from the same style and compares their spectrograms and their note-onset patterns, and fails if the pair correlates too closely. It also times composing, because a slow composer would delay the start of a game.

What the checks cannot do is tell you whether a tune is pleasant. They catch broken audio, not bad taste, and that part still needs a person listening. If one of our tunes grates on you, tell us which game it is.

About Haveli Escape