Skip to content

🎥 Make videos programmatically with React

finish docs for media-utils

Jonny Burger1103092f4902
diff --git a/packages/docs/docs/get-audio-data.md b/packages/docs/docs/get-audio-data.md
index 15570b64e6..35de9f1e95 100644
--- a/packages/docs/docs/get-audio-data.md
+++ b/packages/docs/docs/get-audio-data.md
@@ -68,3 +68,4 @@ Use the [`useAudioData()`](use-audio-data) helper hook to not have to do state m
- [Using audio](/docs/using-audio)
- [Audio visualization](/docs/audio-visualization)
- [`<Audio/>`](/docs/audio)
+- [`visualizeAudio()`](/docs/visualize-audio)
diff --git a/packages/docs/docs/visualize-audio.md b/packages/docs/docs/visualize-audio.md
index a128545c5b..d7476ec741 100644
--- a/packages/docs/docs/visualize-audio.md
+++ b/packages/docs/docs/visualize-audio.md
@@ -9,4 +9,80 @@ This function is part of Remotion 2.0 which is not yet released. The information
_Part of the `@remotion/media-utils`_ package of helper functions.
-TODO
+This function takes in `AudioData` (preferrably fetched by the [`useAudioData()`](use-audio-data) hook) and processes it in a way that makes visualizing the audio 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()`](use-audio-data) or [`getAudioData()`](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.
+
+## 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.
+
+The values on the left of the array are low frequencies (for example bass) and as we move towards the right, we go through the mid and high frequencies like drums and vocals.
+
+Usually the values on left side of the array can become much larger than the values on the right. This is not a mistake, but for some visualizations you might have to apply some postprocessing to it, you can flatten the curve by for example mapping each value to a logarithm of the original function.
+
+## Example
+
+In this example, we render a bar chart visualizing the audio spectrum of an audio file we imported using [`useAudioData()`](use-audio-data) and `visualizeAudio()`.
+
+```tsx
+import {
+ useCurrentFrame,
+ useVideoConfig,
+ useAudioData,
+ visualizeAudio,
+ Audio
+}
+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 = visualizeAudio({
+ fps,
+ frame,
+ audioData,
+ numberOfSamples: 16,
+ }); // [0.22, 0.1, 0.01, 0.01, 0.01, 0.02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
+
+ // Render a bar chart for each frequency, the higher the amplitude,
+ // the longer the bar
+ 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/example/src/AudioVisualization/index.tsx b/packages/example/src/AudioVisualization/index.tsx
index b642dc9884..ad80e8d4b1 100644
--- a/packages/example/src/AudioVisualization/index.tsx
+++ b/packages/example/src/AudioVisualization/index.tsx
@@ -84,15 +84,15 @@ const Text: React.FC<{
const AudioVisualization: React.FC = () => {
const frame = useCurrentFrame();
const {width, height, fps} = useVideoConfig();
- const metadata = useAudioData(music);
+ const audioData = useAudioData(music);
- if (!metadata) {
+ if (!audioData) {
return null;
}
const visualization = visualizeAudio({
fps,
frame,
- metadata,
+ audioData,
numberOfSamples: 32,
});
diff --git a/packages/media-utils/src/visualize-audio.ts b/packages/media-utils/src/visualize-audio.ts
index 9782089eb7..59bbcdbad0 100644
--- a/packages/media-utils/src/visualize-audio.ts
+++ b/packages/media-utils/src/visualize-audio.ts
@@ -5,14 +5,14 @@ import {AudioData} from './types';
const cache: {[key: string]: number[]} = {};
type FnParameters = {
- metadata: AudioData;
+ audioData: AudioData;
frame: number;
fps: number;
numberOfSamples: number;
};
-export const visualizeAudioFrame = ({
- metadata,
+const visualizeAudioFrame = ({
+ audioData: metadata,
frame,
fps,
numberOfSamples,