Skip to documentation content

Live Waveform

Draw audio as it happens, from a microphone stream, an analyser, or a level you measure.

live-waveform-demo

Overview

When someone talks to a voice input, they want proof it hears them. Live Waveform draws that proof: bars of recent loudness that scroll in from the right, or a line of the current samples.

Give it a stream, an analyser, or a level. It never asks for the microphone itself, and it records nothing.

Anatomy

LiveWaveform
  • CanvasThe bars or line, drawn each frame at the screen's pixel density

LiveWaveform is one component. It installs motion, which it uses to read the reduced motion setting.

Behavior

Sources

PropUse it whenWhat happens
streamYou have a MediaStream from getUserMediaThe waveform opens its own AudioContext and analyser, and closes them when the stream changes or it unmounts
analyserYour audio graph already has an AnalyserNodeThe waveform reads it and never closes it
levelYou measure loudness elsewhere, or have no audio at allThe waveform draws that level. Pass a function to have it read once per frame

analyser wins over stream, and both win over level. Stopping the stream's tracks is up to you.

States

StateWhat it shows
idleA flat line
activeThe source
processingLow bars with a soft crest moving across them, while audio is being handled

Without state, the waveform is active when it has a source and idle otherwise. The state is reported as data-state on the root.

Bars and line

variant="bars" keeps a history of loudness, one bar about every 45 ms, and fits as many bars as the width allows. Set barWidth and barGap to change the density. variant="line" draws the analyser's current samples; with only a level, it draws a calm wave of that size.

Size and color

The canvas fills its box, which is 40 px tall by default; set the height with className. Bars are drawn in the text color, read at draw time, so they follow the theme. Set a text color class to change it.

Accessibility

The waveform is a picture with the img role. Its name says the state, such as "Audio waveform, live". It is not a live region, so changes are not announced; announce recording start and stop in your own controls.

With reduced motion, bars change about four times a second instead of scrolling, the line holds still, and processing is a still row of bars.

Every string comes from labels.

Installation

npx honestui@latest add live-waveform

Usage

import { LiveWaveform } from "@/components/ai/live-waveform"

<LiveWaveform stream={microphone} state={transcribing ? "processing" : undefined} />

Examples

Microphone

The microphone opens only when the button is pressed, and stops when it is pressed again.

live-waveform-microphone

API reference

LiveWaveform

Accepts native div props beyond those listed.

PropTypeDefaultDescription
stateLiveWaveformStateFrom the sourceIdle, active, or processing.
streamMediaStream | nullNoneA stream to analyse.
analyserAnalyserNode | nullNoneAn analyser you own.
levelLiveWaveformLevelNoneA level from 0 to 1, or a function that returns one.
variant"bars" | "line""bars"Scrolling bars or a line of samples.
barWidthnumber3Width of one bar in CSS pixels.
barGapnumber2Space between bars in CSS pixels.
sensitivitynumber1Multiplies the level before it is drawn.
labelsPartial<LiveWaveformLabels>EnglishThe name for each state.

Types

ExportShape
LiveWaveformState"idle", "active", or "processing"
LiveWaveformVariant"bars" or "line"
LiveWaveformLevelA number from 0 to 1, or a function that returns one
LiveWaveformLabelsidle, active, processing