Skip to content

Codecs

Helpers exported by @morevoice/sdk from src/lib/codecs.ts.

Exported from @morevoice/sdk (source: packages/sdk-ts/src/lib/codecs.ts).

type

The encodings a MoreVoice media stream carries. Open: an unknown encoding is reported, not guessed.

type MediaEncoding = "audio/x-mulaw" | "audio/l16";
interface
interface MediaFormat {
encoding: MediaEncoding | (string & {});
/** 8000 for μ-law; 16000 or 8000 for L16. */
sampleRate: number;
channels: number;
/** MoreVoice states the L16 byte order explicitly (always "little-endian"); absent for μ-law. */
byteOrder?: "little-endian" | (string & {});
}
constant

μ-law, 8 kHz, mono: the Twilio-compatible default.

const MULAW_8K: MediaFormat;
constant

16-bit linear PCM (little-endian), 16 kHz, mono.

const L16_16K: MediaFormat;
constant

Byte order of audio/l16 payloads on the MoreVoice wire (see the file header).

const L16_BYTE_ORDER;
constant

Media frames are 20 ms of audio (160 μ-law bytes at 8 kHz, 320 samples = 640 bytes of L16 at 16 kHz).

const FRAME_MS = 20;
class
class CodecError extends Error {
readonly name = "CodecError";
}
function

One PCM16 sample → one μ-law byte (G.711, the same algorithm as server/audio/g711.ts).

function linearToMulaw(sample: number): number;
function

One μ-law byte → one PCM16 sample.

function mulawToLinear(byte: number): number;
function

PCM16 samples → μ-law bytes.

function encodeMulaw(pcm: Int16Array): Uint8Array;
function

μ-law bytes → PCM16 samples.

function decodeMulaw(bytes: Uint8Array): Int16Array;
function

PCM16 samples → little-endian bytes (the audio/l16 payload).

function encodeL16(pcm: Int16Array): Uint8Array;
function

Little-endian bytes → PCM16 samples. An odd trailing byte is a broken frame.

function decodeL16(bytes: Uint8Array): Int16Array;
function

Whether this SDK can encode and decode format.

function isSupportedFormat(format: MediaFormat): boolean;
function

PCM16 at the format’s own rate → payload bytes.

function encodePcm(pcm: Int16Array, format: MediaFormat): Uint8Array;
function

Payload bytes → PCM16 at the format’s own rate.

function decodePayloadBytes(bytes: Uint8Array, format: MediaFormat): Int16Array;
function

A base64 media.payload → PCM16 at the format’s rate.

function decodePayload(payload: string, format: MediaFormat): Int16Array;
function

Bytes per 20 ms frame of format.

function frameBytes(format: MediaFormat, frameMs = FRAME_MS): number;
function

Payload bytes → base64 payloads of frameMs each (the last may be shorter). L16 frames never split a sample. This is how the SDK frames outbound media messages.

function framePayloads(bytes: Uint8Array, format: MediaFormat, frameMs = FRAME_MS): string[];
function

PCM16 (at sampleRate, default the format’s) → base64 payloads of 20 ms in format. Stateless: one utterance.

function encodeFrames(pcm: Int16Array, format: MediaFormat, sampleRate = format.sampleRate): string[];
class

A streaming sample-rate converter for mono PCM16: band-limited (windowed sinc), zero phase and stateful, so audio pushed in 20 ms pieces converts exactly like the whole signal at once. It holds back a few milliseconds of output until it has seen the input those samples depend on; flush() releases them at the end.

const up = new Resampler(8000, 16000);
const a = up.push(frame1); const b = up.push(frame2); const tail = up.flush();
class Resampler {
readonly fromRate: number;
readonly toRate: number;
constructor(fromRate: number, toRate: number);
/** Convert the next piece of input; returns what is ready (a few ms are held back until more input or flush()). */
push(input: Int16Array): Int16Array;
/** The held-back output (the input's end is padded with silence). The resampler then starts over. */
flush(): Int16Array;
/** Forget everything (after a `clear`). */
reset(): void;
/** Output samples per input sample. */
get ratio(): number;
}
function

Convert a whole signal between sample rates (band-limited, zero phase; length = ⌊n · to / from⌋).

function resample(pcm: Int16Array, fromRate: number, toRate: number): Int16Array;
function

Signal-to-noise ratio (dB) of actual against reference over [from, to) — for codec tests and diagnostics.

function snrDb(reference: Int16Array, actual: Int16Array, from = 0, to = Math.min(reference.length, actual.length)): number;
constant

The codec helpers as one namespace (codecs.encodeMulaw(…)).

const codecs;