Skip to content

🎥 Make videos programmatically with React

`@remotion/timeline-utils`: Fix waveform playback rate display (#9599)

Jonny Burgerc87aa9e275c2
diff --git a/packages/timeline-utils/src/audio-waveform/slice-waveform-peaks.ts b/packages/timeline-utils/src/audio-waveform/slice-waveform-peaks.ts
index 61b33b956f..9042ec1a77 100644
--- a/packages/timeline-utils/src/audio-waveform/slice-waveform-peaks.ts
+++ b/packages/timeline-utils/src/audio-waveform/slice-waveform-peaks.ts
@@ -25,8 +25,27 @@ export const sliceWaveformPeaks = ({
(startTimeInSeconds + durationInSeconds) * TARGET_SAMPLE_RATE,
);
- return peaks.subarray(
- Math.max(0, startPeakIndex),
- Math.min(peaks.length, endPeakIndex),
- );
+ if (!Number.isFinite(startPeakIndex) || !Number.isFinite(endPeakIndex)) {
+ return peaks.subarray(
+ Math.max(0, startPeakIndex),
+ Math.min(peaks.length, endPeakIndex),
+ );
+ }
+
+ if (startPeakIndex >= 0 && endPeakIndex <= peaks.length) {
+ return peaks.subarray(startPeakIndex, endPeakIndex);
+ }
+
+ const portion = new Float32Array(Math.max(0, endPeakIndex - startPeakIndex));
+ const sourceStart = Math.max(0, startPeakIndex);
+ const sourceEnd = Math.min(peaks.length, endPeakIndex);
+
+ if (sourceStart < sourceEnd) {
+ portion.set(
+ peaks.subarray(sourceStart, sourceEnd),
+ sourceStart - startPeakIndex,
+ );
+ }
+
+ return portion;
};
diff --git a/packages/timeline-utils/src/test/slice-waveform-peaks.test.ts b/packages/timeline-utils/src/test/slice-waveform-peaks.test.ts
index b86a005ba9..c2c81fc32c 100644
--- a/packages/timeline-utils/src/test/slice-waveform-peaks.test.ts
+++ b/packages/timeline-utils/src/test/slice-waveform-peaks.test.ts
@@ -30,3 +30,53 @@ test('Should return an empty waveform unchanged', () => {
expect(sliced).toBe(peaks);
});
+
+test('Should preserve silence after the waveform when playback is faster', () => {
+ const peaks = Float32Array.from({length: 100}, (_, i) => i + 1);
+
+ const sliced = sliceWaveformPeaks({
+ peaks,
+ startFrom: 0,
+ durationInFrames: 30,
+ fps: 30,
+ playbackRate: 2,
+ });
+
+ expect(Array.from(sliced)).toEqual([
+ ...Array.from({length: 100}, (_, i) => i + 1),
+ ...Array.from({length: 100}, () => 0),
+ ]);
+});
+
+test('Should preserve silence before the waveform', () => {
+ const peaks = Float32Array.from({length: 100}, (_, i) => i + 1);
+
+ const sliced = sliceWaveformPeaks({
+ peaks,
+ startFrom: -15,
+ durationInFrames: 30,
+ fps: 30,
+ playbackRate: 1,
+ });
+
+ expect(Array.from(sliced)).toEqual([
+ ...Array.from({length: 50}, () => 0),
+ ...Array.from({length: 50}, (_, i) => i + 1),
+ ]);
+});
+
+test('Should handle an infinite timeline duration', () => {
+ const peaks = Float32Array.from({length: 100}, (_, i) => i + 1);
+
+ const sliced = sliceWaveformPeaks({
+ peaks,
+ startFrom: 15,
+ durationInFrames: Infinity,
+ fps: 30,
+ playbackRate: 1,
+ });
+
+ expect(Array.from(sliced)).toEqual(
+ Array.from({length: 50}, (_, i) => i + 51),
+ );
+});