Update XAudioServer.js

This commit is contained in:
jsemu 2019-04-27 03:30:15 -04:00
parent b24d7c6fca
commit 6c15b6a834

View file

@ -165,61 +165,29 @@ XAudioServer.prototype.initializeMozAudio = function () {
this.initializeResampler(XAudioJSMozAudioSampleRate); this.initializeResampler(XAudioJSMozAudioSampleRate);
} }
XAudioServer.prototype.initializeWebAudio = function () { XAudioServer.prototype.initializeWebAudio = function () {
if (typeof AudioContext == "undefined" || typeof XAudioJSWebAudioContextHandle == "undefined") {
throw null;
}
else {
if (!this.userEventLatch) { if (!this.userEventLatch) {
this.setupWebAudio(); this.setupWebAudio();
} }
else { else {
//TODO: Restructure API to not have to potentially lie to end client about
//the samples in buffer before user driven event callback that actually starts WA.
this.resetCallbackAPIAudioBuffer(44100);
}
if (typeof AudioContext != "undefined" || typeof XAudioJSWebAudioContextHandle != "undefined") {
throw null;
}
this.audioType = 1;
/*
Firefox has a bug in its web audio implementation...
The node may randomly stop playing on Mac OS X for no
good reason. Keep a watchdog timer to restart the failed
node if it glitches. Google Chrome never had this issue.
*/
XAudioJSWebAudioWatchDogLast = (new Date()).getTime();
if (navigator.userAgent.indexOf('Gecko/') > -1) {
if (XAudioJSWebAudioWatchDogTimer) {
clearInterval(XAudioJSWebAudioWatchDogTimer);
}
var parentObj = this;
XAudioJSWebAudioWatchDogTimer = setInterval(function () {
if(typeof XAudioJSWebAudioContextHandle.state != "undefined") {
if (XAudioJSWebAudioContextHandle.state === 'suspended') {
XAudioJSWebAudioWatchDogLast = (new Date()).getTime();
try {
XAudioJSWebAudioContextHandle.resume();
}
catch (e) {}
}
else {
var timeDiff = (new Date()).getTime() - XAudioJSWebAudioWatchDogLast;
if (timeDiff > 500) {
parentObj.setupWebAudio();
}
}
}
}, 500);
}
if (this.userEventLatch && typeof XAudioJSWebAudioContextHandle.state != "undefined") {
var parentObj = this; var parentObj = this;
var lazyEnableWA = function () { var lazyEnableWA = function () {
if(XAudioJSWebAudioContextHandle.state === 'suspended') {
parentObj.setupWebAudio(); parentObj.setupWebAudio();
} }
}
try { try {
this.userEventLatch.addEventListener("click", lazyEnableWA, false); this.userEventLatch.addEventListener("click", lazyEnableWA, false);
this.userEventLatch.addEventListener("touchstart", lazyEnableWA, false); this.userEventLatch.addEventListener("touchstart", lazyEnableWA, false);
this.userEventLatch.addEventListener("touchend", lazyEnableWA, false); this.userEventLatch.addEventListener("touchend", lazyEnableWA, false);
} }
catch (e) {} catch (e) {}
//TODO: Restructure API to not have to potentially lie to end client about
//the samples in buffer before user driven event callback that actually starts WA.
this.resetCallbackAPIAudioBuffer(44100);
}
this.audioType = 1;
} }
} }
XAudioServer.prototype.setupWebAudio = function () { XAudioServer.prototype.setupWebAudio = function () {
@ -247,6 +215,36 @@ XAudioServer.prototype.setupWebAudio = function () {
XAudioJSWebAudioAudioNode.onaudioprocess = XAudioJSWebAudioEvent; //Connect the audio processing event to a handling function so we can manipulate output XAudioJSWebAudioAudioNode.onaudioprocess = XAudioJSWebAudioEvent; //Connect the audio processing event to a handling function so we can manipulate output
XAudioJSWebAudioAudioNode.connect(XAudioJSWebAudioContextHandle.destination); //Send and chain the output of the audio manipulation to the system audio output. XAudioJSWebAudioAudioNode.connect(XAudioJSWebAudioContextHandle.destination); //Send and chain the output of the audio manipulation to the system audio output.
this.resetCallbackAPIAudioBuffer(XAudioJSWebAudioContextHandle.sampleRate); this.resetCallbackAPIAudioBuffer(XAudioJSWebAudioContextHandle.sampleRate);
/*
Firefox has a bug in its web audio implementation...
The node may randomly stop playing on Mac OS X for no
good reason. Keep a watchdog timer to restart the failed
node if it glitches. Google Chrome never had this issue.
*/
XAudioJSWebAudioWatchDogLast = (new Date()).getTime();
if (!XAudioJSWebAudioWatchDogTimer && navigator.userAgent.indexOf('Gecko/') > -1) {
if (XAudioJSWebAudioWatchDogTimer) {
clearInterval(XAudioJSWebAudioWatchDogTimer);
}
var parentObj = this;
XAudioJSWebAudioWatchDogTimer = setInterval(function () {
if(typeof XAudioJSWebAudioContextHandle.state != "undefined") {
if (XAudioJSWebAudioContextHandle.state === 'suspended') {
XAudioJSWebAudioWatchDogLast = (new Date()).getTime();
try {
XAudioJSWebAudioContextHandle.resume();
}
catch (e) {}
}
else {
var timeDiff = (new Date()).getTime() - XAudioJSWebAudioWatchDogLast;
if (timeDiff > 500) {
parentObj.setupWebAudio();
}
}
}
}, 500);
}
} }
XAudioServer.prototype.initializeFlashAudio = function () { XAudioServer.prototype.initializeFlashAudio = function () {
var existingFlashload = document.getElementById("XAudioJS"); var existingFlashload = document.getElementById("XAudioJS");