web-audio-player
Version:
a cross-browser WebAudio player
33 lines (29 loc) • 825 B
JavaScript
var xhr = require('xhr')
var xhrProgress = require('xhr-progress')
module.exports = xhrAudio
function xhrAudio (audioContext, src, cb, progress, decoding) {
var xhrObject = xhr({
uri: src,
responseType: 'arraybuffer'
}, function (err, resp, arrayBuf) {
if (!/^2/.test(resp.statusCode)) {
err = new Error('status code ' + resp.statusCode + ' requesting ' + src)
}
if (err) return cb(err)
decode(arrayBuf)
})
xhrProgress(xhrObject)
.on('data', function (amount, total) {
progress(amount, total)
})
function decode (arrayBuf) {
decoding()
audioContext.decodeAudioData(arrayBuf, function (decoded) {
cb(null, decoded)
}, function () {
var err = new Error('Error decoding audio data')
err.type = 'DECODE_AUDIO_DATA'
cb(err)
})
}
}