Audio music and sound#30
Conversation
- Implement AudioManager using Web Audio API for low-latency playback. - Add royalty-free music and SFX assets (via URL for demo purposes). - Map SFX to game events: click, movement, attack, production, and end turn. - Implement music switching between menu and gameplay. - Add Audio Settings UI with volume sliders and mute toggle. - Persist volume settings in localStorage. - Comply with browser autoplay policies by initializing on user interaction. Co-authored-by: clintjeff2 <119521983+clintjeff2@users.noreply.github.com>
…1149493533 Audio system: music and sound effects implementation
|
@leocagli , kindly review and merge. |
|
|
Hi @clintjeff2 — this conflicts with main after merging #29 (the Zemeroth cleanup removed some files this PR touches). Please rebase on the latest main and resolve; the audio additions themselves look fine. 👍 |
leocagli
left a comment
There was a problem hiding this comment.
Good work overall @clintjeff2 — the AudioManager structure is clean, the Web Audio API approach is correct, and the volume persistence + SFX event hooks are exactly what the issue asked for. One critical bug needs fixing before merge:
🚨 Critical: JSON.parse without try/catch crashes the whole game
In audio.js constructor:
// Current — will throw SyntaxError if localStorage has corrupted/legacy data
this.volumes = saved ? JSON.parse(saved) : { master: 0.8, music: 0.5, sfx: 0.7 };If localStorage contains any corrupted or unexpected value (common on returning users after a code change), JSON.parse throws a SyntaxError. Because audioManager is exported at module level, the entire audio.js module fails to evaluate — and since game.js imports it, the whole game loads as a blank canvas with no interaction.
Fix (one line):
try {
this.volumes = saved ? JSON.parse(saved) : { master: 0.8, music: 0.5, sfx: 0.7 };
} catch {
this.volumes = { master: 0.8, music: 0.5, sfx: 0.7 };
}Minor: misplaced import
The import of audio.js is added after two const declarations in game.js. Move it to line 1 to keep imports at the top (avoids issues with bundlers and linters).
Fix those two things and this is ready to merge. 🎵



I have implemented a comprehensive audio system for the 'Human vs Bots' game demo.
Key features include:
audio.jsfile houses theAudioManagerclass, which manages the Web Audio API lifecycle, separate gain nodes for a mixer effect (Master, Music, SFX), and asset loading.localStorageand restored upon reloading the game.I verified the implementation using a Playwright script that confirmed the triggering of music and SFX events and captured a screenshot of the new UI.
Closes #25