fix : sound issue

This commit is contained in:
GuillaumeSD
2025-06-09 02:48:14 +02:00
parent 91876d11e3
commit fc628a2a32
2 changed files with 35 additions and 27 deletions

View File

@@ -119,7 +119,7 @@ export const useChessActions = (chessAtom: PrimitiveAtom<Chess>) => {
const movesNb = fullGame.history().length; const movesNb = fullGame.history().length;
if (moveIdx > movesNb) return; if (moveIdx > movesNb) return;
let lastMove: Move | null = null; let lastMove: Move | null = {} as Move;
for (let i = movesNb; i > moveIdx; i--) { for (let i = movesNb; i > moveIdx; i--) {
lastMove = newGame.undo(); lastMove = newGame.undo();
} }

View File

@@ -1,40 +1,48 @@
import { Move } from "chess.js"; import { Move } from "chess.js";
let ctx: AudioContext | null = null; let audioContext: AudioContext | null = null;
const bufferCache = new Map<string, AudioBuffer>(); let timeout: NodeJS.Timeout | null = null;
const soundsCache = new Map<string, AudioBuffer>();
const urls = { type Sound = "move" | "capture" | "illegalMove";
const soundUrls: Record<Sound, string> = {
move: "/sounds/move.mp3", move: "/sounds/move.mp3",
capture: "/sounds/capture.mp3", capture: "/sounds/capture.mp3",
illegal: "/sounds/illegal-move.mp3" illegalMove: "/sounds/error.mp3",
} as const; };
type Sound = keyof typeof urls; export const play = async (sound: Sound) => {
if (timeout) clearTimeout(timeout);
async function play(sound: Sound) { timeout = setTimeout(async () => {
try { if (!audioContext) audioContext = new AudioContext();
ctx ??= new (window.AudioContext || (window as any).webkitAudioContext)(); if (audioContext.state === "suspended") await audioContext.resume();
if (ctx.state === "suspended") await ctx.resume();
let buf = bufferCache.get(urls[sound]); let audioBuffer = soundsCache.get(soundUrls[sound]);
if (!buf) { if (!audioBuffer) {
const res = await fetch(urls[sound]); const res = await fetch(soundUrls[sound]);
buf = await ctx.decodeAudioData(await res.arrayBuffer()); const buffer = await audioContext.decodeAudioData(
bufferCache.set(urls[sound], buf); await res.arrayBuffer()
);
audioBuffer = buffer;
soundsCache.set(soundUrls[sound], buffer);
} }
const src = ctx.createBufferSource();
src.buffer = buf; const audioSrc = audioContext.createBufferSource();
src.connect(ctx.destination); audioSrc.buffer = audioBuffer;
src.start(); const volume = audioContext.createGain();
} catch { volume.gain.value = 0.3;
if ("vibrate" in navigator) navigator.vibrate(50); audioSrc.connect(volume);
} volume.connect(audioContext.destination);
} audioSrc.start();
}, 1);
};
export const playCaptureSound = () => play("capture"); export const playCaptureSound = () => play("capture");
export const playIllegalMoveSound = () => play("illegal"); export const playIllegalMoveSound = () => play("illegalMove");
export const playMoveSound = () => play("move"); export const playMoveSound = () => play("move");
export async function playSoundFromMove(move: Move | null) { export const playSoundFromMove = (move: Move | null) => {
if (!move) return playIllegalMoveSound(); if (!move) return playIllegalMoveSound();
if (move.captured) return playCaptureSound(); if (move.captured) return playCaptureSound();
return playMoveSound(); return playMoveSound();
} };