🎥 Make videos programmatically with React
fix: preserve pitch options and render sample rates consistently
JonnyBurger313572dbae35
diff --git a/packages/docs/docs/media/audio.mdx b/packages/docs/docs/media/audio.mdxindex 3bb6fef6f6..7f77caef6a 100644--- a/packages/docs/docs/media/audio.mdx+++ b/packages/docs/docs/media/audio.mdx@@ -226,22 +226,26 @@ When [`loop`](#loop) is enabled, `"repeat"` (default) resets the callback frameIt has no effect on numeric `volume` values, including `volume={interpolate(frame, ...)}`.-### `preservePitch?`<AvailableFrom v="4.0.537" />+### `preservePitch?`<AvailableFrom v="4.0.538" />-Controls whether the audio pitch is preserved when changing `playbackRate`. Defaults to `true`.-Also compensates for playback speed inherited from parent sequences. Applies during preview, server-side rendering, and client-side rendering.+Preserves the audio pitch when changing [`playbackRate`](#playbackrate), including playback speed inherited from parent [`<Sequence>`](/docs/sequence#playbackrate) components. Defaults to `true`. Applies during preview, [server-side rendering](/docs/ssr), and [client-side rendering](/docs/client-side-rendering).-```tsx-<Audio src={staticFile("audio.wav")} playbackRate={1.4} preservePitch />+```tsx twoslash title="MyComposition.tsx"+import {Audio} from '@remotion/media';+import {staticFile} from 'remotion';+// ---cut---+export const MyComposition = () => (+ <Audio src={staticFile('audio.wav')} playbackRate={1.4} preservePitch />+);```-Set `preservePitch={false}` to let speed changes raise or lower the pitch.-`toneFrequency` remains an independent pitch multiplier: for example, `toneFrequency={0.8}` lowers the pitch by 20% even when preserving pitch at a different speed.-For native fallbacks, `preservePitch` only affects preview; server-side rendering always preserves pitch, matching the fallback component's existing behavior.+Before v4.0.538, changing `playbackRate` also changed the pitch in the Mediabunny and WebCodecs path. Set `preservePitch={false}` to retain that behavior: for example, `playbackRate={2}` doubles the pitch.++[`toneFrequency`](#tonefrequency) remains an independent pitch multiplier. For example, `toneFrequency={0.8}` lowers the pitch by 20% even when preserving pitch at a different speed. Keep `preservePitch` and the authored playback speed constant for each asset during rendering.-The Player's own playback speed is separate and is not compensated by this prop.+In the Mediabunny and WebCodecs path, this prop does not compensate for the Studio's or [`<Player>`](/docs/player/player)'s playback speed. It only compensates for speed changes authored in the composition.-When falling back to a native media component, the legacy fallback `preservePitch` setting takes precedence if specified. Native fallbacks do not apply `toneFrequency` during preview.+When [falling back to a native media component](/docs/media/fallback), this prop is forwarded to [`<Html5Audio>`](/docs/html5-audio#preservepitch). An explicit `fallbackHtml5AudioProps.preservePitch` takes precedence. See the fallback component's documentation for server-side support. Native fallbacks do not apply `toneFrequency` during preview.### `muted?`diff --git a/packages/docs/docs/media/video.mdx b/packages/docs/docs/media/video.mdxindex f7119190a0..c9c6595da2 100644--- a/packages/docs/docs/media/video.mdx+++ b/packages/docs/docs/media/video.mdx@@ -254,22 +254,26 @@ Playing a video in reverse is not supported.Audio pitch is preserved by default when changing `playbackRate`. Pass `preservePitch={false}` to let the pitch change with the speed.:::-### `preservePitch?`<AvailableFrom v="4.0.537" />+### `preservePitch?`<AvailableFrom v="4.0.538" />-Controls whether the audio pitch is preserved when changing `playbackRate`. Defaults to `true`.-Also compensates for playback speed inherited from parent sequences. Applies during preview, server-side rendering, and client-side rendering.+Preserves the audio pitch when changing [`playbackRate`](#playbackrate), including playback speed inherited from parent [`<Sequence>`](/docs/sequence#playbackrate) components. Defaults to `true`. Applies during preview, [server-side rendering](/docs/ssr), and [client-side rendering](/docs/client-side-rendering).-```tsx-<Video src={staticFile("clip.mp4")} playbackRate={1.4} preservePitch />+```tsx twoslash title="MyComposition.tsx"+import {Video} from '@remotion/media';+import {staticFile} from 'remotion';+// ---cut---+export const MyComposition = () => (+ <Video src={staticFile('clip.mp4')} playbackRate={1.4} preservePitch />+);```-Set `preservePitch={false}` to let speed changes raise or lower the pitch.-`toneFrequency` remains an independent pitch multiplier: for example, `toneFrequency={0.8}` lowers the pitch by 20% even when preserving pitch at a different speed.-For native fallbacks, `preservePitch` only affects preview; server-side rendering always preserves pitch, matching the fallback component's existing behavior.+Before v4.0.538, changing `playbackRate` also changed the pitch in the Mediabunny and WebCodecs path. Set `preservePitch={false}` to retain that behavior: for example, `playbackRate={2}` doubles the pitch.++[`toneFrequency`](#tonefrequency) remains an independent pitch multiplier. For example, `toneFrequency={0.8}` lowers the pitch by 20% even when preserving pitch at a different speed. Keep `preservePitch` and the authored playback speed constant for each asset during rendering.-The Player's own playback speed is separate and is not compensated by this prop.+In the Mediabunny and WebCodecs path, this prop does not compensate for the Studio's or [`<Player>`](/docs/player/player)'s playback speed. It only compensates for speed changes authored in the composition.-When falling back to a native media component, the legacy fallback `preservePitch` setting takes precedence if specified. Native fallbacks do not apply `toneFrequency` during preview.+When [falling back to a native media component](/docs/media/fallback), this prop is forwarded to [`<OffthreadVideo>`](/docs/offthreadvideo#preservepitch). An explicit `fallbackOffthreadVideoProps.preservePitch` takes precedence. See the fallback component's documentation for server-side support. Native fallbacks do not apply `toneFrequency` during preview.### `muted?`diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsxindex aed1c2cb20..4078f7d782 100644--- a/packages/media/src/audio/audio-for-preview.tsx+++ b/packages/media/src/audio/audio-for-preview.tsx@@ -430,11 +430,11 @@ const AudioForPreviewAssertedShowing: React.FC<NewAudioForPreviewProps> = ({style={style ?? undefined}_remotionInternalStack={_remotionInternalStack ?? undefined}toneFrequency={toneFrequency}- preservePitch={fallbackHtml5AudioProps?.preservePitch ?? preservePitch}audioStreamIndex={audioStreamIndex}pauseWhenBuffering={fallbackHtml5AudioProps?.pauseWhenBuffering}crossOrigin={fallbackHtml5AudioProps?.crossOrigin}{...fallbackHtml5AudioProps}+ preservePitch={fallbackHtml5AudioProps?.preservePitch ?? preservePitch}/>);}@@ -464,7 +464,7 @@ type InnerAudioProps = {readonly trimBefore?: number | undefined;readonly _remotionInternalStack: string | null;readonly disallowFallbackToHtml5Audio?: boolean;- readonly preservePitch?: boolean;+ readonly preservePitch: boolean;readonly toneFrequency?: number;readonly audioStreamIndex?: number;readonly fallbackHtml5AudioProps?: FallbackHtml5AudioProps;@@ -489,7 +489,7 @@ export const AudioForPreview: React.FC<InnerAudioProps> = ({_remotionInternalStack,disallowFallbackToHtml5Audio,toneFrequency,- preservePitch = true,+ preservePitch,audioStreamIndex,fallbackHtml5AudioProps,onError,diff --git a/packages/media/src/audio/audio.tsx b/packages/media/src/audio/audio.tsxindex d38d9562d1..463bbbac7b 100644--- a/packages/media/src/audio/audio.tsx+++ b/packages/media/src/audio/audio.tsx@@ -184,7 +184,11 @@ const AudioInner: React.FC<}validateMediaProps(- {playbackRate: props.playbackRate, volume: props.volume},+ {+ playbackRate: props.playbackRate,+ preservePitch: props.preservePitch,+ volume: props.volume,+ },'Audio',);validateToneFrequency({@@ -230,6 +234,7 @@ const AudioInner: React.FC<<AudioForPreviewname={name}{...otherProps}+ preservePitch={props.preservePitch ?? true}trimAfter={effectiveTrimAfter}style={premountingStyle}_remotionInternalStack={sourceStack}diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsxindex 68903e9c79..47f47ae3d9 100644--- a/packages/media/src/video/video-for-preview.tsx+++ b/packages/media/src/video/video-for-preview.tsx@@ -592,8 +592,10 @@ const VideoForPreviewAssertedShowing: React.FC<loop={loop}showInTimeline={showInTimeline}_remotionInternalStack={_remotionInternalStack ?? undefined}- preservePitch={preservePitch}{...fallbackOffthreadVideoProps}+ preservePitch={+ fallbackOffthreadVideoProps?.preservePitch ?? preservePitch+ }/>);}diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsxindex bea78d3cde..1c2ec5a838 100644--- a/packages/media/src/video/video.tsx+++ b/packages/media/src/video/video.tsx@@ -146,7 +146,6 @@ const InnerVideo: React.FC<trimAfter,});- validateMediaProps({playbackRate, volume}, 'Video');validateToneFrequency({toneFrequency, component: 'Video'});if (environment.isRendering) {@@ -295,6 +294,8 @@ const VideoInner: React.FC<cropBottom,...props}) => {+ validateMediaProps({playbackRate, preservePitch, volume}, 'Video');+const sourceStack = controls? (Internals.getStackForControls(controls) ?? undefined): undefined;diff --git a/packages/studio-shared/src/studio-html.ts b/packages/studio-shared/src/studio-html.tsindex bc0764ff78..06555e3c86 100644--- a/packages/studio-shared/src/studio-html.ts+++ b/packages/studio-shared/src/studio-html.ts@@ -178,7 +178,7 @@ export const studioHtml = ({<script>window.remotion_numberOfAudioTags = ${numberOfAudioTags};</script><script>window.remotion_audioLatencyHint = "${audioLatencyHint}";</script><script>window.remotion_experimentalKeepAudioContextAlive = ${experimentalKeepAudioContextAlive};</script>- <script>window.remotion_sampleRate = ${sampleRate};</script>+ <script>window.remotion_sampleRate = window.remotion_sampleRate ?? ${sampleRate};</script><script>window.remotion_previewSampleRate = ${sampleRate};</script>${mode === 'dev' ? `<script>window.remotion_logLevel = "${logLevel}";</script>` : ''}<script>window.remotion_staticBase = ${staticBaseValue};</script>