Craft·5 min read

How a Game Plays a Tune With No Music File

Happy Birthday, in our 60th birthday game, is twenty-five pairs of numbers. Not one byte of it is audio.

The cassette loading screen from the Tape Loader birthday game, with colour-barred borders and a climbing byte counter.

Our sixtieth birthday game plays Happy Birthday. It contains no music file, no recording, and no audio data of any kind.

The tune is twenty-five pairs of numbers. Each pair is a note and how long to hold it. That is the entire song, and it costs about the same as this paragraph.

A melody is a list

Written down, the first line of Happy Birthday is: same note, same note again but shorter, a note two semitones up, back down, five semitones up, four semitones up held long.

That is exactly how it is stored. A list of pairs, where the first number is how many semitones above the reference pitch the note sits and the second is its length in beats. Twenty-five pairs covers the whole song.

Nothing in that list is a sound. It is a description of a sound, in the same way sheet music is, and it takes up about as much space.

Turning a number into a pitch

There is one formula and it is the same formula behind every piano ever tuned.

Going up an octave doubles the frequency. An octave is twelve semitones. So each semitone multiplies the frequency by the twelfth root of two, roughly 1.0595.

A note is therefore the reference pitch multiplied by two raised to the power of semitones over twelve. Ours uses middle C at 261.63Hz as the reference, so semitone zero is middle C, semitone twelve is the C above, and semitone seven is the G in between.

One line of code. From it, every note in every tune the game will ever play.

This is called equal temperament and it is a compromise, in that most intervals are very slightly out of tune compared with the pure ratios they approximate. It has been the standard compromise for a couple of centuries because it lets you change key without retuning, and for a square wave coming out of a phone speaker nobody is going to file a complaint.

Making a note make a sound

Each note becomes an oscillator: a generator producing a repeating waveform at a set frequency. A square wave, in our case, which is the harsh buzzy sound of early home computers and exactly the reference the game is making.

An oscillator on its own is unbearable. It starts instantly at full volume, holds, and stops instantly, which sounds like a fire alarm and clicks at both ends. So it passes through a gain stage that shapes it: up to full volume over eight milliseconds, then decaying away for the rest of the note.

That shape is the envelope, and it is the difference between a musical note and a test tone. The detail of why is in how a game makes sound without any sound files, which covers the effect side of the same system.

The cassette loading screen from the Tape Loader birthday game, with colour-barred borders and a climbing byte counter.
The loading screen's sound is generated the same way. There is no recording of a tape anywhere in this file.

Why it loops cleanly

A recorded loop clicks unless the file is cut with real care. The reason is physical: a speaker cone is at some position at the end of the loop and some other position at the start, and asking it to jump between them instantly is a step change, which the ear hears as a click.

Generated notes cannot have this problem, because every note fades in from silence and decays back to near-silence. The end of the loop is quiet, the start of the loop is quiet, and there is nothing to step between.

It also loops for free in a second sense: there is no file to hold in memory, no decoder running, and no point at which the loop has to be scheduled against a buffer boundary. The next note is calculated when the previous one finishes.

What it costs, and what it cannot do

The whole music system in that build, formula and melody together, is a few hundred bytes. A recording of the same tune at any listenable quality is several hundred kilobytes. That ratio is the entire reason the game is 104KB, and the arithmetic is laid out in why one game is 104KB and another is 20MB.

What it cannot do is sound like anything other than a computer. A square wave is a square wave. There is no warmth in it, no performance, no human being. For a game about a home computer in 1982, that is not a limitation, it is the point, and a lush orchestral recording would actually be wrong.

For a wedding game where the music should be the couple's song, generated audio is the wrong answer and no amount of cleverness makes it the right one. That is a recording, it costs megabytes, and the megabytes are worth it.

The catch worth knowing about

Browsers will not let a page make noise until the person has interacted with it. This is a good rule that exists because of a decade of pages that shouted at people.

The practical consequence is that a game cannot start its music on load. It has to wait for a click or a key, which in practice means the audio system is created the first time a sound is actually asked for, and any sound requested before then is silently skipped.

Ours does exactly that, and also checks whether the audio system has been suspended and wakes it if so, because a browser will pause audio when a tab goes to the background and does not always resume it on return. Both are three lines and both are the difference between a game with sound and a game that is mysteriously silent for one person on one device.

What this means for a commission

A tune that means something is worth recording. A tune that signals something is worth generating.

Generated audio is nearly free in bytes and not free in time. Somebody still has to transcribe the melody and choose the waveform, and getting it to sound good rather than merely correct takes an afternoon.

And it is genre-specific. Anything quoting the eight-bit era should be generated, because a recording of a real instrument in that setting sounds like a mistake. Anything else, ask what the music is for before deciding.

The tune in question plays over the title screen of the build in the arcade.

◆ Questions

Common questions

How does a game play music without an audio file?+

It stores the melody as a list of notes and durations, then generates each note as a tone at the right frequency for the right length. The tune is data, a few hundred bytes, and the sound is produced at the moment it is needed.

How do you turn a note into a frequency?+

With one formula. Each semitone up multiplies the frequency by the twelfth root of two, so a note is a reference pitch multiplied by two to the power of semitones over twelve. Middle C at 261.63Hz is the reference ours uses.

Why does looping music click or pop?+

Because the waveform is cut at a point where it is not at zero, so the speaker is asked to jump instantly from one value to another. That step is heard as a click. Generated notes avoid it by fading each one in and out.

Is generated music as good as recorded music?+

Not for anything that wants to sound like real instruments. It is very good for anything that wants to sound like an old computer, and for a game that is quoting that era it is more authentic than a recording would be.

When would you record music for a commission instead?+

Whenever the music is meant to move somebody rather than to signal something. A first dance, a song with meaning to the couple, anything the recipient will recognise. Those are recordings, and the size cost is worth paying.

  • craft
  • audio
  • code
  • behind the scenes
◆ Keep reading

Related from the journal.

The Fighting Chess interface with its war log of moves beside the slain pieces panel.
Craft·5 min read

Getting a Game's Volume Right

Every sound in our games passes through a single gain set to about a third. Everything else is decided against that.

Read