Merge pull request #667 from orsi/doublespeakgames

Major optimization of audio size and safari audio fix
This commit is contained in:
Michael Townsend
2020-10-30 11:53:04 -04:00
committed by GitHub
88 changed files with 50 additions and 20 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
+29 -5
View File
@@ -12,15 +12,21 @@ var AudioEngine = {
_initialized: false, _initialized: false,
init: function () { init: function () {
AudioEngine._initAudioContext(); AudioEngine._initAudioContext();
// AudioEngine._preloadAudio(); // removed to save bandwidth
AudioEngine._initialized = true;
},
_preloadAudio: function () {
// start loading music and events early // start loading music and events early
// ** could be used later if we specify a better set of
// audio files to preload -- i.e. we probably don't need to load
// the later villages or events audio, and esp. not the ending
for (var key in AudioLibrary) { for (var key in AudioLibrary) {
if ( if (
key.toString().indexOf('MUSIC_') > -1 || key.toString().indexOf('MUSIC_') > -1 ||
key.toString().indexOf('EVENT_') > -1) { key.toString().indexOf('EVENT_') > -1) {
AudioEngine.loadAudioFile(AudioLibrary[key]); AudioEngine.loadAudioFile(AudioLibrary[key]);
} }
} }
AudioEngine._initialized = true;
}, },
_initAudioContext: function () { _initAudioContext: function () {
AudioEngine._audioContext = new (window.AudioContext || window.webkitAudioContext); AudioEngine._audioContext = new (window.AudioContext || window.webkitAudioContext);
@@ -136,7 +142,9 @@ var AudioEngine = {
var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2; var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2;
// fade out event music and stop // fade out event music and stop
if (AudioEngine._currentEventAudio) { if (AudioEngine._currentEventAudio &&
AudioEngine._currentEventAudio.source &&
AudioEngine._currentEventAudio.source.buffer) {
var currentEventGainValue = AudioEngine._currentEventAudio.envelope.gain.value; var currentEventGainValue = AudioEngine._currentEventAudio.envelope.gain.value;
AudioEngine._currentEventAudio.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime); AudioEngine._currentEventAudio.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentEventAudio.envelope.gain.setValueAtTime(currentEventGainValue, AudioEngine._audioContext.currentTime); AudioEngine._currentEventAudio.envelope.gain.setValueAtTime(currentEventGainValue, AudioEngine._audioContext.currentTime);
@@ -212,10 +220,26 @@ var AudioEngine = {
return AudioEngine._getMissingAudioBuffer(); return AudioEngine._getMissingAudioBuffer();
} }
return AudioEngine._audioContext.decodeAudioData(buffer, function (decodedData) { var decodeAudioDataPromise = AudioEngine._audioContext.decodeAudioData(buffer, function (decodedData) {
AudioEngine.AUDIO_BUFFER_CACHE[src] = decodedData; AudioEngine.AUDIO_BUFFER_CACHE[src] = decodedData;
return AudioEngine.AUDIO_BUFFER_CACHE[src]; return AudioEngine.AUDIO_BUFFER_CACHE[src];
}); });
// Safari WebAudio does not return a promise based API for
// decodeAudioData, so we need to fake it if we want to play
// audio immediately on first fetch
if (decodeAudioDataPromise) {
return decodeAudioDataPromise;
} else {
return new Promise(function (resolve, reject) {
var fakePromiseId = setInterval(function() {
if (AudioEngine.AUDIO_BUFFER_CACHE[src]) {
resolve(AudioEngine.AUDIO_BUFFER_CACHE[src]);
clearInterval(fakePromiseId);
}
}, 20);
});
}
}); });
} }
}, },
+21 -15
View File
@@ -104,6 +104,15 @@
Engine.loadGame(); Engine.loadGame();
} }
// start loading music and events early
for (var key in AudioLibrary) {
if (
key.toString().indexOf('MUSIC_') > -1 ||
key.toString().indexOf('EVENT_') > -1) {
AudioEngine.loadAudioFile(AudioLibrary[key]);
}
}
$('<div>').attr('id', 'locationSlider').appendTo('#main'); $('<div>').attr('id', 'locationSlider').appendTo('#main');
var menu = $('<div>') var menu = $('<div>')
@@ -133,12 +142,11 @@
}); });
} }
// Disable this for now $('<span>')
// $('<span>') .addClass('volume menuBtn')
// .addClass('volume menuBtn') .text(_('sound on.'))
// .text(_('sound on.')) .click(() => Engine.toggleVolume())
// .click(() => Engine.toggleVolume()) .appendTo(menu);
// .appendTo(menu);
$('<span>') $('<span>')
.addClass('appStore menuBtn') .addClass('appStore menuBtn')
@@ -207,7 +215,7 @@
$.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates); $.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates);
$SM.init(); $SM.init();
// AudioEngine.init(); Disable this for now AudioEngine.init();
Notifications.init(); Notifications.init();
Events.init(); Events.init();
Room.init(); Room.init();
@@ -231,24 +239,22 @@
Engine.triggerHyperMode(); Engine.triggerHyperMode();
} }
// Disable this for now Engine.toggleVolume(Boolean($SM.get('config.soundOn')));
// Engine.toggleVolume(Boolean($SM.get('config.soundOn'))); if(!AudioEngine.isAudioContextRunning()){
// if(!AudioEngine.isAudioContextRunning()){ document.addEventListener('click', Engine.resumeAudioContext, true);
// document.addEventListener('click', Engine.resumeAudioContext, true); }
// }
Engine.saveLanguage(); Engine.saveLanguage();
Engine.travelTo(Room); Engine.travelTo(Room);
// Disable this for now setTimeout(notifyAboutSound, 3000);
// setTimeout(notifyAboutSound, 3000);
}, },
resumeAudioContext: function () { resumeAudioContext: function () {
AudioEngine.tryResumingAudioContext(); AudioEngine.tryResumingAudioContext();
// turn on music! // turn on music!
AudioEngine.setMasterVolume($SM.get('config.soundOn') ? 1.0 : 0.0, 0); AudioEngine.setMasterVolume($SM.get('config.soundOn') ? 1.0 : 0.0, 0);
document.removeEventListener('click', Engine.resumeAudioContext); document.removeEventListener('click', Engine.resumeAudioContext);
}, },