🎥 Make videos programmatically with React
`@remotion/media-utils`: Clamp negative start offsets in getWaveformPortion
John Costa2163d5573d1e
diff --git a/packages/media-utils/package.json b/packages/media-utils/package.jsonindex 5eff901162..2bad6cad10 100644--- a/packages/media-utils/package.json+++ b/packages/media-utils/package.json@@ -10,7 +10,8 @@"formatting": "oxfmt src --check","format": "oxfmt src","lint": "eslint src",- "make": "tsgo -d && bun --env-file=../.env.bundle bundle.ts"+ "make": "tsgo -d && bun --env-file=../.env.bundle bundle.ts",+ "test": "bun test src"},"types": "dist/index.d.ts","module": "dist/esm/index.mjs",diff --git a/packages/media-utils/src/get-waveform-portion.ts b/packages/media-utils/src/get-waveform-portion.tsindex 8375965067..d29d71e09c 100644--- a/packages/media-utils/src/get-waveform-portion.ts+++ b/packages/media-utils/src/get-waveform-portion.ts@@ -63,11 +63,13 @@ export const getWaveformPortion = ({audioData.sampleRate,);- const samplesBeforeStart = 0 - startSample;- const samplesAfterEnd = endSample - waveform.length;+ // Only pad the part of the requested window that lies outside the audio+ const samplesBeforeStart = Math.min(endSample, 0) - startSample;+ const samplesAfterEnd = endSample - Math.max(startSample, waveform.length);- const clampedStart = Math.max(startSample, 0);- const clampedEnd = Math.min(waveform.length, endSample);+ // A negative slice index would count from the end and leak real audio+ const clampedStart = Math.min(Math.max(startSample, 0), waveform.length);+ const clampedEnd = Math.min(Math.max(endSample, 0), waveform.length);const padStart =samplesBeforeStart > 0diff --git a/packages/media-utils/src/test/get-waveform-portion.test.ts b/packages/media-utils/src/test/get-waveform-portion.test.tsnew file mode 100644index 0000000000..5bc4e739ec--- /dev/null+++ b/packages/media-utils/src/test/get-waveform-portion.test.ts@@ -0,0 +1,42 @@+import {expect, test} from 'bun:test';+import {getWaveformPortion} from '../get-waveform-portion';+import type {MediaUtilsAudioData} from '../types';++// 10 seconds of constant 0.5 amplitude at 1000 Hz+const audioData: MediaUtilsAudioData = {+ channelWaveforms: [new Float32Array(10000).fill(0.5)],+ sampleRate: 1000,+ durationInSeconds: 10,+ numberOfChannels: 1,+ resultId: 'test',+ isRemote: false,+};++const amplitudes = (startTimeInSeconds: number) =>+ getWaveformPortion({+ audioData,+ startTimeInSeconds,+ durationInSeconds: 0.5,+ numberOfSamples: 5,+ normalize: false,+ }).map((bar) => bar.amplitude);++test('window inside the audio returns real samples', () => {+ expect(amplitudes(1)).toEqual([0.5, 0.5, 0.5, 0.5, 0.5]);+});++test('window entirely before the audio is silence only', () => {+ expect(amplitudes(-2)).toEqual([0, 0, 0, 0, 0]);+});++test('window partially before the audio pads the start with silence', () => {+ expect(amplitudes(-0.2)).toEqual([0, 0, 0.5, 0.5, 0.5]);+});++test('window past the end of the audio pads the end with silence', () => {+ expect(amplitudes(9.8)).toEqual([0.5, 0.5, 0, 0, 0]);+});++test('window entirely after the audio is silence only', () => {+ expect(amplitudes(12)).toEqual([0, 0, 0, 0, 0]);+});