196 lines
6.6 KiB
TypeScript
196 lines
6.6 KiB
TypeScript
/**
|
|
* 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;
|
|
|
|
/** 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();
|
|
if (!master) {
|
|
master = ctx.createGain();
|
|
master.connect(ctx.destination);
|
|
}
|
|
master.gain.value = v;
|
|
return { a: ctx, out: master };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
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.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(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: sfx.stone,
|
|
myTurn: () => {
|
|
tone(660, 120);
|
|
tone(880, 160, 'sine', 0.15, 120);
|
|
},
|
|
warn: () => tone(520, 90, 'square', 0.06),
|
|
win: () => [523, 659, 784, 1046].forEach((f, i) => tone(f, 180, 'triangle', 0.18, i * 120)),
|
|
lose: () => [392, 330].forEach((f, i) => tone(f, 220, 'sine', 0.15, i * 180)),
|
|
/** Bell "땡!" (할리갈리 correct bell). */
|
|
ding: () => {
|
|
tone(1568, 700, 'sine', 0.22);
|
|
tone(2350, 450, 'sine', 0.08);
|
|
},
|
|
/** Buzzer "삐!" (wrong answer). */
|
|
buzz: () => tone(180, 260, 'square', 0.08),
|
|
};
|
|
|
|
export function haptic(pattern: number | number[]): void {
|
|
if (!useSession.getState().settings.vibrate) return;
|
|
try {
|
|
navigator.vibrate?.(pattern);
|
|
} catch {}
|
|
}
|