π₯ Make videos programmatically with React
`@remotion/renderer`: Fix floating point error in audio mixing
Jonny Burger6a1d4133ea71
diff --git a/packages/renderer/src/assets/inline-audio-mixing.ts b/packages/renderer/src/assets/inline-audio-mixing.tsindex 7a53a219bd..9b5acb9cd8 100644--- a/packages/renderer/src/assets/inline-audio-mixing.ts+++ b/packages/renderer/src/assets/inline-audio-mixing.ts@@ -21,6 +21,21 @@ const numberTo16BitLittleEndian = (num: number) => {return new Uint8Array([num & 0xff, (num >> 8) & 0xff]);};+/**+ * When multiplying seconds by sample rate, floating point errors can cause+ * results like 244799.99999999997 instead of 244800 (5.1 * 48000).+ * This snaps to the nearest integer when within tolerance,+ * preventing Math.floor/Math.ceil from rounding incorrectly.+ */+const correctFloatingPointError = (value: number): number => {+ const rounded = Math.round(value);+ if (Math.abs(value - rounded) < 0.00001) {+ return rounded;+ }++ return value;+};+const BIT_DEPTH = 16;const BYTES_PER_SAMPLE = BIT_DEPTH / 8;const NUMBER_OF_CHANNELS = 2;@@ -212,13 +227,18 @@ export const makeInlineAudioMixing = (dir: string) => {// could hit this caseif (isFirst) {- arr = arr.slice(Math.floor(samplesToShaveFromStart) * NUMBER_OF_CHANNELS);+ arr = arr.slice(+ Math.floor(correctFloatingPointError(samplesToShaveFromStart)) *+ NUMBER_OF_CHANNELS,+ );}if (isLast) {arr = arr.slice(0,- arr.length + Math.ceil(samplesToShaveFromEnd) * NUMBER_OF_CHANNELS,+ arr.length ++ Math.ceil(correctFloatingPointError(samplesToShaveFromEnd)) *+ NUMBER_OF_CHANNELS,);}@@ -230,7 +250,9 @@ export const makeInlineAudioMixing = (dir: string) => {// This might lead to overlapping, hopefully aligning perfectly!// Test case: https://github.com/remotion-dev/remotion/issues/5758const position =- Math.floor(positionInSeconds * DEFAULT_SAMPLE_RATE) *+ Math.floor(+ correctFloatingPointError(positionInSeconds * DEFAULT_SAMPLE_RATE),+ ) *NUMBER_OF_CHANNELS *BYTES_PER_SAMPLE;diff --git a/packages/renderer/src/print-useful-error-message.ts b/packages/renderer/src/print-useful-error-message.tsindex ae6e0b2bfe..eb2de742d9 100644--- a/packages/renderer/src/print-useful-error-message.ts+++ b/packages/renderer/src/print-useful-error-message.ts@@ -112,7 +112,7 @@ export const printUsefulErrorMessage = (indent,logLevel,},- 'π‘ You might need to set the OpenGL renderer to "angle-egl", "angle" (or "swangle" if rendering on lambda). Learn why at https://www.remotion.dev/docs/three',+ 'π‘ You might need to set the OpenGL renderer to "angle". Learn why at https://www.remotion.dev/docs/three',);Log.warn({