import readChunk from 'read-chunk'; // npm install read-chunkimport audioType from 'audio-type';var buffer = readChunk.sync('meow.wav', 0, 64);audioType(buffer);//=> wav
Browser
import audioType from './audio-type.js'var xhr = new XMLHttpRequest();xhr.open('GET', 'meow.flac');xhr.responseType = 'arraybuffer';xhr.onload = function () { audioType(this.response); //=> flac};xhr.send();
API
audioType(buffer)
Returns one of the keys below, or undefined if the format isn't recognized.
Key
Format
Magic
Offset
wav
WAVE
RIFF…WAVE
0, 8
aiff
AIFF/AIFC
FORM…AIFF/AIFC
0, 8
mp3
MPEG audio
MPEG sync (0xFF Ex) or TAG (ID3v1 trailer)
0
aac
AAC (ADTS)
0xFFF sync, layer 00
0
flac
FLAC, native or in Ogg
fLaC, or OggS…\x7FFLAC
0, or 0 and 28
Note: OpenMPT's .mptm format is IT-derived and reuses the IMPM magic verbatim — it is indistinguishable from .it by magic bytes alone and also detects as it.
buffer
Type: buffer(Node.js), arrayBuffer, uint8array
The first 64 bytes are enough for every format except mod, whose tag sits at offset 1080 (past the sample header table) — pass at least the first 1084 bytes if you need to detect trackers.
ID3v2 tags
mp3, flac, tta and aac files may carry a leading ID3v2 tag. audioType() skips it and inspects what follows: mp3 is only returned once a real MPEG sync is found after the tag — unless the tag's declared size runs past the end of the buffer you gave it, in which case mp3 is assumed (the overwhelming majority of ID3v2-tagged audio in the wild is MP3, and a truncated probe buffer — the common case — can't prove otherwise). dsf's optional ID3v2 tag lives at the end of the file, not the start, so it never enters into this.