Skip to content

🎥 Make videos programmatically with React

update docs of `visualizeAudioWaveform()`

Shankhadeep Dey1acf47a9ee20
diff --git a/packages/docs/docs/audio-visualization.md b/packages/docs/docs/audio-visualization.md
index ba649b6b91..520a73842b 100644
--- a/packages/docs/docs/audio-visualization.md
+++ b/packages/docs/docs/audio-visualization.md
@@ -68,3 +68,4 @@ export const MyComponent: React.FC = () => {
- [Using audio](/docs/using-audio)
- [`useAudioData()`](/docs/use-audio-data)
- [`visualizeAudio()`](/docs/visualize-audio)
+- [`visualizeAudioWaveform()`](/docs/visualize-audio-waveform)
diff --git a/packages/docs/docs/visualize-audio-waveform.md b/packages/docs/docs/visualize-audio-waveform.md
new file mode 100644
index 0000000000..68ce4e78ab
--- /dev/null
+++ b/packages/docs/docs/visualize-audio-waveform.md
@@ -0,0 +1,78 @@
+---
+title: visualizeAudioWaveform()
+id: visualize-audio-waveform
+---
+
+_Part of the `@remotion/media-utils`_ package of helper functions.
+
+This function takes in `AudioData` (preferrably fetched by the [`useAudioData()`](/docs/use-audio-data) hook) and processes it in a way that makes visualizing the audio waveform that is playing at the current frame easy.
+
+## Arguments
+
+### `options`
+
+The only argument for this function is an object containing the following values:
+
+- `audioData`: `AudioData` - an object containing audio data. You can fetch this object using [`useAudioData()`](/docs/use-audio-data) or [`getAudioData()`](/docs/get-audio-data).
+
+- `frame`: `number` - the time of the track that you want to get the audio information for. The `frame` always refers to the position in the audio track - if you have shifted or trimmed the audio in your timeline, the frame returned by `useCurrentFrame` must also be tweaked before you pass it into this function.
+
+- `fps`: `number` - the frame rate of the composition. This helps the function understand the meaning of the `frame` input.
+
+- `numberOfSamples`: `number` - must be a power of two, such as `32`, `64`, `128`, etc. This parameter controls the length of the output array. A lower number will simplify the spectrum and is useful if you want to animate elements roughly based on the level of lows, mids and highs. A higher number will give the spectrum in more detail, which is useful for displaying a bar chart or waveform-style visualization of the audio.
+
+- `waveformDuration`: `number` - time in seconds (can be float) which represents the duration for which you want to see the audio waveform data with respect to frame. for example let's say if your video `fps` is 30, and you pass 15 as the `frame` on `visualizeAudioWaveform` and passed 0.25 as `waveformDuration` then output will have audio waveform data from (15/30) - .125 = 0.375 sec to (15/30) +0.125 = 0.625 sec.
+
+## Return value
+
+`number[]` - An array of values describing the amplitude of each frequency range. Each value is between 0 and 1. The array is of length defined by the `numberOfSamples` parameter.
+
+## Example
+
+In this example, we render a bar chart visualizing the audio spectrum of an audio file we imported using [`useAudioData()`](/docs/use-audio-data) and `visualizeAudio()`.
+
+```tsx twoslash
+import { Audio, useCurrentFrame, useVideoConfig } from "remotion";
+import { useAudioData, visualizeAudioWaveform } from "@remotion/media-utils";
+import music from "./music.mp3";
+
+export const MyComponent: React.FC = () => {
+ const frame = useCurrentFrame();
+ const { width, height, fps } = useVideoConfig();
+ const audioData = useAudioData(music);
+
+ if (!audioData) {
+ return null;
+ }
+
+ const visualization = visualizeAudioWaveform({
+ fps,
+ frame,
+ audioData,
+ numberOfSamples: 32,
+ waveformDuration: 1/fps,
+ });
+
+ return (
+ <div>
+ <Audio src={music} />
+ {visualization.map((v) => {
+ return (
+ <div
+ style={{ width: 1000 * v, height: 15, backgroundColor: "blue" }}
+ />
+ );
+ })}
+ </div>
+ );
+};
+```
+
+## See also
+
+- [Audio visualization](/docs/audio-visualization)
+- [`useAudioData()`](/docs/use-audio-data)
+- [`getAudioData()`](/docs/get-audio-data)
+- [`<Audio/>`](/docs/audio)
+- [Using audio](/docs/using-audio)
+
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index e2de6bbe0a..2e05ee0f62 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -86,6 +86,7 @@ module.exports = {
"get-waveform-portion",
"use-audio-data",
"visualize-audio",
+ "visualize-audio-waveform"
],
},
"player",
diff --git a/packages/example/src/AudioVisualization/index.tsx b/packages/example/src/AudioVisualization/index.tsx
index f4539fd507..ba60ff4c4a 100644
--- a/packages/example/src/AudioVisualization/index.tsx
+++ b/packages/example/src/AudioVisualization/index.tsx
@@ -143,7 +143,6 @@ const AudioVisualization: React.FC<{isWaveform: boolean}> = ({isWaveform}) => {
waveformDuration: 1 / fps,
});
- console.log(waveform, frame, 'waveform visualization');
const scale =
1 +
interpolate(visualization[1], [0.14, 1], [0, 0.6], {
diff --git a/packages/media-utils/src/visualize-audio-waveform.ts b/packages/media-utils/src/visualize-audio-waveform.ts
index e777ffa204..890995e1de 100644
--- a/packages/media-utils/src/visualize-audio-waveform.ts
+++ b/packages/media-utils/src/visualize-audio-waveform.ts
@@ -48,17 +48,7 @@ const visualizeAudioWaveformFrame = ({
});
};
-export const visualizeAudioWaveform = ({
- smoothing = true,
- ...parameters
-}: FnParameters & {
- smoothing?: boolean;
-}) => {
- if (smoothing) {
- // TODO: Add bezier manipulation?
- console.log('nice');
- }
-
+export const visualizeAudioWaveform = ({...parameters}: FnParameters) => {
const data = visualizeAudioWaveformFrame(parameters);
return data.map((value) => value.amplitude);
};