# Audio Waveform

> A live visualiser for an HTML audio source, driven by WebAudio's AnalyserNode.

Source: https://nyxui.com/components/audio-waveform

---
<ComponentPreview name="audio-waveform-demo" />

Audio Waveform is a React visualiser that reacts in real time to an HTML `<audio>` element, driven by WebAudio's `AnalyserNode`. It supports a mirrored-bars mode and a continuous waveform line, and it exposes a ref handle with `play`, `pause`, `toggle`, and `seek` so the surrounding UI can drive playback.

A shared `AudioContext` is constructed lazily on first play — browsers block it until a user gesture, so playback must come from a click or similar event. Useful for music and podcast UIs, voice-note previews, and any interface where sound needs a visible analogue.

## Installation

<Tabs defaultValue="cli">

<TabsList>
  <TabsTrigger value="cli">CLI</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>

<TabsContent value="cli">

```bash
npx shadcn@latest add "https://nyxui.com/r/audio-waveform.json"
```

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource name="audio-waveform" />

<Step>Update the import paths to match your project setup.</Step>

</Steps>

</TabsContent>

</Tabs>

## Usage

```tsx
import { useRef } from "react";
import {
  AudioWaveform,
  type AudioWaveformHandle,
} from "@/components/ui/audio-waveform";

export function Example() {
  const ref = useRef<AudioWaveformHandle>(null);
  return (
    <div className="flex items-center gap-3">
      <button onClick={() => ref.current?.toggle()}>Play / Pause</button>
      <AudioWaveform
        ref={ref}
        src="/track.mp3"
        className="h-10 w-64 text-brand"
      />
    </div>
  );
}
```

### Continuous waveform line

```tsx
<AudioWaveform src="/track.mp3" mode="wave" className="h-16" />
```

## Props

| Prop           | Type                                          | Default        | Description                                                          |
| -------------- | --------------------------------------------- | -------------- | -------------------------------------------------------------------- |
| `src`          | `string`                                      | —              | URL of the audio source.                                             |
| `barCount`     | `number`                                      | `48`           | Number of frequency bars to draw.                                    |
| `gap`          | `number`                                      | `3`            | Gap between bars in pixels.                                          |
| `minHeight`    | `number`                                      | `2`            | Minimum bar height in pixels (idle baseline).                        |
| `sensitivity`  | `number`                                      | `1`            | Multiplier on the normalised amplitude.                              |
| `color`        | `string`                                      | `currentColor` | Fill colour for bars or stroke colour for the wave.                  |
| `mode`         | `"bars" \| "wave"`                            | `"bars"`       | Render as mirrored bars or a continuous waveform line.               |
| `onPlay`       | `() => void`                                  | —              | Fires when playback starts.                                          |
| `onPause`      | `() => void`                                  | —              | Fires when playback pauses or ends.                                  |
| `onFrame`      | `(amp: number) => void`                       | —              | Fires every animation frame with the current RMS amplitude.          |
| `onProgress`   | `(current: number, duration: number) => void` | —              | Fires as playback advances, with times in seconds.                   |
| `showProgress` | `boolean`                                     | `true`         | Played bars solid, the rest dimmed, like a scrubber. Bars mode only. |
| `seekable`     | `boolean`                                     | `true`         | Click or drag across the waveform to seek.                           |
| `className`    | `string`                                      | —              | Extra classes on the wrapper.                                        |

### Ref handle

| Method   | Signature                   | Description             |
| -------- | --------------------------- | ----------------------- |
| `play`   | `() => Promise<void>`       | Resume playback.        |
| `pause`  | `() => void`                | Pause playback.         |
| `toggle` | `() => Promise<void>`       | Toggle play/pause.      |
| `seek`   | `(seconds: number) => void` | Seek the audio element. |