# audio-export

> Exporting rendered sound to a file: the settings an export picks (format, bit depth or bitrate, a level left as it is, brought to its peak or to a loudness target) and exportSound, which measures, normalizes and encodes, saying how far it is

```bash
npx shadcn@latest add @audio-ui/audio-export
```

```ts
import { DEFAULT_EXPORT_SETTINGS, exportSound } from "@/lib/audio-export";
```

- Files: `lib/audio-export.ts`
- Depends on: nothing; npm: `@audio-ui/engine`
- Used by: `block-audio-editor`, `export-dialog`, `export-dialog-demo`

| Export | Kind | What it is |
| --- | --- | --- |
| `ExportNormalize` | type | How an export is brought to a level: left as it is, its loudest sample to 0 dBFS, or to a loudness in LUFS with its true peak at -1 dBTP at most. |
| `ExportSettings` | interface |  |
| `DEFAULT_EXPORT_SETTINGS` | const | A 24-bit WAV, left at its level; an MP3 at 320 kbps once picked. |
| `ExportStage` | type | What an export is doing, for a progress readout. |
| `ExportedSound` | interface |  |
| `exportSound` | function | Turns rendered channels into a file of `settings`, saying how far it is through `onProgress`: first measured (the page waits about half a second a minute of stereo), then encoded in chunks. |

## Usage

An export in one call: it measures the sound's loudness, normalizes it if asked (to its peak, or to -14, -16 or -23 LUFS with the true peak kept under -1 dBTP), encodes it, and says how loud the file came out.

```ts
import { DEFAULT_EXPORT_SETTINGS, exportSound } from "@/lib/audio-export";

const exported = await exportSound(
  channels,
  sampleRate,
  { ...DEFAULT_EXPORT_SETTINGS, normalize: -14 }, // "off", "peak", or a target in LUFS
  (stage, done) => setProgress(stage, done)
);
```

The [Export dialog](/docs/components/base/export-dialog) holds its settings; the measuring is [`loudness`](/docs/engine/analysis#loudness) and the encoding [`audio-encode`](/docs/engine/files#writing-a-file).
