Tone.js/Tone/shim/AudioContext.js

67 lines
2.1 KiB
JavaScript
Raw Normal View History

import Tone from "../core/Tone";
import "../shim/OfflineAudioContext";
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(){
if (Tone.isFunction(this.suspend)){
this.suspend();
}
return Promise.resolve();
};
}
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);
return Promise.resolve();
};
}
2017-10-26 18:09:20 +00:00
//createGain
if (!AudioContext.prototype.createGain && AudioContext.prototype.createGainNode){
AudioContext.prototype.createGain = AudioContext.prototype.createGainNode;
}
2017-10-26 18:09:20 +00:00
//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
// https://github.com/mohayonao/web-audio-api-shim/blob/master/src/AudioContext.js
// MIT License (c) 2015 @mohayonao
var decodeAudioDataPromise = false;
var offlineContext = new OfflineAudioContext(1, 1, 44100);
var audioData = new Uint32Array([1179011410, 48, 1163280727, 544501094, 16, 131073, 44100, 176400, 1048580, 1635017060, 8, 0, 0, 0, 0]).buffer;
try {
var ret = offlineContext.decodeAudioData(audioData);
if (ret && Tone.isFunction(ret.then)){
decodeAudioDataPromise = true;
2017-12-15 17:48:46 +00:00
}
} catch (e){
decodeAudioDataPromise = false;
}
2017-12-15 17:48:46 +00:00
if (!decodeAudioDataPromise){
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
}
}