🎥 Make videos programmatically with React
`@remotion/media`: measure the audio cache in bytes, not timestamps (#10214)
Alec Watts6b263aecbf2b
diff --git a/packages/media/src/audio-extraction/audio-cache.ts b/packages/media/src/audio-extraction/audio-cache.tsindex e77e9f1a02..75076d0f56 100644--- a/packages/media/src/audio-extraction/audio-cache.ts+++ b/packages/media/src/audio-extraction/audio-cache.ts@@ -1,5 +1,26 @@import type {AudioSample} from 'mediabunny';+type AudioSampleFormat = AudioSample['format'];++const BYTES_PER_SAMPLE: Record<AudioSampleFormat, number> = {+ u8: 1,+ s16: 2,+ s32: 4,+ f32: 4,+ 'u8-planar': 1,+ 's16-planar': 2,+ 's32-planar': 4,+ 'f32-planar': 4,+};++const getAudioSampleByteSize = (sample: AudioSample) => {+ return (+ sample.numberOfFrames *+ sample.numberOfChannels *+ BYTES_PER_SAMPLE[sample.format]+ );+};+export const makeAudioCache = () => {const timestamps: number[] = [];const samples: Record<number, AudioSample> = {};@@ -59,6 +80,15 @@ export const makeAudioCache = () => {return timestamps;};+ const getTotalSize = () => {+ let total = 0;+ for (const timestamp of timestamps) {+ total += getAudioSampleByteSize(samples[timestamp]);+ }++ return total;+ };+const getOldestTimestamp = () => {return timestamps[0];};@@ -80,6 +110,7 @@ export const makeAudioCache = () => {getOldestTimestamp,getNewestTimestamp,getOpenTimestamps,+ getTotalSize,};};diff --git a/packages/media/src/audio-extraction/audio-iterator.ts b/packages/media/src/audio-extraction/audio-iterator.tsindex 71470e7f0d..eb89b7c281 100644--- a/packages/media/src/audio-extraction/audio-iterator.ts+++ b/packages/media/src/audio-extraction/audio-iterator.ts@@ -159,7 +159,7 @@ export const makeAudioIterator = ({const getCacheStats = () => {return {count: cache.getOpenTimestamps().length,- size: cache.getOpenTimestamps().reduce((acc, t) => acc + t, 0),+ size: cache.getTotalSize(),};};diff --git a/packages/media/src/test/audio-cache-size.test.ts b/packages/media/src/test/audio-cache-size.test.tsnew file mode 100644index 0000000000..3ce3aa43f9--- /dev/null+++ b/packages/media/src/test/audio-cache-size.test.ts@@ -0,0 +1,76 @@+import type {AudioSample} from 'mediabunny';+import {expect, test} from 'vitest';+import {makeAudioCache} from '../audio-extraction/audio-cache';++type AudioSampleFormat = AudioSample['format'];++const makeSample = ({+ timestamp,+ format = 'f32',+ numberOfFrames = 1024,+ numberOfChannels = 2,+}: {+ timestamp: number;+ format?: AudioSampleFormat;+ numberOfFrames?: number;+ numberOfChannels?: number;+}) =>+ ({+ timestamp,+ duration: 0.02,+ format,+ numberOfFrames,+ numberOfChannels,+ close: () => undefined,+ }) as unknown as AudioSample;++test('cache size is measured in bytes, not timestamps', () => {+ const cache = makeAudioCache();++ // Two identical samples that happen to sit far apart in the timeline. Their+ // memory footprint is the same, so the reported size must be too.+ cache.addFrame(makeSample({timestamp: 1}));+ cache.addFrame(makeSample({timestamp: 3600}));++ const bytesPerSample = 1024 * 2 * 4;+ expect(cache.getTotalSize()).toBe(bytesPerSample * 2);++ // The old implementation summed the timestamps, which would give 3601.+ expect(cache.getTotalSize()).not.toBe(3601);+});++test('size does not depend on where in the timeline the samples are', () => {+ const early = makeAudioCache();+ early.addFrame(makeSample({timestamp: 0}));+ early.addFrame(makeSample({timestamp: 0.02}));++ const late = makeAudioCache();+ late.addFrame(makeSample({timestamp: 5000}));+ late.addFrame(makeSample({timestamp: 5000.02}));++ expect(late.getTotalSize()).toBe(early.getTotalSize());+});++test('size accounts for sample format', () => {+ const f32 = makeAudioCache();+ f32.addFrame(makeSample({timestamp: 0, format: 'f32'}));++ const s16 = makeAudioCache();+ s16.addFrame(makeSample({timestamp: 0, format: 's16'}));++ expect(f32.getTotalSize()).toBe(1024 * 2 * 4);+ expect(s16.getTotalSize()).toBe(1024 * 2 * 2);+});++test('size accounts for channel count and frame count', () => {+ const cache = makeAudioCache();+ cache.addFrame(+ makeSample({timestamp: 0, numberOfChannels: 6, numberOfFrames: 2048}),+ );++ expect(cache.getTotalSize()).toBe(2048 * 6 * 4);+});++test('an empty cache has no size', () => {+ expect(makeAudioCache().getTotalSize()).toBe(0);+});