@gobing-ai/knowledge-kit 0.1.19 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +1 -1
- package/dist/board/collect/index.js +1 -0
- package/dist/board/daily-voice/index.js +1315 -264
- package/dist/board/itc/index.js +1 -0
- package/dist/index.js +14674 -8775
- package/package.json +1 -1
- package/plugins/generations/dailynews-gen/dist/index.js +285 -16
- package/plugins/generations/dailynews-gen/src/editorial-claims.ts +215 -0
- package/plugins/generations/dailynews-gen/src/index.ts +23 -4
- package/plugins/generations/dailynews-gen/src/script-builder.ts +10 -3
- package/plugins/generations/dailynews-gen/src/script-plan.ts +74 -14
- package/plugins/generations/image-gen/README.md +31 -31
- package/plugins/generations/image-gen/assets/sample/provenance.json +314 -0
- package/plugins/generations/image-gen/config.example.yaml +3 -0
- package/plugins/generations/image-gen/dist/index.js +154 -14
- package/plugins/generations/image-gen/presets/SOURCE.md +65 -0
- package/plugins/generations/image-gen/scripts/eval-style-presets.ts +513 -0
- package/plugins/generations/image-gen/scripts/gen-samples.ts +57 -7
- package/plugins/generations/image-gen/src/presets.ts +6 -5
- package/plugins/generations/image-gen/src/providers/h3-local.ts +196 -0
- package/plugins/generations/image-gen/src/providers/types.ts +11 -1
- package/plugins/generations/music-gen/README.md +277 -0
- package/plugins/generations/music-gen/dist/index.js +23544 -0
- package/plugins/generations/music-gen/package.json +24 -0
- package/plugins/generations/music-gen/plugin.json +23 -0
- package/plugins/generations/music-gen/presets/SOURCE.md +39 -0
- package/plugins/generations/music-gen/presets/formats/clip.yaml +21 -0
- package/plugins/generations/music-gen/presets/formats/jingle.yaml +22 -0
- package/plugins/generations/music-gen/presets/formats/loop.yaml +21 -0
- package/plugins/generations/music-gen/presets/formats/song.yaml +31 -0
- package/plugins/generations/music-gen/presets/formats/underscore.yaml +22 -0
- package/plugins/generations/music-gen/presets/themes/ambient-tech.yaml +30 -0
- package/plugins/generations/music-gen/presets/themes/cinematic.yaml +30 -0
- package/plugins/generations/music-gen/presets/themes/lofi.yaml +30 -0
- package/plugins/generations/music-gen/presets/themes/synthwave.yaml +30 -0
- package/plugins/generations/music-gen/scripts/gen-preset-index.ts +181 -0
- package/plugins/generations/music-gen/scripts/resolve-preset.ts +64 -0
- package/plugins/generations/music-gen/src/adapter/index.ts +35 -0
- package/plugins/generations/music-gen/src/adapter/instance.ts +904 -0
- package/plugins/generations/music-gen/src/adapter/offline.ts +124 -0
- package/plugins/generations/music-gen/src/artifact.ts +223 -0
- package/plugins/generations/music-gen/src/audio/checks.ts +618 -0
- package/plugins/generations/music-gen/src/audio/master.ts +267 -0
- package/plugins/generations/music-gen/src/audio/measure.ts +507 -0
- package/plugins/generations/music-gen/src/audio/profile.ts +131 -0
- package/plugins/generations/music-gen/src/audio.ts +122 -0
- package/plugins/generations/music-gen/src/brief.ts +191 -0
- package/plugins/generations/music-gen/src/config.ts +451 -0
- package/plugins/generations/music-gen/src/content.ts +199 -0
- package/plugins/generations/music-gen/src/digest.ts +100 -0
- package/plugins/generations/music-gen/src/index.ts +147 -0
- package/plugins/generations/music-gen/src/job.ts +131 -0
- package/plugins/generations/music-gen/src/plugin.ts +371 -0
- package/plugins/generations/music-gen/src/presets.ts +918 -0
- package/plugins/generations/music-gen/src/render.ts +87 -0
- package/plugins/generations/music-gen/src/request.ts +102 -0
- package/plugins/generations/music-gen/src/spend.ts +495 -0
- package/plugins/generations/music-gen/tsconfig.json +8 -0
- package/plugins/kk/config.example.yaml +3 -0
- package/plugins/kk/decisions/extract/ai-news.yaml +5 -2
- package/plugins/kk/plugin.json +1 -1
- package/plugins/kk/publish-presets/daily-ai-voice.json +48 -7
- package/plugins/kk/schedules/kk-serve.launchd.plist.tmpl +2 -1
- package/plugins/kk/scripts/cover-compositor.ts +380 -32
- package/plugins/kk/scripts/daily-cover-mock.ts +40 -3
- package/plugins/kk/scripts/daily-stages.ts +897 -34
- package/plugins/kk/scripts/episode-identity.ts +180 -0
- package/plugins/kk/scripts/kk-workflow-stages.ts +37 -1
- package/plugins/kk/scripts/music-stages.ts +624 -0
- package/plugins/kk/scripts/news-editorial-ledger.ts +906 -0
- package/plugins/kk/scripts/source-citation.ts +187 -0
- package/plugins/kk/scripts/topic-selection-attempts.ts +446 -0
- package/plugins/kk/scripts/topic-selection-calibration.ts +161 -0
- package/plugins/kk/skills/content-judge/SKILL.md +11 -6
- package/plugins/kk/skills/content-judge/references/music-quality-rubric.md +147 -0
- package/plugins/kk/skills/content-judge/references/rubrics.md +15 -0
- package/plugins/kk/skills/content-safety/SKILL.md +12 -0
- package/plugins/kk/skills/image-authoring/SKILL.md +7 -3
- package/plugins/kk/skills/image-authoring/references/providers.md +27 -0
- package/plugins/kk/skills/image-authoring/references/style-extraction.md +1 -1
- package/plugins/kk/skills/music-authoring/SKILL.md +276 -0
- package/plugins/kk/skills/music-authoring/references/judge-input.md +117 -0
- package/plugins/kk/skills/news-editorial/SKILL.md +102 -15
- package/plugins/kk/skills/news-editorial/references/selection.md +4 -0
- package/plugins/kk/skills/news-editorial/references/style-contract.zh.md +58 -11
- package/plugins/kk/skills/publish/SKILL.md +11 -0
- package/plugins/kk/skills/web-automation/SKILL.md +31 -1
- package/plugins/kk/workflows/kk-collect.yaml +8 -4
- package/plugins/kk/workflows/kk-daily-ai-voice.yaml +10 -9
- package/plugins/kk/workflows/kk-daily-ai-voice2.yaml +11 -9
- package/plugins/kk/workflows/kk-music.yaml +328 -0
- package/plugins/kk/workflows/kk-news-generate.yaml +58 -3
- package/plugins/publishings/bilibili-pub/dist/index.js +11 -1
- package/plugins/publishings/bilibili-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/cnblogs-pub/dist/index.js +11 -1
- package/plugins/publishings/cnblogs-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/csdn-pub/dist/index.js +11 -1
- package/plugins/publishings/csdn-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/infoq-pub/dist/index.js +11 -1
- package/plugins/publishings/infoq-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/jike-pub/dist/index.js +11 -1
- package/plugins/publishings/jike-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/juejin-pub/dist/index.js +11 -1
- package/plugins/publishings/juejin-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/linkedin-pub/dist/index.js +54 -7
- package/plugins/publishings/linkedin-pub/dist/profile-check.js +43 -6
- package/plugins/publishings/linkedin-pub/src/index.ts +52 -6
- package/plugins/publishings/podcast-pub/dist/index.js +586 -20
- package/plugins/publishings/podcast-pub/src/corrections.ts +509 -0
- package/plugins/publishings/podcast-pub/src/index.ts +257 -5
- package/plugins/publishings/podcast-pub/src/show-notes-brief.ts +240 -0
- package/plugins/publishings/podcast-pub/src/show-notes.ts +69 -13
- package/plugins/publishings/shipinhao-pub/dist/index.js +437 -32
- package/plugins/publishings/shipinhao-pub/dist/profile-check.js +14 -2
- package/plugins/publishings/shipinhao-pub/plugin.json +1 -1
- package/plugins/publishings/shipinhao-pub/src/audio-flow.ts +492 -0
- package/plugins/publishings/shipinhao-pub/src/flow.ts +227 -12
- package/plugins/publishings/shipinhao-pub/src/selectors.ts +13 -0
- package/plugins/publishings/sspai-pub/dist/index.js +11 -1
- package/plugins/publishings/sspai-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/substack-pub/dist/index.js +11 -1
- package/plugins/publishings/substack-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/surfdash-pub/dist/index.js +122 -28
- package/plugins/publishings/surfdash-pub/src/digest-check.ts +7 -3
- package/plugins/publishings/surfdash-pub/src/index.ts +159 -25
- package/plugins/publishings/surfdash-pub/src/topic-claims.ts +37 -4
- package/plugins/publishings/threads-pub/dist/index.js +9206 -296
- package/plugins/publishings/threads-pub/dist/profile-check.js +21149 -0
- package/plugins/publishings/threads-pub/package.json +3 -1
- package/plugins/publishings/threads-pub/plugin.json +6 -1
- package/plugins/publishings/threads-pub/src/index.ts +8 -1
- package/plugins/publishings/threads-pub/src/post-rail.ts +357 -0
- package/plugins/publishings/threads-pub/src/profile-check.ts +17 -0
- package/plugins/publishings/wechatmp-pub/dist/index.js +18 -1
- package/plugins/publishings/wechatmp-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/wechatmp-pub/src/flow.ts +13 -0
- package/plugins/publishings/weibo-pub/dist/index.js +57 -3
- package/plugins/publishings/weibo-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/weibo-pub/src/index.ts +74 -2
- package/plugins/publishings/x-pub/dist/index.js +274 -44
- package/plugins/publishings/x-pub/dist/profile-check.js +14 -2
- package/plugins/publishings/x-pub/src/index.ts +15 -1
- package/plugins/publishings/x-pub/src/post-rail.ts +334 -0
- package/plugins/publishings/xhs-pub/dist/index.js +11 -1
- package/plugins/publishings/xhs-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/xiaoyuzhou-pub/dist/index.js +11 -1
- package/plugins/publishings/xiaoyuzhou-pub/dist/profile-check.js +11 -1
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audio measurement (task 0405 R1): duration, codec, sample rate and channels
|
|
3
|
+
* from `ffprobe`; integrated loudness, true peak, loudness range and threshold
|
|
4
|
+
* from an `ffmpeg loudnorm=print_format=json` analysis pass; leading/trailing
|
|
5
|
+
* silence from `ffmpeg silencedetect`.
|
|
6
|
+
*
|
|
7
|
+
* **One injectable runner.** Every spawn goes through {@link AudioToolRunner}:
|
|
8
|
+
* the default runner is the ts-runtime `ProcessExecutor` seam (`NodeProcessExecutor`,
|
|
9
|
+
* the only sanctioned process seam — see `.spur/rules/strict/runtime-boundaries.yaml`),
|
|
10
|
+
* and tests inject a runner that replays captured stdout/stderr, so CI needs no
|
|
11
|
+
* ffmpeg at all (R6/AC6).
|
|
12
|
+
*
|
|
13
|
+
* **Parsers are pure.** {@link parseProbeOutput}, {@link parseLoudnormOutput} and
|
|
14
|
+
* {@link parseSilencedetectOutput} take captured text and return typed values —
|
|
15
|
+
* no spawn, no clock, no filesystem — so the fixtures in
|
|
16
|
+
* `tests/fixtures/audio/` drive them directly. A tool output that cannot be
|
|
17
|
+
* parsed raises {@link AudioToolError}, which always names the tool and carries
|
|
18
|
+
* the unparsed fragment (AC1): "measurement missing" can never be mistaken for
|
|
19
|
+
* "measurement clean".
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { NodeProcessExecutor } from '@gobing-ai/ts-runtime';
|
|
23
|
+
|
|
24
|
+
/** One tool invocation's captured result. */
|
|
25
|
+
export interface ToolOutput {
|
|
26
|
+
exitCode: number;
|
|
27
|
+
stdout: string;
|
|
28
|
+
stderr: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Injectable spawn seam: `argv[0]` is the binary (`ffmpeg`/`ffprobe`), the rest
|
|
33
|
+
* are its arguments. Resolves with the captured streams; may reject only when
|
|
34
|
+
* the binary could not be spawned at all.
|
|
35
|
+
*/
|
|
36
|
+
export type AudioToolRunner = (argv: readonly string[]) => Promise<ToolOutput>;
|
|
37
|
+
|
|
38
|
+
/** Why a measurement could not be produced. */
|
|
39
|
+
export type AudioToolErrorKind = 'spawn' | 'exit' | 'malformed';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Raised whenever a measurement is unavailable: the binary is missing, it exited
|
|
43
|
+
* non-zero, or its output did not parse. `tool` and `fragment` are always
|
|
44
|
+
* populated so a check record can name both the tool and the text it could not
|
|
45
|
+
* read (AC1).
|
|
46
|
+
*/
|
|
47
|
+
export class AudioToolError extends Error {
|
|
48
|
+
/** Process exit code the command layer surfaces (`fail`, never a silent skip). */
|
|
49
|
+
readonly exitCode = 1;
|
|
50
|
+
/** Tool that failed, e.g. `ffprobe` or `ffmpeg loudnorm`. */
|
|
51
|
+
readonly tool: string;
|
|
52
|
+
readonly kind: AudioToolErrorKind;
|
|
53
|
+
/** The unparsed text (or the leading slice of it) — AC1's fragment. */
|
|
54
|
+
readonly fragment: string;
|
|
55
|
+
|
|
56
|
+
constructor(tool: string, kind: AudioToolErrorKind, message: string, fragment: string) {
|
|
57
|
+
const trimmed = fragment.trim();
|
|
58
|
+
super(`${tool}: ${message}${trimmed === '' ? ' (no output)' : ` — unparsed fragment: ${trimmed}`}`);
|
|
59
|
+
this.name = 'AudioToolError';
|
|
60
|
+
this.tool = tool;
|
|
61
|
+
this.kind = kind;
|
|
62
|
+
this.fragment = trimmed;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Longest tool-output fragment an error or check record carries. */
|
|
67
|
+
const MAX_FRAGMENT_LENGTH = 400;
|
|
68
|
+
|
|
69
|
+
/** Trim a tool output to something an error message or a check note can hold. */
|
|
70
|
+
function fragmentOf(text: string): string {
|
|
71
|
+
const trimmed = text.trim();
|
|
72
|
+
return trimmed.length <= MAX_FRAGMENT_LENGTH ? trimmed : `${trimmed.slice(0, MAX_FRAGMENT_LENGTH)}…`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Default binary names; the options below override them for a non-PATH install. */
|
|
76
|
+
export const DEFAULT_FFMPEG_BIN = 'ffmpeg';
|
|
77
|
+
export const DEFAULT_FFPROBE_BIN = 'ffprobe';
|
|
78
|
+
|
|
79
|
+
/** Seek/decoding tolerance when deciding a silence span touches the file's start or end. */
|
|
80
|
+
export const SILENCE_EDGE_TOLERANCE_SECONDS = 0.05;
|
|
81
|
+
|
|
82
|
+
/** Default `silencedetect` noise floor: −50 dB sits below any musical tail. */
|
|
83
|
+
export const DEFAULT_SILENCE_NOISE_DB = -50;
|
|
84
|
+
|
|
85
|
+
/** Default `silencedetect` minimum span: shorter gaps are phrasing, not dead air. */
|
|
86
|
+
export const DEFAULT_SILENCE_MIN_SECONDS = 0.5;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The one process executor every default audio spawn uses: buffered capture, no
|
|
90
|
+
* per-call allocation, no registry/event wiring (an audio measurement is not a
|
|
91
|
+
* supervised process).
|
|
92
|
+
*/
|
|
93
|
+
const processExecutor = new NodeProcessExecutor();
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The default runner: the ts-runtime {@link ProcessExecutor} seam, buffered.
|
|
97
|
+
*
|
|
98
|
+
* `ProcessResult` reports `exitCode: number | null` and does not throw for a tool
|
|
99
|
+
* it could not start, so this adapter keeps the {@link ToolOutput} shape the
|
|
100
|
+
* parsers read: a null code is "the tool never ran to an exit" (missing binary or
|
|
101
|
+
* a signal kill, both surfaced as {@link AudioToolError} kind `spawn` with the
|
|
102
|
+
* install hint) and every real exit code passes through untouched.
|
|
103
|
+
*/
|
|
104
|
+
export const spawnRunner: AudioToolRunner = async (argv) => {
|
|
105
|
+
const [bin, ...args] = argv;
|
|
106
|
+
if (bin === undefined) {
|
|
107
|
+
throw new AudioToolError('ffmpeg', 'spawn', 'no binary was given to the audio runner', '');
|
|
108
|
+
}
|
|
109
|
+
const result = await processExecutor.run({ command: bin, args, label: `audio ${bin}` });
|
|
110
|
+
if (result.exitCode === null) {
|
|
111
|
+
const detail = result.stderr.trim();
|
|
112
|
+
const killed = result.signal === undefined ? '' : ` (killed by ${result.signal})`;
|
|
113
|
+
throw new AudioToolError(
|
|
114
|
+
bin,
|
|
115
|
+
'spawn',
|
|
116
|
+
`could not be executed${detail === '' ? '' : ` (${detail})`}${killed} — install ffmpeg or point ffmpegBin/ffprobeBin at it`,
|
|
117
|
+
'',
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return { exitCode: result.exitCode, stdout: result.stdout, stderr: result.stderr };
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/** Where the measurement comes from: binary names plus the injected runner. */
|
|
124
|
+
export interface MeasureOptions {
|
|
125
|
+
runner?: AudioToolRunner;
|
|
126
|
+
ffmpegBin?: string;
|
|
127
|
+
ffprobeBin?: string;
|
|
128
|
+
/** `silencedetect` noise floor in dB (default {@link DEFAULT_SILENCE_NOISE_DB}). */
|
|
129
|
+
silenceNoiseDb?: number;
|
|
130
|
+
/** `silencedetect` minimum duration in seconds (default {@link DEFAULT_SILENCE_MIN_SECONDS}). */
|
|
131
|
+
silenceMinSeconds?: number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** `ffprobe` argv: one audio stream plus the container/format block. */
|
|
135
|
+
export function ffprobeArgs(path: string, bin = DEFAULT_FFPROBE_BIN): string[] {
|
|
136
|
+
return [bin, '-v', 'error', '-print_format', 'json', '-show_format', '-show_streams', path];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** `ffmpeg` argv for the loudness analysis pass (`-f null -` decodes without writing audio). */
|
|
140
|
+
export function loudnormArgs(path: string, bin = DEFAULT_FFMPEG_BIN): string[] {
|
|
141
|
+
return [bin, '-hide_banner', '-nostats', '-i', path, '-af', 'loudnorm=print_format=json', '-f', 'null', '-'];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** `ffmpeg` argv for the silence scan. */
|
|
145
|
+
export function silencedetectArgs(
|
|
146
|
+
path: string,
|
|
147
|
+
options: { bin?: string; noiseDb?: number; minSeconds?: number } = {},
|
|
148
|
+
): string[] {
|
|
149
|
+
const noise = options.noiseDb ?? DEFAULT_SILENCE_NOISE_DB;
|
|
150
|
+
const minSeconds = options.minSeconds ?? DEFAULT_SILENCE_MIN_SECONDS;
|
|
151
|
+
return [
|
|
152
|
+
options.bin ?? DEFAULT_FFMPEG_BIN,
|
|
153
|
+
'-hide_banner',
|
|
154
|
+
'-nostats',
|
|
155
|
+
'-i',
|
|
156
|
+
path,
|
|
157
|
+
'-af',
|
|
158
|
+
`silencedetect=noise=${String(noise)}dB:d=${String(minSeconds)}`,
|
|
159
|
+
'-f',
|
|
160
|
+
'null',
|
|
161
|
+
'-',
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** `ffprobe -version` / `ffmpeg -version` — the `<tool> <version>` source label. */
|
|
166
|
+
export function versionArgs(bin: string): string[] {
|
|
167
|
+
return [bin, '-version'];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Container/stream facts `ffprobe` reported. */
|
|
171
|
+
export interface AudioProbe {
|
|
172
|
+
/** Whole-file duration in seconds (stream duration when present, else the format's). */
|
|
173
|
+
durationSeconds: number;
|
|
174
|
+
/** `codec_name` of the first audio stream (`pcm_s16le`, `mp3`, …). */
|
|
175
|
+
codec: string;
|
|
176
|
+
sampleRate: number;
|
|
177
|
+
channels: number;
|
|
178
|
+
/** `format_name` of the container (`wav`, `mp3`, …). */
|
|
179
|
+
container: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Loudness facts the `loudnorm` analysis pass reported. */
|
|
183
|
+
export interface LoudnessMeasurement {
|
|
184
|
+
/** `input_i` — integrated loudness in LUFS. */
|
|
185
|
+
integratedLufs: number;
|
|
186
|
+
/** `input_tp` — measured true peak in dBTP. */
|
|
187
|
+
truePeakDbtp: number;
|
|
188
|
+
/** `input_lra` — loudness range in LU. */
|
|
189
|
+
loudnessRangeLu: number;
|
|
190
|
+
/** `input_thresh` — the gating threshold in LUFS; pass-2 `measured_thresh`. */
|
|
191
|
+
thresholdLufs: number;
|
|
192
|
+
/** `target_offset` — pass-2 `offset`. */
|
|
193
|
+
targetOffsetDb: number;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** One detected silence span. */
|
|
197
|
+
export interface SilenceSpan {
|
|
198
|
+
startSeconds: number;
|
|
199
|
+
/** `null` for a span that runs to the end of the file. */
|
|
200
|
+
endSeconds: number | null;
|
|
201
|
+
durationSeconds: number;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Silence facts the `silencedetect` scan reported. */
|
|
205
|
+
export interface SilenceMeasurement {
|
|
206
|
+
/** Dead air before the music starts; `0` when the file opens on audio. */
|
|
207
|
+
leadingSeconds: number;
|
|
208
|
+
/** Dead air after the music stops; `0` when the file ends on audio. */
|
|
209
|
+
trailingSeconds: number;
|
|
210
|
+
spans: SilenceSpan[];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Everything one take's measurement yields. */
|
|
214
|
+
export interface AudioMeasurement {
|
|
215
|
+
probe: AudioProbe;
|
|
216
|
+
loudness: LoudnessMeasurement;
|
|
217
|
+
silence: SilenceMeasurement;
|
|
218
|
+
/** `ffprobe 8.1.2` / `ffmpeg 8.1.2` — what every check record's `source` quotes. */
|
|
219
|
+
tools: { ffprobe: string; ffmpeg: string };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Read a numeric field `ffprobe` reported (it quotes numbers as strings). */
|
|
223
|
+
function probeNumber(stream: Record<string, unknown>, field: string): number | undefined {
|
|
224
|
+
const raw = stream[field];
|
|
225
|
+
if (typeof raw === 'number' && Number.isFinite(raw)) {
|
|
226
|
+
return raw;
|
|
227
|
+
}
|
|
228
|
+
if (typeof raw === 'string') {
|
|
229
|
+
const parsed = Number(raw);
|
|
230
|
+
if (Number.isFinite(parsed)) {
|
|
231
|
+
return parsed;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Parse `ffprobe -print_format json -show_format -show_streams`. Pure over the
|
|
239
|
+
* captured stdout; a missing audio stream or a missing duration is malformed
|
|
240
|
+
* output, not a zero measurement.
|
|
241
|
+
*/
|
|
242
|
+
export function parseProbeOutput(stdout: string): AudioProbe {
|
|
243
|
+
const tool = 'ffprobe';
|
|
244
|
+
let parsed: unknown;
|
|
245
|
+
try {
|
|
246
|
+
parsed = JSON.parse(stdout);
|
|
247
|
+
} catch {
|
|
248
|
+
throw new AudioToolError(
|
|
249
|
+
tool,
|
|
250
|
+
'malformed',
|
|
251
|
+
'output is not the JSON document `-print_format json` produces',
|
|
252
|
+
fragmentOf(stdout),
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
if (typeof parsed !== 'object' || parsed === null) {
|
|
256
|
+
throw new AudioToolError(tool, 'malformed', 'output is not a JSON object', fragmentOf(stdout));
|
|
257
|
+
}
|
|
258
|
+
const document = parsed as { streams?: unknown; format?: unknown };
|
|
259
|
+
const streams = Array.isArray(document.streams) ? document.streams : [];
|
|
260
|
+
const audio = streams.find(
|
|
261
|
+
(stream): stream is Record<string, unknown> =>
|
|
262
|
+
typeof stream === 'object' && stream !== null && (stream as Record<string, unknown>).codec_type === 'audio',
|
|
263
|
+
);
|
|
264
|
+
if (audio === undefined) {
|
|
265
|
+
throw new AudioToolError(tool, 'malformed', 'no audio stream in the probe output', fragmentOf(stdout));
|
|
266
|
+
}
|
|
267
|
+
const format =
|
|
268
|
+
typeof document.format === 'object' && document.format !== null
|
|
269
|
+
? (document.format as Record<string, unknown>)
|
|
270
|
+
: {};
|
|
271
|
+
const durationSeconds = probeNumber(audio, 'duration') ?? probeNumber(format, 'duration');
|
|
272
|
+
if (durationSeconds === undefined || durationSeconds <= 0) {
|
|
273
|
+
throw new AudioToolError(
|
|
274
|
+
tool,
|
|
275
|
+
'malformed',
|
|
276
|
+
'the audio stream reports no positive duration',
|
|
277
|
+
fragmentOf(JSON.stringify(audio)),
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
const sampleRate = probeNumber(audio, 'sample_rate');
|
|
281
|
+
if (sampleRate === undefined || sampleRate <= 0) {
|
|
282
|
+
throw new AudioToolError(
|
|
283
|
+
tool,
|
|
284
|
+
'malformed',
|
|
285
|
+
'the audio stream reports no sample rate',
|
|
286
|
+
fragmentOf(JSON.stringify(audio)),
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
const channels = probeNumber(audio, 'channels');
|
|
290
|
+
if (channels === undefined || channels <= 0) {
|
|
291
|
+
throw new AudioToolError(
|
|
292
|
+
tool,
|
|
293
|
+
'malformed',
|
|
294
|
+
'the audio stream reports no channel count',
|
|
295
|
+
fragmentOf(JSON.stringify(audio)),
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
const codec = typeof audio.codec_name === 'string' ? audio.codec_name : '';
|
|
299
|
+
if (codec === '') {
|
|
300
|
+
throw new AudioToolError(
|
|
301
|
+
tool,
|
|
302
|
+
'malformed',
|
|
303
|
+
'the audio stream reports no codec name',
|
|
304
|
+
fragmentOf(JSON.stringify(audio)),
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
const container = typeof format.format_name === 'string' && format.format_name !== '' ? format.format_name : codec;
|
|
308
|
+
return { durationSeconds, codec, sampleRate, channels, container };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Parse one loudnorm numeric field; `-inf`/`inf` are real measured values, not malformed text. */
|
|
312
|
+
function loudnormNumber(document: Record<string, unknown>, field: string, stderr: string): number {
|
|
313
|
+
const raw = document[field];
|
|
314
|
+
if (typeof raw === 'number' && !Number.isNaN(raw)) {
|
|
315
|
+
return raw;
|
|
316
|
+
}
|
|
317
|
+
if (typeof raw === 'string') {
|
|
318
|
+
const text = raw.trim().toLowerCase();
|
|
319
|
+
if (text === '-inf') {
|
|
320
|
+
return Number.NEGATIVE_INFINITY;
|
|
321
|
+
}
|
|
322
|
+
if (text === 'inf' || text === '+inf') {
|
|
323
|
+
return Number.POSITIVE_INFINITY;
|
|
324
|
+
}
|
|
325
|
+
const parsed = Number(text);
|
|
326
|
+
if (!Number.isNaN(parsed)) {
|
|
327
|
+
return parsed;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
throw new AudioToolError(
|
|
331
|
+
'ffmpeg loudnorm',
|
|
332
|
+
'malformed',
|
|
333
|
+
`the analysis JSON has no numeric "${field}"`,
|
|
334
|
+
fragmentOf(stderr),
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Parse the `loudnorm=print_format=json` analysis block. ffmpeg writes the JSON
|
|
340
|
+
* to **stderr**, wrapped in its own log lines, so the block is located between
|
|
341
|
+
* the last `{` and the last `}` rather than assumed to be the whole stream.
|
|
342
|
+
*/
|
|
343
|
+
export function parseLoudnormOutput(stderr: string): LoudnessMeasurement {
|
|
344
|
+
const start = stderr.lastIndexOf('{');
|
|
345
|
+
const end = stderr.lastIndexOf('}');
|
|
346
|
+
if (start === -1 || end <= start) {
|
|
347
|
+
throw new AudioToolError(
|
|
348
|
+
'ffmpeg loudnorm',
|
|
349
|
+
'malformed',
|
|
350
|
+
'no loudnorm JSON block (`print_format=json`) in the output',
|
|
351
|
+
fragmentOf(stderr),
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
const block = stderr.slice(start, end + 1);
|
|
355
|
+
let parsed: unknown;
|
|
356
|
+
try {
|
|
357
|
+
parsed = JSON.parse(block);
|
|
358
|
+
} catch {
|
|
359
|
+
throw new AudioToolError(
|
|
360
|
+
'ffmpeg loudnorm',
|
|
361
|
+
'malformed',
|
|
362
|
+
'the loudnorm JSON block does not parse',
|
|
363
|
+
fragmentOf(block),
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
// The block always starts with `{`, so a successful parse is always an object.
|
|
367
|
+
const document = parsed as Record<string, unknown>;
|
|
368
|
+
return {
|
|
369
|
+
integratedLufs: loudnormNumber(document, 'input_i', block),
|
|
370
|
+
truePeakDbtp: loudnormNumber(document, 'input_tp', block),
|
|
371
|
+
loudnessRangeLu: loudnormNumber(document, 'input_lra', block),
|
|
372
|
+
thresholdLufs: loudnormNumber(document, 'input_thresh', block),
|
|
373
|
+
targetOffsetDb: loudnormNumber(document, 'target_offset', block),
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** `silence_start: 0` — one number after the label. */
|
|
378
|
+
const SILENCE_START = /silence_start:\s*(-?\d+(?:\.\d+)?)/;
|
|
379
|
+
/** `silence_end: 2.000021 | silence_duration: 2.000021`. */
|
|
380
|
+
const SILENCE_END = /silence_end:\s*(-?\d+(?:\.\d+)?)\s*\|\s*silence_duration:\s*(-?\d+(?:\.\d+)?)/;
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Parse `silencedetect` stderr into spans plus the leading/trailing dead air.
|
|
384
|
+
*
|
|
385
|
+
* A span that opens with `silence_start` and never closes is a silence that runs
|
|
386
|
+
* to EOF: `silencedetect` only prints `silence_end` when audio resumes.
|
|
387
|
+
*/
|
|
388
|
+
export function parseSilencedetectOutput(stderr: string, durationSeconds: number): SilenceMeasurement {
|
|
389
|
+
const tool = 'ffmpeg silencedetect';
|
|
390
|
+
const spans: SilenceSpan[] = [];
|
|
391
|
+
let open: number | null = null;
|
|
392
|
+
for (const rawLine of stderr.split('\n')) {
|
|
393
|
+
const line = rawLine.trim();
|
|
394
|
+
const startMatch = SILENCE_START.exec(line);
|
|
395
|
+
if (startMatch !== null) {
|
|
396
|
+
open = Number(startMatch[1]);
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
const endMatch = SILENCE_END.exec(line);
|
|
400
|
+
if (endMatch !== null) {
|
|
401
|
+
const endSeconds = Number(endMatch[1]);
|
|
402
|
+
const startSeconds = open ?? Math.max(0, endSeconds - Number(endMatch[2]));
|
|
403
|
+
spans.push({ startSeconds, endSeconds, durationSeconds: Math.max(0, endSeconds - startSeconds) });
|
|
404
|
+
open = null;
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (line.includes('silence_start') || line.includes('silence_end')) {
|
|
408
|
+
throw new AudioToolError(tool, 'malformed', 'a silence line has no readable timestamp', fragmentOf(line));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (open !== null) {
|
|
412
|
+
spans.push({ startSeconds: open, endSeconds: null, durationSeconds: Math.max(0, durationSeconds - open) });
|
|
413
|
+
}
|
|
414
|
+
const first = spans[0];
|
|
415
|
+
const leadingSeconds =
|
|
416
|
+
first !== undefined && first.startSeconds <= SILENCE_EDGE_TOLERANCE_SECONDS ? first.durationSeconds : 0;
|
|
417
|
+
const last = spans[spans.length - 1];
|
|
418
|
+
const touchesEnd =
|
|
419
|
+
last !== undefined &&
|
|
420
|
+
(last.endSeconds === null || last.endSeconds >= durationSeconds - SILENCE_EDGE_TOLERANCE_SECONDS);
|
|
421
|
+
const trailingSeconds = touchesEnd && last !== undefined ? last.durationSeconds : 0;
|
|
422
|
+
return { leadingSeconds, trailingSeconds, spans };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** Read `<tool> <version>` from a `-version` banner's first line. */
|
|
426
|
+
export function parseToolVersion(stdout: string, tool: string): string {
|
|
427
|
+
const firstLine = stdout.split('\n')[0]?.trim() ?? '';
|
|
428
|
+
const match = /version\s+(\S+)/i.exec(firstLine);
|
|
429
|
+
if (match === null || match[1] === undefined) {
|
|
430
|
+
throw new AudioToolError(tool, 'malformed', '`-version` banner carries no version', fragmentOf(stdout));
|
|
431
|
+
}
|
|
432
|
+
return `${tool} ${match[1]}`;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Run one argv through the runner and fail loudly on a non-zero exit. */
|
|
436
|
+
async function run(runner: AudioToolRunner, argv: readonly string[], tool: string): Promise<ToolOutput> {
|
|
437
|
+
const output = await runner(argv);
|
|
438
|
+
if (output.exitCode !== 0) {
|
|
439
|
+
const detail = output.stderr.trim() === '' ? output.stdout : output.stderr;
|
|
440
|
+
throw new AudioToolError(tool, 'exit', `exited ${String(output.exitCode)}`, fragmentOf(detail));
|
|
441
|
+
}
|
|
442
|
+
return output;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** `ffprobe 8.1.2` — the check-record source label, version included. */
|
|
446
|
+
export async function toolVersions(options: MeasureOptions = {}): Promise<{ ffprobe: string; ffmpeg: string }> {
|
|
447
|
+
const runner = options.runner ?? spawnRunner;
|
|
448
|
+
const ffprobeBin = options.ffprobeBin ?? DEFAULT_FFPROBE_BIN;
|
|
449
|
+
const ffmpegBin = options.ffmpegBin ?? DEFAULT_FFMPEG_BIN;
|
|
450
|
+
const [probe, ffmpeg] = await Promise.all([
|
|
451
|
+
run(runner, versionArgs(ffprobeBin), ffprobeBin),
|
|
452
|
+
run(runner, versionArgs(ffmpegBin), ffmpegBin),
|
|
453
|
+
]);
|
|
454
|
+
return {
|
|
455
|
+
ffprobe: parseToolVersion(probe.stdout, 'ffprobe'),
|
|
456
|
+
ffmpeg: parseToolVersion(ffmpeg.stdout, 'ffmpeg'),
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** Probe one file (R1: duration, codec, sample rate, channels). */
|
|
461
|
+
export async function probeAudio(path: string, options: MeasureOptions = {}): Promise<AudioProbe> {
|
|
462
|
+
const runner = options.runner ?? spawnRunner;
|
|
463
|
+
const bin = options.ffprobeBin ?? DEFAULT_FFPROBE_BIN;
|
|
464
|
+
const output = await run(runner, ffprobeArgs(path, bin), bin);
|
|
465
|
+
return parseProbeOutput(output.stdout);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Run the `loudnorm` analysis pass on one file (R1: integrated LUFS, true peak, LRA). */
|
|
469
|
+
export async function measureLoudness(path: string, options: MeasureOptions = {}): Promise<LoudnessMeasurement> {
|
|
470
|
+
const runner = options.runner ?? spawnRunner;
|
|
471
|
+
const bin = options.ffmpegBin ?? DEFAULT_FFMPEG_BIN;
|
|
472
|
+
const output = await run(runner, loudnormArgs(path, bin), `${bin} loudnorm`);
|
|
473
|
+
return parseLoudnormOutput(output.stderr);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** Run the `silencedetect` scan on one file (R1: leading/trailing silence). */
|
|
477
|
+
export async function measureSilence(
|
|
478
|
+
path: string,
|
|
479
|
+
durationSeconds: number,
|
|
480
|
+
options: MeasureOptions = {},
|
|
481
|
+
): Promise<SilenceMeasurement> {
|
|
482
|
+
const runner = options.runner ?? spawnRunner;
|
|
483
|
+
const bin = options.ffmpegBin ?? DEFAULT_FFMPEG_BIN;
|
|
484
|
+
const output = await run(
|
|
485
|
+
runner,
|
|
486
|
+
silencedetectArgs(path, {
|
|
487
|
+
bin,
|
|
488
|
+
...(options.silenceNoiseDb === undefined ? {} : { noiseDb: options.silenceNoiseDb }),
|
|
489
|
+
...(options.silenceMinSeconds === undefined ? {} : { minSeconds: options.silenceMinSeconds }),
|
|
490
|
+
}),
|
|
491
|
+
`${bin} silencedetect`,
|
|
492
|
+
);
|
|
493
|
+
return parseSilencedetectOutput(output.stderr, durationSeconds);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Measure one audio file completely (R1): four spawns — two `-version` banners,
|
|
498
|
+
* one `ffprobe`, one `loudnorm` analysis, one `silencedetect` — through the one
|
|
499
|
+
* injected runner. The first failure aborts the measurement, so a partially
|
|
500
|
+
* measured take can never be reported as clean.
|
|
501
|
+
*/
|
|
502
|
+
export async function measureAudio(path: string, options: MeasureOptions = {}): Promise<AudioMeasurement> {
|
|
503
|
+
const [tools, probe] = await Promise.all([toolVersions(options), probeAudio(path, options)]);
|
|
504
|
+
const loudness = await measureLoudness(path, options);
|
|
505
|
+
const silence = await measureSilence(path, probe.durationSeconds, options);
|
|
506
|
+
return { probe, loudness, silence, tools };
|
|
507
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delivery profiles (task 0405 R5): the loudness/true-peak/container/sample-rate
|
|
3
|
+
* target a mastered file is normalized to.
|
|
4
|
+
*
|
|
5
|
+
* **Profiles are config, not constants.** `music.delivery.<name>` in the kk user
|
|
6
|
+
* config overrides a built-in or defines a new one, so a channel target can be
|
|
7
|
+
* added without a code change. Exactly one built-in ships — `streaming`
|
|
8
|
+
* (−14 LUFS, −1 dBTP, wav, 44100) — because no 小红书 / B站 / 视频号 number is
|
|
9
|
+
* defended by a measured reference upload yet (see task 0404): inventing them
|
|
10
|
+
* here would bake an unverified guess into every delivered file.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { AUDIO_FORMATS, type AudioFormat } from '../brief';
|
|
14
|
+
|
|
15
|
+
/** A resolved delivery target. */
|
|
16
|
+
export interface DeliveryProfile {
|
|
17
|
+
/** Integrated loudness target in LUFS (`loudnorm=I=`). */
|
|
18
|
+
lufs: number;
|
|
19
|
+
/** True-peak ceiling in dBTP (`loudnorm=TP=`); measured on the encoded output (R4). */
|
|
20
|
+
truePeak: number;
|
|
21
|
+
/** Loudness range in LU (`loudnorm=LRA=`); omitted unless set, because forcing it invites dynamic mode. */
|
|
22
|
+
lra?: number;
|
|
23
|
+
/** Delivered container; also selects the codec and the artifact extension. */
|
|
24
|
+
container: AudioFormat;
|
|
25
|
+
/** Output sample rate (`-ar`), which is what guards against loudnorm's internal 192 kHz upsample (R3). */
|
|
26
|
+
sampleRate: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What `music.delivery.<name>` may set: any subset of a profile. */
|
|
30
|
+
export type DeliveryProfileOverride = Partial<DeliveryProfile>;
|
|
31
|
+
|
|
32
|
+
/** Profile name → what the user config declares for it. */
|
|
33
|
+
export type DeliveryProfileConfig = Record<string, DeliveryProfileOverride>;
|
|
34
|
+
|
|
35
|
+
/** The one built-in profile (R5): the default for a streaming/digital delivery. */
|
|
36
|
+
export const BUILTIN_DELIVERY_PROFILES: Readonly<Record<string, DeliveryProfile>> = {
|
|
37
|
+
streaming: { lufs: -14, truePeak: -1, container: 'wav', sampleRate: 44_100 },
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** Profile used when a caller does not name one. */
|
|
41
|
+
export const DEFAULT_DELIVERY_PROFILE = 'streaming';
|
|
42
|
+
|
|
43
|
+
/** Integrated-loudness tolerance the delivery check allows around the profile target (R4). */
|
|
44
|
+
export const DELIVERY_LOUDNESS_TOLERANCE_LU = 1;
|
|
45
|
+
|
|
46
|
+
/** Raised for an unknown profile name, an incomplete definition or an out-of-domain value. */
|
|
47
|
+
export class DeliveryProfileError extends Error {
|
|
48
|
+
/** Process exit code the command layer surfaces. */
|
|
49
|
+
readonly exitCode = 1;
|
|
50
|
+
/** The profile name the caller asked for (or the definition key at fault). */
|
|
51
|
+
readonly profile: string;
|
|
52
|
+
/** Every profile that *is* defined, so the message can list them (R5). */
|
|
53
|
+
readonly defined: string[];
|
|
54
|
+
|
|
55
|
+
constructor(profile: string, message: string, defined: readonly string[]) {
|
|
56
|
+
const list = [...defined].sort();
|
|
57
|
+
super(`music delivery profile "${profile}": ${message} — defined profiles: ${list.join(', ')}`);
|
|
58
|
+
this.name = 'DeliveryProfileError';
|
|
59
|
+
this.profile = profile;
|
|
60
|
+
this.defined = list;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Every profile name that resolves: built-ins plus the configured ones. */
|
|
65
|
+
export function definedDeliveryProfiles(config: DeliveryProfileConfig = {}): string[] {
|
|
66
|
+
return [...new Set([...Object.keys(BUILTIN_DELIVERY_PROFILES), ...Object.keys(config)])].sort();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Validate one profile against its domain. Applies to the merged result, so an
|
|
71
|
+
* override cannot smuggle in an impossible target, and a partial definition of a
|
|
72
|
+
* new profile is reported as missing rather than defaulted (a defaulted channel
|
|
73
|
+
* target would be a guess, which R5 forbids).
|
|
74
|
+
*/
|
|
75
|
+
export function validateDeliveryProfile(name: string, value: Record<string, unknown>): DeliveryProfile {
|
|
76
|
+
const defined = Object.keys(BUILTIN_DELIVERY_PROFILES);
|
|
77
|
+
const fail = (message: string): never => {
|
|
78
|
+
throw new DeliveryProfileError(name, message, defined);
|
|
79
|
+
};
|
|
80
|
+
const lufs = value.lufs;
|
|
81
|
+
if (typeof lufs !== 'number' || !Number.isFinite(lufs)) {
|
|
82
|
+
fail('lufs must be a finite number of LUFS (set `lufs`)');
|
|
83
|
+
}
|
|
84
|
+
const truePeak = value.truePeak;
|
|
85
|
+
if (typeof truePeak !== 'number' || !Number.isFinite(truePeak)) {
|
|
86
|
+
fail('truePeak must be a finite number of dBTP (set `truePeak`)');
|
|
87
|
+
}
|
|
88
|
+
if ((truePeak as number) > 0) {
|
|
89
|
+
fail(`truePeak ${String(truePeak)} dBTP is above full scale; a ceiling is 0 dBTP or lower`);
|
|
90
|
+
}
|
|
91
|
+
const container = value.container;
|
|
92
|
+
if (typeof container !== 'string' || !(AUDIO_FORMATS as readonly string[]).includes(container)) {
|
|
93
|
+
fail(`container ${JSON.stringify(container)} is not one of ${AUDIO_FORMATS.join(', ')}`);
|
|
94
|
+
}
|
|
95
|
+
const sampleRate = value.sampleRate;
|
|
96
|
+
if (typeof sampleRate !== 'number' || !Number.isInteger(sampleRate) || sampleRate <= 0) {
|
|
97
|
+
fail(`sampleRate ${JSON.stringify(sampleRate)} must be a positive integer number of Hz`);
|
|
98
|
+
}
|
|
99
|
+
const lra = value.lra;
|
|
100
|
+
if (lra !== undefined && (typeof lra !== 'number' || !Number.isFinite(lra) || lra < 0)) {
|
|
101
|
+
fail('lra must be a non-negative finite number of LU when set');
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
lufs: lufs as number,
|
|
105
|
+
truePeak: truePeak as number,
|
|
106
|
+
container: container as AudioFormat,
|
|
107
|
+
sampleRate: sampleRate as number,
|
|
108
|
+
...(typeof lra === 'number' ? { lra } : {}),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Resolve a profile name against the built-ins and the user config (R5).
|
|
114
|
+
*
|
|
115
|
+
* `music.delivery.streaming` overrides field by field; any other name must be
|
|
116
|
+
* defined completely in config. An unknown name throws
|
|
117
|
+
* {@link DeliveryProfileError} listing every defined profile.
|
|
118
|
+
*/
|
|
119
|
+
export function resolveDeliveryProfile(name: string, config: DeliveryProfileConfig = {}): DeliveryProfile {
|
|
120
|
+
const defined = definedDeliveryProfiles(config);
|
|
121
|
+
const builtin = BUILTIN_DELIVERY_PROFILES[name];
|
|
122
|
+
const override = config[name];
|
|
123
|
+
if (builtin === undefined && override === undefined) {
|
|
124
|
+
throw new DeliveryProfileError(
|
|
125
|
+
name,
|
|
126
|
+
`no such profile (pass one of the defined profiles, or define music.delivery.${name})`,
|
|
127
|
+
defined,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return validateDeliveryProfile(name, { ...(builtin ?? {}), ...(override ?? {}) });
|
|
131
|
+
}
|