From 37c14408922d29ffe895ddff7127551c3d5407e6 Mon Sep 17 00:00:00 2001 From: EJClaw Date: Mon, 19 Oct 2026 23:41:51 +0900 Subject: [PATCH] =?UTF-8?q?=EC=86=8C=EB=A6=AC=20=ED=81=AC=EA=B8=B0=20?= =?UTF-8?q?=EB=A7=9E=EC=B6=94=EA=B8=B0(=EB=85=B8=EB=A9=80=EB=9D=BC?= =?UTF-8?q?=EC=9D=B4=EC=A6=88):=20=EA=B3=A1=EB=A7=88=EB=8B=A4=20=EC=9C=A0?= =?UTF-8?q?=ED=8A=9C=EB=B8=8C=EA=B0=80=20=EC=9E=B0=20=ED=81=AC=EA=B8=B0(?= =?UTF-8?q?=EC=98=81=EC=83=81=20=ED=8E=98=EC=9D=B4=EC=A7=80=20audioConfig?= =?UTF-8?q?=20LUFS)=EB=A5=BC=20=EC=A0=80=EC=9E=A5=ED=95=98=EA=B3=A0,=20?= =?UTF-8?q?=EC=9E=AC=EC=83=9D=20=EB=B3=BC=EB=A5=A8=20=3D=20=EB=82=B4=20?= =?UTF-8?q?=EB=B3=BC=EB=A5=A8=20=C3=97=20=EA=B3=A1=20=EB=B3=B4=EC=A0=95?= =?UTF-8?q?=EA=B0=92=EC=9C=BC=EB=A1=9C=20-18=20LUFS=EC=97=90=20=EB=A7=9E?= =?UTF-8?q?=EC=B6=A4(=EC=9C=A0=ED=8A=9C=EB=B8=8C=20IFrame=EC=9D=B4=20-14?= =?UTF-8?q?=EB=A1=9C=20=EC=A4=84=EC=9D=B4=EB=8A=94=20=EA=B2=83=20=EC=9C=84?= =?UTF-8?q?=EC=97=90,=20=EC=9E=91=EC=9D=80=20=EA=B3=A1=EC=9D=80=20?= =?UTF-8?q?=EC=B5=9C=EB=8C=80).=20prepare=C2=B7catchup=EC=97=90=20gain,=20?= =?UTF-8?q?=EC=95=88=20=EC=9E=B0=20=EA=B3=A1=EC=9D=80=20=EB=B3=B4=ED=86=B5?= =?UTF-8?q?=20=EA=B3=A1=20=EA=B0=92(0.63).=20=EC=98=81=EC=83=81=EC=9D=B4?= =?UTF-8?q?=20=EB=B0=94=EB=80=8C=EB=A9=B4=20=EB=8B=A4=EC=8B=9C=20=EC=9E=BC?= =?UTF-8?q?(loudness=5Fvideo).=20cli=20loudness=20=EC=9E=91=EC=97=85(?= =?UTF-8?q?=EC=9D=B8=EA=B8=B0=EA=B3=A1=EB=B6=80=ED=84=B0=C2=B7=EC=B0=A8?= =?UTF-8?q?=EB=8B=A8=20=EC=8B=9C=20=EB=A9=88=EC=B6=A4=C2=B7--deadline)?= =?UTF-8?q?=C2=B7=EA=B4=80=EB=A6=AC=EC=9E=90=20=EC=88=98=EC=A7=91=20?= =?UTF-8?q?=ED=99=94=EB=A9=B4,=20heatmap=EB=8F=84=20=EA=B0=99=EC=9D=80=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=EC=97=90=EC=84=9C=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5.=20=EC=8B=A4=EC=A0=9C=20=EC=9C=A0=ED=8A=9C=EB=B8=8C?= =?UTF-8?q?=20e2e=EB=A1=9C=20=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4=20?= =?UTF-8?q?=EB=B3=BC=EB=A5=A8=20=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- apps/server/src/collector/cli.ts | 7 +- apps/server/src/collector/control.ts | 1 + .../fixtures/youtube-audioconfig.txt | 1 + apps/server/src/collector/heatmap.ts | 18 ++-- apps/server/src/collector/loudness.test.ts | 66 ++++++++++++++ apps/server/src/collector/loudness.ts | 86 +++++++++++++++++++ .../server/src/db/migrations/007_loudness.sql | 4 + apps/server/src/game.test.ts | 8 +- apps/server/src/game/engine.ts | 8 +- apps/server/src/songs/quiz.ts | 24 +++++- apps/web/src/net/player.ts | 18 ++-- apps/web/src/net/socket.ts | 4 +- docs/08-youtube-playback.md | 18 ++++ docs/09-deploy-ops.md | 2 +- docs/12-decisions.md | 6 ++ e2e/game.e2e.ts | 19 ++-- packages/shared/src/protocol.ts | 9 +- 17 files changed, 262 insertions(+), 37 deletions(-) create mode 100644 apps/server/src/collector/fixtures/youtube-audioconfig.txt create mode 100644 apps/server/src/collector/loudness.test.ts create mode 100644 apps/server/src/collector/loudness.ts create mode 100644 apps/server/src/db/migrations/007_loudness.sql diff --git a/apps/server/src/collector/cli.ts b/apps/server/src/collector/cli.ts index a2bc52e..aa15cff 100644 --- a/apps/server/src/collector/cli.ts +++ b/apps/server/src/collector/cli.ts @@ -6,6 +6,7 @@ * bun apps/server/src/collector/cli.ts charts [--from YYYY] [--to YYYY] [--sources melon-ye,genie-ye,tj-ye,melon-rt,bugs-rt] [--refetch] * bun apps/server/src/collector/cli.ts melon-detail [--limit N] * bun apps/server/src/collector/cli.ts heatmap [--limit N] + * bun apps/server/src/collector/cli.ts loudness [--limit N] [--delay ms] [--deadline epochMs] (소리 크기: YouTube's LUFS per matched video, most popular level first) * bun apps/server/src/collector/cli.ts levels * bun apps/server/src/collector/cli.ts fix-text (one-off: decode `'`/`&` saved in titles, let 원곡 찾기 retry them) * bun apps/server/src/collector/cli.ts stats @@ -21,6 +22,7 @@ import { catalogStats } from '../songs/store'; import { currentJob } from './catalog'; import { collectCharts, fillMelonDetails } from './charts'; import { collectHeatmaps } from './heatmap'; +import { collectLoudness } from './loudness'; import { computeLevels } from './levels'; import { fixEntities } from './text'; @@ -64,6 +66,9 @@ switch (cmd) { case 'heatmap': console.log(await collectHeatmaps(db, { limit: flag('limit'), delayMs: flag('delay') })); break; + case 'loudness': + console.log(await collectLoudness(db, { limit: flag('limit'), delayMs: flag('delay'), deadline: flag('deadline') })); + break; case 'levels': console.log(computeLevels(db)); break; @@ -74,7 +79,7 @@ switch (cmd) { console.log(JSON.stringify(catalogStats(db), null, 1)); break; default: - console.log('usage: cli.ts import-norabose|import-sta|match-youtube|charts|melon-detail|heatmap|levels|fix-text|stats'); + console.log('usage: cli.ts import-norabose|import-sta|match-youtube|charts|melon-detail|heatmap|loudness|levels|fix-text|stats'); process.exitCode = 1; } db.close(); diff --git a/apps/server/src/collector/control.ts b/apps/server/src/collector/control.ts index 471217d..91f65a0 100644 --- a/apps/server/src/collector/control.ts +++ b/apps/server/src/collector/control.ts @@ -12,6 +12,7 @@ export const COLLECT_KINDS: CollectControl['kinds'] = [ { id: 'charts', label: '차트 모으기 (멜론·지니 연도별, TJ 노래방, 멜론·벅스 실시간)' }, { id: 'melon-detail', label: '멜론 곡 정보 채우기 (발매일·장르·가사)', limit: true }, { id: 'heatmap', label: '하이라이트 찾기 (유튜브 많이 다시 본 구간)', limit: true }, + { id: 'loudness', label: '소리 크기 재기 (유튜브가 잰 곡별 크기)', limit: true }, { id: 'levels', label: '난이도 다시 계산' }, ]; diff --git a/apps/server/src/collector/fixtures/youtube-audioconfig.txt b/apps/server/src/collector/fixtures/youtube-audioconfig.txt new file mode 100644 index 0000000..8aacd69 --- /dev/null +++ b/apps/server/src/collector/fixtures/youtube-audioconfig.txt @@ -0,0 +1 @@ +rity":4},{"label":"3.0","value":300,"isPremiumUpsell":true,"priority":1}]},"vssClientConfig":{"vssUsePostRequest":true},"audioConfig":{"loudnessDb":3.5799999,"perceptualLoudnessDb":-10.42,"enablePerFormatLoudness":true,"trackAbsoluteLoudnessLkfs":-10.42,"loudnessTargetLkfs":-14,"loudnessNormalizationConfig":{"applyStatefulNormalization":false,"preserveStatefulLoudnessTarget":true,"maxStatefulTimeThresholdSec":300,"minimumLoudnessTargetLkfs":-31}} diff --git a/apps/server/src/collector/heatmap.ts b/apps/server/src/collector/heatmap.ts index c3d5a9d..96cedb7 100644 --- a/apps/server/src/collector/heatmap.ts +++ b/apps/server/src/collector/heatmap.ts @@ -5,7 +5,8 @@ */ import type { Database } from 'bun:sqlite'; import { Catalog, Job, sleep } from './catalog'; -import { BROWSER_UA, Blocked } from './charts'; +import { Blocked } from './charts'; +import { fetchWatchPage, parseLoudness, saveLoudness } from './loudness'; export interface Marker { startMs: number; @@ -50,22 +51,17 @@ export async function collectHeatmaps(db: Database, opts: { limit?: number; dela ORDER BY s.id LIMIT ?`, ) .all(Date.now() - 30 * 86400_000, opts.limit ?? 500); - const fetchPage = - opts.fetchPage ?? - (async (id: string) => { - const res = await fetch(`https://www.youtube.com/watch?v=${id}&hl=ko`, { headers: { 'user-agent': BROWSER_UA, 'accept-language': 'ko-KR,ko;q=0.9' }, signal: AbortSignal.timeout(20_000) }); - if (res.status === 429 || res.status === 403) throw new Blocked(`youtube ${res.status}`); - const text = await res.text(); - if (/consent\.youtube\.com|google\.com\/sorry/.test(res.url) || (/g-recaptcha|unusual traffic/i.test(text) && text.length < 200_000)) throw new Blocked('youtube bot check'); - return text; - }); + const fetchPage = opts.fetchPage ?? fetchWatchPage; const job = new Job(db, 'heatmap', rows.length); const cat = new Catalog(db); const stats = { set: 0, none: 0, failed: 0 }; try { for (const [i, s] of rows.entries()) { try { - const sec = pickHighlight(parseHeatmap(await fetchPage(s.youtube_id))); + const page = await fetchPage(s.youtube_id); + // same page: keep its loudness too, so the loudness job can skip this song + saveLoudness(db, s.id, s.youtube_id, parseLoudness(page)); + const sec = pickHighlight(parseHeatmap(page)); if (sec !== null) { db.query("UPDATE songs SET highlight_sec = ?, highlight_source = 'heatmap', updated_at = ? WHERE id = ? AND COALESCE(highlight_source, '') <> 'admin'").run(sec, Date.now(), s.id); stats.set++; diff --git a/apps/server/src/collector/loudness.test.ts b/apps/server/src/collector/loudness.test.ts new file mode 100644 index 0000000..724e529 --- /dev/null +++ b/apps/server/src/collector/loudness.test.ts @@ -0,0 +1,66 @@ +import { describe, expect, test } from 'bun:test'; +import { join } from 'node:path'; +import { openDb } from '../db'; +import { loudnessGain, quizSong } from '../songs/quiz'; +import { tempDbPath } from '../test-utils'; +import { Catalog } from './catalog'; +import { Blocked } from './charts'; +import { collectHeatmaps } from './heatmap'; +import { collectLoudness, parseLoudness } from './loudness'; + +const fx = (f: string) => Bun.file(join(import.meta.dir, 'fixtures', f)).text(); +const page = (lufs: number) => `..."audioConfig":{"loudnessDb":${lufs + 14},"perceptualLoudnessDb":${lufs},"enablePerFormatLoudness":true,"trackAbsoluteLoudnessLkfs":${lufs},"loudnessTargetLkfs":-14}...`; + +describe('소리 크기 맞추기', () => { + test('reads YouTube\'s loudness from a real watch page (광 Topic: -10.42 LUFS)', async () => { + expect(parseLoudness(await fx('youtube-audioconfig.txt'))).toBe(-10.42); + expect(parseLoudness('"audioConfig":{"loudnessDb":3.58,"perceptualLoudnessDb":-10.4}')).toBe(-10.4); + expect(parseLoudness('no player')).toBeNull(); + }); + + test('gain: loud songs go down to -18 LUFS (on top of YouTube\'s own -14), quiet ones play at full volume', () => { + // YouTube already plays anything louder at -14, so -6 and -14 sound alike: both need -4 dB more + expect(loudnessGain(-6)).toBe(loudnessGain(-14)); + expect(loudnessGain(-14)).toBeCloseTo(10 ** (-4 / 20), 3); + expect(loudnessGain(-16)).toBeCloseTo(10 ** (-2 / 20), 3); + expect(loudnessGain(-18)).toBe(1); + expect(loudnessGain(-25)).toBe(1); + // not measured: like a typical (loud) song + expect(loudnessGain(null)).toBe(loudnessGain(-14)); + }); + + test('stores the value per video, popular songs first; a value for an older video is ignored', async () => { + const db = openDb(tempDbPath()); + const c = new Catalog(db); + const [a, b, d] = ['a', 'b', 'd'].map((t) => c.upsert({ title: t, artists: [{ name: 'x' }], source: 't', ref: t })[0]!); + db.query("UPDATE songs SET youtube_status = 'matched', youtube_id = 'v' || id").run(); + c.autoLevel(b!, 'all', 1); + const asked: string[] = []; + const lufs: Record = { [`v${a}`]: -9, [`v${b}`]: -20 }; + const r = await collectLoudness(db, { delayMs: 0, fetchPage: async (id) => (asked.push(id), id in lufs ? page(lufs[id]!) : 'no config') }); + expect(r).toEqual({ set: 2, none: 1, failed: 0 }); + expect(asked[0]).toBe(`v${b}`); + expect(quizSong(db, a!)!.loudness).toBe(-9); + expect(quizSong(db, b!)!.loudness).toBe(-20); + expect(quizSong(db, d!)!.loudness).toBeNull(); + // nothing left to do (the miss is remembered) + expect((await collectLoudness(db, { delayMs: 0, fetchPage: async () => page(-9) })).set).toBe(0); + // the admin picks another video: the old number no longer applies, and the song is measured again + db.query("UPDATE songs SET youtube_id = 'new' WHERE id = ?").run(a!); + expect(quizSong(db, a!)!.loudness).toBeNull(); + expect(await collectLoudness(db, { delayMs: 0, fetchPage: async () => page(-12) })).toEqual({ set: 1, none: 0, failed: 0 }); + expect(quizSong(db, a!)!.loudness).toBe(-12); + }); + + test('stops at a block signal; the heatmap job keeps the loudness of the page it already fetched', async () => { + const db = openDb(tempDbPath()); + const c = new Catalog(db); + const [a] = ['a', 'b'].map((t) => c.upsert({ title: t, artists: [{ name: 'x' }], source: 't', ref: t })[0]!); + db.query("UPDATE songs SET youtube_status = 'matched', youtube_id = 'v' || id").run(); + let calls = 0; + expect(await collectLoudness(db, { delayMs: 0, fetchPage: async () => (calls++, Promise.reject(new Blocked('youtube 429'))) })).toEqual({ set: 0, none: 0, failed: 1 }); + expect(calls).toBe(1); + await collectHeatmaps(db, { delayMs: 0, fetchPage: async () => page(-15) }); + expect(quizSong(db, a!)!.loudness).toBe(-15); + }); +}); diff --git a/apps/server/src/collector/loudness.ts b/apps/server/src/collector/loudness.ts new file mode 100644 index 0000000..7a37b84 --- /dev/null +++ b/apps/server/src/collector/loudness.ts @@ -0,0 +1,86 @@ +/** + * 소리 크기 맞추기: YouTube measures every video's loudness (LUFS) and puts it in the watch page's audioConfig — the same + * number its own player uses to turn loud videos down to -14 LUFS. We keep it per song (tied to the video it was read from); + * the game turns louder songs down further so most songs sound alike (songs/quiz.ts loudnessGain). + * One page per song, a few seconds apart, most popular level first, stops at the first block signal. + */ +import type { Database } from 'bun:sqlite'; +import { Catalog, Job, sleep } from './catalog'; +import { BROWSER_UA, Blocked } from './charts'; + +/** The watch page (heatmap markers, audioConfig loudness); throws Blocked on a block signal. */ +export async function fetchWatchPage(id: string): Promise { + const res = await fetch(`https://www.youtube.com/watch?v=${id}&hl=ko`, { headers: { 'user-agent': BROWSER_UA, 'accept-language': 'ko-KR,ko;q=0.9' }, signal: AbortSignal.timeout(20_000) }); + if (res.status === 429 || res.status === 403) throw new Blocked(`youtube ${res.status}`); + const text = await res.text(); + if (/consent\.youtube\.com|google\.com\/sorry/.test(res.url) || (/g-recaptcha|unusual traffic/i.test(text) && text.length < 200_000)) throw new Blocked('youtube bot check'); + return text; +} + +/** Integrated loudness (LUFS) of the video from the watch page, or null when the page has none. */ +export function parseLoudness(html: string): number | null { + const cfg = /"audioConfig":\{([^}]*)/.exec(html)?.[1]; + if (!cfg) return null; + const num = (k: string) => { + const m = new RegExp(`"${k}":(-?[\\d.]+(?:[eE][-+]?\\d+)?)`).exec(cfg); + return m ? Number(m[1]) : null; + }; + const v = num('trackAbsoluteLoudnessLkfs') ?? num('perceptualLoudnessDb'); + return v !== null && Number.isFinite(v) && v > -70 && v < 10 ? Math.round(v * 100) / 100 : null; +} + +/** Store a measured value only while the song still plays that video. */ +export function saveLoudness(db: Database, songId: number, videoId: string, lufs: number | null): boolean { + if (lufs === null) return false; + return db.query('UPDATE songs SET loudness_lufs = ?, loudness_video = ? WHERE id = ? AND youtube_id = ?').run(lufs, videoId, songId, videoId).changes === 1; +} + +export async function collectLoudness( + db: Database, + opts: { limit?: number; delayMs?: number; deadline?: number; fetchPage?: (id: string) => Promise } = {}, +) { + const rows = db + .query<{ id: number; youtube_id: string }, [number, number]>( + `SELECT s.id, s.youtube_id FROM songs s + LEFT JOIN song_levels l ON l.song_id = s.id AND l.scope = 'all' + WHERE s.deleted_at IS NULL AND s.youtube_status = 'matched' AND s.youtube_id IS NOT NULL + AND COALESCE(s.loudness_video, '') <> s.youtube_id + AND NOT EXISTS (SELECT 1 FROM signals g WHERE g.song_id = s.id AND g.kind = 'yt_loudness_tried' AND g.value > ?) + ORDER BY l.level IS NULL, l.level, s.id LIMIT ?`, + ) + .all(Date.now() - 30 * 86400_000, opts.limit ?? 1_000_000); + const fetchPage = opts.fetchPage ?? fetchWatchPage; + const job = new Job(db, 'loudness', rows.length); + const cat = new Catalog(db); + const stats = { set: 0, none: 0, failed: 0 }; + job.log(`start ${rows.length} songs${opts.deadline !== undefined ? ` deadline ${new Date(opts.deadline).toISOString()}` : ''}`); + try { + for (const [i, s] of rows.entries()) { + if (opts.deadline !== undefined && Date.now() >= opts.deadline) { + job.log(`deadline reached after ${i}/${rows.length}`); + break; + } + try { + if (saveLoudness(db, s.id, s.youtube_id, parseLoudness(await fetchPage(s.youtube_id)))) stats.set++; + else (stats.none++, cat.signal(s.id, 'yt_loudness_tried', Date.now())); + } catch (e) { + stats.failed++; + if (e instanceof Blocked) { + job.log(`stopped: ${(e as Error).message}`); + break; + } + job.log(`${s.youtube_id} failed: ${(e as Error).message}`); + } + job.progress(i + 1); + if ((i + 1) % 100 === 0) job.log(`loudness ${i + 1}/${rows.length}: set ${stats.set}, no data ${stats.none}`); + await sleep(opts.delayMs ?? 3000); + } + job.log(`loudness done: set ${stats.set}, no data ${stats.none}, failed ${stats.failed}`); + job.finish('done'); + } catch (e) { + job.log(`stopped: ${(e as Error).message}`); + job.finish('failed'); + throw e; + } + return stats; +} diff --git a/apps/server/src/db/migrations/007_loudness.sql b/apps/server/src/db/migrations/007_loudness.sql new file mode 100644 index 0000000..5dafe50 --- /dev/null +++ b/apps/server/src/db/migrations/007_loudness.sql @@ -0,0 +1,4 @@ +-- 소리 크기 맞추기: YouTube's own loudness measurement of the played video (watch page audioConfig, LUFS). +-- Tied to the video it was measured on: a value for an older youtube_id is ignored. +ALTER TABLE songs ADD COLUMN loudness_lufs REAL; +ALTER TABLE songs ADD COLUMN loudness_video TEXT; diff --git a/apps/server/src/game.test.ts b/apps/server/src/game.test.ts index 246e4e9..75fa50e 100644 --- a/apps/server/src/game.test.ts +++ b/apps/server/src/game.test.ts @@ -5,6 +5,7 @@ import { afterEach, describe, expect, test } from 'bun:test'; import type { ChatLine, GameView, ServerMessage } from '@mq/shared'; import { Catalog } from './collector/catalog'; +import { loudnessGain } from './songs/quiz'; import { Client, api, boot, discordLogin, guest, sleep } from './test-utils'; type Msg = Extract; @@ -104,6 +105,8 @@ describe('game flow', () => { expect(pa).toMatchObject({ n: 1, startSec: 0 }); expect(pa.videoId).toMatch(/^vid\d$/); expect(pb.videoId).toBe(pa.videoId); + // 소리 크기 맞추기: not measured yet → a typical song's gain + expect(pa.gain).toBe(loudnessGain(null)); // the room state never carries the video id expect(JSON.stringify(last(a, 'game'))).not.toContain(pa.videoId); a.send({ t: 'audio', n: 1, ev: 'ready' }); @@ -237,7 +240,8 @@ describe('game flow', () => { }); test('새로고침 while the music runs: the new page catches up on the same timeline, nobody else stops, the answer still counts', async () => { - seedLater(() => seed(2)); + // both songs measured (one loud, one quiet), so the gain follows the song + seedLater(() => seed(2).forEach((id, i) => s.db.query('UPDATE songs SET loudness_lufs = ?, loudness_video = youtube_id WHERE id = ?').run([-6, -20][i]!, id))); const { users, cls, code } = await room(['방장', '새로고침']); const [a, b] = cls as [Client, Client]; const stop = autoReady([a, b]); @@ -252,6 +256,8 @@ describe('game flow', () => { const cu = await nextOf(b2, 'catchup', 0); const prep = (await nextOf(a, 'prepare', 0)) as Msg<'prepare'>; expect(cu).toMatchObject({ n: 1, videoId: prep.videoId, clipStart: prep.startSec, startAt: play.startAt, atSec: play.atSec, endSec: play.endSec }); + expect(cu.gain).toBe(prep.gain); + expect(prep.gain).toBe(prep.videoId === 'vid1' ? loudnessGain(-6) : 1); expect(b2.messages.some((m) => m.t === 'prepare' || m.t === 'play')).toBe(false); // the others were never paused or re-prepared, and no "들어왔어요" line for someone who was already here await sleep(60); diff --git a/apps/server/src/game/engine.ts b/apps/server/src/game/engine.ts index f5b0e2a..17dce79 100644 --- a/apps/server/src/game/engine.ts +++ b/apps/server/src/game/engine.ts @@ -8,7 +8,7 @@ * everyone is paused; it keeps running after the clip ends. */ import { hintText, isCorrect, voteThreshold, type GameHistoryItem, type GamePhase, type GameView, type ServerMessage } from '@mq/shared'; -import { clipOf, lyricsCard, mainArtists, markUnplayable, orderSongs, quizSong, type QuizSong } from '../songs/quiz'; +import { clipOf, loudnessGain, lyricsCard, mainArtists, markUnplayable, orderSongs, quizSong, type QuizSong } from '../songs/quiz'; import { poolIds } from '../songs/pool'; import type { Room } from '../rooms/manager'; @@ -206,7 +206,7 @@ export class Game { this.ready.clear(); this.waitingSince.clear(); for (const id of this.active()) this.waitingSince.set(id, now); - this.toActive({ t: 'prepare', code: this.room.code, n: this.n, videoId: this.song!.videoId!, startSec: atSec, endSec: this.clip.endSec }); + this.toActive({ t: 'prepare', code: this.room.code, n: this.n, videoId: this.song!.videoId!, startSec: atSec, endSec: this.clip.endSec, gain: loudnessGain(this.song!.loudness) }); } private checkReady(): void { @@ -515,10 +515,10 @@ export class Game { if (this.awaitingReady) { this.ready.delete(userId); if (!this.waitingSince.has(userId)) this.waitingSince.set(userId, this.now()); - send({ t: 'prepare', code: this.room.code, n: this.n, videoId: this.song.videoId, startSec: this.resumeAt, endSec: this.clip.endSec }); + send({ t: 'prepare', code: this.room.code, n: this.n, videoId: this.song.videoId, startSec: this.resumeAt, endSec: this.clip.endSec, gain: loudnessGain(this.song.loudness) }); this.broadcast(); } else if (this.playing && !this.clipOver) { - send({ t: 'catchup', code: this.room.code, n: this.n, videoId: this.song.videoId, clipStart: this.clip.startSec, startAt: this.audioStartAt, atSec: this.audioAtSec, endSec: this.clip.endSec }); + send({ t: 'catchup', code: this.room.code, n: this.n, videoId: this.song.videoId, clipStart: this.clip.startSec, startAt: this.audioStartAt, atSec: this.audioAtSec, endSec: this.clip.endSec, gain: loudnessGain(this.song.loudness) }); } } diff --git a/apps/server/src/songs/quiz.ts b/apps/server/src/songs/quiz.ts index af20ed0..c925b7d 100644 --- a/apps/server/src/songs/quiz.ts +++ b/apps/server/src/songs/quiz.ts @@ -11,6 +11,8 @@ export interface QuizSong { year: number | null; durationSec: number | null; videoId: string | null; + /** YouTube's loudness of that video (LUFS), null when not measured (collector/loudness.ts). */ + loudness: number | null; highlightSec: number | null; genres: string[]; /** Level shown on the reveal card: the 'all' scope level when there is one. */ @@ -25,9 +27,9 @@ export interface QuizSong { export function quizSong(db: Database, id: number): QuizSong | null { const s = db .query< - { id: number; title: string; artist: string; year: number | null; duration_sec: number | null; youtube_id: string | null; highlight_sec: number | null; youtube_status: string; deleted_at: number | null }, + { id: number; title: string; artist: string; year: number | null; duration_sec: number | null; youtube_id: string | null; highlight_sec: number | null; youtube_status: string; deleted_at: number | null; loudness_lufs: number | null; loudness_video: string | null }, [number] - >('SELECT id, title, artist, year, duration_sec, youtube_id, highlight_sec, youtube_status, deleted_at FROM songs WHERE id = ?') + >('SELECT id, title, artist, year, duration_sec, youtube_id, highlight_sec, youtube_status, deleted_at, loudness_lufs, loudness_video FROM songs WHERE id = ?') .get(id); if (!s || s.deleted_at) return null; const col = (sql: string) => db.query<{ v: T }, [number]>(sql).all(id).map((r) => r.v); @@ -38,6 +40,7 @@ export function quizSong(db: Database, id: number): QuizSong | null { year: s.year, durationSec: s.duration_sec, videoId: s.youtube_status === 'matched' ? s.youtube_id : null, + loudness: s.youtube_id && s.loudness_video === s.youtube_id ? s.loudness_lufs : null, highlightSec: s.highlight_sec, genres: col('SELECT genre_id v FROM song_genres WHERE song_id = ?'), level: db.query<{ level: number }, [number]>("SELECT level FROM song_levels WHERE song_id = ? ORDER BY scope = 'all' DESC LIMIT 1").get(id)?.level ?? null, @@ -74,6 +77,23 @@ export function markUnplayable(db: Database, id: number, code: number): void { ); } +/** YouTube's own player already turns anything louder than this down to it (embedded too; it never turns quiet videos up). */ +export const YOUTUBE_TARGET_LUFS = -14; +/** + * How loud every song should sound. The player's volume can only go down, so this sits near the quiet end: about 96% of + * the catalog (a 60-song sample, 2026-10-19) is at or above it and sounds the same; the quietest old recordings play at full volume. + */ +export const TARGET_LUFS = -18; + +/** + * 소리 크기 맞추기: factor (0..1) for the player volume so this song sounds like TARGET_LUFS — on top of YouTube's own + * turn-down. Not measured yet: the gain of a typical song (louder than YouTube's target). + */ +export function loudnessGain(lufs: number | null): number { + const heard = Math.min(lufs ?? YOUTUBE_TARGET_LUFS, YOUTUBE_TARGET_LUFS); + return Math.round(Math.min(1, 10 ** ((TARGET_LUFS - heard) / 20)) * 1000) / 1000; +} + /** Where playback starts and how long the clip is (docs/04 재생위치, docs/08 하이라이트). */ export function clipOf(song: QuizSong, c: RoomConfig): { startSec: number; endSec: number | null } { const dur = song.durationSec; diff --git a/apps/web/src/net/player.ts b/apps/web/src/net/player.ts index fd9a494..df9186d 100644 --- a/apps/web/src/net/player.ts +++ b/apps/web/src/net/player.ts @@ -222,6 +222,10 @@ let timeline: { startAt: number; atSec: number; endSec: number | null } | null = /** catchUp loaded but this page has no click yet: play along right after "참가 (소리 켜기)". */ let pendingCatchUp: { n: number; tk: number; startAt: number; atSec: number; endSec: number | null } | null = null; +/** 소리 크기 맞추기: the current song's factor from the server (prepare / catchup); the player gets volume × gain. */ +let gain = 1; +const applyVolume = (e: Engine) => e.volume(Math.round(usePlayer.getState().volume * gain)); + const useFake = () => { try { return localStorage.getItem('mq:fakePlayer') === '1'; @@ -307,7 +311,7 @@ export const player = { const release = () => held && held.tk === token && report(held.n, 'ready'); if (useFake()) return void (along ? player.play(along.n, along.startAt, along.atSec, along.endSec) : release()); void waitFor(() => e.state() === 1, 5000, tk).then(async () => { - e.volume(usePlayer.getState().volume); + applyVolume(e); if (tk !== token || timeline) return; // Came back mid-question: join the music where everyone is now. if (along) return player.play(along.n, along.startAt, along.atSec, along.endSec); @@ -321,9 +325,10 @@ export const player = { }); }, - async prepare(n: number, videoId: string, startSec: number, endSec: number | null): Promise { + async prepare(n: number, videoId: string, startSec: number, endSec: number | null, g = 1): Promise { const tk = ++token; curN = n; + gain = g; stopLoop(); timeline = null; pendingReady = null; @@ -367,7 +372,7 @@ export const player = { usePlayer.setState({ clip: { start: usePlayer.getState().clip?.start ?? atSec, end: endSec }, pos: atSec }); const go = () => { if (tk !== token) return; - e.volume(usePlayer.getState().volume); + applyVolume(e); e.mute(false); // On time this is atSec; joining a running timeline (catchUp, a late start) it is where everyone is now. const at = atSec + Math.max(0, now() - startAt) / 1000; @@ -407,11 +412,12 @@ export const player = { }, /** Back in a question whose music is running (refresh / reconnect): load muted where everyone is, then play along. */ - async catchUp(n: number, videoId: string, clipStart: number, startAt: number, atSec: number, endSec: number | null): Promise { + async catchUp(n: number, videoId: string, clipStart: number, startAt: number, atSec: number, endSec: number | null, g = 1): Promise { const at = () => atSec + Math.max(0, now() - startAt) / 1000; if (endSec !== null && at() >= endSec) return; // the clip is over: nothing to hear until the next question const tk = ++token; curN = n; + gain = g; stopLoop(); timeline = null; pendingReady = null; @@ -457,7 +463,7 @@ export const player = { timeline = null; const e = engine; if (!e || !e.videoId() || e.videoId() === UNLOCK_VIDEO || usePlayer.getState().needsGesture) return; - e.volume(usePlayer.getState().volume); + applyVolume(e); e.mute(false); e.play(); usePlayer.setState({ playing: true, loading: false }); @@ -481,7 +487,7 @@ export const player = { } catch { /* private mode */ } - engine?.volume(vol); + if (engine) applyVolume(engine); }, time(): number { diff --git a/apps/web/src/net/socket.ts b/apps/web/src/net/socket.ts index 5a9f8b4..95c96bf 100644 --- a/apps/web/src/net/socket.ts +++ b/apps/web/src/net/socket.ts @@ -128,7 +128,7 @@ function onMessage(m: ServerMessage): void { return; } case 'prepare': - if (m.code === useRoom.getState().code) void player.prepare(m.n, m.videoId, m.startSec, m.endSec); + if (m.code === useRoom.getState().code) void player.prepare(m.n, m.videoId, m.startSec, m.endSec, m.gain); return; case 'play': if (m.code === useRoom.getState().code) player.play(m.n, m.startAt, m.atSec, m.endSec); @@ -137,7 +137,7 @@ function onMessage(m: ServerMessage): void { if (m.code === useRoom.getState().code) player.pause(); return; case 'catchup': - if (m.code === useRoom.getState().code) void player.catchUp(m.n, m.videoId, m.clipStart, m.startAt, m.atSec, m.endSec); + if (m.code === useRoom.getState().code) void player.catchUp(m.n, m.videoId, m.clipStart, m.startAt, m.atSec, m.endSec, m.gain); return; case 'typing': if (m.code === useRoom.getState().code) useRoom.setState({ typing: m.ids }); diff --git a/docs/08-youtube-playback.md b/docs/08-youtube-playback.md index 0627106..6d0e475 100644 --- a/docs/08-youtube-playback.md +++ b/docs/08-youtube-playback.md @@ -73,6 +73,24 @@ - 우선순위: 관리자 지정 > heatmap > 음원 분석 > 30% 규칙 - "듣고맞출시간"이 하이라이트 이후 남은 길이보다 길면 남은 곳까지만 +## 3-1. 소리 크기 맞추기 (2026-10-19) +곡마다 소리 크기가 달라서(옛날 녹음·라이브는 작고 요즘 곡은 큼) 재생할 때 볼륨을 곡마다 자동으로 바꾼다. + +직접 확인한 사실 (2026-10-19) +| 시험 | 결과 | +|---|---| +| 유튜브 영상 페이지 `audioConfig` | 영상마다 유튜브가 잰 크기가 들어 있음: `trackAbsoluteLoudnessLkfs`(LUFS), `loudnessTargetLkfs: -14`. 오디오를 받을 필요 없음 | +| 퍼가기(IFrame) 플레이어, 실제 크롬 | -14보다 큰 곡은 유튜브가 직접 -14로 줄임(광 -10.4 → 66%, -5.4 곡 → 37%). 작은 곡(-21.8)은 키우지 않음(100%). `setVolume`은 이 위에 곱해지고 소수점은 버림 | +| 매칭된 곡 무작위 60곡 | 87%가 -14보다 큼(유튜브가 이미 같은 크기로 맞춤). 작은 곡은 거의 80년대 녹음, 가장 작은 곡 -21.8, -18 아래는 약 4% | + +방식 +- 기준 크기 **-18 LUFS**(`songs/quiz.ts` `TARGET_LUFS`). 볼륨은 줄이는 것만 되므로 작은 쪽에 맞춘다. 곡의 약 96%가 같은 크기로 들리고, 이보다 작은 옛날 곡 몇 개만 최대 볼륨으로 조금 작게 들린다 +- 보정값 = min(1, 10^((-18 − min(곡 크기, −14)) / 20)). 실제 볼륨 = 내 볼륨 슬라이더 × 보정값. 서버가 `prepare`·`catchup`에 `gain`으로 보낸다(정답 공개 중 이어 듣기도 같은 값) +- 아직 안 잰 곡은 보통 곡(-14 이상)과 같은 값(0.63) +- 곡 크기는 `songs.loudness_lufs`에 저장하고, 잰 영상 id(`loudness_video`)가 지금 영상과 다르면 쓰지 않는다(관리자가 영상을 바꾸면 다시 잼) +- 모으기: `cli.ts loudness`(인기곡부터, 곡당 영상 페이지 1번, 3초 간격, 차단 신호면 멈춤). 하이라이트 찾기(heatmap)도 같은 페이지에서 크기를 함께 저장. 관리자 "수집" 화면에서도 실행 가능 +- 한계: 곡 전체 평균 크기라서 한 곡 안의 차이(조용한 인트로와 후렴)는 그대로다 + ## 4. 가사 (가사보고 맞추기) - 멜론 곡 상세 페이지의 가사를 수집해 DB에 저장(가사 없는 곡은 이 모드에서 제외) - 출제할 때 보여줄 부분: 후렴 아닌 1절 가운데 4줄 → 시간이 지나거나 힌트를 쓰면 2줄씩 추가 diff --git a/docs/09-deploy-ops.md b/docs/09-deploy-ops.md index e5185a6..6cdd45d 100644 --- a/docs/09-deploy-ops.md +++ b/docs/09-deploy-ops.md @@ -47,6 +47,6 @@ - .9 `/home/claude/projects/music_quiz` (gitea main 클론), 컨테이너 `music_quiz-app-1`, 볼륨 `music_quiz_app-data`, `deploy/.env` 권한 600 - NPMplus 프록시 호스트 id 14 → `192.168.10.9:2974`, 고급 설정은 `deploy/npmplus-advanced.conf`, 인증서는 `deploy/cert.sh`(다시 돌려도 안전) - 백업(매일 `VACUUM INTO`)은 노래 DB가 생기는 3단계에서 붙인다 -- 수집 cron(.9 crontab, 컨테이너 안에서 `docker exec`): 02:00 차트, 02:20 멜론 곡 정보 1,000곡, 03:15 하이라이트 400곡(4초 간격), 03:55 난이도, 08:10 유튜브 원곡 찾기 3,000곡(04:00~08:00은 봇이 Claude를 부르지 않는 시간이라 피함). 로그 `~/music_quiz-collect.log`, `~/music_quiz-match.log`. 관리자 "수집" 화면에서 따로 실행·멈추기 가능 +- 수집 cron(.9 crontab, 컨테이너 안에서 `docker exec`): 02:00 차트, 02:20 멜론 곡 정보 1,000곡, 03:15 하이라이트 400곡(4초 간격), 03:55 난이도, 08:10 유튜브 원곡 찾기 3,000곡(04:00~08:00은 봇이 Claude를 부르지 않는 시간이라 피함), 12:30 소리 크기 재기 3,000곡(3초 간격, 새로 확정된 곡). 로그 `~/music_quiz-collect.log`, `~/music_quiz-match.log`. 관리자 "수집" 화면에서 따로 실행·멈추기 가능 - 9단계 전 DB 백업: `~/backups/music_quiz/app-before-stage9-*.db` diff --git a/docs/12-decisions.md b/docs/12-decisions.md index 2206de0..250b40a 100644 --- a/docs/12-decisions.md +++ b/docs/12-decisions.md @@ -161,3 +161,9 @@ - 서버 업데이트(재시작) 때는 지금처럼 방이 닫힌다(방은 메모리에만 있음) 2. 업데이트 알림은 joke-app과 같다: 웹 빌드 id(``)가 서버와 다르면 위쪽에 "업데이트 내용이 있습니다 · 지금 새로고침하기 · ✕". 퀴즈 중에도 바로 띄우고 저절로 새로고침하지 않는다 3. 곡 제목·가수의 HTML 특수문자(`'`, `&`)는 수집할 때 풀어서 저장한다. 이미 저장된 곡은 `cli.ts fix-text`로 한 번 고치고, 고친 곡은 원곡 찾기를 다시 하며, 깨진 키 때문에 생긴 중복은 오래된 곡으로 합친다 + +## 소리 크기 맞추기 (2026-10-19, 사용자: "재생할 때 볼륨을 자동으로, 노멀라이즈 느낌으로") +1. 곡마다 볼륨을 자동으로 바꾼다. 곡 크기는 서버가 오디오를 받아 재는 대신 유튜브가 잰 값(영상 페이지 `audioConfig`, LUFS)을 쓴다. 오디오 다운로드가 없어서 봇 차단 위험이 거의 없고 유튜브 자신의 줄이기와 같은 기준이다 +2. 기준은 -18 LUFS(곡의 약 96%가 같은 크기). 전체 소리는 보통 곡 기준 약 4dB(볼륨 0.63배) 작아진다. 끄는 옵션은 두지 않는다(항상 켬) +3. 곡 안 구간별(처음부터/하이라이트부터) 보정은 하지 않는다. 유튜브 값은 곡 전체 평균 하나뿐이다 +4. 자세한 내용은 08 문서 3-1 diff --git a/e2e/game.e2e.ts b/e2e/game.e2e.ts index 0269c5b..370fe25 100644 --- a/e2e/game.e2e.ts +++ b/e2e/game.e2e.ts @@ -13,6 +13,7 @@ import { join } from 'node:path'; import { loadConfig } from '../apps/server/src/config'; import { startServer } from '../apps/server/src/server'; import { Catalog } from '../apps/server/src/collector/catalog'; +import { loudnessGain } from '../apps/server/src/songs/quiz'; const PORT = Number(process.env.E2E_PORT ?? 4621); const ORIGIN = `http://localhost:${PORT}`; @@ -43,15 +44,16 @@ const db = app.db; db.query("UPDATE songs SET youtube_status = 'matched', embeddable = 1, youtube_id = ?, duration_sec = 200 WHERE id = ?").run(`fake${String(i).padStart(7, '0')}`, id!); if (i <= 2) db.query('INSERT INTO lyrics (song_id, text, source, fetched_at) VALUES (?, ?, ?, 0)').run(id!, `첫 소절 곡${i}\n둘째 줄 ${i}\n셋째 줄\n넷째 줄\n다섯째 줄\n여섯째 줄\n후렴 곡${i}\n후렴 곡${i}`, 'e2e'); } - const real: [string, string, string, number][] = [ - ['광 (狂)', 'SG워너비', 'VO88htcm09E', 237], - ['가져가', '홍경민', 'Q7Q_qavUPdM', 221], - ['Be Natural', '에스이에스', 'YWP5K88l9G0', 282], + // loudness: 광 as YouTube measured it, 가져가 as if quiet (full volume), Be Natural not measured + const real: [string, string, string, number, number | null][] = [ + ['광 (狂)', 'SG워너비', 'VO88htcm09E', 237, -10.42], + ['가져가', '홍경민', 'Q7Q_qavUPdM', 221, -20], + ['Be Natural', '에스이에스', 'YWP5K88l9G0', 282, null], ]; - for (const [title, artist, vid, dur] of real) { + for (const [title, artist, vid, dur, lufs] of real) { const [id] = c.upsert({ title, artists: [{ name: artist }], year: 2000, genres: ['kpop'], source: 'e2e', ref: vid }); c.autoLevel(id!, 'all', 9); - db.query("UPDATE songs SET youtube_status = 'matched', embeddable = 1, youtube_id = ?, duration_sec = ? WHERE id = ?").run(vid, dur, id!); + db.query("UPDATE songs SET youtube_status = 'matched', embeddable = 1, youtube_id = ?, duration_sec = ?, loudness_lufs = ?, loudness_video = ? WHERE id = ?").run(vid, dur, lufs, lufs === null ? null : vid, id!); } } const titleOf = (vid: string) => db.query<{ title: string; artist: string }, [string]>('SELECT title, artist FROM songs WHERE youtube_id = ?').get(vid)!; @@ -388,6 +390,11 @@ try { expect(st.every((s) => s.state === 1 && !s.muted), `재생 상태 ${JSON.stringify(st)}`); expect(Math.abs(st[0]!.pos - st[1]!.pos) < 0.3, `두 사람 위치 차이 ${JSON.stringify(st)}`); console.log(` 위치 ${st.map((s) => s.pos.toFixed(3)).join(' / ')}`); + // 소리 크기 맞추기: the YouTube player itself runs at volume (70 by default) × this song's gain + const lufs = db.query<{ v: number | null }, [string]>('SELECT loudness_lufs v FROM songs WHERE youtube_id = ?').get((await video(x))!)!.v; + const ytVol = await Promise.all([x, y].map((p) => p.frames().find((f) => f.url().includes('youtube.com/embed'))!.evaluate(() => (document.getElementById('movie_player') as any).getVolume() as number))); + console.log(` 유튜브 볼륨 ${ytVol.join(' / ')} (곡 ${lufs ?? '안 잼'} LUFS)`); + expect(ytVol.every((v) => v === Math.round(70 * loudnessGain(lufs))), `유튜브 볼륨 ${ytVol} ≠ 70 × ${loudnessGain(lufs)}`); // 3s clip: stops by position await x.locator('.stage .mode', { hasText: '재생이 끝났어요' }).waitFor({ timeout: 8000 }); const end = await Promise.all([x, y].map((p) => p.evaluate(() => ({ pos: (window as any).mqPlayer.pos(), state: (window as any).mqPlayer.state() })))); diff --git a/packages/shared/src/protocol.ts b/packages/shared/src/protocol.ts index 6788f78..71e38b1 100644 --- a/packages/shared/src/protocol.ts +++ b/packages/shared/src/protocol.ts @@ -149,8 +149,11 @@ export type ServerMessage = /** 정답 줄 + 5줄 블록 in one message (docs/06). */ | { t: 'answer'; code: string; line: ChatLine } | { t: 'game'; code: string; game: GameView | null } - /** Load the video muted at `startSec`, buffer, report audio ready (docs/12 Q1). */ - | { t: 'prepare'; code: string; n: number; videoId: string; startSec: number; endSec: number | null } + /** + * Load the video muted at `startSec`, buffer, report audio ready (docs/12 Q1). + * `gain` (0..1): 소리 크기 맞추기 — the player plays at the listener's volume × gain, so songs sound equally loud. + */ + | { t: 'prepare'; code: string; n: number; videoId: string; startSec: number; endSec: number | null; gain: number } /** Everyone unmutes and plays from `atSec` at server time `startAt`; stop at `endSec` by playback position. */ | { t: 'play'; code: string; n: number; startAt: number; atSec: number; endSec: number | null } /** Stop now (someone is buffering / the host paused / the clip is over). */ @@ -159,7 +162,7 @@ export type ServerMessage = * Back in a question whose music is running (refresh / reconnect): load `videoId` and play along the running timeline * (position `atSec` at server time `startAt`); `clipStart` is where the clip began, for the progress bar. */ - | { t: 'catchup'; code: string; n: number; videoId: string; clipStart: number; startAt: number; atSec: number; endSec: number | null } + | { t: 'catchup'; code: string; n: number; videoId: string; clipStart: number; startAt: number; atSec: number; endSec: number | null; gain: number } | { t: 'typing'; code: string; ids: string[] } | { t: 'saved'; id: number; name: string } /** Recent lines for someone who just joined (or came back). */