Skip to content

🎥 Make videos programmatically with React

Use discriminated unions for timeline volume curves

JonnyBurgerfac795929e01
diff --git a/packages/studio/src/components/AudioWaveform.tsx b/packages/studio/src/components/AudioWaveform.tsx
index 0fe56a77d8..6be496a90d 100644
--- a/packages/studio/src/components/AudioWaveform.tsx
+++ b/packages/studio/src/components/AudioWaveform.tsx
@@ -1,6 +1,5 @@
import {
drawBars,
- getVisibleWaveformVolume,
subscribeToWaveformPeaks,
type WaveformDrawRange,
} from '@remotion/timeline-utils';
@@ -79,39 +78,53 @@ const AudioWaveformInner: React.FC<{
const waveformCanvas = useRef<HTMLCanvasElement>(null);
const volumeCanvas = useRef<HTMLCanvasElement>(null);
- const shouldRenderVolumeOverlay = typeof volume !== 'number';
+ const shouldRenderVolumeOverlay = volume.type !== 'constant';
const visualizationMaxVolume = useMemo(() => {
- if (typeof volume === 'number') {
- return Math.max(1, volume);
+ if (volume.type === 'constant') {
+ return Math.max(1, volume.value);
}
- if ('points' in volume) {
+ if (volume.type === 'linear') {
return volume.points.reduce(
(highest, point) => Math.max(highest, point.value),
1,
);
}
- return volume.reduce((highest, value) => {
+ return volume.samples.reduce((highest, value) => {
return Number.isFinite(value) ? Math.max(highest, value) : highest;
}, 1);
}, [volume]);
const visibleVolume = useMemo((): TimelineVolume => {
if (muted) {
- return 0;
+ return {type: 'constant', value: 0};
}
- if (typeof volume !== 'number' && 'points' in volume) {
+ if (volume.type !== 'sampled') {
return volume;
}
- return getVisibleWaveformVolume({
- displayDurationInFrames,
- displayOffsetInFrames,
- volume,
- });
+ if (
+ !Number.isFinite(displayDurationInFrames) ||
+ displayDurationInFrames <= 0
+ ) {
+ return {type: 'sampled', samples: []};
+ }
+
+ // Samples already cover the composition timeline, including media loops.
+ return {
+ type: 'sampled',
+ samples: volume.samples.slice(
+ Math.max(0, Math.floor(displayOffsetInFrames)),
+ Math.min(
+ volume.samples.length,
+ Math.ceil(displayOffsetInFrames + displayDurationInFrames),
+ ),
+ ),
+ };
}, [displayDurationInFrames, displayOffsetInFrames, muted, volume]);
- const waveformVolume = typeof visibleVolume === 'number' ? visibleVolume : 1;
+ const waveformVolume =
+ visibleVolume.type === 'constant' ? visibleVolume.value : 1;
// Layout effect so that a cache hit sets the peaks synchronously and the
// waveform is painted on the very first frame after mounting.
@@ -212,14 +225,14 @@ const AudioWaveformInner: React.FC<{
}
context.clearRect(0, 0, w, h);
- if (typeof visibleVolume === 'number' || drawingWidth <= 0 || h <= 0) {
+ if (visibleVolume.type === 'constant' || drawingWidth <= 0 || h <= 0) {
return;
}
context.setTransform(1, 0, 0, 1, horizontalOffset, 0);
context.beginPath();
context.moveTo(0, 0);
- if ('points' in visibleVolume) {
+ if (visibleVolume.type === 'linear') {
for (const point of visibleVolume.points) {
context.lineTo(
((point.frame - displayOffsetInFrames) / displayDurationInFrames) *
@@ -234,15 +247,15 @@ const AudioWaveformInner: React.FC<{
// 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.min(
- visibleVolume.length,
+ visibleVolume.samples.length,
Math.max(1, Math.ceil(drawingWidth)),
);
for (let point = 0; point < numberOfPoints; point++) {
const volumeIndex = Math.floor(
- (point / numberOfPoints) * visibleVolume.length,
+ (point / numberOfPoints) * visibleVolume.samples.length,
);
const nextVolumeIndex = Math.floor(
- ((point + 1) / numberOfPoints) * visibleVolume.length,
+ ((point + 1) / numberOfPoints) * visibleVolume.samples.length,
);
const firstFrame = Math.max(0, Math.floor(displayOffsetInFrames));
const x = Math.max(
@@ -261,7 +274,10 @@ const AudioWaveformInner: React.FC<{
drawingWidth,
);
const unclampedY =
- (1 - (visibleVolume[volumeIndex] ?? 1) / visualizationMaxVolume) * h;
+ (1 -
+ (visibleVolume.samples[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.
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
index 720c629668..6c617b84c8 100644
--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
@@ -635,13 +635,14 @@ const TimelineVideoInfoInner: React.FC<{
const getSegmentVolume = (
segmentDisplayOffsetInFrames: number,
segmentDurationInFrames: number,
- ) => {
- if (typeof volume === 'number') {
+ ): TimelineVolume => {
+ if (volume.type === 'constant') {
return volume;
}
- if ('points' in volume) {
+ if (volume.type === 'linear') {
return {
+ type: 'linear',
points: volume.points.map((point) => ({
frame: point.frame - segmentDisplayOffsetInFrames,
value: point.value,
@@ -649,13 +650,16 @@ const TimelineVideoInfoInner: React.FC<{
};
}
- return volume.slice(
- Math.max(0, Math.floor(segmentDisplayOffsetInFrames)),
- Math.min(
- volume.length,
- Math.ceil(segmentDisplayOffsetInFrames + segmentDurationInFrames),
+ return {
+ type: 'sampled',
+ samples: volume.samples.slice(
+ Math.max(0, Math.floor(segmentDisplayOffsetInFrames)),
+ Math.min(
+ volume.samples.length,
+ Math.ceil(segmentDisplayOffsetInFrames + segmentDurationInFrames),
+ ),
),
- );
+ };
};
return (
diff --git a/packages/studio/src/components/Timeline/get-timeline-volume.ts b/packages/studio/src/components/Timeline/get-timeline-volume.ts
index 85c9c6d4a0..ea142e725b 100644
--- a/packages/studio/src/components/Timeline/get-timeline-volume.ts
+++ b/packages/studio/src/components/Timeline/get-timeline-volume.ts
@@ -1,4 +1,3 @@
-import type {WaveformVolume} from '@remotion/timeline-utils';
import type {CanUpdateSequencePropStatusKeyframed} from 'remotion';
import {Internals} from 'remotion';
import {
@@ -7,8 +6,10 @@ import {
} from './get-timeline-keyframes';
export type TimelineVolume =
- | WaveformVolume
+ | {readonly type: 'constant'; readonly value: number}
+ | {readonly type: 'sampled'; readonly samples: readonly number[]}
| {
+ readonly type: 'linear';
readonly points: readonly {
readonly frame: number;
readonly value: number;
@@ -29,14 +30,14 @@ export const getTimelineVolume = ({
readonly keyframePlaybackRate: number;
}): TimelineVolume => {
if (status === null) {
- return 1;
+ return {type: 'constant', value: 1};
}
if (
!Number.isFinite(displayDurationInFrames) ||
displayDurationInFrames <= 0
) {
- return [];
+ return {type: 'sampled', samples: []};
}
const resolvedDisplayOffset = getKeyframeDisplayOffset({
@@ -53,7 +54,7 @@ export const getTimelineVolume = ({
status,
});
if (evaluateVolume === null) {
- return 1;
+ return {type: 'constant', value: 1};
}
if (
@@ -89,7 +90,7 @@ export const getTimelineVolume = ({
resolvedPlaybackRate,
);
if (value === null || !Number.isFinite(value)) {
- return 1;
+ return {type: 'constant', value: 1};
}
const previousPoint = points.at(-1);
@@ -108,7 +109,7 @@ export const getTimelineVolume = ({
previousValue = value;
}
- return {points};
+ return {type: 'linear', points};
}
const curve: number[] = [];
@@ -118,11 +119,11 @@ export const getTimelineVolume = ({
resolvedPlaybackRate,
);
if (value === null || !Number.isFinite(value)) {
- return 1;
+ return {type: 'constant', value: 1};
}
curve.push(Math.max(0, value));
}
- return curve;
+ return {type: 'sampled', samples: curve};
};