Skip to content

extractHeader

Extracts the container header from an audio buffer for a known format.

function extractHeader(buffer, format): ArrayBuffer | null;

Defined in: src/utils/audioFormat.ts:269

Extracts the container header from an audio buffer for a known format.

Parameters

ParameterTypeDescription
bufferArrayBufferThe audio data buffer containing the header.
formatDetectedAudioFormatThe detected audio format.

Returns

ArrayBuffer | null

A new ArrayBuffer containing just the header, or null if the header cannot be extracted for this format.

Remarks

Different audio formats embed metadata in their initial bytes (the “header”). When a WebSocket reconnection occurs, the remote service needs this header re-injected so it can parse the subsequent audio frames correctly.

Header extraction is format-specific:

  • WAV: Fixed 44-byte RIFF header
  • OGG: First OGG page (variable length, located by scanning for the next OggS sync)
  • FLAC: Everything up to and including the first FLAC metadata block
  • AIFF: Fixed 12-byte FORM header (callers may need more for full format info)
  • WebM: EBML header (variable length, heuristic: first 64 bytes or full buffer)
  • MP4: First ftyp box (8 bytes + box length from bytes 0–3)
  • MP3/AAC: No header extraction (stream formats without a fixed header); returns null

Returns null when the buffer is too small to contain the header or the format does not have an extractable header.

Example

const format = detectAudioFormat(audioBuffer);
if (format) {
  const header = extractHeader(audioBuffer, format);
  if (header) {
    console.log(`Cached ${header.byteLength}-byte ${format} header`);
  }
}

See

© 2026 CompositeVoice. All rights reserved.

Font size
Contrast
Motion
Transparency