Install react-audio-visualize via npm
masterTo use the audio visualization components in your React project, install the package using npm:
npm install react-audio-visualizerepository·master·Indexed 41 days ago
https://github.com/samhirtarif/react-audio-visualizeA React library for audio visualization providing the AudioVisualizer component for static audio blobs with progress tracking and the LiveAudioVisualizer component for real-time audio streams via MediaRecorder and the Web Audio API.
To use the audio visualization components in your React project, install the package using npm:
npm install react-audio-visualizeThe LiveAudioVisualizer component provides real-time visualization of an active audio stream from a MediaRecorder instance. It uses Web Audio API concepts like FFT size and decibel scaling to render the visualization.
| Prop | Description | Default | Optional |
|---|---|---|---|
mediaRecorder | Media recorder whose stream needs to be visualized | N/A | No |
width | Width of the visualizer | 100% | Yes |
height | Height of the visualizer | 100% | Yes |
barWidth | Width of each individual bar | 2 | Yes |
gap | Gap between each bar | 1 | Yes |
backgroundColor | Background color for the visualization | transparent | Yes |
barColor | Color for bars not yet played | "rgb(160, 198, 255)" | Yes |
fftSize | Unsigned integer representing the window size of the FFT (number of samples) | 1024 | Yes |
maxDecibels | Double representing the maximum decibel value for scaling FFT analysis | -10 | Yes |
minDecibels | Double representing the minimum decibel value for scaling FFT analysis | -90 | Yes |
smoothingTimeConstant | Double (0 to 1) representing time averaging (0 = no averaging) | 0.4 | Yes |
import React, { useState } from 'react';
import { LiveAudioVisualizer } from 'react-audio-visualize';
const Visualizer = () => {
const [mediaRecorder, setMediaRecorder] = useState<MediaRecorder>();
// set media recorder somewhere in code
return (
<div>
{mediaRecorder && (
<LiveAudioVisualizer
mediaRecorder={mediaRecorder}
width={200}
height={75}
/>
)}
</div>
)
}The AudioVisualizer component is used to visualize pre-recorded audio data provided as a Blob. It renders a canvas-based visualization where bars can change color based on the currentTime prop, allowing you to show progress through the audio file.
| Prop | Description | Default | Optional |
|---|---|---|---|
blob | Audio blob to visualize | N/A | No |
width | Width of the visualizer | N/A | No |
height | Height of the visualizer | N/A | No |
barWidth | Width of each individual bar | 2 | Yes |
gap | Gap between each bar | 1 | Yes |
backgroundColor | Background color for the visualization | transparent | Yes |
barColor | Color for bars not yet played | "rgb(184, 184, 184)" | Yes |
barPlayedColor | Color for bars that have been played | "rgb(160, 198, 255)" | Yes |
currentTime | Current timestamp of playback. Bars before this time use barPlayedColor; bars after use barColor. | N/A | Yes |
style | Custom styles for the canvas element | N/A | Yes |
ref | A ForwardedRef for the HTMLCanvasElement | N/A | Yes |
import React, { useState, useRef } from 'react';
import { AudioVisualizer } from 'react-audio-visualize';
const Visualizer = () => {
const [blob, setBlob] = useState<Blob>();
const visualizerRef = useRef<HTMLCanvasElement>(null)
// set blob somewhere in code
return (
<div>
{blob && (
<AudioVisualizer
ref={visualizerRef}
blob={blob}
width={500}
height={75}
barWidth={1}
gap={0}
barColor={'#f76565'}
/>
)}
</div>
)
}The LiveAudioVisualizer component accepts the following props to control the visual style and the underlying Web Audio API analysis:
mediaRecorder (Required): The MediaRecorder instance whose stream will be visualized.width (Optional): The width of the visualization. Defaults to `The LiveAudioVisualizer component provides a real-time canvas-based visualization of an audio stream provided via a MediaRecorder. It uses the Web Audio API's AnalyserNode to process frequency data and renders it as a bar chart. The component automatically manages the AudioContext lifecycle, connecting the stream to an analyser and cleaning up resources when the component unmounts or the recorder state changes.
import { LiveAudioVisualizer } from 'react-audio-visualize';
// Assuming you have a MediaRecorder instance from navigator.mediaDevices.getUserMedia
<LiveAudioVisualizer
mediaRecorder={myMediaRecorder}
barColor="rgb(255, 0, 0)"
/>The LiveAudioVisualizer component is exported from the package root. It is designed to visualize live audio input from a user's microphone using the Web Audio API. To use it, import the component and render it within your React application.
import { LiveAudioVisualizer } from 'react-audio-visualize';
// Usage within a component
const MyComponent = () => {
return <LiveAudioVisualizer />;
};The following configuration options are available for the LiveAudioVisualizer component.
export interface Props {
/** Media recorder who's stream needs to visualized */
mediaRecorder: MediaRecorder;
/** Width of the visualization. Default "100%" */
width?: number | string;
/** Height of the visualization. Default "100%" */
height?: number | string;
/** Width of each individual bar in the visualization. Default: `2` */
barWidth?: number;
/** Gap between each bar in the visualization. Default `1` */
gap?: number;
/** BackgroundColor for the visualization: Default `transparent` */
backgroundColor?: string;
/** Color of the bars drawn in the visualization. Default: `"rgb(160, 198, 255)"` */
barColor?: string;
/** An unsigned integer, representing the window size of the FFT, given in number of samples. */
fftSize?: 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768;
/** A double, representing the maximum decibel value for scaling the FFT analysis data */
maxDecibels?: number;
/** A double, representing the minimum decibel value for scaling the FFT analysis data */
minDecibels?: number;
/** A double within the range 0 to 1 (0 meaning no time averaging). The default value is 0.4. */
smoothingTimeConstant?: number;
}The dataPoint interface defines the structure of a single data point used within the audio visualization components. Each point represents a range of values, typically used to map audio frequency or amplitude data to visual elements.
export interface dataPoint {
max: number;
min: number;
}Import AudioVisualizer from react-audio-visualize to render a visualizer for a specific audio source (such as an HTML5 Audio element or an AudioContext).
import { AudioVisualizer } from 'react-audio-visualize';
// Use in your component
<AudioVisualizer audio={yourAudioSource} />The AudioVisualizer component is the primary entry point for visualizing pre-recorded audio files. It accepts an audio element (HTMLAudioElement) and renders a visual representation of the audio data.
import { AudioVisualizer } from 'react-audio-visualize';
// Usage requires an HTMLAudioElement
// <AudioVisualizer audio={audioRef.current} />