@audin.ai/operator-sdk
Version:
Headless browser SDK for the Audin operator softphone — make and receive calls over the Audin operator WebSockets.
49 lines (48 loc) • 2.19 kB
TypeScript
/**
* Linear sample-rate conversion — pure, dependency-free, browser-safe.
*
* The audio WebSocket carries 8 kHz μ-law, but a browser `AudioContext`
* typically runs at 48 kHz (sometimes 44.1 kHz). These helpers bridge the gap:
*
* - capture path: downsample(captureRate → 8000) then μ-law encode
* - playback path: μ-law decode then upsample(8000 → playbackRate)
*
* Linear interpolation is intentionally simple. It is NOT a brick-wall
* anti-alias filter — for an 8 kHz voice band downsampled from 48 kHz the
* aliasing is mild and speech-intelligible, which is the right trade-off for a
* dependency-free SDK hot path. (The `AudioWorklet` additionally benefits from
* the browser's own resampling at the graph boundary.)
*
* Both functions are referentially transparent (no shared state, no
* allocation beyond the returned array), so they are unit-testable without a
* browser and safe to call per audio frame.
*/
/**
* Resample a block of mono Float32 PCM samples from `fromRate` to `toRate`
* using linear interpolation.
*
* The output length is `round(input.length * toRate / fromRate)`. Edge samples
* are clamped (no out-of-range reads). When `fromRate === toRate` the input is
* returned as a copy.
*/
export declare function resampleLinear(input: Float32Array, fromRate: number, toRate: number): Float32Array;
/**
* Downsample to 8 kHz. Thin wrapper around {@link resampleLinear} for the
* capture path (any context rate → 8000).
*/
export declare function downsampleTo8k(input: Float32Array, fromRate: number): Float32Array;
/**
* Upsample from 8 kHz. Thin wrapper around {@link resampleLinear} for the
* playback path (8000 → any context rate).
*/
export declare function upsampleFrom8k(input: Float32Array, toRate: number): Float32Array;
/**
* Convert Float32 PCM in [-1, 1] to signed 16-bit PCM. Values outside the
* range are clamped. Pure; returns a fresh `Int16Array`.
*/
export declare function floatToInt16(input: Float32Array): Int16Array;
/**
* Convert signed 16-bit PCM to Float32 in [-1, 1]. Pure; returns a fresh
* `Float32Array`.
*/
export declare function int16ToFloat(input: Int16Array): Float32Array;