fix: skip chained initial trims before recording starts

Startup trim-skip only consulted the first active region at t=0, so
back-to-back or overlapping trims starting at zero (e.g. [0,500ms]
followed by [500ms,1000ms]) left the second region un-skipped. The
in-flight tick loop would catch it, but MediaRecorder was already
running by then, capturing up to one rAF frame of trimmed audio into
the blob and shifting the downstream timeline.

Loop findActiveTrimRegion from the advancing startPosition until no
region matches or startPosition >= effectiveEnd, bounded by
trimRegions.length for safety. Recompute initialSpeedRegion from the
final startPosition so playbackRate reflects the true start point.
This commit is contained in:
Enriquefft
2026-04-16 14:31:51 -05:00
parent 61e895a75a
commit 4d4b08db07
+9 -6
View File
@@ -227,15 +227,18 @@ export class AudioProcessor {
await audioContext.resume();
}
// Skip past any initial trim region before recording starts
// to avoid capturing trimmed audio during the first frames.
// Skip past any initial trim region(s) before recording starts to avoid
// capturing trimmed audio during the first rAF frames of playback.
// Loops to handle back-to-back or overlapping trims at t=0.
const effectiveEnd = validatedDurationSec ?? media.duration;
let startPosition = 0;
const initialTrim = this.findActiveTrimRegion(0, trimRegions);
if (initialTrim) {
startPosition = initialTrim.endMs / 1000;
for (let i = 0; i <= trimRegions.length; i++) {
const activeTrim = this.findActiveTrimRegion(startPosition * 1000, trimRegions);
if (!activeTrim) break;
startPosition = activeTrim.endMs / 1000;
if (startPosition >= effectiveEnd) break;
}
const effectiveEnd = validatedDurationSec ?? media.duration;
if (startPosition >= effectiveEnd) {
// All content is trimmed — return silent blob
return new Blob([], { type: "audio/webm" });