diff --git a/apps/web/src/lib/feedback.ts b/apps/web/src/lib/feedback.ts index 13e09eb..33f9ef3 100644 --- a/apps/web/src/lib/feedback.ts +++ b/apps/web/src/lib/feedback.ts @@ -1,36 +1,176 @@ -/** Tiny synthesized sounds + vibration (no audio files; docs/07 §6). */ +/** + * Synthesized sounds + vibration (no audio files; docs/07 §6). + * Every sound goes through one master gain set from the site volume (0~100, default 50; the old sounds at full level + * are now volume 100, so the default plays them at half). Games use `sfx.*` (shared palette) or `tone`/`noise` + * directly for something of their own. + */ import { useSession } from '../store/session'; let ctx: AudioContext | null = null; +let master: GainNode | null = null; -function audio(): AudioContext | null { - if (!useSession.getState().settings.sound) return null; +/** 0~1 from the settings (0 when sound is off). */ +export function volume01(): number { + const s = useSession.getState().settings; + if (!s.sound) return 0; + return Math.max(0, Math.min(100, s.volume ?? 50)) / 100; +} + +function audio(): { a: AudioContext; out: GainNode } | null { + const v = volume01(); + if (v <= 0) return null; try { ctx ??= new AudioContext(); if (ctx.state === 'suspended') void ctx.resume(); - return ctx; + if (!master) { + master = ctx.createGain(); + master.connect(ctx.destination); + } + master.gain.value = v; + return { a: ctx, out: master }; } catch { return null; } } -function tone(freq: number, ms: number, type: OscillatorType = 'sine', gain = 0.15, delay = 0): void { - const a = audio(); - if (!a) return; +export interface ToneOpts { + /** Glide to this frequency by the end. */ + slideTo?: number; + /** Attack in ms (default 4). */ + attack?: number; +} + +/** One enveloped oscillator note. `gain` is the level at volume 100. */ +export function tone(freq: number, ms: number, type: OscillatorType = 'sine', gain = 0.15, delay = 0, opts: ToneOpts = {}): void { + const au = audio(); + if (!au) return; + const { a, out } = au; const t = a.currentTime + delay / 1000; + const end = t + ms / 1000; const o = a.createOscillator(); const g = a.createGain(); o.type = type; - o.frequency.value = freq; - g.gain.setValueAtTime(gain, t); - g.gain.exponentialRampToValueAtTime(0.0001, t + ms / 1000); - o.connect(g).connect(a.destination); + o.frequency.setValueAtTime(freq, t); + if (opts.slideTo) o.frequency.exponentialRampToValueAtTime(Math.max(1, opts.slideTo), end); + g.gain.setValueAtTime(0.0001, t); + g.gain.exponentialRampToValueAtTime(gain, t + (opts.attack ?? 4) / 1000); + g.gain.exponentialRampToValueAtTime(0.0001, end); + o.connect(g).connect(out); o.start(t); - o.stop(t + ms / 1000 + 0.02); + o.stop(end + 0.02); } +let noiseBuf: AudioBuffer | null = null; +/** A burst of filtered noise (card slides, shuffles, dice, chips). */ +export function noise(ms: number, gain = 0.12, delay = 0, filter: BiquadFilterType = 'bandpass', freq = 3000, q = 1): void { + const au = audio(); + if (!au) return; + const { a, out } = au; + if (!noiseBuf) { + noiseBuf = a.createBuffer(1, a.sampleRate, a.sampleRate); + const d = noiseBuf.getChannelData(0); + for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1; + } + const t = a.currentTime + delay / 1000; + const src = a.createBufferSource(); + src.buffer = noiseBuf; + const f = a.createBiquadFilter(); + f.type = filter; + f.frequency.value = freq; + f.Q.value = q; + const g = a.createGain(); + g.gain.setValueAtTime(gain, t); + g.gain.exponentialRampToValueAtTime(0.0001, t + ms / 1000); + src.connect(f).connect(g).connect(out); + src.start(t, Math.random() * 0.5); + src.stop(t + ms / 1000 + 0.02); +} + +/** Shared sound palette for every game. Short, soft, and never louder than the old "win" fanfare. */ +export const sfx = { + /** UI button / selection. */ + click: () => tone(1200, 35, 'triangle', 0.12), + select: () => tone(880, 60, 'triangle', 0.12), + /** A card turned face up. */ + cardFlip: () => { + noise(60, 0.12, 0, 'highpass', 2500); + tone(1400, 40, 'triangle', 0.05, 10); + }, + /** One card dealt / slid onto the table. */ + cardDeal: (delay = 0) => noise(90, 0.14, delay, 'bandpass', 2200, 0.8), + /** A card played onto the pile (firmer than a deal). */ + cardPlay: () => { + noise(70, 0.18, 0, 'bandpass', 1500, 0.9); + tone(220, 60, 'sine', 0.1); + }, + /** Deal `n` cards in a row. */ + deal: (n = 5, gapMs = 70) => { + for (let i = 0; i < n; i++) noise(80, 0.12, i * gapMs, 'bandpass', 2000 + (i % 3) * 300, 0.8); + }, + shuffle: () => { + for (let i = 0; i < 10; i++) noise(40, 0.08, i * 28, 'highpass', 3000 + (i % 4) * 400); + }, + /** Drawing cards from the deck (one per call, or `n`). */ + draw: (n = 1) => { + for (let i = 0; i < n; i++) { + noise(70, 0.13, i * 90, 'bandpass', 2600, 0.8); + tone(500 + i * 40, 50, 'triangle', 0.05, i * 90 + 20); + } + }, + /** Chips / coins put in. */ + chip: (delay = 0) => { + tone(2600, 45, 'square', 0.04, delay); + tone(3400, 60, 'triangle', 0.06, delay + 15); + }, + chips: (n = 4) => { + for (let i = 0; i < n; i++) sfx.chip(i * 55); + }, + /** Pot collected by the winner. */ + collect: () => { + for (let i = 0; i < 8; i++) tone(2200 + i * 120, 50, 'triangle', 0.05, i * 45); + }, + coin: () => { + tone(1318, 70, 'square', 0.05); + tone(1976, 160, 'square', 0.05, 70); + }, + /** Dice shaken and rolled, landing at the end. */ + dice: () => { + for (let i = 0; i < 6; i++) noise(35, 0.12, i * 55, 'bandpass', 1800 + i * 150, 2); + noise(90, 0.18, 360, 'lowpass', 900); + }, + /** Wooden piece / stone placed. */ + piece: () => { + tone(900, 70, 'triangle', 0.22); + noise(40, 0.08, 0, 'lowpass', 1200); + }, + stone: () => { + tone(1100, 60, 'triangle', 0.22); + noise(30, 0.1, 0, 'highpass', 4000); + }, + /** Something taken / captured. */ + capture: () => { + tone(600, 80, 'square', 0.06); + tone(300, 140, 'triangle', 0.12, 60); + }, + /** Light movement swish. */ + whoosh: () => noise(180, 0.08, 0, 'bandpass', 900, 0.6), + pop: () => tone(700, 90, 'sine', 0.18, 0, { slideTo: 1400 }), + /** Small positive / negative feedback. */ + good: () => { + tone(784, 90, 'triangle', 0.14); + tone(1175, 140, 'triangle', 0.12, 80); + }, + bad: () => tone(200, 220, 'sawtooth', 0.05, 0, { slideTo: 140 }), + /** Countdown tick (last seconds). */ + tick: () => tone(1500, 30, 'square', 0.04), + /** Big moment (땡, 우노!, 원카드!, 체크메이트 …). */ + fanfare: () => [659, 784, 988, 1318].forEach((f, i) => tone(f, 160, 'square', 0.06, i * 90)), + /** Something alarming (penalty, bomb, 덤터기). */ + alarm: () => [880, 660, 880, 660].forEach((f, i) => tone(f, 110, 'square', 0.05, i * 120)), +}; + export const sounds = { - stone: () => tone(900, 70, 'triangle', 0.25), + stone: sfx.stone, myTurn: () => { tone(660, 120); tone(880, 160, 'sine', 0.15, 120); diff --git a/apps/web/src/net/api.ts b/apps/web/src/net/api.ts index 0a54b17..e25170f 100644 --- a/apps/web/src/net/api.ts +++ b/apps/web/src/net/api.ts @@ -30,6 +30,8 @@ export interface Settings { fontScale?: 'normal' | 'large' | 'xlarge'; theme?: 'auto' | 'light' | 'dark'; sound?: boolean; + /** Site volume 0~100 (this device only). */ + volume?: number; vibrate?: boolean; colorBlind?: boolean; tapConfirm?: boolean; diff --git a/apps/web/src/store/session.ts b/apps/web/src/store/session.ts index 5645622..5497d59 100644 --- a/apps/web/src/store/session.ts +++ b/apps/web/src/store/session.ts @@ -10,6 +10,7 @@ export const DEFAULT_SETTINGS: Required = { fontScale: 'normal', theme: 'auto', sound: true, + volume: 50, vibrate: true, colorBlind: false, // Two-step placing is the default only on touch devices; on PC one click places (docs/07 §3.5). @@ -17,7 +18,7 @@ export const DEFAULT_SETTINGS: Required = { }; /** Settings that belong to this device and are never synced to the account. */ -const DEVICE_ONLY = ['tapConfirm'] as const; +const DEVICE_ONLY = ['tapConfirm', 'volume'] as const; const withoutDeviceOnly = (s: Settings): Settings => { const out: Record = { ...s }; for (const k of DEVICE_ONLY) delete out[k]; diff --git a/apps/web/src/styles/global.css b/apps/web/src/styles/global.css index 9dd6da4..d2d05cb 100644 --- a/apps/web/src/styles/global.css +++ b/apps/web/src/styles/global.css @@ -1633,3 +1633,65 @@ button.header-user { .bot-dots::after { content: '…'; display: inline-block; width: 1.2em; animation: bot-dots 1.2s steps(4, end) infinite; overflow: hidden; vertical-align: bottom; } @keyframes bot-dots { 0% { width: 0; } 100% { width: 1.2em; } } @media (prefers-reduced-motion: reduce) { .bot-dots::after { animation: none; } } + +/* ---------- shared motion kit (2026-10-13) ---------- + * Small, game-agnostic animations. Put the class on an element when it appears (React key change replays it). + * `--anim-delay` staggers (e.g. dealing), `--from-x/--from-y` say where a dealt card comes from. + * prefers-reduced-motion already shortens every animation to nothing (see the rule near the top). */ +.anim-deal { animation: anim-deal 0.38s cubic-bezier(0.2, 0.8, 0.3, 1.15) both; animation-delay: var(--anim-delay, 0ms); } +@keyframes anim-deal { + from { opacity: 0; transform: translate(var(--from-x, 0), var(--from-y, -60px)) rotate(-12deg) scale(0.7); } +} +.anim-flip { animation: anim-flip 0.42s ease both; animation-delay: var(--anim-delay, 0ms); backface-visibility: hidden; } +@keyframes anim-flip { + 0% { transform: perspective(600px) rotateY(90deg); } + 60% { transform: perspective(600px) rotateY(-12deg); } + 100% { transform: perspective(600px) rotateY(0); } +} +.anim-pop { animation: anim-pop 0.32s cubic-bezier(0.3, 1.6, 0.5, 1) both; animation-delay: var(--anim-delay, 0ms); } +@keyframes anim-pop { + from { transform: scale(0.4); opacity: 0; } +} +.anim-drop { animation: anim-drop 0.45s cubic-bezier(0.5, 0, 0.75, 0) both, anim-bounce 0.25s ease-out 0.45s; } +@keyframes anim-drop { + from { transform: translateY(var(--drop-from, -240px)); } +} +@keyframes anim-bounce { + 40% { transform: translateY(-8%); } +} +.anim-shake { animation: anim-shake 0.45s ease both; } +@keyframes anim-shake { + 20%, 60% { transform: translateX(-6px) rotate(-2deg); } + 40%, 80% { transform: translateX(6px) rotate(2deg); } +} +.anim-slide-up { animation: anim-slide-up 0.3s ease-out both; animation-delay: var(--anim-delay, 0ms); } +@keyframes anim-slide-up { + from { transform: translateY(16px); opacity: 0; } +} +.anim-fade { animation: fade 0.25s ease both; } +/* Pulsing highlight for "your turn" / the thing to press. */ +.anim-glow { animation: anim-glow 1.4s ease-in-out infinite; } +@keyframes anim-glow { + 50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 45%, transparent), 0 0 18px color-mix(in srgb, var(--primary) 55%, transparent); } +} +/* A score / count that just changed. */ +.anim-bump { animation: anim-bump 0.35s ease both; } +@keyframes anim-bump { + 40% { transform: scale(1.35); color: var(--primary); } +} +/* Dice tumbling before showing the face. */ +.anim-roll { animation: anim-roll 0.6s cubic-bezier(0.2, 0.7, 0.3, 1) both; } +@keyframes anim-roll { + 0% { transform: translateY(-30px) rotate(-200deg) scale(0.8); } + 70% { transform: translateY(4px) rotate(15deg); } + 100% { transform: none; } +} +/* Big centered banner (땡!, 우노!, 승리 …): shown briefly over the board. */ +.anim-banner { animation: anim-banner 1.6s ease both; pointer-events: none; } +@keyframes anim-banner { + 0% { transform: scale(0.3); opacity: 0; } + 15% { transform: scale(1.15); opacity: 1; } + 25% { transform: scale(1); } + 80% { opacity: 1; } + 100% { transform: scale(1.05); opacity: 0; } +}