📚 Encyclopedia

Audio & Music · updated 2026-11-30

Generating game music loops and a countdown you can hear

game-audioprocedural-musicnumpyogg-vorbisloopingmediaplayerandroidhaptics

A small game can ship original music with no audio tools and no licensing questions by synthesizing it in a script: a Python file with numpy writes each track as samples, ffmpeg encodes it, and both the script and the output are committed. These notes come from a phone arcade game with three selectable tracks and a power-up that lasts six seconds. One caveat applies to all of it: the author could not listen to the result, so everything here is about structure and wiring, and levels and taste still need a human ear.

A track as data

Describe each track as a dictionary (tempo, key, a four-chord progression, drum pattern, per-part levels) and render them all with one function. Three tracks that feel different came from changing only that data:

Feel Tempo Progression Drums
Driving 128 BPM, A minor Am F C G four-on-the-floor kick
Drifting 100 BPM, D minor Dm7 B♭ F C half-time, pad and lead up front
Urgent 140 BPM, E minor Em C D Bm rolling bass, open hats throughout

Sixteen bars each gives 27 to 38 seconds, long enough not to grate. Adding a track is one more dictionary plus one entry in the app's track list. Keep the mix controls (the per-part levels) in the data so a listener's "bass is too loud" is a one-number change.

Making the loop seamless

A loop clicks or gaps when the end does not flow into the start. Two rules remove that: - Fold the tails. A note or reverb tail that would ring past the end is added onto the beginning of the buffer instead of being cut. - Echo by rotation. Make a delay with np.roll on the whole buffer, so echoes from the last bar land in the first.

Choose a length that is an exact whole number of bars at the tempo, and loop with the platform's own looping flag rather than restarting on completion.

Music that counts down

A timed power-up first got a ticking sound that sped up near the end. The better idea was to let the music itself change and its tempo tell the time: - A separate clip is rendered exactly as long as the power-up. Its kick drum plays every 1 s, then 0.5 s, 0.25 s and 0.125 s, switching at the same moments the game's own warning steps do; chords climb; a riser fills the last 1.5 s; it stops dead at the end. - On pickup the looping track pauses and the clip restarts from zero. The game still emits its tick events, and on each one the player is re-synced to the game clock (seek only if it has drifted more than about 250 ms). - The clip's timing is duplicated in two places, the game constants and the generator. Say so in both, or changing the duration in one silently breaks the match. - Keep a short vibration on each tick. It carries the countdown with the sound off, and it survives music being disabled; the plain tick sound plays only when music is off.

Wiring it into the game

Checking playback without ears

On an emulator started without audio, the player state is still visible: adb shell dumpsys audio | grep MediaPlayer lists each player with state:started|paused|stopped. That proves the right player runs in each game state. It proves nothing about how it sounds (Driving the Android emulator headlessly for QA).

Gotchas

See also

Linked from

Sources: swerve memory (swerve-audio, swerve-testing, swerve-app-shell), swerve repo (tool/make_music.py), 2026-11-25