Make the game's music react to play using the music files the person has: stems of one piece in sync that fade in and out with an intensity value (calm to danger), switching between pieces on the next bar or beat so changes land in time, intros that lead into a loop, stingers on the next beat for big moments, and the current bar and beat for syncing gameplay to music. Includes a music module on...
Download skill (.zip) Files on GitHubAdaptive game music
Set up
import { createEngine } from "./engine.js";
import { createMusic } from "./music.js"; // assets/music.js (engine.js is included)
const music = createMusic(engine, { to: mixer.bus("music"), stingers: { levelup: "music/levelup" } });
music.define("forest", { bpm: 100, beatsPerBar: 4,
stems: { pads: "music/forest-pads", drums: "music/forest-drums", lead: "music/forest-lead" },
layers: { pads: 0, drums: 0.35, lead: 0.7 } }); // intensity where each stem comes in
music.define("boss", { bpm: 140, stems: { all: "music/boss" }, intro: "music/boss-intro" });
await music.preload();
music.play("forest");
During play
music.setIntensity(enemiesNear / 5); // layers fade in and out over 1.5 s
music.switchTo("boss", { on: "bar", stinger: "levelup" }); // waits for the next bar
music.stinger("levelup"); // on the next beat
const { bar, beat } = music.position(); // move enemies on the beat
The files
- Stems are parts of one piece exported separately at exactly the same length and tempo (pads, drums, melody). Ask the person or the composer for them, or use one full track per section.
- Loops must be cut exactly on a bar line. Know the bpm.
- MP3 adds a little silence at the start, which breaks seamless loops. Use OGG plus M4A, or set
loopStart/loopEnd. - Only use music the person made or has the rights to, and credit it (bash-game-audio-credits, audio-license-audit).
Design
Intensity from something the player feels: enemies near, health low, timer running out. Smooth it so it doesn’t flicker (change it at most every second or two). Switch pieces for big changes (boss, menu, victory); use layers for everything in between.
Pitfalls
Stems of different lengths (they drift), switching mid-bar, intensity that jumps every frame, MP3 loops with gaps, and music that never calms down.