forked from mohayonao/promise-decode-audio-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (28 loc) · 1.16 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
/* global Uint32Array, Promise */
"use strict";
var BaseAudioContext = require("base-audio-context");
var OfflineAudioContext = global.OfflineAudioContext || global.webkitOfflineAudioContext;
if (OfflineAudioContext) {
var silent = new Uint32Array([
0x46464952, 0x00000038, 0x45564157, 0x20746d66,
0x00000010, 0x00010001, 0x0000ac44, 0x00015888,
0x00100002, 0x61746164, 0x00000014, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
]).buffer;
var isPromiseBased = (function() {
var context = new OfflineAudioContext(1, 128, 44100);
return context.decodeAudioData(silent, function() {}, function() {});
})();
if (!isPromiseBased) {
var decodeAudioData = BaseAudioContext.prototype.decodeAudioData;
BaseAudioContext.prototype.decodeAudioData = function(audioData, successCallback, errorCallback) {
var _this = this;
var promise = new Promise(function(resolve, reject) {
return decodeAudioData.call(_this, audioData, resolve, reject);
});
promise.then(successCallback, errorCallback);
return promise;
};
BaseAudioContext.prototype.decodeAudioData.original = decodeAudioData;
}
}