detectAudioFormat
Detects the audio container format from magic bytes in the buffer.
function detectAudioFormat(buffer): DetectedAudioFormat | null;
Defined in: src/utils/audioFormat.ts:117
Detects the audio container format from magic bytes in the buffer.
Parameters
| Parameter | Type | Description |
|---|---|---|
buffer | ArrayBuffer | The audio data buffer to inspect. Must be at least MIN_SNIFF_BYTES bytes for reliable detection. |
Returns
DetectedAudioFormat | null
The detected format, or null if the format cannot be determined.
Remarks
Inspects the first MIN_SNIFF_BYTES bytes of the given buffer to identify the audio container format. The detection order is chosen to minimize false positives: unambiguous signatures (WAV, OGG, FLAC, EBML) are checked first, followed by more ambiguous sync-word-based formats (MP3, AAC).
Returns null when:
- The buffer has fewer than MIN_SNIFF_BYTES bytes
- No known magic bytes match (likely raw PCM or an unsupported format)
Example
// WAV detection
const wavHeader = new Uint8Array([
0x52, 0x49, 0x46, 0x46, // "RIFF"
0x00, 0x00, 0x00, 0x00, // file size placeholder
0x57, 0x41, 0x56, 0x45, // "WAVE"
]);
detectAudioFormat(wavHeader.buffer); // => 'wav'
// Raw PCM (no magic bytes)
const pcm = new Uint8Array(64).buffer;
detectAudioFormat(pcm); // => null
See
- DetectedAudioFormat for the set of possible return values
- extractHeader for extracting the header once the format is known