-
Notifications
You must be signed in to change notification settings - Fork 1
feat: sound design — synthesised audio feedback + mute toggle (Phase 4) #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // @vitest-environment jsdom | ||
| import { renderHook } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { initialSettings, useSettingsStore } from "../store/settingsStore"; | ||
|
|
||
| // synthSounds uses the Web Audio API which is not available in the node test | ||
| // environment; mock the module so useSound can be tested without a real AudioContext. | ||
| vi.mock("../utils/synthSounds", () => ({ | ||
| getAudioContext: vi.fn(() => null), | ||
| synthClick: vi.fn(), | ||
| synthEvolution: vi.fn(), | ||
| synthPurchase: vi.fn(), | ||
| synthWelcomeBack: vi.fn(), | ||
| })); | ||
|
|
||
| // useReducedMotion depends on window.matchMedia — stub it out | ||
| const mockUseReducedMotion = vi.fn(() => false); | ||
| vi.mock("./useReducedMotion", () => ({ | ||
| get useReducedMotion() { | ||
| return mockUseReducedMotion; | ||
| }, | ||
| })); | ||
|
|
||
| import * as synthSounds from "../utils/synthSounds"; | ||
| import { useSound } from "./useSound"; | ||
|
|
||
| // useSound is a React hook; we call it directly because all its inner | ||
| // hook dependencies are stubbed above — no React render is required. | ||
| function callHook() { | ||
| return renderHook(() => useSound()).result.current; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| useSettingsStore.setState(initialSettings); | ||
| vi.clearAllMocks(); | ||
| mockUseReducedMotion.mockReturnValue(false); | ||
| }); | ||
|
|
||
| describe("useSound", () => { | ||
| it("returns four play functions", () => { | ||
| const sound = callHook(); | ||
| expect(typeof sound.playClick).toBe("function"); | ||
| expect(typeof sound.playPurchase).toBe("function"); | ||
| expect(typeof sound.playEvolution).toBe("function"); | ||
| expect(typeof sound.playWelcomeBack).toBe("function"); | ||
| }); | ||
|
|
||
| it("does not call getAudioContext when soundEnabled is false", () => { | ||
| useSettingsStore.setState({ soundEnabled: false }); | ||
| const sound = callHook(); | ||
| sound.playClick(); | ||
| expect(synthSounds.getAudioContext).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("calls getAudioContext when soundEnabled is true", () => { | ||
| useSettingsStore.setState({ soundEnabled: true }); | ||
| const sound = callHook(); | ||
| sound.playClick(); | ||
| expect(synthSounds.getAudioContext).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not call synthPurchase when audio context is null", () => { | ||
| useSettingsStore.setState({ soundEnabled: true }); | ||
| vi.mocked(synthSounds.getAudioContext).mockReturnValue(null); | ||
| const sound = callHook(); | ||
| sound.playPurchase(); | ||
| expect(synthSounds.getAudioContext).toHaveBeenCalled(); | ||
| expect(synthSounds.synthPurchase).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not call getAudioContext for playEvolution when muted", () => { | ||
| useSettingsStore.setState({ soundEnabled: false }); | ||
| const sound = callHook(); | ||
| sound.playEvolution(); | ||
| expect(synthSounds.getAudioContext).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not call getAudioContext for playWelcomeBack when muted", () => { | ||
| useSettingsStore.setState({ soundEnabled: false }); | ||
| const sound = callHook(); | ||
| sound.playWelcomeBack(); | ||
| expect(synthSounds.getAudioContext).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("silences all sounds when prefers-reduced-motion is true", () => { | ||
| mockUseReducedMotion.mockReturnValue(true); | ||
| useSettingsStore.setState({ soundEnabled: true }); | ||
| const { result } = renderHook(() => useSound()); | ||
| result.current.playClick(); | ||
| expect(synthSounds.getAudioContext).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { useCallback } from "react"; | ||
| import { useSettingsStore } from "../store/settingsStore"; | ||
| import { | ||
| getAudioContext, | ||
| synthClick, | ||
| synthEvolution, | ||
| synthPurchase, | ||
| synthWelcomeBack, | ||
| } from "../utils/synthSounds"; | ||
| import { useReducedMotion } from "./useReducedMotion"; | ||
|
|
||
| type SynthFn = (ctx: AudioContext) => void; | ||
|
|
||
| /** | ||
| * Returns stable callbacks for each in-game sound. | ||
| * Sounds are silenced when the user has muted audio or enabled | ||
| * prefers-reduced-motion (which implies reducing sensory stimulation). | ||
| */ | ||
| export function useSound() { | ||
| const soundEnabled = useSettingsStore((s) => s.soundEnabled); | ||
| const prefersReduced = useReducedMotion(); | ||
|
|
||
| const play = useCallback( | ||
| (fn: SynthFn) => { | ||
| if (!soundEnabled || prefersReduced) return; | ||
| const ctx = getAudioContext(); | ||
| if (!ctx) return; | ||
| const resume = | ||
| ctx.state === "suspended" ? ctx.resume() : Promise.resolve(); | ||
| resume | ||
| .then(() => fn(ctx)) | ||
| .catch(() => { | ||
| // AudioContext blocked by browser policy — ignore silently | ||
| }); | ||
| }, | ||
| [soundEnabled, prefersReduced], | ||
| ); | ||
|
|
||
| const playClick = useCallback(() => play(synthClick), [play]); | ||
| const playPurchase = useCallback(() => play(synthPurchase), [play]); | ||
| const playEvolution = useCallback(() => play(synthEvolution), [play]); | ||
| const playWelcomeBack = useCallback(() => play(synthWelcomeBack), [play]); | ||
|
|
||
| return { playClick, playPurchase, playEvolution, playWelcomeBack }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫
playPurchase()fires before the purchase action executes. If the player can't afford the upgrade, they hear the chime but nothing happens — confusing audio feedback.Move
playPurchase()after the purchase call, or gate it on an affordability check.