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
- A thin wrapper around the platform media player with
set(wanted, volume)called every frame is easier to get right than start and stop calls scattered through game states: the loop states what it wants (0.6 while playing, 0.4 in menus, 0.25 after a crash, silent when paused, in the background or during the power-up clip) and the wrapper reconciles. - Short effects (pickup, crash, level up) are WAV files from a second, standard-library-only script, played as one-shot samples (Generating app assets with Python scripts (logos, GIFs, sounds)).
- Put track choice, effects on/off and vibration on/off in one settings screen. Volume keys should control the media stream while the game is in front.
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
- Vorbis encoding is not byte-reproducible. Rerunning the generator changes every
.oggeven though the audio is identical, so a commit would show every track modified. Restore the files that were not meant to change. - The generator needs numpy; a project virtual environment may not have it while the system Python does. Note which interpreter to use in the script's header.
- An adaptive score that crossfades between moods is a different technique; see Adaptive game audio (mood crossfades and procedural sound).
See also
- Adaptive game audio (mood crossfades and procedural sound)
- Procedural music and sound effects with the Web Audio API
- Arcade game design toolkit (loops, enemies, scoring, structure)