one-hook
Size
0.67 kb
View source

useAudioAnalyser

Real-time audio analysis based on the Web Audio API.

Installation

npm install @1hook/use-audio-analyser

Quick Start

import {  } from '@1hook/use-audio-analyser'

Pass the MediaStream or HTMLAudioElement to analyze to useAudioAnalyser.

(, {
  : 'getByteFrequencyData',
  : () => .(),
})

Check out the API reference to learn more about the available methods

Examples

1. Visualize audio frequencies of a media element

Visualize audio frequencies in real-time by analyzing data from an HTML audio element using the useAudioAnalyser hook.

function () {
  const [, ] = <number[]>([])
  const [, ] = <HTMLAudioElement | null>(null)

  (, {
    : 'getByteFrequencyData',
    : 256, // the resolution of the frequency data
    : ,
  })

  return (
    <>
      < ={} ="/path/to/audio.mp3"  />

      < ="flex h-64">
        {.((, ) => (
          <
            ={}
            ={{
              : '4px',
              : '100%',
              : `scaleY(${ / 255})`,
              : 'blue',
            }}
          />
        ))}
      </>
    </>
  )
}

2. Visualize audio frequencies from a microphone

Combine the useUserMedia hook to capture microphone input with the useAudioAnalyser hook to process and visualize the audio frequency data in real-time.

function () {
  const [, ] = <number[]>([])
  const  = ({ : true })

  (., {
    : 'getByteFrequencyData',
    : 256,
    : ,
  })

  return (
    <>
      < ="flex h-64">
        {.((, ) => (
          <
            ={}
            ={{
              : '4px',
              : '100%',
              : `scaleY(${ / 255})`,
              : 'blue',
            }}
          />
        ))}
      </>

      < ={. === 'closed' ? . : .}>
        {. === 'closed' ? 'Start' : 'Stop'} Visualizer
      </>
    </>
  )
}

3. Draw the frequency data on a canvas

This example uses the useAudioAnalyser hook to analyze the audio frequency data from an audio element and draw the frequency data on a canvas.

function () {
  const  = <HTMLCanvasElement | null>(null)
  const  = <HTMLAudioElement | null>(null)

  (., {
    : 'getByteFrequencyData',
    : 2048,
    () {
      const  = .
      if (!) return

      const  = .('2d')
      if (!) return

      // Clear the canvas
      .(0, 0, ., .)

      // Draw frequency bars
      const  = . / .
      . = '#4CAF50'

      .((, ) => {
        const  = ( / 255) * .
        const  =  * 
        const  = . - 

        .(, ,  - 1, )
      })
    },
  })

  return (
    < ="flex flex-col items-center gap-4">
      <
        ={}
        ={800}
        ={200}
        ="border border-gray-300"
      />
      < ={} ="/path/to/audio.mp3"  />
    </>
  )
}

API Reference

Parameters

PropTypeDefault
options?
AudioAnalyserOptions
-
source
MediaStream | HTMLMediaElement | null
-

AudioAnalyserOptions

The options for the audio analyzer.

PropTypeDefault
onData?
((data: number[]) => void)
-
smoothingTimeConstant?
number
-
maxDecibels?
number
-
minDecibels?
number
-
fftSize?
number
-
sampleRate?
number
-
method
string
-

On this page