@air-jam/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +87 -0
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/chunk-XSXI3RIS.js +72 -0
  4. package/dist/chunk-XSXI3RIS.js.map +1 -0
  5. package/dist/index.cjs +3514 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +1529 -0
  8. package/dist/index.d.ts +1529 -0
  9. package/dist/index.js +3366 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/protocol.cjs +85 -0
  12. package/dist/protocol.cjs.map +1 -0
  13. package/dist/protocol.d.cts +242 -0
  14. package/dist/protocol.d.ts +242 -0
  15. package/dist/protocol.js +3 -0
  16. package/dist/protocol.js.map +1 -0
  17. package/package.json +81 -0
  18. package/src/AirJamClient.ts +775 -0
  19. package/src/audio/audio-manager.ts +395 -0
  20. package/src/audio/hooks.tsx +140 -0
  21. package/src/audio/index.ts +3 -0
  22. package/src/audio/volume-store.ts +77 -0
  23. package/src/components/air-jam-overlay.tsx +456 -0
  24. package/src/components/controller-shell.tsx +329 -0
  25. package/src/components/qr-scanner-dialog.tsx +373 -0
  26. package/src/components/ui/alert.tsx +66 -0
  27. package/src/components/ui/avatar.tsx +46 -0
  28. package/src/components/ui/badge.tsx +47 -0
  29. package/src/components/ui/button.tsx +61 -0
  30. package/src/components/ui/card.tsx +92 -0
  31. package/src/components/ui/slider.tsx +61 -0
  32. package/src/components/ui/tabs.tsx +64 -0
  33. package/src/components/volume-controls.tsx +93 -0
  34. package/src/constants.ts +45 -0
  35. package/src/context/air-jam-context.tsx +478 -0
  36. package/src/context/index.ts +2 -0
  37. package/src/context/socket-manager.ts +98 -0
  38. package/src/events.ts +151 -0
  39. package/src/hooks/use-air-jam-controller.ts +608 -0
  40. package/src/hooks/use-air-jam-host.ts +624 -0
  41. package/src/hooks/use-air-jam-shell.ts +343 -0
  42. package/src/hooks/use-fullscreen.ts +108 -0
  43. package/src/hooks/use-get-input.ts +97 -0
  44. package/src/hooks/use-send-signal.ts +164 -0
  45. package/src/index.ts +33 -0
  46. package/src/internal/input-manager.ts +362 -0
  47. package/src/protocol.ts +331 -0
  48. package/src/state/connection-store.ts +84 -0
  49. package/src/styles.css +128 -0
  50. package/src/types/browser.ts +42 -0
  51. package/src/utils/cn.ts +4 -0
  52. package/src/utils/ids.ts +31 -0
  53. package/src/utils/index.ts +1 -0
  54. package/src/utils/mode.ts +13 -0
  55. package/src/utils/network-ip.ts +145 -0
  56. package/src/utils/url-builder.ts +170 -0
@@ -0,0 +1,395 @@
1
+ import { Howl, Howler } from "howler";
2
+ import type { Socket } from "socket.io-client";
3
+ import type { ClientToServerEvents, ServerToClientEvents } from "../protocol";
4
+ import { useVolumeStore } from "./volume-store";
5
+
6
+ export type SoundCategory = "music" | "sfx";
7
+
8
+ export interface SoundConfig {
9
+ src: string[];
10
+ volume?: number;
11
+ loop?: boolean;
12
+ html5?: boolean; // Force HTML5 Audio (good for large files/music)
13
+ sprite?: {
14
+ [key: string]: [number, number];
15
+ };
16
+ category?: SoundCategory; // Optional category for volume control grouping
17
+ }
18
+
19
+ export type SoundManifest = Record<string, SoundConfig>;
20
+
21
+ /**
22
+ * Auto-detect sound category based on config heuristics
23
+ * - If explicitly set, use it
24
+ * - If loop=true and html5=true, likely music
25
+ * - Otherwise, default to sfx
26
+ */
27
+ export function detectSoundCategory(config: SoundConfig): SoundCategory {
28
+ if (config.category) {
29
+ return config.category;
30
+ }
31
+
32
+ // Heuristic: loop + html5 typically indicates music
33
+ if (config.loop && config.html5) {
34
+ return "music";
35
+ }
36
+
37
+ return "sfx";
38
+ }
39
+
40
+ export interface PlayOptions {
41
+ remote?: boolean; // If true, send to network (Host->Controller or Controller->Host)
42
+ target?: string; // Optional: Specific controller ID (only used if Host sending to Controller)
43
+ volume?: number;
44
+ loop?: boolean;
45
+ sprite?: string;
46
+ pitch?: number;
47
+ }
48
+
49
+ export class AudioManager<T extends string = string> {
50
+ private sounds: Map<string, Howl> = new Map();
51
+ private categories: Map<string, SoundCategory> = new Map();
52
+ private manifest: SoundManifest = {};
53
+ private _muted: boolean = false;
54
+ private socket: Socket<ServerToClientEvents, ClientToServerEvents> | null =
55
+ null;
56
+ private roomId: string | null = null;
57
+ private activeSoundIds: Map<
58
+ number,
59
+ { soundId: string; category: SoundCategory }
60
+ > = new Map();
61
+ private volumeUnsubscribe: (() => void) | null = null;
62
+
63
+ constructor(manifest?: SoundManifest) {
64
+ // Initialize volume store FIRST to ensure values are loaded from localStorage
65
+ // This must happen before loading sounds so they can use the stored volumes
66
+ const initialState = useVolumeStore.getState();
67
+
68
+ // Set up volume change subscription
69
+ const updateAllSounds = (
70
+ state: ReturnType<typeof useVolumeStore.getState>,
71
+ ) => {
72
+ // Update all currently playing sounds
73
+ this.activeSoundIds.forEach((info, howlSoundId) => {
74
+ const sound = this.sounds.get(info.soundId);
75
+ if (sound) {
76
+ const config = this.manifest[info.soundId];
77
+ const baseVolume = config?.volume ?? 1.0;
78
+ const effectiveVolume = state.getEffectiveVolume(info.category);
79
+ const finalVolume = baseVolume * effectiveVolume;
80
+ sound.volume(finalVolume, howlSoundId);
81
+ }
82
+ });
83
+
84
+ // Also update the default volume for all loaded sounds (for future plays)
85
+ this.sounds.forEach((sound, soundId) => {
86
+ const category = this.categories.get(soundId) ?? "sfx";
87
+ const config = this.manifest[soundId];
88
+ const baseVolume = config?.volume ?? 1.0;
89
+ const effectiveVolume = state.getEffectiveVolume(category);
90
+ const newVolume = baseVolume * effectiveVolume;
91
+ // Update default volume for the sound
92
+ sound.volume(newVolume);
93
+ });
94
+ };
95
+
96
+ // Subscribe to future changes
97
+ this.volumeUnsubscribe = useVolumeStore.subscribe(updateAllSounds);
98
+
99
+ // Now load sounds - they will use the stored volumes from the start
100
+ if (manifest) {
101
+ this.load(manifest);
102
+ // Apply initial volumes to all loaded sounds immediately
103
+ updateAllSounds(initialState);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Cleanup method to unsubscribe from volume changes
109
+ */
110
+ public destroy() {
111
+ if (this.volumeUnsubscribe) {
112
+ this.volumeUnsubscribe();
113
+ this.volumeUnsubscribe = null;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Get the category for a sound (with auto-detection)
119
+ */
120
+ public getCategory(soundId: string): SoundCategory {
121
+ return this.categories.get(soundId) ?? "sfx";
122
+ }
123
+
124
+ public setSocket(
125
+ socket: Socket<ServerToClientEvents, ClientToServerEvents> | null,
126
+ roomId?: string,
127
+ ) {
128
+ this.socket = socket;
129
+ if (roomId) this.roomId = roomId;
130
+ }
131
+
132
+ /**
133
+ * Initialize audio context (must be called on user interaction)
134
+ */
135
+ public init() {
136
+ if (Howler.ctx && Howler.ctx.state === "suspended") {
137
+ Howler.ctx.resume();
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Check if audio context is ready to play sounds
143
+ */
144
+ public isReady(): boolean {
145
+ return Howler.ctx !== null && Howler.ctx.state === "running";
146
+ }
147
+
148
+ public load(manifest: SoundManifest) {
149
+ this.manifest = { ...this.manifest, ...manifest };
150
+
151
+ // Get current volume store state to apply to new sounds
152
+ const volumeStore = useVolumeStore.getState();
153
+
154
+ Object.entries(manifest).forEach(([key, config]) => {
155
+ if (this.sounds.has(key)) return;
156
+
157
+ // Detect and store category
158
+ const category = detectSoundCategory(config);
159
+ this.categories.set(key, category);
160
+
161
+ // Calculate effective volume for this sound
162
+ const baseVolume = config.volume ?? 1.0;
163
+ const effectiveVolume = volumeStore.getEffectiveVolume(category);
164
+ const initialVolume = baseVolume * effectiveVolume;
165
+
166
+ const sound = new Howl({
167
+ src: config.src,
168
+ volume: initialVolume, // Use effective volume from the start
169
+ loop: config.loop ?? false,
170
+ html5: config.html5 ?? false,
171
+ sprite: config.sprite,
172
+ preload: true,
173
+ });
174
+
175
+ this.sounds.set(key, sound);
176
+ });
177
+ }
178
+
179
+ public play(id: T, options?: PlayOptions): number | null {
180
+ const {
181
+ remote = false,
182
+ target,
183
+ volume,
184
+ loop,
185
+ sprite,
186
+ pitch,
187
+ } = options || {};
188
+
189
+ if (remote) {
190
+ this.playRemote(id, target, volume, loop);
191
+ return null; // Remote play doesn't return a local sound ID
192
+ } else {
193
+ return this.playLocal(id, volume, loop, sprite, pitch);
194
+ }
195
+ }
196
+
197
+ private playLocal(
198
+ id: string,
199
+ volume?: number,
200
+ loop?: boolean,
201
+ sprite?: string,
202
+ pitch?: number,
203
+ ): number | null {
204
+ const sound = this.sounds.get(id);
205
+ if (!sound) {
206
+ console.warn(`Sound "${id}" not found`);
207
+ return null;
208
+ }
209
+
210
+ const category = this.getCategory(id);
211
+ const config = this.manifest[id];
212
+ const baseVolume = config?.volume ?? 1.0;
213
+
214
+ // Get effective volume from volume store (always get fresh state)
215
+ const volumeStore = useVolumeStore.getState();
216
+ const effectiveVolume = volumeStore.getEffectiveVolume(category);
217
+
218
+ // Calculate final volume BEFORE playing
219
+ const finalVolume =
220
+ volume !== undefined
221
+ ? volume * effectiveVolume
222
+ : baseVolume * effectiveVolume;
223
+
224
+ // Play the sound
225
+ const soundId = sound.play(sprite);
226
+
227
+ // Store active sound ID for volume updates (do this before setting volume)
228
+ this.activeSoundIds.set(soundId, { soundId: id, category });
229
+
230
+ // Clean up when sound ends
231
+ sound.once("end", () => {
232
+ this.activeSoundIds.delete(soundId);
233
+ });
234
+
235
+ // Apply volume immediately after play (this overrides any default volume)
236
+ sound.volume(finalVolume, soundId);
237
+
238
+ if (loop !== undefined) sound.loop(loop, soundId);
239
+ if (pitch !== undefined) sound.rate(pitch, soundId);
240
+
241
+ return soundId;
242
+ }
243
+
244
+ private playRemote(
245
+ id: string,
246
+ target?: string,
247
+ volume?: number,
248
+ loop?: boolean,
249
+ ) {
250
+ if (!this.socket || !this.roomId) return;
251
+
252
+ if (this.isHost()) {
253
+ // Host -> Controller(s)
254
+ this.socket.emit("host:play_sound", {
255
+ roomId: this.roomId,
256
+ targetControllerId: target, // If undefined, broadcasts to all
257
+ soundId: id,
258
+ volume,
259
+ loop,
260
+ });
261
+ } else {
262
+ // Controller -> Host
263
+ this.socket.emit("controller:play_sound", {
264
+ roomId: this.roomId,
265
+ soundId: id,
266
+ volume,
267
+ loop,
268
+ });
269
+ }
270
+ }
271
+
272
+ private isHost(): boolean {
273
+ // Heuristic: If we have a socket and it's connected as host...
274
+ // Or we can just check if we are running in a browser environment that looks like host?
275
+ // Better: The socket instance usually has query params or we can pass a flag.
276
+ // For now, let's assume if we are calling playRemote with a target, we are likely host.
277
+ // Actually, we can just rely on the fact that `host:play_sound` is only available on Host socket type?
278
+ // No, both share types.
279
+
280
+ // Let's add a role property to AudioManager or infer from socket.
281
+ const query = this.socket?.io?.opts?.query;
282
+ return (
283
+ (query &&
284
+ typeof query === "object" &&
285
+ "role" in query &&
286
+ query.role === "host") ||
287
+ false
288
+ );
289
+ }
290
+
291
+ /**
292
+ * Play a sound at a specific 3D position
293
+ */
294
+ public playSpatial(
295
+ id: T,
296
+ pos: { x: number; y: number; z: number },
297
+ sprite?: string,
298
+ ): number | null {
299
+ const soundId = this.playLocal(id, undefined, undefined, sprite);
300
+ if (soundId !== null) {
301
+ const sound = this.sounds.get(id);
302
+ if (sound) {
303
+ sound.pos(pos.x, pos.y, pos.z, soundId);
304
+ // Enable 3D spatialization for this sound instance
305
+ sound.pannerAttr(
306
+ {
307
+ panningModel: "HRTF",
308
+ refDistance: 1,
309
+ maxDistance: 1000,
310
+ rolloffFactor: 1,
311
+ distanceModel: "inverse",
312
+ },
313
+ soundId,
314
+ );
315
+ }
316
+ }
317
+ return soundId;
318
+ }
319
+
320
+ public stop(id?: T, soundId?: number) {
321
+ if (id) {
322
+ const sound = this.sounds.get(id);
323
+ sound?.stop(soundId);
324
+ // Clean up active sound tracking
325
+ if (soundId !== undefined) {
326
+ this.activeSoundIds.delete(soundId);
327
+ } else {
328
+ // Remove all instances of this sound
329
+ this.activeSoundIds.forEach((info, howlSoundId) => {
330
+ if (info.soundId === id) {
331
+ this.activeSoundIds.delete(howlSoundId);
332
+ }
333
+ });
334
+ }
335
+ } else {
336
+ // Stop all
337
+ Howler.stop();
338
+ this.activeSoundIds.clear();
339
+ }
340
+ }
341
+
342
+ public volume(vol: number, id?: T, soundId?: number) {
343
+ if (id) {
344
+ const sound = this.sounds.get(id);
345
+ if (sound) {
346
+ if (soundId !== undefined) {
347
+ sound.volume(vol, soundId);
348
+ } else {
349
+ sound.volume(vol);
350
+ }
351
+ }
352
+ } else {
353
+ Howler.volume(vol);
354
+ }
355
+ }
356
+
357
+ public mute(muted: boolean, id?: T, soundId?: number) {
358
+ if (id) {
359
+ const sound = this.sounds.get(id);
360
+ sound?.mute(muted, soundId);
361
+ } else {
362
+ this._muted = muted;
363
+ Howler.mute(muted);
364
+ }
365
+ }
366
+
367
+ public isMuted(): boolean {
368
+ return this._muted;
369
+ }
370
+
371
+ /**
372
+ * Update the listener position for spatial audio
373
+ */
374
+ public setListenerPos(x: number, y: number, z: number) {
375
+ Howler.pos(x, y, z);
376
+ }
377
+
378
+ /**
379
+ * Update the listener orientation for spatial audio
380
+ */
381
+ public setListenerOrientation(
382
+ x: number,
383
+ y: number,
384
+ z: number,
385
+ xUp: number,
386
+ yUp: number,
387
+ zUp: number,
388
+ ) {
389
+ Howler.orientation(x, y, z, xUp, yUp, zUp);
390
+ }
391
+ }
392
+
393
+ export const createAudioManager = <T extends string = string>(
394
+ manifest?: SoundManifest,
395
+ ) => new AudioManager<T>(manifest);
@@ -0,0 +1,140 @@
1
+ /* eslint-disable react-refresh/only-export-components */
2
+ import { createContext, useContext, useEffect, useRef } from "react";
3
+ import { useStore } from "zustand";
4
+ import { useAirJamContext } from "../context/air-jam-context";
5
+ import { AudioManager, SoundManifest } from "./audio-manager";
6
+
7
+ // Module-level singleton manager cache
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ const managerCache = new WeakMap<SoundManifest, AudioManager<any>>();
10
+ let globalInitialized = false;
11
+
12
+ /**
13
+ * Hook to use audio with a sound manifest
14
+ * Creates a singleton AudioManager for the manifest and handles socket injection
15
+ * @param manifest The sound manifest configuration
16
+ * @returns The AudioManager instance with type-safe sound IDs
17
+ */
18
+ export function useAudio<M extends SoundManifest>(
19
+ manifest: M,
20
+ ): AudioManager<keyof M & string> {
21
+ const { store, getSocket } = useAirJamContext();
22
+ const roomId = useStore(store, (state) => state.roomId);
23
+ const role = useStore(store, (state) => state.role);
24
+ const initRef = useRef(false);
25
+
26
+ // Get or create singleton manager for this manifest
27
+ type SoundId = keyof M & string;
28
+ let manager = managerCache.get(manifest) as AudioManager<SoundId> | undefined;
29
+ if (!manager) {
30
+ manager = new AudioManager<SoundId>(manifest);
31
+ managerCache.set(manifest, manager);
32
+ }
33
+
34
+ // Inject socket into manager when connection is available
35
+ useEffect(() => {
36
+ if (role && roomId) {
37
+ const socket = getSocket(role);
38
+ manager.setSocket(socket, roomId);
39
+ }
40
+ }, [manager, role, roomId, getSocket]);
41
+
42
+ // Ensure audio context is resumed on first interaction (only once globally)
43
+ useEffect(() => {
44
+ if (initRef.current || globalInitialized) return;
45
+
46
+ const handleInteraction = () => {
47
+ manager.init();
48
+ globalInitialized = true;
49
+ initRef.current = true;
50
+ window.removeEventListener("click", handleInteraction);
51
+ window.removeEventListener("touchstart", handleInteraction);
52
+ window.removeEventListener("keydown", handleInteraction);
53
+ };
54
+
55
+ window.addEventListener("click", handleInteraction);
56
+ window.addEventListener("touchstart", handleInteraction);
57
+ window.addEventListener("keydown", handleInteraction);
58
+
59
+ return () => {
60
+ window.removeEventListener("click", handleInteraction);
61
+ window.removeEventListener("touchstart", handleInteraction);
62
+ window.removeEventListener("keydown", handleInteraction);
63
+ };
64
+ }, [manager]);
65
+
66
+ return manager;
67
+ }
68
+
69
+ // Legacy exports for backward compatibility (can be removed later)
70
+ const LegacyAudioContext = createContext<AudioManager | null>(null);
71
+
72
+ export const AudioProvider = ({
73
+ children,
74
+ manager,
75
+ }: {
76
+ children: React.ReactNode;
77
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
+ manager: AudioManager<any>;
79
+ }) => {
80
+ const { store, getSocket } = useAirJamContext();
81
+ const roomId = useStore(store, (state) => state.roomId);
82
+ const role = useStore(store, (state) => state.role);
83
+
84
+ // Inject socket into manager
85
+ useEffect(() => {
86
+ if (role && roomId) {
87
+ const socket = getSocket(role);
88
+ manager.setSocket(socket, roomId);
89
+ }
90
+ }, [manager, role, roomId, getSocket]);
91
+
92
+ // Ensure audio context is resumed on first interaction
93
+ useEffect(() => {
94
+ const handleInteraction = () => {
95
+ manager.init();
96
+ window.removeEventListener("click", handleInteraction);
97
+ window.removeEventListener("touchstart", handleInteraction);
98
+ window.removeEventListener("keydown", handleInteraction);
99
+ };
100
+
101
+ window.addEventListener("click", handleInteraction);
102
+ window.addEventListener("touchstart", handleInteraction);
103
+ window.addEventListener("keydown", handleInteraction);
104
+
105
+ return () => {
106
+ window.removeEventListener("click", handleInteraction);
107
+ window.removeEventListener("touchstart", handleInteraction);
108
+ window.removeEventListener("keydown", handleInteraction);
109
+ };
110
+ }, [manager]);
111
+
112
+ return (
113
+ <LegacyAudioContext.Provider value={manager}>
114
+ {children}
115
+ </LegacyAudioContext.Provider>
116
+ );
117
+ };
118
+
119
+ export const useAudioLegacy = <T extends string = string>() => {
120
+ const manager = useContext(LegacyAudioContext);
121
+ if (!manager) {
122
+ throw new Error("useAudioLegacy must be used within an AudioProvider");
123
+ }
124
+ return manager as AudioManager<T>;
125
+ };
126
+
127
+ /**
128
+ * Hook to create a stable AudioManager instance
129
+ */
130
+ export const useAudioManager = <T extends string = string>(
131
+ manifest: SoundManifest,
132
+ ) => {
133
+ const managerRef = useRef<AudioManager<T> | null>(null);
134
+
135
+ if (!managerRef.current) {
136
+ managerRef.current = new AudioManager<T>(manifest);
137
+ }
138
+
139
+ return managerRef.current;
140
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./audio-manager";
2
+ export * from "./hooks";
3
+ export * from "./volume-store";
@@ -0,0 +1,77 @@
1
+ import { create } from "zustand";
2
+
3
+ interface VolumeSettings {
4
+ masterVolume: number; // 0-1
5
+ musicVolume: number; // 0-1
6
+ sfxVolume: number; // 0-1
7
+ setMasterVolume: (volume: number) => void;
8
+ setMusicVolume: (volume: number) => void;
9
+ setSfxVolume: (volume: number) => void;
10
+ getEffectiveVolume: (category: "music" | "sfx") => number;
11
+ }
12
+
13
+ const STORAGE_KEY = "air-jam-volume-settings";
14
+
15
+ // Load initial state from localStorage
16
+ const loadFromStorage = (): Partial<VolumeSettings> => {
17
+ if (typeof window === "undefined") return {};
18
+ try {
19
+ const stored = localStorage.getItem(STORAGE_KEY);
20
+ if (stored) {
21
+ const parsed = JSON.parse(stored);
22
+ return {
23
+ masterVolume: parsed.masterVolume ?? 1.0,
24
+ musicVolume: parsed.musicVolume ?? 0.8,
25
+ sfxVolume: parsed.sfxVolume ?? 1.0,
26
+ };
27
+ }
28
+ } catch {
29
+ // Ignore parse errors
30
+ }
31
+ return {};
32
+ };
33
+
34
+ // Save to localStorage
35
+ const saveToStorage = (state: VolumeSettings) => {
36
+ if (typeof window === "undefined") return;
37
+ try {
38
+ localStorage.setItem(
39
+ STORAGE_KEY,
40
+ JSON.stringify({
41
+ masterVolume: state.masterVolume,
42
+ musicVolume: state.musicVolume,
43
+ sfxVolume: state.sfxVolume,
44
+ }),
45
+ );
46
+ } catch {
47
+ // Ignore storage errors (e.g., quota exceeded)
48
+ }
49
+ };
50
+
51
+ const initialValues = loadFromStorage();
52
+
53
+ export const useVolumeStore = create<VolumeSettings>((set, get) => ({
54
+ masterVolume: initialValues.masterVolume ?? 1.0,
55
+ musicVolume: initialValues.musicVolume ?? 0.8,
56
+ sfxVolume: initialValues.sfxVolume ?? 1.0,
57
+ setMasterVolume: (volume) => {
58
+ const clamped = Math.max(0, Math.min(1, volume));
59
+ set({ masterVolume: clamped });
60
+ saveToStorage({ ...get(), masterVolume: clamped });
61
+ },
62
+ setMusicVolume: (volume) => {
63
+ const clamped = Math.max(0, Math.min(1, volume));
64
+ set({ musicVolume: clamped });
65
+ saveToStorage({ ...get(), musicVolume: clamped });
66
+ },
67
+ setSfxVolume: (volume) => {
68
+ const clamped = Math.max(0, Math.min(1, volume));
69
+ set({ sfxVolume: clamped });
70
+ saveToStorage({ ...get(), sfxVolume: clamped });
71
+ },
72
+ getEffectiveVolume: (category) => {
73
+ const { masterVolume, musicVolume, sfxVolume } = get();
74
+ const categoryVolume = category === "music" ? musicVolume : sfxVolume;
75
+ return masterVolume * categoryVolume;
76
+ },
77
+ }));