audiocnSolid

LCD Spectrum

A spectrum analyzer drawn as LCD segments, in the display's ink, with the Spectrum's axes, bar and dot modes, and sticky peak caps that fade and accelerate as they fall behind the bars.

Installation

$ pnpm dlx shadcn@latest add @audiocn-solid/lcd-spectrum

The CLI also adds the LCD Display and the Spectrum.

Usage

import { LcdDisplay } from "@/components/ui/lcd-display";
import { LcdSpectrum } from "@/components/ui/lcd-spectrum";
const analyser = useAudioAnalyser(() => stream(), { bands: 32 });

<LcdDisplay matrix={false}>
  <LcdSpectrum source={analyser.visual} />
</LcdDisplay>;

An LCD Spectrum is a Spectrum with LCD segments in place of the bars. It has the same level axis, frequency axis and grid, and the same ranges and defaults. Set minHz, maxHz, minDb and maxDb to match the analyser that produced the bands, so the axes are labelled correctly. The defaults match useAudioAnalyser.

Put it in an LcdDisplay with matrix={false}. The segments are a canvas that draws its own dot grid, so the bezel and the glass come from the display and the segments stay whole.

Each band is one column of segments. The unlit dots sit behind them in the display's dim ink, and the lit segments use its ink. The source feeds the bands as 0..1 values, the same as the Spectrum.

Anatomy

<LcdSpectrum>
  <LcdSpectrumLevelAxis />
  <LcdSpectrumCanvas />
  <LcdSpectrumFrequencyAxis />
</LcdSpectrum>

Leave out an axis to hide it. The axes are the Spectrum's, with the labels in the display's dim ink.

A canvas of your own can draw inside a Spectrum with useSpectrum() and drawSpectrumGrid(), both exported from the Spectrum's file. useSpectrum() returns the Spectrum's live settings object, as the Solid Spectrum keeps them, rather than a snapshot.

Examples

Bar and dot

mode="bar" lights every segment up to the level. mode="dot" lights only the top one, like the LCD VU Meter's dot mode. The peak caps show in both.

Bar
Dot

Peak caps

With peakHold, each column keeps a cap at its highest level. The cap sticks there for a short hold, then falls, accelerating, and fades out as it reaches the bar. It is painted before the bars, so a falling cap passes behind the segments and never covers them.

peakHold is off by default, as on the Spectrum.

Rows

rows sets the number of lit segments per column. The default is 24. Fewer rows read as a coarser, more hardware-like screen; more rows read as a finer display.

12
24

Inks

The analyzer draws in its display's ink, so it follows the display's variant: the theme's --primary by default, or a fixed amber, green, cyan or red.

Microphone

Theming

The analyzer reads the display's variables, so it follows the display's variant, appearance and size. The axis labels are sized in em, so they grow with the display's size.

Variable or attributeMeaning
--lcd-display-litThe lit segments and the peak caps
--lcd-display-ink-unlitThe unlit dots behind the columns
--lcd-display-ink-dimThe axis labels
--lcd-spectrum-gridThe grid lines. Default: the lit ink at 20%
data-lcd-modeOn the spectrum: bar or dot

Outside a display the canvas falls back to its currentColor. The colours refresh every half second, so a theme switch repaints without a remount.

Accessibility

The spectrum is role="img" with the label "Frequency spectrum". Give it an aria-label of your own when the analyzer shows a specific source, such as a track or a bus. The canvas and the axes are hidden from screen readers.

Under prefers-reduced-motion the peak caps are hidden and the bars repaint only when the source changes. Peak motion is decorative, so nothing is lost for a reader.

API reference

LcdSpectrum

Takes every div prop, plus:

Prop

Type

LcdSpectrumCanvas

Takes every canvas prop, plus:

Prop

Type

LcdSpectrumLevelAxis and LcdSpectrumFrequencyAxis

They take the same ticks and format props as the Spectrum's axes.