bar-visualizer
elevenlabs/bar-visualizerFreeComponent
shadcn/ui publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/elevenlabs/ui/main/apps/www/public/r/bar-visualizer.jsonSource
1"use client"23import {4 forwardRef,5 memo,6 useEffect,7 useMemo,8 useRef,9 useState,10 type HTMLAttributes,11} from "react"1213import { cn } from "@/lib/utils"1415export interface AudioAnalyserOptions {16 fftSize?: number17 smoothingTimeConstant?: number18 minDecibels?: number19 maxDecibels?: number20}2122function createAudioAnalyser(23 mediaStream: MediaStream,24 options: AudioAnalyserOptions = {}25) {26 const audioContext = new (window.AudioContext ||27 (window as unknown as { webkitAudioContext: typeof AudioContext })28 .webkitAudioContext)()29 const source = audioContext.createMediaStreamSource(mediaStream)30 const analyser = audioContext.createAnalyser()3132 if (options.fftSize) analyser.fftSize = options.fftSize33 if (options.smoothingTimeConstant !== undefined) {34 analyser.smoothingTimeConstant = options.smoothingTimeConstant35 }36 if (options.minDecibels !== undefined)37 analyser.minDecibels = options.minDecibels38 if (options.maxDecibels !== undefined)39 analyser.maxDecibels = options.maxDecibels4041 source.connect(analyser)4243 const cleanup = () => {44 source.disconnect()45 audioContext.close()46 }4748 return { analyser, audioContext, cleanup }49}5051/**52 * Hook for tracking the volume of an audio stream using the Web Audio API.53 * @param mediaStream - The MediaStream to analyze54 * @param options - Audio analyser options55 * @returns The current volume level (0-1)56 */57export function useAudioVolume(58 mediaStream?: MediaStream | null,59 options: AudioAnalyserOptions = { fftSize: 32, smoothingTimeConstant: 0 }60) {61 const [volume, setVolume] = useState(0)62 const volumeRef = useRef(0)63 const frameId = useRef<number | undefined>(undefined)6465 // Memoize options to prevent unnecessary re-renders66 const memoizedOptions = useMemo(67 () => options,68 [69 options.fftSize,70 options.smoothingTimeConstant,71 options.minDecibels,72 options.maxDecibels,73 ]74 )7576 useEffect(() => {77 if (!mediaStream) {78 setVolume(0)79 volumeRef.current = 080 return81 }8283 const { analyser, cleanup } = createAudioAnalyser(84 mediaStream,85 memoizedOptions86 )8788 const bufferLength = analyser.frequencyBinCount89 const dataArray = new Uint8Array(bufferLength)90 let lastUpdate = 091 const updateInterval = 1000 / 30 // 30 FPS9293 const updateVolume = (timestamp: number) => {94 if (timestamp - lastUpdate >= updateInterval) {95 analyser.getByteFrequencyData(dataArray)96 let sum = 097// … truncated
Files it writes
More from shadcn/ui
All 50 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| audio-playeraudio-player | shadcn/ui | Components | Free | 2 deps | |
| conversationconversation | shadcn/ui | Components | Free | 1 dep | |
| conversation-barconversation-bar | shadcn/ui | Components | Free | 1 dep | |
| live-waveformlive-waveform | shadcn/ui | Components | Free | no deps | |
| matrixmatrix | shadcn/ui | Components | Free | no deps | |
| messagemessage | shadcn/ui | Components | Free | 1 dep | |
| mic-selectormic-selector | shadcn/ui | Components | Free | no deps | |
| orborb | shadcn/ui | Components | Free | 4 deps |
