A React component that creates interactive 3D audio visualizations with Three.js and WebGL shaders.
This package creates immersive 3D visualizations that respond to audio frequencies in real-time. It features:
- 3D icosahedron mesh that deforms based on audio frequencies
- WebGL shader-based animations
- Interactive camera controls with mouse movement
- Audio playback controls with timeline
- Customizable colors and dimensions
- Bloom post-processing effects
npm i @dafilabs/audio-visualizer
# or
yarn add @dafilabs/audio-visualizer
This package requires the following dependencies:
- React
- Three.js
- React Hooks (useState, useEffect, useRef)
import { AudioVisualizer } from 'audio-visualizer-3d';
function App() {
return (
<div className="app">
<h1>Audio Visualizer Demo</h1>
<AudioVisualizer
src="/path/to/audio.mp3"
width={600}
height={400}
/>
</div>
);
}
export default App;
Prop | Type | Default | Description |
---|---|---|---|
src |
string | (Required) | Path to the audio file to visualize |
width |
number | 400 | Width of the visualizer in pixels |
height |
number | 400 | Height of the visualizer in pixels |
uniforms |
object | See below | Custom shader uniforms for visualization |
{
u_time: { value: 0.0 },
u_frequency: { value: 0.0 },
u_red: { value: 0.61 },
u_green: { value: 0.61 },
u_blue: { value: 0.89 }
}
You can customize the appearance by providing custom uniform values:
import { AudioVisualizer } from 'audio-visualizer-3d';
function App() {
const customUniforms = {
u_time: { value: 0.0 },
u_frequency: { value: 0.0 },
u_red: { value: 0.8 }, // More red
u_green: { value: 0.3 }, // Less green
u_blue: { value: 0.9 } // More blue
};
return (
<AudioVisualizer
src="/path/to/audio.mp3"
width={800}
height={600}
uniforms={customUniforms}
/>
);
}
The package consists of two main components:
-
AudioVisualizer
- A simplified wrapper component that handles state and default values -
AudioVisualizerShader
- The core component that integrates Three.js, shaders, and audio analysis
- Audio is loaded and analyzed using Three.js's
AudioAnalyser
- Frequency data is passed to shader uniforms
- The vertex shader deforms a 3D icosahedron mesh based on Perlin noise and frequency data
- The fragment shader applies color based on RGB uniform values
- Post-processing adds bloom effects for enhanced visuals
- Camera position responds to mouse movement for interactive experience
The visualizer uses two main shaders:
Deforms the mesh using Perlin noise techniques, creating dynamic movement based on audio frequency.
Applies color to the mesh based on the RGB uniform values provided.
- Adjust dimensions for performance on lower-end devices
- For mobile, consider using smaller dimensions (300x300 or less)
- Larger audio files may take longer to load and analyze
-
Audio doesn't play on initial load
- Browser policies often require user interaction before audio can play
- Add a play button or user interaction trigger
-
Performance issues
- Reduce the size of the visualizer
- Use simpler audio files (smaller size, lower bitrate)
-
Loading indicator never disappears
- Check that the audio file path is correct
- Ensure audio format is supported by the browser
- Chrome 49+
- Firefox 45+
- Safari 12+
- Edge 79+
WebGL and Web Audio API support required.