🎥 Make videos programmatically with React
Elements: Add audio oscilloscope (#10821)
Igor Samokhovets93e36accd23b
diff --git a/packages/docs/elements/audio/oscilloscope/audio-oscilloscope.tsx b/packages/docs/elements/audio/oscilloscope/audio-oscilloscope.tsxnew file mode 100644index 0000000000..b4a92211bc--- /dev/null+++ b/packages/docs/elements/audio/oscilloscope/audio-oscilloscope.tsx@@ -0,0 +1,194 @@+import {Audio} from '@remotion/media';+import {+ createSmoothSvgPath,+ getWaveformPortion,+ useWindowedAudioData,+} from '@remotion/media-utils';+import React, {forwardRef, useImperativeHandle, useRef} from 'react';+import {+ Interactive,+ Sequence,+ useCurrentFrame,+ useVideoConfig,+ type InteractiveBaseProps,+ type InteractiveTransformProps,+ type InteractivitySchema,+ type SequenceControls,+} from 'remotion';++type AudioOscilloscopeProps = InteractiveBaseProps &+ InteractiveTransformProps & {+ readonly amplitude?: number;+ readonly audioSrc?: string;+ readonly backgroundColor?: string;+ readonly lineColor?: string;+ readonly lineWidth?: number;+ readonly windowInSeconds?: number;+ };++const audioOscilloscopeSchema = {+ ...Interactive.baseSchema,+ audioSrc: {+ type: 'asset',+ default:+ 'https://remotion.media/elements/remotion-made-this-picture-move.mp3',+ description: 'Audio source',+ },+ backgroundColor: {+ type: 'color',+ default: '#07111f',+ description: 'Background color',+ },+ lineColor: {+ type: 'color',+ default: '#55e6ff',+ description: 'Waveform color',+ },+ lineWidth: {+ type: 'number',+ min: 1,+ max: 16,+ step: 1,+ default: 6,+ description: 'Line width',+ hiddenFromList: false,+ },+ amplitude: {+ type: 'number',+ min: 0.25,+ max: 4,+ step: 0.05,+ default: 2,+ description: 'Amplitude',+ hiddenFromList: false,+ },+ windowInSeconds: {+ type: 'number',+ min: 0.05,+ max: 1,+ step: 0.05,+ default: 0.35,+ description: 'Time window in seconds',+ hiddenFromList: false,+ },+ ...Interactive.transformSchema,+} as const satisfies InteractivitySchema;++const AudioOscilloscopeInner = forwardRef<+ HTMLDivElement,+ AudioOscilloscopeProps & {readonly controls: SequenceControls | undefined}+>(+ (+ {+ amplitude = 2,+ audioSrc = 'https://remotion.media/elements/remotion-made-this-picture-move.mp3',+ backgroundColor = '#07111f',+ controls,+ lineColor = '#55e6ff',+ lineWidth = 6,+ name,+ style,+ windowInSeconds = 0.35,+ ...sequenceProps+ },+ ref,+ ) => {+ const frame = useCurrentFrame();+ const {fps} = useVideoConfig();+ const outlineRef = useRef<HTMLDivElement>(null);+ const {audioData, dataOffsetInSeconds} = useWindowedAudioData({+ fps,+ frame,+ src: audioSrc,+ windowInSeconds: 10,+ });+ const waveform = audioData+ ? getWaveformPortion({+ audioData,+ channel: 0,+ dataOffsetInSeconds,+ durationInSeconds: windowInSeconds,+ normalize: false,+ numberOfSamples: 128,+ outputRange: 'minus-one-to-one',+ startTimeInSeconds: frame / fps - windowInSeconds / 2,+ }).map((sample) => sample.amplitude)+ : [];+ const path = createSmoothSvgPath({+ points: waveform.map((value, index) => ({+ x: (index / (waveform.length - 1)) * 900,+ y: 150 + value * 150 * amplitude,+ })),+ });++ useImperativeHandle(ref, () => outlineRef.current as HTMLDivElement, []);++ return (+ <Sequence+ layout="none"+ {...sequenceProps}+ controls={controls}+ name={name ?? 'Audio oscilloscope'}+ outlineRef={outlineRef}+ >+ <div+ ref={outlineRef}+ style={{+ backgroundColor,+ backgroundImage:+ 'linear-gradient(rgba(255, 255, 255, 0.055) 1px, transparent 1px), linear-gradient(90deg, rgba(255, 255, 255, 0.055) 1px, transparent 1px)',+ backgroundSize: '45px 45px',+ borderRadius: 32,+ boxSizing: 'border-box',+ height: 300,+ overflow: 'hidden',+ width: 900,+ ...style,+ }}+ >+ <Audio showInTimeline={false} src={audioSrc} />+ <svg height={300} viewBox="0 0 900 300" width={900}>+ <line+ x1={0}+ x2={900}+ y1={150}+ y2={150}+ stroke={lineColor}+ strokeOpacity={0.18}+ strokeWidth={1}+ />+ {path ? (+ <>+ <path+ d={path}+ fill="none"+ stroke={lineColor}+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeOpacity={0.22}+ strokeWidth={lineWidth * 4}+ />+ <path+ d={path}+ fill="none"+ stroke={lineColor}+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth={lineWidth}+ />+ </>+ ) : null}+ </svg>+ </div>+ </Sequence>+ );+ },+);++export const AudioOscilloscope = Interactive.withSchema({+ Component: AudioOscilloscopeInner,+ componentName: '<AudioOscilloscope>',+ componentIdentity: null,+ schema: audioOscilloscopeSchema,+ supportsEffects: false,+}) as React.FC<AudioOscilloscopeProps>;diff --git a/packages/docs/elements/audio/oscilloscope/index.mdx b/packages/docs/elements/audio/oscilloscope/index.mdxnew file mode 100644index 0000000000..f050fa2560--- /dev/null+++ b/packages/docs/elements/audio/oscilloscope/index.mdx@@ -0,0 +1,12 @@+---+title: Audio Oscilloscope+description: A glowing oscilloscope waveform for visualizing voices, podcasts, and other audio.+image: /elements/audio-oscilloscope-preview.png+---++import {ElementPage} from '@site/src/components/Elements/ElementPage';+import {elementDefinitions} from '@site/src/components/Elements/element-definitions';++# Audio Oscilloscope++<ElementPage definition={elementDefinitions['audio/oscilloscope']} sourceFile="./audio-oscilloscope.tsx" />diff --git a/packages/docs/src/components/Elements/element-definitions.ts b/packages/docs/src/components/Elements/element-definitions.tsindex e4f0598127..a5bdaa046e 100644--- a/packages/docs/src/components/Elements/element-definitions.ts+++ b/packages/docs/src/components/Elements/element-definitions.ts@@ -4,6 +4,7 @@ import type {} from '@remotion/studio-protocol';import type {ComponentType} from 'react';import {MirroredAudioSpectrum} from '../../../elements/audio/mirrored-spectrum/mirrored-spectrum';+import {AudioOscilloscope} from '../../../elements/audio/oscilloscope/audio-oscilloscope';import {LiquidContours} from '../../../elements/backgrounds/liquid-contours/liquid-contours';import {NotebookPaper} from '../../../elements/backgrounds/notebook-paper/notebook-paper';import {PaperTexture} from '../../../elements/backgrounds/paper-texture/paper-texture';@@ -74,6 +75,29 @@ export type ElementDefinition = {};const elementImplementations = {+ 'audio/oscilloscope': {+ component: AudioOscilloscope,+ contributors: [{username: 'samohovets', contribution: 'Author'}],+ description:+ 'A glowing oscilloscope waveform for visualizing voices, podcasts, and other audio.',+ dependencies: [+ {name: '@remotion/media', version: null},+ {name: '@remotion/media-utils', version: null},+ ],+ durationInFrames: 271,+ elementHeight: 300,+ elementWidth: 900,+ fps: 30,+ height: 1080,+ posterFrame: 105,+ preview: {+ posterUrl: '/elements/audio-oscilloscope-preview.png',+ videoUrl: '/elements/audio-oscilloscope-preview.mp4',+ },+ safeArea: 120,+ installationMode: 'component-owned-sequence',+ width: 1920,+ },'audio/mirrored-spectrum': {component: MirroredAudioSpectrum,contributors: [{username: 'JonnyBurger', contribution: 'Author'}],diff --git a/packages/docs/src/components/Elements/element-registry.ts b/packages/docs/src/components/Elements/element-registry.tsindex 9d8ec8c568..2f77087cad 100644--- a/packages/docs/src/components/Elements/element-registry.ts+++ b/packages/docs/src/components/Elements/element-registry.ts@@ -14,6 +14,10 @@ export const elementCategories = [export type ElementCategory = (typeof elementCategories)[number]['category'];export const elementRegistry = {+ 'audio/oscilloscope': {+ category: 'audio',+ displayName: 'Audio Oscilloscope',+ },'audio/mirrored-spectrum': {category: 'audio',displayName: 'Mirrored Audio Spectrum',diff --git a/packages/docs/src/test/elements.test.ts b/packages/docs/src/test/elements.test.tsindex dec7b91910..e7fa30903e 100644--- a/packages/docs/src/test/elements.test.ts+++ b/packages/docs/src/test/elements.test.ts@@ -507,7 +507,7 @@ describe('Elements sidebar', () => {{category: 'audio',label: 'Audio',- items: ['audio/mirrored-spectrum/index'],+ items: ['audio/oscilloscope/index', 'audio/mirrored-spectrum/index'],},{category: 'backgrounds',@@ -663,6 +663,7 @@ describe('Element preview definitions', () => {test('only Elements with one interactive timeline item own their Sequence', () => {const componentOwnedSequenceSlugs = new Set([+ 'audio/oscilloscope','audio/mirrored-spectrum','captions/moving-pill-captions','captions/popping-word-captions',diff --git a/packages/docs/static/elements/audio-oscilloscope-preview.mp4 b/packages/docs/static/elements/audio-oscilloscope-preview.mp4new file mode 100644index 0000000000..1df7c68122Binary files /dev/null and b/packages/docs/static/elements/audio-oscilloscope-preview.mp4 differdiff --git a/packages/docs/static/elements/audio-oscilloscope-preview.png b/packages/docs/static/elements/audio-oscilloscope-preview.pngnew file mode 100644index 0000000000..aa2a38bcdbBinary files /dev/null and b/packages/docs/static/elements/audio-oscilloscope-preview.png differ