Tone.js/Tone/shim/AudioContext.js

66 lines
2.2 KiB
JavaScript
Raw Normal View History

define(["../core/Tone", "../shim/OfflineAudioContext"], function(Tone){
2017-10-26 04:52:09 +00:00
if (Tone.supported){
if (!Tone.global.hasOwnProperty("AudioContext") && Tone.global.hasOwnProperty("webkitAudioContext")){
Tone.global.AudioContext = Tone.global.webkitAudioContext;
2017-10-26 04:52:09 +00:00
}
//not functionally equivalent, but only an API placeholder
if (!AudioContext.prototype.close){
AudioContext.prototype.close = function(){
2018-03-05 17:25:33 +00:00
if (Tone.isFunction(this.suspend)){
2017-12-30 06:06:23 +00:00
this.suspend();
2017-10-26 04:52:09 +00:00
}
2017-12-30 06:06:23 +00:00
return Promise.resolve();
2017-10-26 05:07:53 +00:00
};
2017-10-26 04:52:09 +00:00
}
//not functionally equivalent
if (!AudioContext.prototype.resume){
AudioContext.prototype.resume = function(){
//play some silent audio to jumpstart the context
var buffer = this.createBuffer(1, 1, this.sampleRate);
var source = this.createBufferSource();
source.buffer = buffer;
source.connect(this.destination);
source.start(0);
2017-10-26 04:52:09 +00:00
return Promise.resolve();
2017-10-26 05:07:53 +00:00
};
2017-10-26 04:52:09 +00:00
}
2017-10-26 18:09:20 +00:00
//createGain
if (!AudioContext.prototype.createGain && AudioContext.prototype.createGainNode){
AudioContext.prototype.createGain = AudioContext.prototype.createGainNode;
}
//createDelay
if (!AudioContext.prototype.createDelay && AudioContext.prototype.createDelayNode){
AudioContext.prototype.createDelay = AudioContext.prototype.createDelayNode;
}
2017-12-15 17:48:46 +00:00
//test decodeAudioData returns a promise
2017-12-15 17:50:30 +00:00
// https://github.com/mohayonao/web-audio-api-shim/blob/master/src/AudioContext.js
// MIT License (c) 2015 @mohayonao
var decodeAudioDataPromise = false;
2017-12-15 17:48:46 +00:00
var offlineContext = new OfflineAudioContext(1, 1, 44100);
2017-12-15 17:50:30 +00:00
var audioData = new Uint32Array([1179011410, 48, 1163280727, 544501094, 16, 131073, 44100, 176400, 1048580, 1635017060, 8, 0, 0, 0, 0]).buffer;
2017-12-15 17:48:46 +00:00
try {
var ret = offlineContext.decodeAudioData(audioData);
if (ret && Tone.isFunction(ret.then)){
2017-12-15 17:48:46 +00:00
decodeAudioDataPromise = true;
}
2018-03-05 17:25:33 +00:00
} catch (e){
2017-12-15 17:48:46 +00:00
decodeAudioDataPromise = false;
}
2018-03-05 17:25:33 +00:00
if (!decodeAudioDataPromise){
2017-12-15 17:48:46 +00:00
AudioContext.prototype._native_decodeAudioData = AudioContext.prototype.decodeAudioData;
AudioContext.prototype.decodeAudioData = function(audioData){
return new Promise(function(success, error){
this._native_decodeAudioData(audioData, success, error);
}.bind(this));
};
}
2017-10-26 04:52:09 +00:00
}
});