...

Source file src/github.com/gabriel-vasile/mimetype/internal/magic/audio.go

Documentation: github.com/gabriel-vasile/mimetype/internal/magic

     1  package magic
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  )
     7  
     8  var (
     9  	// Flac matches a Free Lossless Audio Codec file.
    10  	Flac = prefix([]byte("\x66\x4C\x61\x43\x00\x00\x00\x22"))
    11  	// Midi matches a Musical Instrument Digital Interface file.
    12  	Midi = prefix([]byte("\x4D\x54\x68\x64"))
    13  	// Ape matches a Monkey's Audio file.
    14  	Ape = prefix([]byte("\x4D\x41\x43\x20\x96\x0F\x00\x00\x34\x00\x00\x00\x18\x00\x00\x00\x90\xE3"))
    15  	// MusePack matches a Musepack file.
    16  	MusePack = prefix([]byte("MPCK"))
    17  	// Au matches a Sun Microsystems au file.
    18  	Au = prefix([]byte("\x2E\x73\x6E\x64"))
    19  	// Amr matches an Adaptive Multi-Rate file.
    20  	Amr = prefix([]byte("\x23\x21\x41\x4D\x52"))
    21  	// Voc matches a Creative Voice file.
    22  	Voc = prefix([]byte("Creative Voice File"))
    23  	// M3u matches a Playlist file.
    24  	M3u = prefix([]byte("#EXTM3U"))
    25  	// AAC matches an Advanced Audio Coding file.
    26  	AAC = prefix([]byte{0xFF, 0xF1}, []byte{0xFF, 0xF9})
    27  )
    28  
    29  // Mp3 matches an mp3 file.
    30  func Mp3(raw []byte, limit uint32) bool {
    31  	if len(raw) < 3 {
    32  		return false
    33  	}
    34  
    35  	if bytes.HasPrefix(raw, []byte("ID3")) {
    36  		// MP3s with an ID3v2 tag will start with "ID3"
    37  		// ID3v1 tags, however appear at the end of the file.
    38  		return true
    39  	}
    40  
    41  	// Match MP3 files without tags
    42  	switch binary.BigEndian.Uint16(raw[:2]) & 0xFFFE {
    43  	case 0xFFFA:
    44  		// MPEG ADTS, layer III, v1
    45  		return true
    46  	case 0xFFF2:
    47  		// MPEG ADTS, layer III, v2
    48  		return true
    49  	case 0xFFE2:
    50  		// MPEG ADTS, layer III, v2.5
    51  		return true
    52  	}
    53  
    54  	return false
    55  }
    56  
    57  // Wav matches a Waveform Audio File Format file.
    58  func Wav(raw []byte, limit uint32) bool {
    59  	return len(raw) > 12 &&
    60  		bytes.Equal(raw[:4], []byte("RIFF")) &&
    61  		bytes.Equal(raw[8:12], []byte{0x57, 0x41, 0x56, 0x45})
    62  }
    63  
    64  // Aiff matches Audio Interchange File Format file.
    65  func Aiff(raw []byte, limit uint32) bool {
    66  	return len(raw) > 12 &&
    67  		bytes.Equal(raw[:4], []byte{0x46, 0x4F, 0x52, 0x4D}) &&
    68  		bytes.Equal(raw[8:12], []byte{0x41, 0x49, 0x46, 0x46})
    69  }
    70  
    71  // Qcp matches a Qualcomm Pure Voice file.
    72  func Qcp(raw []byte, limit uint32) bool {
    73  	return len(raw) > 12 &&
    74  		bytes.Equal(raw[:4], []byte("RIFF")) &&
    75  		bytes.Equal(raw[8:12], []byte("QLCM"))
    76  }
    77  

View as plain text