Skip to content

AudioHeaderCache

Caches the audio container header from a stream for re-injection on reconnect.

Defined in: src/core/pipeline/AudioHeaderCache.ts:104

Caches the audio container header from a stream for re-injection on reconnect.

Remarks

The cache operates in two phases:

  1. Accumulation phase — Chunks are concatenated into an internal buffer until MIN_SNIFF_BYTES (12) bytes have been accumulated. During this phase, getHeader returns null.

  2. Resolved phase — Once enough bytes are available, the format is detected and the header is extracted and cached. Subsequent calls to process are no-ops (the header is already captured). getHeader returns the cached header (or null if the format has no extractable header, e.g. MP3).

Call reset to return to the accumulation phase for a new stream.

Example

const cache = new AudioHeaderCache();

// Phase 1: accumulate
cache.process(smallChunk1); // < 12 bytes total
cache.getHeader(); // null (still accumulating)

// Phase 2: resolved
cache.process(smallChunk2); // ≥ 12 bytes total now
cache.getHeader(); // ArrayBuffer (WAV header, etc.) or null (PCM/MP3)
cache.getFormat(); // 'wav', 'ogg', etc. or null

// Reset for a new stream
cache.reset();

See

Constructors

Constructor

new AudioHeaderCache(): AudioHeaderCache;

Returns

AudioHeaderCache

Methods

getFormat()

getFormat(): DetectedAudioFormat | null;

Defined in: src/core/pipeline/AudioHeaderCache.ts:188

Returns the detected audio format.

Returns

DetectedAudioFormat | null

The detected format, or null.

Remarks

Returns null if the cache hasn’t resolved yet or the format is raw PCM / unknown.

See

DetectedAudioFormat


getHeader()

getHeader(): ArrayBuffer | null;

Defined in: src/core/pipeline/AudioHeaderCache.ts:174

Returns the cached container header for re-injection on reconnect.

Returns

ArrayBuffer | null

The cached header as an ArrayBuffer, or null.

Remarks

Returns null in these cases:

  • The cache is still accumulating bytes (not yet resolved)
  • The detected format has no extractable header (MP3, AAC, raw PCM)
  • The buffer was too small to contain the header

isResolved()

isResolved(): boolean;

Defined in: src/core/pipeline/AudioHeaderCache.ts:197

Returns whether the cache has finished format detection.

Returns

boolean

true if the cache has resolved (format detected or PCM fallback).


process()

process(chunk): void;

Defined in: src/core/pipeline/AudioHeaderCache.ts:127

Processes an incoming audio chunk.

Parameters

ParameterTypeDescription
chunkArrayBufferRaw audio data from the input provider.

Returns

void

Remarks

During the accumulation phase, the chunk’s bytes are appended to an internal buffer. Once the buffer reaches MIN_SNIFF_BYTES bytes, the format is detected and the header is extracted. After resolution, further calls are no-ops.


reset()

reset(): void;

Defined in: src/core/pipeline/AudioHeaderCache.ts:209

Resets the cache for a new audio stream.

Returns

void

Remarks

Clears the accumulated buffer, detected format, and cached header. The cache returns to the accumulation phase and will re-detect the format from the next stream’s initial bytes.

© 2026 CompositeVoice. All rights reserved.

Font size
Contrast
Motion
Transparency