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