Loading articleโฆ
Loading 4weirdโฆ
Loading 4weirdโฆ
Loading articleโฆ
DocsGames
Docs ยท music ยท games
The in-game runtime is a single dependency-free script: include /games/html/fourweird-music.js, call window.FourWeirdMusic, and your game plays $music:1 songs and SFX through lazy WebAudio โ fail-open when audio is missing, silent when a payload is invalid.
The runtime
Reference the runtime by its absolute /games/html/ path โ never a relative copy โ and it guards itself idempotently on window.FourWeirdMusic. The AudioContext is created lazily on the first play call and resumed if suspended, so autoplay policies stay happy.
game.html โ music wiring
/games/html/Walkthrough
Wire once, then drive everything through the M handle: songs for loops, baked SFX kind names for one-liners, volumes and mute for the options screen. Invalid payloads fail silently to false โ your game never crashes on a bad note.
<script src="/games/html/fourweird-music.js"> with the absolute path, then grab window.FourWeirdMusic. Including it twice is safe โ the second load returns early.playSong with a song object, a $music:1 wrapper, or a seed URL (fetched fail-open). Stop it with stopSong (alias: stop).playSfx with a baked kind name (raygun, death, coin, jump, win, lose, click, alarmโฆ), an SFX object, or JSON text via playSfxJson.setVolumes({ master, music, sfx }) and honor the player's choice with setMuted(true) on the options screen.Seed format
Game soundtracks live on as small JSON seeds: *.song.json loops transcribed from each game's signature sound (menu, boss, death, coin) and *.sfx.json one-shot recipes. The embed runtime enforces its own tight budgets โ songs โค 8KB, sfx โค 1KB, 8 tracks and 512 notes max โ so every seed stays playable anywhere.
Worked example
Wire a menu loop that starts on first input and a coin blip that fires on pickup. Both calls are fail open, so a missing file never breaks the game.
game.html โ loop plus pickup
copy patternTroubleshooting
Music bugs in games are almost always wiring, policy, or payload shape. Check in this order and most sessions recover in a minute.
/games/html/fourweird-music.js and that window.FourWeirdMusic exists before your first call. A relative copy or a doubled bundle path is the most common silence.playSong to a start button or any tap, and create the AudioContext lazily inside that handler. Resume it if the browser reports suspended.setVolumes using separate music and sfx levels, then honor the options screen mute flag on every scene change. Persist the mute choice so returning players keep their setting.