Skip to content

Add an ability to preload a sound file without decoding #4010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Core/GDCore/Project/ResourcesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,17 @@ std::map<gd::String, gd::PropertyDescriptor> AudioResource::GetProperties()
const {
std::map<gd::String, gd::PropertyDescriptor> 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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -569,13 +577,15 @@ void AudioResource::UnserializeFrom(const SerializerElement& element) {
SetFile(element.GetStringAttribute("file"));
SetPreloadAsMusic(element.GetBoolAttribute("preloadAsMusic"));
SetPreloadAsSound(element.GetBoolAttribute("preloadAsSound"));
SetPreloadInCache(element.GetBoolAttribute("preloadInCache"));
}

void AudioResource::SerializeTo(SerializerElement& element) const {
element.SetAttribute("userAdded", IsUserAdded());
element.SetAttribute("file", GetFile());
element.SetAttribute("preloadAsMusic", PreloadAsMusic());
element.SetAttribute("preloadAsSound", PreloadAsSound());
element.SetAttribute("preloadInCache", PreloadInCache());
}

void FontResource::SetFile(const gd::String& newFile) {
Expand Down
13 changes: 12 additions & 1 deletion Core/GDCore/Project/ResourcesManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(){};
Expand Down Expand Up @@ -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;
};

/**
Expand Down
45 changes: 31 additions & 14 deletions GDJS/Runtime/howler-sound-manager/howler-sound-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions GDJS/Runtime/types/project-data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ declare interface ResourceData {
disablePreload?: boolean;
preloadAsSound?: boolean;
preloadAsMusic?: boolean;
preloadInCache?: boolean;
}

declare type ResourceKind =
Expand Down