import { type Span, parseSilencedetect } from './media-logs.ts' import { round } from './promo-text.ts' export const VOICE_SILENCE = { noiseDb: -45, minSeconds: 0.15 } as const export type VoicePausesInput = { durationSeconds: number silencedetectLog?: string silences?: Span[] /** Longest acceptable pause inside speech. Default 0.6 s. */ maxPauseSeconds?: number } export type Gap = { start: number; end: number; dur: number } export type VoicePausesResult = { pass: boolean durationSeconds: number preRollSeconds: number postRollSeconds: number interiorGapCount: number maxInteriorGapSeconds: number overLimit: Gap[] interiorGaps: Gap[] next: string params: { maxPauseSeconds: number; silence: typeof VOICE_SILENCE; ffmpegFilter: string } } // ffmpeg args for the measurement (append to `ffmpeg -nostats -i `; read stderr). export function voicePausesArgs(file: string) { return ['-nostats', '-i', file, '-af', `silencedetect=noise=${VOICE_SILENCE.noiseDb}dB:d=${VOICE_SILENCE.minSeconds}`, '-f', 'null', '-'] } /** * Measure pauses inside an isolated narration take (before music is mixed in) * and fail any pause longer than the limit. Pure: pass ffmpeg `silencedetect` * stderr (or parsed spans); the runner `scripts/voice-pauses.mjs` produces it * with `-af silencedetect=noise=-45dB:d=0.15`. * * Pre-roll (silence starting at 0) and post-roll (silence running to the end) * are reported separately and never count as pauses. * * @param input - silencedetect log or spans, take duration, optional maxPauseSeconds * @returns interiorGaps, maxInteriorGapSeconds, overLimit, pass, next * * @example * import voicePauses from 'kody:@kody/skills/voice-pauses' * * const qa = await voicePauses({ silencedetectLog: stderr, durationSeconds: 82.06 }) */ export default function voicePauses(input: VoicePausesInput): VoicePausesResult { if (!input || typeof input !== 'object') throw new Error('input object is required') const allowed = new Set(['durationSeconds', 'silencedetectLog', 'silences', 'maxPauseSeconds']) const unknown = Object.keys(input).filter((k) => !allowed.has(k)) if (unknown.length) throw new Error(`unknown input key(s): ${unknown.join(', ')}`) if (!(input.durationSeconds > 0)) throw new Error('durationSeconds must be > 0') if ((input.silencedetectLog === undefined) === (input.silences === undefined)) { throw new Error('pass exactly one of silencedetectLog or silences') } const dur = input.durationSeconds const max = input.maxPauseSeconds ?? 0.6 const spans = input.silences ?? parseSilencedetect(input.silencedetectLog!, dur) const gaps = spans.map((s) => ({ start: round(s.start), end: round(s.end), dur: round(s.end - s.start) })) const pre = gaps.filter((g) => g.start <= 0.02) const post = gaps.filter((g) => g.end >= dur - 0.02) const interior = gaps.filter((g) => !pre.includes(g) && !post.includes(g)) const overLimit = interior.filter((g) => g.dur > max) const pass = overLimit.length === 0 return { pass, durationSeconds: round(dur), preRollSeconds: pre[0]?.dur ?? 0, postRollSeconds: post[0]?.dur ?? 0, interiorGapCount: interior.length, maxInteriorGapSeconds: interior.reduce((m, g) => Math.max(m, g.dur), 0), overLimit, interiorGaps: interior, next: pass ? 'Pass interiorGaps to voice-timing as `pauses`.' : 'Re-record or regenerate the take as one continuous read; never trim pauses by cutting inside speech.', params: { maxPauseSeconds: max, silence: VOICE_SILENCE, ffmpegFilter: `silencedetect=noise=${VOICE_SILENCE.noiseDb}dB:d=${VOICE_SILENCE.minSeconds}`, }, } }