Audio Bars

A dithered spectrum of audio bars. Each bar stacks a live level, a slow trail and a stepped peak hold over a grey base, driven by a built-in groove or by real audio.

Props
Cell size3
Bars24
Gap4
Steps6
Floor0.16
BPM118
Level
#ff7a2f
Trail
#f0457a
Peak
#8b5cf6

Installation

pnpm dlx shadcn@latest add @breeze/audio-bars

Usage

import { AudioBars } from "@/components/breeze/audio-bars"

It fills its box, so size it with className. With no audio it plays a built-in groove on bpm, which is enough for a hero, a "now playing" card or a loading state.

<AudioBars className="h-32 w-80" />

To show real sound, pass an AnalyserNode. The bars pick up whatever plays through it, lows on the left.

"use client"

import { useRef, useState } from "react"

import { AudioBars } from "@/components/breeze/audio-bars"

export function Player({ src }: { src: string }) {
  const audio = useRef<HTMLAudioElement>(null)
  const ctx = useRef<AudioContext | null>(null)
  const [analyser, setAnalyser] = useState<AnalyserNode | null>(null)
  const [paused, setPaused] = useState(true)

  async function toggle() {
    const el = audio.current
    if (!el) return
    if (!el.paused) return el.pause()
    // An element can only be routed into one AudioContext, once, so build
    // the graph on the first play and reuse it after that
    if (!ctx.current) {
      ctx.current = new AudioContext()
      const node = ctx.current.createAnalyser()
      node.fftSize = 2048
      ctx.current.createMediaElementSource(el).connect(node)
      node.connect(ctx.current.destination)
      setAnalyser(node)
    }
    await ctx.current.resume()
    await el.play()
  }

  return (
    <div>
      <audio
        ref={audio}
        src={src}
        onPlay={() => setPaused(false)}
        onPause={() => setPaused(true)}
      />
      <button onClick={toggle}>{paused ? "Play" : "Pause"}</button>
      <AudioBars analyser={analyser} playing={!paused} />
    </div>
  )
}

Browsers only start an AudioContext after a click or key press, so create it from your play button, and resume() it there each time in case the browser suspended it. Create it and the MediaElementSource once: routing the same <audio> element a second time throws.

To chart your own numbers, pass values, each 0..1, one bar each. The bars rise to them and rest there (it stops drawing once they settle). Change them and the bars move, leaving the trail and peak behind for a moment. The Usage Chart block is built this way.

<AudioBars values={daily.map((v) => v / max)} steps={0} label={null} />

Props

PropTypeDefaultDescription
barsnumber24How many bars, lows on the left
gapnumber4Space between bars, in px
stepsnumber6Peak-hold steps over the full height. 0 is smooth
floornumber0.16Height of the grey base, as a share of the bar
colorstring"#ff7a2f"Live level colour. Any CSS colour or var()
trailColorstring"#f0457a"Recent level colour
peakColorstring"#8b5cf6"Peak-hold colour
floorColorstringfaint text colourBase colour
ditherbooleantrueOrdered dither. false draws flat blocks
cellSizenumber3Dither cell size, in px
bpmnumber118Tempo of the built-in groove
analyserAnalyserNode | nullDrive the bars from real audio
valuesnumber[] | nullYour own numbers, 0..1, one per bar (overrides bars)
playingbooleantrueWhen false, the bars settle to the base
labelstring | null"Audio levels"Accessible name. null hides the bars from screen readers
classNamestringSize the bars here

Each bar tracks one band on three clocks. The live level jumps up and falls fast. The trail rises with it and sinks slowly, so you see where the band has been. The peak holds for a moment and then drops, rounded up to steps, so neighbouring bars line up into blocks. A thin cut separates the live level from everything above it.

With dither on, each band is an ordered (Bayer 4x4) dither at its own density: the live level is dense and fills in solid toward its head, the trail is a half-tone and the peak is sparse under a solid one-cell cap. The grid is shared across bars, so the pattern lines up from one bar to the next. It's drawn on a canvas, pauses when it's off screen, stops drawing once playing is false and every bar has settled, and shows a single still frame with reduced motion.

The canvas has role="img" and label as its name. If the bars sit next to your own "Now playing" text, pass label={null} to hide them from screen readers. Other props such as id, style or data-* go on the canvas.