-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (85 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* load file from remote
* @param {string} url - audio file url
* @param {string=''} responseType
* @returns
*/
function loadFile(url, responseType = '') {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
resolve(xhr)
}
}
xhr.onerror = reject
xhr.open('GET', url, true)
xhr.responseType = responseType
xhr.send('')
})
}
/**
* convert buffer to hex
* @param {ArrayBuffer} buffer
* @returns
*/
function buf2hex(buffer) {
// buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('')
}
// audio file signature map
const hexMap = {
'494433': 'mp3',
'664c614300000022': 'flac',
'2321414d52': 'amr',
'fff1': 'aac',
'fff9': 'aac',
'4f67675300020000000000000000': 'oga',
'52494646xxxxxxxx57415645666d7420': 'wav',
'3026b2758e66cf11a6d900aa0062ce6c': 'wma'
}
const isAAC = hex => !!hexMap[hex.slice(0, 4)]
const isMP3 = hex => !!hexMap[hex.slice(0, 6)]
const isAMR = hex => !!hexMap[hex.slice(0, 10)]
const isFLAC = hex => !!hexMap[hex.slice(0, 16)]
const isWAV = hex => !!hexMap[hex.slice(0, 32)]
const isWMA = hex => !!hexMap[hex.slice(0, 32)]
const isOGA = hex => !!hexMap[hex.slice(0, 28)]
/**
* get audio file type from remote server
* @param {string} url - audio file url
* @returns {Promise}
* @example
* getAudioType('https://0345-1400187352-1256635546.cos.ap-shanghai.myqcloud.com/rychou/e3801cfc517873a5a5471241e1da1869')
* .then(type => { console.log(type) })
* // output 'aac'
*/
export default function getAudioType(url) {
return loadFile(url, 'arraybuffer').then(xhr => getAudioTypeFromBuffer(xhr.response))
}
/**
* get audio file type from array buffer
* @export
* @param {ArrayBuffer} buffer
* @returns {String} type of file
*/
export function getAudioTypeFromBuffer(buffer){
const hex = buf2hex(buffer)
if (isAAC(hex)) {
return 'aac'
} else if (isAMR(hex)) {
return 'amr'
} else if (isMP3(hex)) {
return 'mp3'
} else if (isFLAC(hex)) {
return 'flac'
} else if (isWAV(hex)) {
return 'wav'
} else if (isWMA(hex)) {
return 'wma'
} else if (isOGA(hex)) {
return 'oga'
} else {
return false
}
}