diff --git a/web-client/docs/viewer-publishing.md b/web-client/docs/viewer-publishing.md index 88926ab0..428f1810 100644 --- a/web-client/docs/viewer-publishing.md +++ b/web-client/docs/viewer-publishing.md @@ -349,3 +349,5 @@ Viewer 0.1.114 / engine 0.4.83 corrects E17 (A Splendid Year) to grant its state Viewer 0.1.115 / engine 0.4.84 separates the natural influence die roll from advance bonuses in journal messages, for example “rolls a 2 (+1 bonus = 3)”, including success and failure outcomes. Viewer 0.1.116 / engine 0.4.85 shortens influence roll bonuses to “rolls a 2 (+1)”. + +Viewer 0.1.117 makes hover more audible, sustains action sounds longer, and adds battle-roll, unit-loss and ship-sinking cues. Action sounds wait for browser audio to resume and respect mute cancellation. Adds a local-only sound tester. Engine remains 0.4.85. diff --git a/web-client/index.html b/web-client/index.html index 02bdb58a..9a5e3883 100644 --- a/web-client/index.html +++ b/web-client/index.html @@ -99,6 +99,7 @@ + Test sounds Connecting to local game… diff --git a/web-client/package-lock.json b/web-client/package-lock.json index 9db41512..bb386a1d 100644 --- a/web-client/package-lock.json +++ b/web-client/package-lock.json @@ -1,12 +1,12 @@ { "name": "@boardgamers/clash-web-client", - "version": "0.1.116", + "version": "0.1.117", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@boardgamers/clash-web-client", - "version": "0.1.116", + "version": "0.1.117", "dependencies": { "@boardgamers/protocol": "0.9.0", "lucide-svelte": "^0.577.0", diff --git a/web-client/package.json b/web-client/package.json index 3f07f70f..37ee4fa0 100644 --- a/web-client/package.json +++ b/web-client/package.json @@ -1,6 +1,6 @@ { "name": "@boardgamers/clash-web-client", - "version": "0.1.116", + "version": "0.1.117", "private": true, "type": "module", "scripts": { diff --git a/web-client/sounds.html b/web-client/sounds.html new file mode 100644 index 00000000..f00a744f --- /dev/null +++ b/web-client/sounds.html @@ -0,0 +1,29 @@ + + + + + + Clash · Sound preview + + + +
+

Sound preview

+

Click a sound to hear it at the game’s current volume. These use the same audio as the game. Hover is softer than action sounds.

+
+

Choose a sound to play.

+
+ + + diff --git a/web-client/src/audio.ts b/web-client/src/audio.ts index f5afdf5c..3fcda7de 100644 --- a/web-client/src/audio.ts +++ b/web-client/src/audio.ts @@ -1,7 +1,7 @@ import type { Move } from './types.ts'; export type SoundCue = - 'hover' | 'select' | 'collect' | 'build' | 'research' | 'draw' | 'move' | 'confirm' | 'undo' | 'error'; + 'hover' | 'select' | 'collect' | 'build' | 'research' | 'draw' | 'move' | 'confirm' | 'undo' | 'error' | 'battle' | 'casualty' | 'sink'; // Keep action cues audible without making repeated map hovers intrusive. const MASTER_GAIN = 0.4; const notes: Record = { @@ -15,6 +15,9 @@ const notes: Record = { confirm: [392, 494], undo: [330, 220], error: [180, 160], + battle: [110, 165, 110, 220], + casualty: [294, 196, 98], + sink: [220, 165, 110, 55], }; export function moveSound(move: Move | null): SoundCue { @@ -37,6 +40,7 @@ export class GameAudio { private enabled = false; private disposed = false; private lastHover = -Infinity; + private soundGeneration = 0; private createContext: () => AudioContext; constructor(createContext: () => AudioContext = () => new AudioContext()) { this.createContext = createContext; @@ -70,29 +74,44 @@ export class GameAudio { this.disposed || !context || !this.volume || - context.state !== 'running' || (typeof document !== 'undefined' && document.hidden) ) return; - if (this.voices.size >= 12) return; + if (context.state === 'suspended') { + if (cue !== 'hover') { + const generation = this.soundGeneration; + void context.resume().then(() => { + if (context.state === 'running' && generation === this.soundGeneration) this.play(cue); + }).catch(() => {}); + } + return; + } + if (context.state !== 'running') return; + if (this.voices.size >= 12) { + if (cue === 'hover') return; + this.stop(); + } if (cue === 'hover') { if (context.currentTime - this.lastHover < 0.09) return; this.lastHover = context.currentTime; } const duration = - cue === 'hover' ? 0.025 : cue === 'select' ? 0.075 : cue === 'research' || cue === 'draw' ? 0.24 : 0.18; + cue === 'hover' ? 0.055 : cue === 'select' ? 0.075 : cue === 'sink' ? 0.32 : cue === 'casualty' ? 0.22 : cue === 'research' || cue === 'draw' ? 0.24 : 0.18; notes[cue].forEach((frequency, index) => { const start = context.currentTime + index * (cue === 'research' ? 0.075 : 0.055); const oscillator = context.createOscillator(); const gain = context.createGain(); - oscillator.type = cue === 'build' || cue === 'move' ? 'triangle' : 'sine'; + oscillator.type = ['build', 'move', 'battle', 'casualty', 'sink'].includes(cue) ? 'triangle' : 'sine'; oscillator.frequency.setValueAtTime(frequency, start); - oscillator.frequency.exponentialRampToValueAtTime(frequency * 0.96, start + duration); + oscillator.frequency.exponentialRampToValueAtTime(frequency * (cue === 'sink' ? 0.4 : cue === 'casualty' ? 0.65 : 0.96), start + duration); gain.gain.setValueAtTime(0, start); gain.gain.linearRampToValueAtTime( - cue === 'hover' ? 0.025 : cue === 'select' ? 0.25 : 0.4, + cue === 'hover' ? 0.18 : cue === 'select' ? 0.25 : 0.4, start + 0.006, ); + if (cue !== 'hover') { + gain.gain.setValueAtTime(cue === 'select' ? 0.25 : 0.4, start + duration * 0.35); + } gain.gain.exponentialRampToValueAtTime(0.001, start + duration); oscillator.connect(gain); gain.connect(this.volume!); @@ -108,6 +127,7 @@ export class GameAudio { } private stop() { + this.soundGeneration++; for (const [oscillator, gain] of this.voices) { oscillator.stop(); oscillator.disconnect(); diff --git a/web-client/src/controller.ts b/web-client/src/controller.ts index fc26c398..7998246c 100644 --- a/web-client/src/controller.ts +++ b/web-client/src/controller.ts @@ -139,6 +139,7 @@ export class Controller { private destroyed = false; private playbackTimer: ReturnType | undefined; private effectTimer: ReturnType | undefined; + private battleSoundTimers: ReturnType[] = []; private battleTimer: ReturnType | undefined; private chatOff: () => void; constructor( @@ -882,15 +883,34 @@ export class Controller { } private showBattles(battles: BattleCue[], animate = true) { clearTimeout(this.battleTimer); + const previous = get(this.session).battles?.[0]; + if (!battles.length) this.clearBattleSounds(); + else if (battles[0].key !== previous?.key) this.playBattleSounds(battles[0]); this.patch({ battles, battleAnimate: animate && !get(this.session).reducedMotion }); this.scheduleBattle(); } + private clearBattleSounds() { + for (const timer of this.battleSoundTimers) clearTimeout(timer); + this.battleSoundTimers = []; + } + private playBattleSounds(battle: BattleCue) { + this.clearBattleSounds(); + const cues: ('battle' | 'casualty' | 'sink')[] = []; + if (battle.roll) cues.push('battle'); + if (battle.losses.some((loss) => !/ships?/i.test(loss.label))) cues.push('casualty'); + if (battle.losses.some((loss) => /ships?/i.test(loss.label))) cues.push('sink'); + cues.forEach((cue, index) => { + if (index === 0) this.audio.play(cue); + else this.battleSoundTimers.push(setTimeout(() => this.audio.play(cue), index * 450)); + }); + } private scheduleBattle() { if (!get(this.session).battles?.length) return; this.battleTimer = setTimeout(() => { const s = get(this.session); if (s.playback && s.battles?.length === 1) return; // Keep the result readable when stepping manually. this.patch({ battles: s.battles?.slice(1) ?? [] }); + if (s.battles?.[1]) this.playBattleSounds(s.battles[1]); this.scheduleBattle(); }, BATTLE_DURATION); } @@ -1929,6 +1949,7 @@ export class Controller { clearTimeout(this.timer); clearTimeout(this.refreshTimer); this.chatOff(); + this.clearBattleSounds(); this.audio.destroy(); } } diff --git a/web-client/src/preferences.test.ts b/web-client/src/preferences.test.ts index 3b7dfd76..ae1cbda6 100644 --- a/web-client/src/preferences.test.ts +++ b/web-client/src/preferences.test.ts @@ -253,3 +253,26 @@ test('home-at-bottom rotation is opt-in and only accepts an enabled boolean pref assert.equal(readPreferences({}).homeAtBottom, false); assert.equal(readPreferences({ homeAtBottom: true }).homeAtBottom, true); }); + +test('action sounds wait for browser audio to resume, and mute cancels them', async () => { + const { device, voices } = audioDevice(); + let finish!: () => void; + device.resume = () => new Promise((resolve) => { + finish = () => { device.state = 'running'; resolve(); }; + }); + const audio = new GameAudio(() => device as unknown as AudioContext); + audio.setEnabled(true); + audio.unlock(); + audio.play('collect'); + assert.equal(voices.length, 0); + finish(); + await Promise.resolve(); + assert.equal(voices.length, 2); + device.state = 'suspended'; + audio.play('battle'); + audio.setEnabled(false); + finish(); + await Promise.resolve(); + assert.equal(voices.length, 2, 'Muted pending sounds must not play later'); + audio.destroy(); +}); diff --git a/web-client/src/sound-preview.ts b/web-client/src/sound-preview.ts new file mode 100644 index 00000000..370a911a --- /dev/null +++ b/web-client/src/sound-preview.ts @@ -0,0 +1,39 @@ +import { GameAudio, type SoundCue } from './audio.ts'; + +const context = new AudioContext(); +const audio = new GameAudio(() => context); +audio.setEnabled(true); +const cues: [SoundCue, string, string][] = [ + ['hover', 'Hover', 'Moving over an interactive target'], + ['select', 'Select', 'Selecting a unit or option'], + ['collect', 'Collect', 'Collecting resources'], + ['build', 'Build / recruit', 'Constructing, founding or recruiting'], + ['research', 'Research', 'Gaining an advance'], + ['draw', 'Draw card', 'Drawing a card'], + ['move', 'Move', 'Moving units'], + ['confirm', 'Confirm', 'Other confirmed actions'], + ['undo', 'Undo', 'Reversing an action'], + ['battle', 'Battle rolls', 'Dice rolling in a battle'], + ['casualty', 'Unit loss', 'Land units or leaders lost in battle'], + ['sink', 'Ship sinking', 'Ships lost in battle'], + ['error', 'Error', 'An action could not be completed'], +]; +for (const [cue, title, description] of cues) { + const button = document.createElement('button'); + button.textContent = `▶ ${title}`; + const detail = document.createElement('span'); + detail.textContent = description; + button.append(detail); + button.addEventListener('click', async () => { + try { + audio.unlock(); + await context.resume(); + audio.play(cue); + document.querySelector('#status')!.textContent = `Played: ${title}`; + } catch { + document.querySelector('#status')!.textContent = 'Audio could not start. Check your browser’s sound permissions.'; + } + }); + document.querySelector('#sounds')!.append(button); +} +window.addEventListener('pagehide', () => audio.destroy());