// A clip's sound at its segment encode: laid on its own clock, and the // per-clip `audio` knobs. // // timeline entry: "audio": { "offset": 120, "gain": -3, "fadeIn": 0.5, "fadeOut": 1 } // // `offset` (ms, −2000…2000) moves the sound against the picture: + is LATER. // It is read from the source again at the shifted window, so nothing is cut // off the far end. `gain` (dB, −20…20) after any levelling; `fadeIn` / // `fadeOut` (s, 0…5) at the segment's edges. Every key optional; absent, // the segment's sound is what it was. // // No imports from build-video.mjs: it imports this file, and so do the sync // report and umtool's writers (K2's updateClip validates through // validateClipAudio). /** * A sound laid on its own timestamps from 0: silence where its first sample * starts late or where a hole opens, samples dropped where two overlap -- * whatever is more than 10 ms off (a third of a frame); no resampling, and a * stream with neither passes sample for sample. Every filter after it that * counts samples (apad, acrossfade, concat) then counts the clock. */ export const AUDIO_ON_CLOCK = "aresample=async=1:min_hard_comp=0.01:first_pts=0"; /** Each knob's range and unit. */ export const CLIP_AUDIO_LIMITS = Object.freeze({ offset: Object.freeze({ min: -2000, max: 2000, unit: "ms" }), gain: Object.freeze({ min: -20, max: 20, unit: "dB" }), fadeIn: Object.freeze({ min: 0, max: 5, unit: "s" }), fadeOut: Object.freeze({ min: 0, max: 5, unit: "s" }), }); export const CLIP_AUDIO_KEYS = Object.freeze(Object.keys(CLIP_AUDIO_LIMITS)); const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); /** * Every reason a clip's `audio` cannot be used, as sentences naming `where` * (empty: it can). `undefined` / `null` is none. Unknown keys are refused. * * @param {unknown} audio * @param {string} [where] * @returns {string[]} */ export function validateClipAudio(audio, where = "audio") { if (audio === undefined || audio === null) return []; if (!isObj(audio)) return [`${where} must be { offset, gain, fadeIn, fadeOut }`]; const errors = []; for (const k of Object.keys(audio)) { if (!CLIP_AUDIO_KEYS.includes(k)) errors.push(`${where}.${k} is not an audio setting (${CLIP_AUDIO_KEYS.join(", ")})`); } for (const [k, { min, max, unit }] of Object.entries(CLIP_AUDIO_LIMITS)) { const v = audio[k]; if (v === undefined) continue; if (typeof v !== "number" || !Number.isFinite(v) || v < min || v > max) { errors.push(`${where}.${k} must be a number from ${min} to ${max} (${unit})`); } } return errors; } /** Every clip's `audio` in a manifest, checked (the build's and the lint's). */ export function validateClipAudioEntries(manifest) { const errors = []; (manifest?.timeline ?? []).forEach((e, i) => { // A video entry's `audio` (bass, gain) is its own: video-entry.mjs validates it. So is an analysis item's // (`{clip, start, end}`: the clip whose sound plays under it -- analysis-plan.mjs validates it). if (e?.audio === undefined || e?.type === "video" || e?.type === "analysis") return; const where = `timeline[${i}] (${e.id ?? "?"}).audio`; if (e.type && e.type !== "clip") { errors.push(`${where}: only a clip's sound has these settings`); return; } errors.push(...validateClipAudio(e.audio, where)); }); return errors; } /** The knobs that do anything: an empty object (or none) changes nothing. */ export function clipAudioOf(entry) { const a = isObj(entry?.audio) ? entry.audio : null; if (!a || validateClipAudio(a).length) return null; const out = {}; if (a.offset) out.offset = a.offset; if (a.gain) out.gain = a.gain; if (a.fadeIn) out.fadeIn = a.fadeIn; if (a.fadeOut) out.fadeOut = a.fadeOut; return Object.keys(out).length ? out : null; } const num = (v) => String(Number(v.toFixed(4))); /** * The gain and fades as filters on a segment `seconds` long, in its own clock * (after AUDIO_ON_CLOCK). Empty without any. * * @returns {string[]} */ export function clipAudioFilters(audio, seconds) { if (!audio) return []; const out = []; if (audio.gain) out.push(`volume=${num(audio.gain)}dB`); if (audio.fadeIn) out.push(`afade=t=in:st=0:d=${num(Math.min(audio.fadeIn, seconds))}`); if (audio.fadeOut) { const d = Math.min(audio.fadeOut, seconds); out.push(`afade=t=out:st=${num(Math.max(0, seconds - d))}:d=${num(d)}`); } return out; } /** * Where the sound is read from for a segment cut at [a, b] of its source * file: [a − offset, b − offset] (seconds), so at the segment's second t the * source's t + a − offset plays -- the sound `offset` later. Null without an * offset: the picture's own input carries it. */ export function offsetWindow(audio, a, b) { if (!audio?.offset) return null; const s = audio.offset / 1000; return { from: a - s, to: b - s }; }