fix(subsync): address PR review findings and deterministic CI check

This commit is contained in:
sudacode committed 2026-11-25 20:17:25 -08:00
1 parent 8e3d9e21e0
commit fc113cfbca
16 files changed
+852 -82

No files matched your search

+87 -1
View File
@@ -1,6 +1,15 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { detectSpeechSpansFromPcm, parseRawSileroVadSegments } from './audio-speech';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {
clearAudioSpeechCache,
detectSpeechSpansFromPcm,
extractAudioSpeechSpans,
getCachedAudioSpeechSpans,
parseRawSileroVadSegments,
} from './audio-speech';
test('detectSpeechSpansFromPcm detects voice-band bursts separated by quiet background', () => {
const sampleRate = 8000;
@@ -54,3 +63,80 @@ test('parseRawSileroVadSegments parses fine-grained speech segments from whisper
{ start: 5.105, end: 6.9 },
]);
});
test('audio speech cache keys distinguish built-in VAD from Silero VAD config', async () => {
if (process.platform === 'win32') return;
clearAudioSpeechCache();
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subsync-vad-cache-'));
const videoPath = path.join(tmpDir, 'ep01.mkv');
const modelPath = path.join(tmpDir, 'silero.bin');
const ffmpegScript = path.join(tmpDir, 'ffmpeg.sh');
const vadScript = path.join(tmpDir, 'whisper-vad.sh');
fs.writeFileSync(videoPath, 'video');
fs.writeFileSync(modelPath, 'model');
// Fake ffmpeg outputs silence for pipe:1 (built-in VAD) and a tiny WAV when given an output file (Silero).
fs.writeFileSync(
ffmpegScript,
`#!/bin/sh
last=""
for arg in "$@"; do last="$arg"; done
if [ "$last" = "pipe:1" ]; then
dd if=/dev/zero bs=1600 count=1 2>/dev/null
else
printf 'RIFF....WAVEdata................................................' > "$last"
fi
exit 0
`,
{ mode: 0o755 },
);
fs.chmodSync(ffmpegScript, 0o755);
fs.writeFileSync(
vadScript,
`#!/bin/sh
echo "Speech segment 0: start = 100.00, end = 300.00"
exit 0
`,
{ mode: 0o755 },
);
fs.chmodSync(vadScript, 0o755);
try {
const builtin = await extractAudioSpeechSpans({
ffmpegPath: ffmpegScript,
videoPath,
audioStreamIndex: 1,
});
assert.equal(builtin.source, 'builtin-vad');
assert.equal(builtin.spans.length, 0);
assert.ok(getCachedAudioSpeechSpans(videoPath, 1) !== null);
assert.equal(
getCachedAudioSpeechSpans(videoPath, 1, {
vadExecutablePath: vadScript,
vadModelPath: modelPath,
}),
null,
);
const silero = await extractAudioSpeechSpans({
ffmpegPath: ffmpegScript,
videoPath,
audioStreamIndex: 1,
vadExecutablePath: vadScript,
vadModelPath: modelPath,
});
assert.equal(silero.source, 'silero-vad');
assert.deepEqual(silero.spans, [{ start: 1.0, end: 3.0 }]);
assert.equal(
getCachedAudioSpeechSpans(videoPath, 1, {
vadExecutablePath: vadScript,
vadModelPath: modelPath,
})?.source,
'silero-vad',
);
} finally {
clearAudioSpeechCache();
fs.rmSync(tmpDir, { recursive: true, force: true });
}
});