@silyze/async-audio-format-ulaw
adds µ‑law (G.711) support to the
@silyze/async-audio-stream
ecosystem.
It performs in‑memory conversion between 8‑bit µ‑law and 16‑bit little‑endian PCM,
streaming‑friendly and fully async.
npm install @silyze/async-audio-format-ulaw
Depends on
alawmulaw
for the actual codec maths (bundled as a normal dependency).
import ULawFormat from "@silyze/async-audio-format-ulaw";
// Typically, µ‑law is 8 kHz mono:
const format = new ULawFormat(8000);
// Encode raw PCM → µ‑law:
const ulawStream = format.encode(pcmStream);
// Decode µ‑law → raw PCM:
const pcmStream = format.decode(ulawStream);
Class | Container | Codec | Channels | Notes |
---|---|---|---|---|
ULawFormat |
Raw bytes | µ‑law | Mono | Bidirectional encode / decode streams |
class ULawFormat extends AudioFormat {
constructor(sampleRate: number); // e.g. 8000
readonly name: string; // "ulaw"
readonly pcmSampleRate: number; // constructor value
/** PCM‑16‑LE → µ‑law (8‑bit) */
encode(input: AsyncReadStream<Buffer>): AsyncReadStream<Buffer>;
/** µ‑law (8‑bit) → PCM‑16‑LE */
decode(input: AsyncReadStream<Buffer>): AsyncReadStream<Buffer>;
}
Both encode()
and decode()
are implemented with the
alawmulaw
library and run inside
stream map()
transforms, so they start processing as soon as data arrives.
-
Sample‑rate‑agnostic – although typical µ‑law telephony audio is 8 kHz, you
may instantiateULawFormat
with any sample rate if your pipeline uses
different values. -
Lossy – µ‑law is a companded 8‑bit representation; converting back to PCM
cannot fully restore the original 16‑bit dynamic range.