Skip to content

πŸŽ₯ Make videos programmatically with React

add wsola in renderer

JonnyBurgerd28632fc6d83
diff --git a/packages/renderer/src/assets/change-tempo.ts b/packages/renderer/src/assets/change-tempo.ts
new file mode 100644
index 0000000000..5a49f7e9a3
--- /dev/null
+++ b/packages/renderer/src/assets/change-tempo.ts
@@ -0,0 +1,237 @@
+function clamp16(x: number): number {
+ const y = Math.round(x);
+ return y < -32768 ? -32768 : y > 32767 ? 32767 : y;
+}
+
+/**
+ * WSOLA time-scale modification for interleaved Int16 PCM (multi-channel).
+ * - Preserves pitch approximately while changing tempo by factor f.
+ * - Works for N interleaved channels.
+ * - Mitigates head/tail fade-out via overlap-weight normalization and boundary reinforcement.
+ *
+ * @param input Interleaved Int16 PCM (e.g., LRLRLR... for stereo)
+ * @param channels Number of channels (>=1)
+ * @param f Tempo factor: >1 = faster/shorter, <1 = slower/longer
+ * @param opts Optional tuning parameters
+ * @returns Interleaved Int16Array with length β‰ˆ round(input.length * f)
+ */
+export function wsolaInt16Interleaved(
+ input: Int16Array,
+ channels: number,
+ f: number,
+): Int16Array {
+ if (!Number.isFinite(f) || f <= 0)
+ throw new Error('f must be a positive finite number');
+ if (!Number.isInteger(channels) || channels <= 0)
+ throw new Error('channels must be a positive integer');
+
+ const n = input.length;
+ if (n === 0) return new Int16Array(0);
+ if (n % channels !== 0)
+ throw new Error('input length must be a multiple of channels');
+
+ // Parameters and sensible defaults
+ const sampleRate = 48_000;
+ const frameMs = 30; // 20–40 ms typical
+ const overlapRatio = 0.5;
+ const searchMs = 8; // +/- 8 ms local search
+ const winKind = 'hann';
+ const headReinf = 3;
+ const tailReinf = 3;
+
+ // Work per-channel
+ const samplesPerChannel = (n / channels) | 0;
+
+ // Frame and hop sizing
+ const frameSize = Math.max(128, Math.floor((sampleRate * frameMs) / 1_000));
+ const overlap = Math.floor(frameSize * overlapRatio);
+ const anaHop = Math.max(1, frameSize - overlap);
+ const synHop = Math.max(1, Math.round(anaHop * f));
+
+ // Search radius in samples
+ const searchRadius = Math.max(0, Math.floor((sampleRate * searchMs) / 1_000));
+
+ // Window
+ const win = new Float32Array(frameSize);
+ for (let i = 0; i < frameSize; i++) {
+ const x = (Math.PI * 2 * i) / (frameSize - 1);
+ win[i] =
+ winKind === 'hann' ? 0.5 * (1 - Math.cos(x)) : 0.54 - 0.46 * Math.cos(x); // Hamming
+ }
+
+ // Estimate output length per channel and allocate with extra headroom
+ const estFrames = Math.max(
+ 1,
+ Math.ceil(Math.max(0, samplesPerChannel - frameSize) / anaHop) + 1,
+ );
+ const estLen = Math.max(0, frameSize + synHop * (estFrames - 1));
+ const extraHead = frameSize * (headReinf + 1);
+ const extraTail = frameSize * (tailReinf + 2);
+ const outLenAlloc = estLen + searchRadius + extraHead + extraTail;
+
+ const out = Array.from(
+ {length: channels},
+ () => new Float32Array(outLenAlloc),
+ );
+ const outWeight = new Float32Array(outLenAlloc);
+
+ // Temporary buffers
+ const chanFrames = Array.from(
+ {length: channels},
+ () => new Float32Array(frameSize),
+ );
+ const guideFrame = new Float32Array(frameSize);
+
+ // Helpers
+ function readChannelFrame(chan: number, start: number, dst: Float32Array) {
+ let srcIndex = start * channels + chan;
+ for (let i = 0; i < frameSize; i++) {
+ const pos = start + i;
+ dst[i] = pos >= 0 && pos < samplesPerChannel ? input[srcIndex] : 0;
+ srcIndex += channels;
+ }
+ }
+
+ function readGuideFrame(start: number) {
+ for (let i = 0; i < frameSize; i++) {
+ const pos = start + i;
+ if (pos >= 0 && pos < samplesPerChannel) {
+ let sum = 0;
+ const base = pos * channels;
+ for (let c = 0; c < channels; c++) sum += input[base + c];
+ guideFrame[i] = sum / channels;
+ } else {
+ guideFrame[i] = 0;
+ }
+ }
+ }
+
+ // Find best local alignment around outPos using normalized cross-correlation
+ function bestAlignment(outPoss: number): number {
+ let bestShift = 0;
+ let bestScore = -Infinity;
+ for (let shift = -searchRadius; shift <= searchRadius; shift++) {
+ const pos = outPoss + shift - overlap;
+ let score = 0;
+ let normA = 0;
+ let normB = 0;
+ for (let i = 0; i < overlap; i++) {
+ const idx = pos + i;
+ const outVal = idx >= 0 && idx < outLenAlloc ? out[0][idx] : 0; // channel 0 proxy
+ const frmVal = guideFrame[i];
+ score += outVal * frmVal;
+ normA += outVal * outVal;
+ normB += frmVal * frmVal;
+ }
+
+ const denom = Math.sqrt((normA || 1e-9) * (normB || 1e-9));
+ const corr = score / denom;
+ if (corr > bestScore) {
+ bestScore = corr;
+ bestShift = shift;
+ }
+ }
+
+ return bestShift;
+ }
+
+ // Overlap-add a frame for all channels at writeStart with windowing
+ function olaAllChannels(writeStart: number) {
+ for (let c = 0; c < channels; c++) {
+ for (let i = 0; i < frameSize; i++) {
+ const idx = writeStart + i;
+ if (idx >= 0 && idx < outLenAlloc) {
+ const w = win[i];
+ out[c][idx] += chanFrames[c][i] * w;
+ if (c === 0) outWeight[idx] += w; // track weights once
+ }
+ }
+ }
+ }
+
+ // 1) Seed: place the first frame at t=0
+ readGuideFrame(0);
+ for (let c = 0; c < channels; c++) readChannelFrame(c, 0, chanFrames[c]);
+ olaAllChannels(0);
+
+ // 2) Head reinforcement: place extra frames whose writeStart <= 0
+ for (let h = 0; h < headReinf; h++) {
+ // Option 1: reuse the first analysis position to strengthen early region
+ const headIn = Math.min(
+ anaHop * h,
+ Math.max(0, samplesPerChannel - frameSize),
+ );
+ readGuideFrame(headIn);
+ for (let c = 0; c < channels; c++)
+ readChannelFrame(c, headIn, chanFrames[c]);
+ // Align around outPos=0 so we bias writeStart near/before 0
+ const shift = bestAlignment(0);
+ const writeStart = shift - overlap; // likely negative; ok, we clamp on write
+ olaAllChannels(writeStart);
+ }
+
+ // 3) Main WSOLA loop
+ let inPos = anaHop; // next analysis position (we already seeded at 0)
+ let outPos = synHop; // next synthesis position
+
+ while (inPos < samplesPerChannel - 1) {
+ readGuideFrame(inPos);
+ for (let c = 0; c < channels; c++)
+ readChannelFrame(c, inPos, chanFrames[c]);
+
+ const shift = bestAlignment(outPos);
+ const writeStart = outPos + shift - overlap;
+ olaAllChannels(writeStart);
+
+ inPos += anaHop;
+ outPos += synHop;
+
+ // Safety: if we're very close to capacity, break to handle tail separately
+ if (outPos > outLenAlloc - (frameSize + searchRadius + 8)) break;
+ }
+
+ // 4) Tail reinforcement: ensure the end gets full coverage
+ // Place a few extra frames around the last outPos using the last available input frames.
+ for (let t = 0; t < tailReinf; t++) {
+ const tailIn = Math.max(
+ 0,
+ Math.min(samplesPerChannel - frameSize, inPos - anaHop * t),
+ );
+ readGuideFrame(tailIn);
+ for (let c = 0; c < channels; c++)
+ readChannelFrame(c, tailIn, chanFrames[c]);
+
+ const shift = bestAlignment(outPos);
+ const writeStart = outPos + shift - overlap;
+ olaAllChannels(writeStart);
+
+ outPos += synHop;
+ }
+
+ // 5) Normalize by accumulated weights BEFORE trimming
+ for (let i = 0; i < outLenAlloc; i++) {
+ const w = outWeight[i];
+ if (w > 1e-9) {
+ const inv = 1 / w;
+ for (let c = 0; c < channels; c++) out[c][i] *= inv;
+ } else {
+ for (let c = 0; c < channels; c++) out[c][i] = 0;
+ }
+ }
+
+ // 6) Produce final interleaved Int16Array with length β‰ˆ round(n * f)
+ const targetPerChan = Math.max(1, Math.round(samplesPerChannel * f));
+ const targetTotal = targetPerChan * channels;
+ const result = new Int16Array(targetTotal);
+
+ // Interleave and clamp/round
+ for (let i = 0; i < targetPerChan; i++) {
+ for (let c = 0; c < channels; c++) {
+ const v = i < out[c].length ? out[c][i] : 0;
+ const y = clamp16(v);
+ result[i * channels + c] = y;
+ }
+ }
+
+ return result;
+}
diff --git a/packages/renderer/src/assets/change-tonefrequency.ts b/packages/renderer/src/assets/change-tonefrequency.ts
new file mode 100644
index 0000000000..3a2065635b
--- /dev/null
+++ b/packages/renderer/src/assets/change-tonefrequency.ts
@@ -0,0 +1,44 @@
+import {wsolaInt16Interleaved} from './change-tempo';
+import {resampleAudioData} from './resample-audiodata';
+
+const TARGET_SAMPLE_RATE = 48000;
+const NUMBER_OF_CHANNELS = 2;
+
+export const applyToneFrequency = (
+ numberOfFrames: number,
+ audioData: Int16Array,
+ toneFrequency: number,
+): Int16Array => {
+ // In FFmpeg, we apply toneFrequency as follows:
+ // `asetrate=${DEFAULT_SAMPLE_RATE}*${toneFrequency},aresample=${DEFAULT_SAMPLE_RATE},atempo=1/${toneFrequency}`
+ // So there are 2 steps:
+ // 1. Change the assumed sample rate
+ // 2. Resample to 48Khz
+ // 3. Apply playback rate
+
+ const step1SampleRate = TARGET_SAMPLE_RATE * toneFrequency;
+
+ const newNumberOfFrames = Math.round(
+ numberOfFrames * (TARGET_SAMPLE_RATE / step1SampleRate),
+ );
+
+ const step2Data = new Int16Array(newNumberOfFrames * NUMBER_OF_CHANNELS);
+
+ const chunkSize = numberOfFrames / newNumberOfFrames;
+
+ resampleAudioData({
+ srcNumberOfChannels: NUMBER_OF_CHANNELS,
+ sourceChannels: audioData,
+ destination: step2Data,
+ targetFrames: newNumberOfFrames,
+ chunkSize,
+ });
+
+ const step3Data = wsolaInt16Interleaved(
+ step2Data,
+ NUMBER_OF_CHANNELS,
+ toneFrequency,
+ );
+
+ return step3Data;
+};
diff --git a/packages/renderer/src/assets/inline-audio-mixing.ts b/packages/renderer/src/assets/inline-audio-mixing.ts
index b46d930952..7d9ad7d346 100644
--- a/packages/renderer/src/assets/inline-audio-mixing.ts
+++ b/packages/renderer/src/assets/inline-audio-mixing.ts
@@ -26,6 +26,7 @@ export const makeInlineAudioMixing = (dir: string) => {
// asset id -> file descriptor
const openFiles: Record<string, number> = {};
const writtenHeaders: Record<string, boolean> = {};
+ const toneFrequencies: Record<string, number> = {};
const cleanup = () => {
for (const fd of Object.values(openFiles)) {
@@ -132,6 +133,12 @@ export const makeInlineAudioMixing = (dir: string) => {
); // Remaining size
};
+ const finish = () => {
+ for (const fd of Object.keys(openFiles)) {
+ console.log('finished', fd, toneFrequencies[fd]);
+ }
+ };
+
const addAsset = ({
asset,
fps,
@@ -154,6 +161,16 @@ export const makeInlineAudioMixing = (dir: string) => {
trimLeftOffset,
trimRightOffset,
});
+ if (
+ toneFrequencies[asset.id] !== undefined &&
+ toneFrequencies[asset.id] !== asset.toneFrequency
+ ) {
+ throw new Error(
+ `toneFrequency must be the same across the audio, got ${asset.toneFrequency}, but before it was ${toneFrequencies[asset.id]}`,
+ );
+ }
+
+ toneFrequencies[asset.id] = asset.toneFrequency;
const filePath = getFilePath(asset);
const fileDescriptor = openFiles[filePath];
@@ -219,6 +236,7 @@ export const makeInlineAudioMixing = (dir: string) => {
cleanup,
addAsset,
getListOfAssets,
+ finish,
};
};
diff --git a/packages/renderer/src/assets/resample-audiodata.ts b/packages/renderer/src/assets/resample-audiodata.ts
new file mode 100644
index 0000000000..91f8311369
--- /dev/null
+++ b/packages/renderer/src/assets/resample-audiodata.ts
@@ -0,0 +1,92 @@
+// Remotion exports all videos with 2 channels.
+export const TARGET_NUMBER_OF_CHANNELS = 2;
+
+// Remotion exports all videos with 48kHz sample rate.
+export const TARGET_SAMPLE_RATE = 48000;
+
+const fixFloatingPoint = (value: number) => {
+ if (value % 1 < 0.0000001) {
+ return Math.floor(value);
+ }
+
+ if (value % 1 > 0.9999999) {
+ return Math.ceil(value);
+ }
+
+ return value;
+};
+
+export const resampleAudioData = ({
+ srcNumberOfChannels,
+ sourceChannels,
+ destination,
+ targetFrames,
+ chunkSize,
+}: {
+ srcNumberOfChannels: number;
+ sourceChannels: Int16Array;
+ destination: Int16Array;
+ targetFrames: number;
+ chunkSize: number;
+}) => {
+ const getSourceValues = (
+ startUnfixed: number,
+ endUnfixed: number,
+ channelIndex: number,
+ ) => {
+ const start = fixFloatingPoint(startUnfixed);
+ const end = fixFloatingPoint(endUnfixed);
+ const startFloor = Math.floor(start);
+ const startCeil = Math.ceil(start);
+ const startFraction = start - startFloor;
+ const endFraction = end - Math.floor(end);
+ const endFloor = Math.floor(end);
+
+ let weightedSum = 0;
+ let totalWeight = 0;
+
+ // Handle first fractional sample
+ if (startFraction > 0) {
+ const firstSample =
+ sourceChannels[startFloor * srcNumberOfChannels + channelIndex];
+ weightedSum += firstSample * (1 - startFraction);
+ totalWeight += 1 - startFraction;
+ }
+
+ // Handle full samples
+ for (let k = startCeil; k < endFloor; k++) {
+ const num = sourceChannels[k * srcNumberOfChannels + channelIndex];
+ weightedSum += num;
+ totalWeight += 1;
+ }
+
+ // Handle last fractional sample
+ if (endFraction > 0) {
+ const lastSample =
+ sourceChannels[endFloor * srcNumberOfChannels + channelIndex];
+ weightedSum += lastSample * endFraction;
+ totalWeight += endFraction;
+ }
+
+ const average = weightedSum / totalWeight;
+
+ return average;
+ };
+
+ for (let newFrameIndex = 0; newFrameIndex < targetFrames; newFrameIndex++) {
+ const start = newFrameIndex * chunkSize;
+ const end = start + chunkSize;
+
+ if (TARGET_NUMBER_OF_CHANNELS !== srcNumberOfChannels) {
+ throw new Error('srcNumberOfChannels must be 2');
+ }
+
+ for (let i = 0; i < srcNumberOfChannels; i++) {
+ destination[newFrameIndex * srcNumberOfChannels + i] = getSourceValues(
+ start,
+ end,
+ i,
+ );
+ }
+ }
+};
diff --git a/packages/renderer/src/create-audio.ts b/packages/renderer/src/create-audio.ts
index 54f006ceb0..2c414f6b29 100644
--- a/packages/renderer/src/create-audio.ts
+++ b/packages/renderer/src/create-audio.ts
@@ -123,6 +123,8 @@ export const createAudio = async ({
}),
);
+ downloadMap.inlineAudioMixing.finish();
+
const inlinedAudio = downloadMap.inlineAudioMixing.getListOfAssets();
const preprocessed: PreprocessedAudioTrack[] = [