1.0.1 • Published 30 days ago

@silyze/async-audio-format-ulaw v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
30 days ago

Async Audio Format µ‑law (G.711)

@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.


Install

npm install @silyze/async-audio-format-ulaw

Depends on alawmulaw for the actual codec maths (bundled as a normal dependency).


Usage

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);

Available Format

ClassContainerCodecChannelsNotes
ULawFormatRaw bytesµ‑lawMonoBidirectional encode / decode streams

API

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.


Implementation Notes

  • Sample‑rate‑agnostic – although typical µ‑law telephony audio is 8 kHz, you \ may instantiate ULawFormat 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.