Installation
npx kinetic-ui-cli@latest add audio-visualizerArchitecture
This component is built directly on top of the browser's native Web Audio API and bypasses heavy 3rd-party audio libraries entirely.
- Initialization: Due to modern browser autoplay policies, the
AudioContextis strictly instantiated only after the user clicks the central Play button. - Analysis: It routes the
<audio>source through anAnalyserNodewith anfftSizeof 256, giving us 128 usable frequency bins. - Canvas Drawing: In a highly optimized
requestAnimationFrameloop, we extract theUint8Arrayfrequency data and draw glowing vector lines distributed evenly in a circle around the center point. - DPI Scaling: It natively supports high-DPI (Retina) displays by scaling the canvas matrix to the
window.devicePixelRatio.
Note: Ensure your audio files are served with proper CORS headers if they are hosted on a different domain, otherwise the Web Audio API will be blocked from reading the frequency data!
Usage Guide
1. Import the Component
Import the component into your React or Next.js page:
import { AudioVisualizer } from "@/components/creative/audio-visualizer";
2. Implementation Example
Render the component and customize its behavior by passing props:
import { AudioVisualizer } from "@/components/creative/audio-visualizer";
export default function Example() {
return (
<div className="w-full flex items-center justify-center p-8">
<AudioVisualizer
audioUrl="example"
color="example"
/>
</div>
);
}