# @audio/encode-flac
Encode PCM audio samples to FLAC (lossless) format.
WASM (libFLAC via libflacjs) — works in both node and browser.
[](https://npmjs.org/package/@audio/encode-flac/)
```js
import flac from '@audio/encode-flac';
const encoder = await flac({ sampleRate: 44100 });
const chunk = encoder.encode(channelData); // → Uint8Array (FLAC frames)
const tail = encoder.flush(); // → Uint8Array (remaining)
// concatenate chunk + tail for complete FLAC file
```
### Options
| Option | Default | Description |
|--------|---------|-------------|
| `sampleRate` | — | Sample rate (required) |
| `channels` | auto | `1` or `2`. Auto-detected from first encode call. |
| `bitDepth` | `16` | `16` or `24` |
| `compression` | `5` | Compression level 0–8 (higher = smaller, slower) |
### Streaming
```js
const encoder = await flac({ sampleRate: 44100, compression: 8 });
const a = encoder.encode(chunk1);
const b = encoder.encode(chunk2);
const c = encoder.flush();
// complete FLAC = concat(a, b, c)
encoder.free();
```
### Streaming output
After `flush()`, `head()` returns the first 42 bytes with the final STREAMINFO (total samples, MD5, frame sizes), which libFLAC only knows at the end: write it over the start. `stream: true` also puts `meta` into the metadata blocks ahead of the frames; `frames` (the exact length, known upfront) puts the sample count into STREAMINFO from the start.
## License
[MIT](LICENSE)
ॐ