From bcdeaada0fe767e478cdad5c0d9affa160fc2ade Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Wed, 8 Jun 2022 08:12:14 +0200 Subject: [PATCH] Add an ability to preload a sound file without decoding PR #2006 made the preloaded sounds be decoded right away, bringing back old memory consumption issues from before #431. For games that want to download all their assets during the loading screen, but don't want to actually decode all sounds right away because of memory consumption concerns, being able to dynamically load/unload Howler objects via events is not enough, as the application may already go out-of-memory during loading, and not doing that will cause the game to download additional assets during gameplay. This change adds a new property, "preloadInCache", which makes GDJS request the sound asset via XHR. This puts the file into browser cache, so it can be reasonably expected to be decoded later without issuing network requests. While at it, add user visible descriptions for all preload options. --- Core/GDCore/Project/ResourcesManager.cpp | 8 ++++ Core/GDCore/Project/ResourcesManager.h | 13 +++++- .../howler-sound-manager.ts | 45 +++++++++++++------ GDJS/Runtime/types/project-data.d.ts | 1 + 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/Core/GDCore/Project/ResourcesManager.cpp b/Core/GDCore/Project/ResourcesManager.cpp index e31ad179e647..23eff3888890 100644 --- a/Core/GDCore/Project/ResourcesManager.cpp +++ b/Core/GDCore/Project/ResourcesManager.cpp @@ -184,11 +184,17 @@ std::map AudioResource::GetProperties() const { std::map properties; properties[_("Preload as sound")] + .SetDescription(_("Loads the fully decoded file into cache, so it can be played right away as Sound with no further delays.")) .SetValue(preloadAsSound ? "true" : "false") .SetType("Boolean"); properties[_("Preload as music")] + .SetDescription(_("Prepares the file for immediate streaming as Music (does not wait for complete download).")) .SetValue(preloadAsMusic ? "true" : "false") .SetType("Boolean"); + properties[_("Preload in cache")] + .SetDescription(_("Loads the complete file into cache, but does not decode it into memory until requested.")) + .SetValue(preloadInCache ? "true" : "false") + .SetType("Boolean"); return properties; } @@ -199,6 +205,8 @@ bool AudioResource::UpdateProperty(const gd::String& name, preloadAsSound = value == "1"; else if (name == _("Preload as music")) preloadAsMusic = value == "1"; + else if (name == _("Preload in cache")) + preloadInCache = value == "1"; return true; } diff --git a/Core/GDCore/Project/ResourcesManager.h b/Core/GDCore/Project/ResourcesManager.h index fd62a6c1cd91..a2b9a41b6b23 100644 --- a/Core/GDCore/Project/ResourcesManager.h +++ b/Core/GDCore/Project/ResourcesManager.h @@ -223,7 +223,7 @@ class GD_CORE_API ImageResource : public Resource { */ class GD_CORE_API AudioResource : public Resource { public: - AudioResource() : Resource(), preloadAsMusic(false), preloadAsSound(false) { + AudioResource() : Resource(), preloadAsMusic(false), preloadAsSound(false), preloadInCache(false) { SetKind("audio"); }; virtual ~AudioResource(){}; @@ -263,10 +263,21 @@ class GD_CORE_API AudioResource : public Resource { */ void SetPreloadAsSound(bool enable = true) { preloadAsSound = enable; } + /** + * \brief Return true if the audio resource should be preloaded in cache (without decoding into memory). + */ + bool PreloadInCache() const { return preloadInCache; } + + /** + * \brief Set if the audio resource should be preloaded in cache (without decoding into memory). + */ + void SetPreloadInCache(bool enable = true) { preloadInCache = enable; } + private: gd::String file; bool preloadAsSound; bool preloadAsMusic; + bool preloadInCache; }; /** diff --git a/GDJS/Runtime/howler-sound-manager/howler-sound-manager.ts b/GDJS/Runtime/howler-sound-manager/howler-sound-manager.ts index a4795c8a15df..94202489d39c 100644 --- a/GDJS/Runtime/howler-sound-manager/howler-sound-manager.ts +++ b/GDJS/Runtime/howler-sound-manager/howler-sound-manager.ts @@ -802,24 +802,41 @@ namespace gdjs { if (!filesToLoad.length) return; const file = filesToLoad.shift()!; const fileData = files[file][0]; - if (!fileData.preloadAsSound && !fileData.preloadAsMusic) { - onLoad(); - } else if (fileData.preloadAsSound && fileData.preloadAsMusic) { - let loadedOnce = false; - const callback = (_, error) => { - if (!loadedOnce) { - loadedOnce = true; - return; - } + + let loadCounter = 0; + const callback = (_?: any, error?: string) => { + loadCounter--; + if (!loadCounter) { onLoad(_, error); - }; + } + }; + if (fileData.preloadAsMusic) { + loadCounter++; preloadAudioFile(file, callback, /* isMusic= */ true); + } + + if (fileData.preloadAsSound) { + loadCounter++; preloadAudioFile(file, callback, /* isMusic= */ false); - } else if (fileData.preloadAsSound) { - preloadAudioFile(file, onLoad, /* isMusic= */ false); - } else if (fileData.preloadAsMusic) - preloadAudioFile(file, onLoad, /* isMusic= */ true); + } else if (fileData.preloadInCache) { + // preloading as sound already does a XHR request, hence "else if" + loadCounter++; + const sound = new XMLHttpRequest(); + sound.addEventListener('load', callback); + sound.addEventListener('error', (_) => + callback(_, 'XHR error: ' + file) + ); + sound.addEventListener('abort', (_) => + callback(_, 'XHR abort: ' + file) + ); + sound.open('GET', file); + sound.send(); + } + + if (!loadCounter) { + onLoad(); + } }; loadNextFile(); } diff --git a/GDJS/Runtime/types/project-data.d.ts b/GDJS/Runtime/types/project-data.d.ts index db9d58f289fc..afd62e1d84e8 100644 --- a/GDJS/Runtime/types/project-data.d.ts +++ b/GDJS/Runtime/types/project-data.d.ts @@ -220,6 +220,7 @@ declare interface ResourceData { disablePreload?: boolean; preloadAsSound?: boolean; preloadAsMusic?: boolean; + preloadInCache?: boolean; } declare type ResourceKind =