audio

@audio/encode-flac1.4.3

Encode PCM audio samples to FLAC format via WASM

Status
calibrated
Updated
Family
encode, 15 packages under it
Category
codecs
Licence
MIT AND BSD-3-Clause
Bundles
libflac
Dependencies
none
Downloads
9,534 last month
Tests
at least 34 in audiojs/encode's CI, passed at 22bf060,

Encode PCM audio samples to FLAC (lossless) format.
WASM (libFLAC via libflacjs) — works in both node and browser.

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

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. src/libflac.wasm.js is libFLAC, BSD-3-Clause, built by libflac.js, MIT (LICENSE.libflac).

ॐ