Skip to content

🎥 Make videos programmatically with React

WIP: waveform

Jonny Burgerec94eeba7781
diff --git a/packages/cli/src/editor/components/AudioWaveform.tsx b/packages/cli/src/editor/components/AudioWaveform.tsx
new file mode 100644
index 0000000000..b9ec83cbc3
--- /dev/null
+++ b/packages/cli/src/editor/components/AudioWaveform.tsx
@@ -0,0 +1,74 @@
+import React, {useEffect, useState} from 'react';
+
+const filterData = (audioBuffer: AudioBuffer) => {
+ const rawData = audioBuffer.getChannelData(0); // We only need to work with one channel of data
+ const samples = 200; // Number of samples we want to have in our final data set
+ const blockSize = Math.floor(rawData.length / samples); // the number of samples in each subdivision
+ const filteredData = [];
+ for (let i = 0; i < samples; i++) {
+ const blockStart = blockSize * i; // the location of the first sample in the block
+ let sum = 0;
+ for (let j = 0; j < blockSize; j++) {
+ sum = sum + Math.abs(rawData[blockStart + j]); // find the sum of all the samples in the block
+ }
+ filteredData.push(sum / blockSize); // divide the sum by the block size to get the average
+ }
+ return filteredData;
+};
+
+const normalizeData = (filteredData: number[]) => {
+ const multiplier = Math.pow(Math.max(...filteredData), -1);
+ return filteredData.map((n) => n * multiplier);
+};
+
+export const AudioWaveform: React.FC<{
+ src: string;
+}> = ({src}) => {
+ // TODO: Resize to timeline length so it aligns with cursor
+
+ const [waveform, setWaveform] = useState<number[] | null>(null);
+ useEffect(() => {
+ const audioContext = new AudioContext();
+
+ fetch(src)
+ .then((response) => response.arrayBuffer())
+ .then((arrayBuffer) => audioContext.decodeAudioData(arrayBuffer))
+ .then((audioBuffer) => filterData(audioBuffer))
+ .then((filtered) => normalizeData(filtered))
+ .then((wave) => setWaveform(wave))
+ .catch((err) => {
+ // TODO: Error handling
+ console.log(err);
+ });
+ }, [src]);
+ if (!waveform) {
+ return null;
+ }
+ return (
+ <div
+ style={{
+ display: 'flex',
+ flexDirection: 'row',
+ alignItems: 'center',
+ position: 'absolute',
+ height: 60,
+ }}
+ >
+ {waveform.map((w, i) => {
+ return (
+ <div
+ // TODO: Replace key
+ key={i}
+ style={{
+ height: 40 * w,
+ width: 4,
+ backgroundColor: 'rgba(255, 255, 255, 0.2)',
+ marginLeft: 2,
+ borderRadius: 2,
+ }}
+ />
+ );
+ })}
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/TimelineElements.tsx b/packages/cli/src/editor/components/TimelineElements.tsx
index 3841706c2b..2d4b0e8033 100644
--- a/packages/cli/src/editor/components/TimelineElements.tsx
+++ b/packages/cli/src/editor/components/TimelineElements.tsx
@@ -7,6 +7,7 @@ import {
TIMELINE_RIGHT_PADDING,
} from '../helpers/timeline-layout';
import {useWindowSize} from '../hooks/use-window-size';
+import {AudioWaveform} from './AudioWaveform';
const Pre = styled.pre`
color: white;
@@ -83,6 +84,9 @@ export const TimelineElements: React.FC = () => {
}}
title={s.sequence.displayName}
>
+ {track.trackType === 'audio' ? (
+ <AudioWaveform src={track.audioForWaveform} />
+ ) : null}
<Pre>{s.sequence.displayName}</Pre>
</div>
);
diff --git a/packages/cli/src/editor/helpers/calculate-timeline.ts b/packages/cli/src/editor/helpers/calculate-timeline.ts
index f0d074bc60..47c9ceadc1 100644
--- a/packages/cli/src/editor/helpers/calculate-timeline.ts
+++ b/packages/cli/src/editor/helpers/calculate-timeline.ts
@@ -5,11 +5,20 @@ export type SequenceWithOverlap = {
overlaps: TSequence[];
};
+type DynamicTrackAttributes =
+ | {
+ trackType: 'audio';
+ audioForWaveform: string;
+ }
+ | {
+ trackType: 'sequence';
+ };
+
export type Track = {
trackId: string;
sequences: SequenceWithOverlap[];
- trackType: 'sequence' | 'audio';
-};
+ audioForWaveform: string | null;
+} & DynamicTrackAttributes;
export const calculateOverlays = (
sequences: TSequence[]
@@ -71,6 +80,7 @@ const getAudioTracks = (
],
trackId: `audio-${i}`,
trackType: 'audio',
+ audioForWaveform: a.src,
};
}
);
@@ -105,6 +115,7 @@ export const calculateTimeline = ({
],
trackId: '0',
trackType: 'sequence',
+ audioForWaveform: null,
},
...getAudioTracks(assets, sequenceDuration),
];
@@ -122,6 +133,7 @@ export const calculateTimeline = ({
sequences: [],
trackId: String(overlayCount),
trackType: 'sequence',
+ audioForWaveform: null,
};
}
tracks[overlayCount].sequences.push(sequence);