react-audio-visualize

repository·master·Indexed 41 days ago

https://github.com/samhirtarif/react-audio-visualize

A 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.

Tokens
2.5K
Snippets
9
Records
10
Agent score
51%

What's inside react-audio-visualize

  1. Use the LiveAudioVisualizer component for real-time audio

    master

    The 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.

    Props

    PropDescriptionDefaultOptional
    mediaRecorderMedia recorder whose stream needs to be visualizedN/ANo
    widthWidth of the visualizer100%Yes
    heightHeight of the visualizer100%Yes
    barWidthWidth of each individual bar2Yes
    gapGap between each bar1Yes
    backgroundColorBackground color for the visualizationtransparentYes
    barColorColor for bars not yet played"rgb(160, 198, 255)"Yes
    fftSizeUnsigned integer representing the window size of the FFT (number of samples)1024Yes
    maxDecibelsDouble representing the maximum decibel value for scaling FFT analysis-10Yes
    minDecibelsDouble representing the minimum decibel value for scaling FFT analysis-90Yes
    smoothingTimeConstantDouble (0 to 1) representing time averaging (0 = no averaging)0.4Yes
    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>
      )
    }
  2. Use the AudioVisualizer component for audio blobs

    master

    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.

    Props

    PropDescriptionDefaultOptional
    blobAudio blob to visualizeN/ANo
    widthWidth of the visualizerN/ANo
    heightHeight of the visualizerN/ANo
    barWidthWidth of each individual bar2Yes
    gapGap between each bar1Yes
    backgroundColorBackground color for the visualizationtransparentYes
    barColorColor for bars not yet played"rgb(184, 184, 184)"Yes
    barPlayedColorColor for bars that have been played"rgb(160, 198, 255)"Yes
    currentTimeCurrent timestamp of playback. Bars before this time use barPlayedColor; bars after use barColor.N/AYes
    styleCustom styles for the canvas elementN/AYes
    refA ForwardedRef for the HTMLCanvasElementN/AYes
    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>
      )
    }
  3. Configure LiveAudioVisualizer props

    master

    The LiveAudioVisualizer component accepts the following props to control the visual style and the underlying Web Audio API analysis:

    Media & Dimensions

    • mediaRecorder (Required): The MediaRecorder instance whose stream will be visualized.
    • width (Optional): The width of the visualization. Defaults to `
  4. Use the LiveAudioVisualizer component

    master

    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)" 
    />
  5. Use the LiveAudioVisualizer component

    master

    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 />;
    };
  6. Reference: LiveAudioVisualizer Props

    master

    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;
    }
  7. Use the AudioVisualizer component

    master

    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} />
  8. Use the AudioVisualizer component

    master

    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} />