Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Fix hold transitions in timeline volume curves

Jonny Burger45163e6f384a
diff --git a/packages/example/src/AudioTesting/VolumeHoldCurve.tsx b/packages/example/src/AudioTesting/VolumeHoldCurve.tsx
new file mode 100644
index 0000000000..389c178b8a
--- /dev/null
+++ b/packages/example/src/AudioTesting/VolumeHoldCurve.tsx
@@ -0,0 +1,85 @@
+import {Audio, Video} from '@remotion/media';
+import React from 'react';
+import {
+ AbsoluteFill,
+ Composition,
+ Easing,
+ interpolate,
+ staticFile,
+ useCurrentFrame,
+} from 'remotion';
+
+const VolumeHoldCurve: React.FC = () => {
+ const frame = useCurrentFrame();
+
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: '#111827',
+ color: 'white',
+ fontFamily: 'sans-serif',
+ padding: 48,
+ }}
+ >
+ <h1 style={{fontSize: 48, margin: 0}}>Volume hold curves</h1>
+ <p style={{fontSize: 26, lineHeight: 1.5}}>
+ Zoom into the timeline waveform. Holds should have vertical edges at
+ frames 60, 120, and 188. The fade changes from frames 180 to 240.
+ </p>
+ <p style={{fontSize: 24}}>Frame {frame} / 299</p>
+ <Video
+ name="Video: hold drops at 188"
+ src={staticFile('demo_smpte_h264_aac.mp4')}
+ loop
+ loopVolumeCurveBehavior="extend"
+ durationInFrames={300}
+ style={{
+ width: 480,
+ height: 270,
+ position: 'absolute',
+ right: 48,
+ bottom: 48,
+ }}
+ volume={(f) =>
+ interpolate(f, [187, 188], [1, 0], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ easing: [Easing.step1],
+ })
+ }
+ />
+ <Audio
+ name="Audio: holds at 60 and 120, then smooth fade"
+ src={staticFile('audio-48000hz.wav')}
+ loop
+ loopVolumeCurveBehavior="extend"
+ durationInFrames={300}
+ volume={(f) =>
+ interpolate(f, [0, 60, 120, 180, 240], [1, 0.25, 1, 1, 0], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ easing: [
+ Easing.step1,
+ Easing.step1,
+ Easing.linear,
+ Easing.bezier(0.42, 0, 0.58, 1),
+ ],
+ })
+ }
+ />
+ </AbsoluteFill>
+ );
+};
+
+export const VolumeHoldCurveComposition: React.FC = () => {
+ return (
+ <Composition
+ id="volume-hold-curve"
+ component={VolumeHoldCurve}
+ durationInFrames={300}
+ fps={30}
+ width={1280}
+ height={720}
+ />
+ );
+};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index baa666cf6e..50aa589e61 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -231,6 +231,7 @@ import {
issue10468DefaultProps,
issue5758DefaultProps,
} from './AudioTesting/Issue10468';
+import {VolumeHoldCurveComposition} from './AudioTesting/VolumeHoldCurve';
import {BrowserTest} from './BrowserTest';
import {
CanvasCapturePreview,
@@ -1795,6 +1796,7 @@ export const Index: React.FC = () => {
/>
</Folder>
<Folder name="audio-tests">
+ <VolumeHoldCurveComposition />
<Composition
id="complex-sounds"
component={ComplexSounds}
diff --git a/packages/studio/src/components/AudioWaveform.tsx b/packages/studio/src/components/AudioWaveform.tsx
index 287dbd8213..72679ffb24 100644
--- a/packages/studio/src/components/AudioWaveform.tsx
+++ b/packages/studio/src/components/AudioWaveform.tsx
@@ -219,20 +219,40 @@ const AudioWaveformInner: React.FC<{
context.moveTo(0, 0);
// The canvas only spans the virtualized range. Sampling by its physical
// width keeps drawing work bounded to the visible part of the timeline.
- const numberOfPoints = Math.max(2, Math.ceil(drawingWidth));
+ const numberOfPoints = Math.min(
+ visibleVolume.length,
+ Math.max(1, Math.ceil(drawingWidth)),
+ );
for (let point = 0; point < numberOfPoints; point++) {
- const progress = point / (numberOfPoints - 1);
- const volumeIndex = progress * (visibleVolume.length - 1);
- const leftIndex = Math.floor(volumeIndex);
- const rightIndex = Math.ceil(volumeIndex);
- const leftVolume = visibleVolume[leftIndex] ?? 1;
- const rightVolume = visibleVolume[rightIndex] ?? leftVolume;
- const interpolatedVolume =
- leftVolume + (rightVolume - leftVolume) * (volumeIndex - leftIndex);
- const x = progress * drawingWidth;
- const unclampedY = (1 - interpolatedVolume / visualizationMaxVolume) * h;
+ const volumeIndex = Math.floor(
+ (point / numberOfPoints) * visibleVolume.length,
+ );
+ const nextVolumeIndex = Math.floor(
+ ((point + 1) / numberOfPoints) * visibleVolume.length,
+ );
+ const firstFrame = Math.max(0, Math.floor(displayOffsetInFrames));
+ const x = Math.max(
+ 0,
+ ((firstFrame + volumeIndex - displayOffsetInFrames) /
+ displayDurationInFrames) *
+ drawingWidth,
+ );
+ const nextX =
+ point === numberOfPoints - 1
+ ? drawingWidth
+ : Math.min(
+ drawingWidth,
+ ((firstFrame + nextVolumeIndex - displayOffsetInFrames) /
+ displayDurationInFrames) *
+ drawingWidth,
+ );
+ const unclampedY =
+ (1 - (visibleVolume[volumeIndex] ?? 1) / visualizationMaxVolume) * h;
const y = Math.max(0, Math.min(h, unclampedY));
+ // Volume samples apply for one frame. Keep each value until the next
+ // frame boundary instead of blending a hold into a diagonal ramp.
context.lineTo(x, y);
+ context.lineTo(nextX, y);
}
context.lineTo(drawingWidth, 0);
@@ -243,6 +263,8 @@ const AudioWaveformInner: React.FC<{
);
context.fill();
}, [
+ displayDurationInFrames,
+ displayOffsetInFrames,
height,
peaks,
shouldRenderVolumeOverlay,