Soundwave

Slant / Media & motion

Six ways to draw the audio levels you supply.

Demo

Usage

@tuttn/slant
import { Soundwave } from '@tuttn/slant'

// Levels run from 0 (silence) to 1, oldest first; they're resampled to `count` marks.
<Soundwave levels={levels} label="Microphone level" />
<Soundwave levels={levels} variant="capsules" count={5} size="sm" />
<Soundwave levels={levels} variant="rings" tone="success" size="lg" label="Ada is speaking" />

// The levels are yours: Slant never opens the microphone. One way to measure
// them, from an AnalyserNode your app owns (illustration only):
//   const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
//   const context = new AudioContext()
//   const analyser = context.createAnalyser()
//   context.createMediaStreamSource(stream).connect(analyser)
function useLevels(analyser: AnalyserNode | null, length = 48) {
  const [levels, setLevels] = useState<number[]>(() => Array(length).fill(0))
  useEffect(() => {
    if (!analyser) return
    const samples = new Uint8Array(analyser.fftSize)
    let frame = 0
    const read = () => {
      analyser.getByteTimeDomainData(samples)
      let sum = 0
      for (const sample of samples) sum += ((sample - 128) / 128) ** 2
      const loudness = Math.min(1, Math.sqrt(sum / samples.length) * 4)
      setLevels(previous => [...previous.slice(1), loudness])
      frame = requestAnimationFrame(read)
    }
    frame = requestAnimationFrame(read)
    return () => cancelAnimationFrame(frame)
  }, [analyser, length])
  return levels
}

const levels = useLevels(analyser)
<Soundwave levels={levels} variant="line" />

// Hold the drawing yourself, e.g. while muted: the last frame stays.
<Soundwave levels={levels} running={!muted} />

Props

PropTypeDefaultDescription
levelsreadonly number[]—How loud it is, from 0 (silence) to 1 (the loudest), oldest first: a short history, or one value per frequency band. Values outside 0 to 1 are clamped and anything that isn't a number reads as 0. You supply them; Slant never opens the microphone.
variantVariant'bars'The drawing. `capsules` are pills mirrored about the centre line, `bars` rise from the baseline, `line` is a smooth trace, `dots` are slanted pixels sized by the level, `spokes` radiate from a ring, and `rings` are concentric slanted outlines, the newest sound innermost.
countnumber24, or 4 for ringsHow many marks to draw (rings, for `rings`). The levels are resampled to fit: averaged when there are more, interpolated when there are fewer.
toneTone'accent'The marks' colour.
sizeWaveSize'md'The strip's height, 24, 40 or 64px, or for `spokes` and `rings` the side of the square, 40, 64 or 96px. The width follows from `count`, and the wave scales down to fit a narrower container.
labelstring—Names the wave for screen readers (`role="img"`), e.g. "Ada is speaking". Omitted, the wave is decorative and `aria-hidden`.
runningbooleantrueFalse holds the last frame drawn and ignores new levels, as the wave does by itself off screen and in a hidden tab.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the root.

Soundwave draws loudness, nothing more: it records nothing and never asks for the microphone. Pass levels from your own audio graph, a recording's saved envelope or frequency bands. Each level is clamped to 0–1 (a value that isn't a number reads as 0), and the array is resampled to count marks, averaged when it is longer and interpolated when it is shorter. For rings, the levels split into one band per ring, the newest innermost, so a loud moment ripples outward as the history moves on. Quiet marks keep a minimum size, so silence still draws a row of slanted pixels.

Without a label the wave is decorative and hidden from screen readers. With one it is a single image with that name, e.g. "Ada is speaking". It never announces the level; if the state matters, say it in text near the wave, such as a "Listening" status.

Each new frame eases in over --sl-duration-fast. Off screen, in a hidden tab or with running={false}, the wave keeps its last frame and ignores new levels, so you can keep feeding it without cost. Under prefers-reduced-motion it stops following each sample and draws a still, symmetric shape whose size follows the overall loudness (the mean of the levels, in tenths), with no transitions.

The drawing is physical, like BarChart's: under dir="rtl" the oldest level stays on the left. For a single level with a value, use a Meter.