소리 크기 맞추기(노멀라이즈): 곡마다 유튜브가 잰 크기(영상 페이지 audioConfig LUFS)를 저장하고, 재생 볼륨 = 내 볼륨 × 곡 보정값으로 -18 LUFS에 맞춤(유튜브 IFrame이 -14로 줄이는 것 위에, 작은 곡은 최대). prepare·catchup에 gain, 안 잰 곡은 보통 곡 값(0.63). 영상이 바뀌면 다시 잼(loudness_video). cli loudness 작업(인기곡부터·차단 시 멈춤·--deadline)·관리자 수집 화면, heatmap도 같은 페이지에서 저장. 실제 유튜브 e2e로 플레이어 볼륨 확인
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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: '난이도 다시 계산' },
|
||||
];
|
||||
|
||||
|
||||
@@ -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}}
|
||||
@@ -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++;
|
||||
|
||||
66
apps/server/src/collector/loudness.test.ts
Normal file
66
apps/server/src/collector/loudness.test.ts
Normal file
@@ -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('<html>no player</html>')).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<string, number> = { [`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);
|
||||
});
|
||||
});
|
||||
86
apps/server/src/collector/loudness.ts
Normal file
86
apps/server/src/collector/loudness.ts
Normal file
@@ -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<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;
|
||||
}
|
||||
|
||||
/** 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<string> } = {},
|
||||
) {
|
||||
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;
|
||||
}
|
||||
4
apps/server/src/db/migrations/007_loudness.sql
Normal file
4
apps/server/src/db/migrations/007_loudness.sql
Normal file
@@ -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;
|
||||
@@ -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<T extends ServerMessage['t']> = Extract<ServerMessage, { t: T }>;
|
||||
@@ -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);
|
||||
|
||||
@@ -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) });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = <T>(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<string>('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;
|
||||
|
||||
Reference in New Issue
Block a user