- site/server: Discord OAuth2 + 봇 토큰 멤버 확인, 세션, 1회용 등록 코드·기기 키 인증, 게임 PC WebSocket 중계(JSON·음성 바이너리), 15초 무응답 잠금, 게임 이미지 캐시, 관리 CLI - site/web: 타임라인·따라 읽기 재생(단어 강조·이어듣기·현장 모드·재생 게인)·필터·검색·실시간·교정, 발로란트/롤 경기 상세, 잠금 화면(관리자 등록 코드), 모바일, 효과음 - deploy/site: Dockerfile·compose·deploy.sh·make-env.sh (.5, 포트 2972) - 앱 코어: 사이트 필터 묶음(킬·라운드)과 유저 필터 규칙 맞춤, hello 에 서버 ID Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
110 lines
3.9 KiB
JavaScript
110 lines
3.9 KiB
JavaScript
// 목 음성: 단어 타이밍(words s/e)에 맞춰 음절마다 모음 비슷한 소리를 합성한 16bit WAV.
|
|
// 실제 앱은 Ogg Opus 를 주지만 브라우저 입장에서는 둘 다 <audio> 로 재생된다.
|
|
|
|
const SR = 22050;
|
|
const VOWELS = [
|
|
[800, 1200], [500, 1900], [300, 2300], [500, 900], [350, 800], [650, 1600],
|
|
];
|
|
const PITCH = { '310482915522101248': 118, '310482915522101249': 104, '310482915522101250': 205, '310482915522101251': 140 };
|
|
|
|
function hash(str) {
|
|
let h = 2166136261;
|
|
for (let i = 0; i < str.length; i++) h = Math.imul(h ^ str.charCodeAt(i), 16777619);
|
|
return h >>> 0;
|
|
}
|
|
|
|
/** @param {any} utt UtteranceItem */
|
|
export function synthWav(utt) {
|
|
const dur = utt.audio.durationMs / 1000;
|
|
const n = Math.ceil(dur * SR);
|
|
const out = new Float32Array(n);
|
|
// 원래 음량: 게인이 클수록(조용한 사람) 작게 녹음된 것
|
|
const amp = 0.45 * Math.pow(10, -utt.audio.gainDb / 20);
|
|
const f0base = PITCH[utt.userId] ?? 130;
|
|
let seed = hash(utt.id);
|
|
const rand = () => {
|
|
seed = (seed * 1664525 + 1013904223) >>> 0;
|
|
return seed / 4294967296;
|
|
};
|
|
if (utt.noise || !utt.words.length) {
|
|
// 잡음 / 처리 중: 짧은 숨소리·키보드 같은 잡음 덩어리
|
|
const bursts = utt.noise ? 5 : 0;
|
|
if (!bursts) {
|
|
// 처리 중 발화도 소리는 있다 (텍스트만 없음) → 음절 몇 개
|
|
const syl = Math.max(3, Math.round(dur * 4));
|
|
return encode(voice(out, [{ s: 0.1, e: dur - 0.1, syl }], f0base, amp, rand));
|
|
}
|
|
for (let b = 0; b < bursts; b++) {
|
|
const st = Math.floor(rand() * n * 0.8);
|
|
const len = Math.floor(SR * (0.04 + rand() * 0.12));
|
|
let lp = 0;
|
|
for (let i = 0; i < len && st + i < n; i++) {
|
|
lp += (rand() * 2 - 1 - lp) * 0.3;
|
|
const env = Math.sin((Math.PI * i) / len);
|
|
out[st + i] += lp * env * amp * 0.9;
|
|
}
|
|
}
|
|
return encode(out);
|
|
}
|
|
const segs = utt.words.map((w) => ({ s: w.s, e: w.e, syl: Math.max(1, [...w.w].length) }));
|
|
return encode(voice(out, segs, f0base, amp, rand));
|
|
}
|
|
|
|
function voice(out, segs, f0base, amp, rand) {
|
|
let phase = 0;
|
|
for (const seg of segs) {
|
|
const s0 = Math.floor(seg.s * SR);
|
|
const s1 = Math.min(out.length, Math.floor(seg.e * SR));
|
|
const total = s1 - s0;
|
|
if (total <= 0) continue;
|
|
const sylLen = total / seg.syl;
|
|
for (let k = 0; k < seg.syl; k++) {
|
|
const [F1, F2] = VOWELS[Math.floor(rand() * VOWELS.length)];
|
|
const a = s0 + Math.floor(k * sylLen);
|
|
const b = s0 + Math.floor((k + 1) * sylLen);
|
|
const pitchStart = f0base * (0.95 + rand() * 0.15);
|
|
const pitchEnd = pitchStart * (0.9 + rand() * 0.15);
|
|
const loud = 0.7 + rand() * 0.3;
|
|
for (let i = a; i < b; i++) {
|
|
const x = (i - a) / (b - a);
|
|
const f0 = pitchStart + (pitchEnd - pitchStart) * x;
|
|
phase += (2 * Math.PI * f0) / SR;
|
|
// 음절 엔벌로프: 빠른 어택, 부드러운 릴리즈
|
|
const env = Math.min(1, x / 0.12) * Math.min(1, (1 - x) / 0.3) * loud;
|
|
let v = 0;
|
|
for (let h = 1; h * f0 < 3800; h++) {
|
|
const f = h * f0;
|
|
const r1 = 1 / (1 + ((f - F1) / 140) ** 2);
|
|
const r2 = 0.6 / (1 + ((f - F2) / 200) ** 2);
|
|
v += (r1 + r2 + 0.02) * Math.sin(h * phase) / Math.sqrt(h);
|
|
}
|
|
out[i] += v * env * amp * 0.55;
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function encode(samples) {
|
|
const n = samples.length;
|
|
const buf = Buffer.alloc(44 + n * 2);
|
|
buf.write('RIFF', 0);
|
|
buf.writeUInt32LE(36 + n * 2, 4);
|
|
buf.write('WAVE', 8);
|
|
buf.write('fmt ', 12);
|
|
buf.writeUInt32LE(16, 16);
|
|
buf.writeUInt16LE(1, 20);
|
|
buf.writeUInt16LE(1, 22);
|
|
buf.writeUInt32LE(SR, 24);
|
|
buf.writeUInt32LE(SR * 2, 28);
|
|
buf.writeUInt16LE(2, 32);
|
|
buf.writeUInt16LE(16, 34);
|
|
buf.write('data', 36);
|
|
buf.writeUInt32LE(n * 2, 40);
|
|
for (let i = 0; i < n; i++) {
|
|
const v = Math.max(-1, Math.min(1, samples[i]));
|
|
buf.writeInt16LE(Math.round(v * 32767), 44 + i * 2);
|
|
}
|
|
return buf;
|
|
}
|