@audin.ai/operator-sdk
Version:
Headless browser SDK for the Audin operator softphone — make and receive calls over the Audin operator WebSockets.
64 lines (63 loc) • 2.83 kB
TypeScript
/**
* G.711 μ-law (mu-law) codec — pure, dependency-free, browser-safe.
*
* This is the ONLY wire-format-critical piece of the SDK: the bytes produced
* by {@link encodeMuLaw} are streamed verbatim over the audio WebSocket, and
* the platform expects them to be byte-compatible with the ITU-T G.711 μ-law
* standard at 8 kHz. A Known-Answer-Test (`__tests__/mulaw.test.ts`) pins the
* encoder output for canonical PCM inputs so any regression in this module
* fails loudly before it can corrupt a live call.
*
* Reference: ITU-T Recommendation G.711, μ-law encoding.
*
* This is the canonical Sun Microsystems `g711.c` `linear2ulaw` / `ulaw2linear`
* implementation, the universally-cited C reference for the standard. In this
* convention the sign bit is SET for NEGATIVE samples, which yields the
* standard telephony anchors the Audin audio channel expects:
*
* - digital silence (PCM 0) → 0xFF
* - full-scale positive (+32767) → 0x80
* - full-scale negative (-32768) → 0x00
*
* Algorithm (BIAS = 0x84, segment/exponent table):
* - Input is signed 16-bit linear PCM (Int16, -32768..32767).
* - sign = (sample >> 8) & 0x80 (set for negative); take |sample|.
* - Clamp magnitude to 32635 (CLIP), add BIAS (132).
* - Find the exponent (segment) = position of the highest set bit above the
* bias floor; mantissa = next 4 bits.
* - μ-law byte = ~(sign | (exponent << 4) | mantissa) (one's complement).
*
* Decoding is the exact inverse, reconstructing the quantised linear value.
*
* NOTE: μ-law has two codes (0x7F and 0xFF) that both decode to 0, so
* `encode(decode(b))` is NOT the identity for every one of the 256 codes —
* but `decode(encode(x))` is a faithful quantisation of `x`. The KAT pins the
* encoder, which is the only direction that touches the wire.
*
* No allocation per-sample beyond the output typed array; suitable for the
* audio hot path.
*/
/**
* Encode a single signed 16-bit PCM sample to one μ-law byte (0..255).
*
* Exposed for tests / single-sample use; {@link encodeMuLaw} is the bulk path.
*/
export declare function encodeMuLawSample(sample: number): number;
/**
* Decode a single μ-law byte (0..255) back to a signed 16-bit PCM sample.
*
* Exposed for tests / single-sample use; {@link decodeMuLaw} is the bulk path.
*/
export declare function decodeMuLawSample(muLawByte: number): number;
/**
* Encode a buffer of signed 16-bit PCM samples to μ-law bytes.
*
* One output byte per input sample. Returns a fresh `Uint8Array`.
*/
export declare function encodeMuLaw(pcm: Int16Array): Uint8Array;
/**
* Decode a buffer of μ-law bytes to signed 16-bit PCM samples.
*
* One output sample per input byte. Returns a fresh `Int16Array`.
*/
export declare function decodeMuLaw(muLaw: Uint8Array): Int16Array;