Skip to content

πŸŽ₯ Make videos programmatically with React

Keep s16 fast path in convertAudioData, only normalize f32 inputs

Jonny Burgerac7749d5b13b
diff --git a/packages/media/src/convert-audiodata/convert-audiodata.ts b/packages/media/src/convert-audiodata/convert-audiodata.ts
index 60656da82d..1ff6fbb8b5 100644
--- a/packages/media/src/convert-audiodata/convert-audiodata.ts
+++ b/packages/media/src/convert-audiodata/convert-audiodata.ts
@@ -83,37 +83,49 @@ export const convertAudioData = ({
);
}
- // Normalize to f32-planar first, then convert to s16. This ensures
- // consistent output across browsers β€” Firefox decodes as f32 while
- // Chrome decodes as s16-planar, and their native copyTo(s16) conversions
- // can differ. By going through a f32-planar AudioData, the final s16
- // conversion always starts from the same representation.
- const bytesPerPlane = frameCount * 4;
- const f32Buffer = new ArrayBuffer(srcNumberOfChannels * bytesPerPlane);
- for (let ch = 0; ch < srcNumberOfChannels; ch++) {
- audioData.copyTo(
- new Float32Array(f32Buffer, ch * bytesPerPlane, frameCount),
- {planeIndex: ch, frameOffset, frameCount, format: 'f32-planar'},
- );
- }
-
- const f32AudioData = new AudioData({
- format: 'f32-planar',
- sampleRate: currentSampleRate,
- numberOfFrames: frameCount,
- numberOfChannels: srcNumberOfChannels,
- timestamp: audioData.timestamp,
- data: f32Buffer,
- });
-
const srcChannels = new Int16Array(srcNumberOfChannels * frameCount);
- f32AudioData.copyTo(srcChannels, {
- planeIndex: 0,
- format: FORMAT,
- frameOffset: 0,
- frameCount,
- });
- f32AudioData.close();
+
+ // https://github.com/remotion-dev/remotion/issues/6493
+ const isF32 =
+ audioData.format === 'f32' || audioData.format === 'f32-planar';
+
+ if (isF32) {
+ // Firefox decodes as f32 β€” normalize to f32-planar first so the
+ // final s16 conversion always starts from the same representation.
+ const bytesPerPlane = frameCount * 4;
+ const f32Buffer = new ArrayBuffer(srcNumberOfChannels * bytesPerPlane);
+ for (let ch = 0; ch < srcNumberOfChannels; ch++) {
+ audioData.copyTo(
+ new Float32Array(f32Buffer, ch * bytesPerPlane, frameCount),
+ {planeIndex: ch, frameOffset, frameCount, format: 'f32-planar'},
+ );
+ }
+
+ const f32AudioData = new AudioData({
+ format: 'f32-planar',
+ sampleRate: currentSampleRate,
+ numberOfFrames: frameCount,
+ numberOfChannels: srcNumberOfChannels,
+ timestamp: audioData.timestamp,
+ data: f32Buffer,
+ });
+
+ f32AudioData.copyTo(srcChannels, {
+ planeIndex: 0,
+ format: FORMAT,
+ frameOffset: 0,
+ frameCount,
+ });
+ f32AudioData.close();
+ } else {
+ // Chrome decodes as s16-planar β€” copy directly to interleaved s16.
+ audioData.copyTo(srcChannels, {
+ planeIndex: 0,
+ format: FORMAT,
+ frameOffset,
+ frameCount,
+ });
+ }
const data = new Int16Array(newNumberOfFrames * TARGET_NUMBER_OF_CHANNELS);
const chunkSize = frameCount / newNumberOfFrames;