Skip to content

🎥 Make videos programmatically with React

docs: add postprocessing example to visualizeAudio

Marcus Stenbeck14a6974b2dc3
diff --git a/packages/docs/docs/visualize-audio.md b/packages/docs/docs/visualize-audio.md
index 4c5fd89d80..55d604b0f0 100644
--- a/packages/docs/docs/visualize-audio.md
+++ b/packages/docs/docs/visualize-audio.md
@@ -29,6 +29,29 @@ The values on the left of the array are low frequencies (for example bass) and a
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.
+<details>
+
+<summary>Postprocessing Example</summary>
+
+```tsx twoslash
+// get the frequency data
+const frequencyData = visualizeAudio({ /* ... */ });
+
+// default scaling factors from the W3C spec for getByteFrequencyData
+const minDb = -100;
+const maxDb = -30;
+
+const amplitudes = frequencyData.map((value) => {
+ // convert to decibels (will be in the range `-Infinity` to `0`)
+ const db = 20 * Math.log10(value);
+
+ // scale to fit between min and max
+ const scaled = (db - minDb) / (maxDb - minDb);
+});
+```
+
+</details>
+
## 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()`.