Skip to content

Audio music and sound#30

Open
clintjeff2 wants to merge 2 commits into
Bitcoindefi:mainfrom
clintjeff2:audio-music-and-sound
Open

Audio music and sound#30
clintjeff2 wants to merge 2 commits into
Bitcoindefi:mainfrom
clintjeff2:audio-music-and-sound

Conversation

@clintjeff2

Copy link
Copy Markdown

I have implemented a comprehensive audio system for the 'Human vs Bots' game demo.

Key features include:

  • Audio Manager: A new audio.js file houses the AudioManager class, which manages the Web Audio API lifecycle, separate gain nodes for a mixer effect (Master, Music, SFX), and asset loading.
  • Game Integration: I mapped sound effects to essential game actions including unit selection (click), movement, attacks, unit production, and turn transitions.
  • Music Management: The system automatically switches between menu music and battle music when a match starts or ends.
  • Audio Controls UI: A new 'Audio Settings' section was added to the menu drawer, featuring independent volume sliders for Master, Music, and SFX, along with a Mute toggle.
  • Persistence: User volume preferences are saved in localStorage and restored upon reloading the game.
  • Policy Compliance: The audio context initializes only after the first user interaction (e.g., opening the menu or clicking the arena) to follow modern browser autoplay policies.

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

google-labs-jules Bot and others added 2 commits June 26, 2026 01:18
- 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
@clintjeff2

Copy link
Copy Markdown
Author

@leocagli , kindly review and merge.

@sonarqubecloud

Copy link
Copy Markdown

@leocagli

Copy link
Copy Markdown
Collaborator

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 leocagli left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. 🎵

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audio: música y efectos de sonido

2 participants