Skip to content
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

Add audio support to the GLTF module #88204

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aaronfranke
Copy link
Member

@aaronfranke aaronfranke commented Feb 11, 2024

Implements this proposal godotengine/godot-proposals#8814

This PR adds support for audio import and export in the GLTF module using the not-yet-finalized KHR_audio_emitter GLTF extension (KhronosGroup/glTF#2137). This allows you to save audio inside of GLTF scenes, and load them back later. In the future this will also allow you to use GLTF as an interchange format between game engines.

Some example use cases: a fountain that makes water noises, a gun that makes custom sounds when fired, the radio from Portal that plays music on a loop, or a tree that includes bird chirping noises or rustling leaves or something. For more details about the intended use cases, see the proposal.

Freely licensed example file: https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/KHR_audio_emitter/examples/boom_box This file contains a boom box, a short looping music clip, OMI physics, and licensing information via KHR_xmp_json_ld. The model is CC0, created by The Khronos Group, and the music is CC-BY 3.0, created by Kevin MacLeod.

The code in this PR is ready for review, but note that the extension is not yet finalized. Usually my approach is for us to be pioneers with Godot and implement extensions that may not be finalized, like with OMI physics. However, for this extension, since it is using the KHR_ namespace, we must tread carefully and avoid shipping features in Khronos's namespace into production without Khronos's approval. Don't put words in their mouth, so to speak.

Production edit: closes godotengine/internal-team-priorities#44

Comment on lines 6 to 22
# For MP3 support in the audio GLTFDocumentExtension.
if env["module_minimp3_enabled"]:
thirdparty_dir = "#thirdparty/minimp3/"
if not env.msvc:
env_modules.Append(CPPFLAGS=["-isystem", Dir(thirdparty_dir).path])
else:
env_modules.Prepend(CPPPATH=[thirdparty_dir])
if not env["minimp3_extra_formats"]:
env_modules.Append(CPPDEFINES=["MINIMP3_ONLY_MP3"])

# For OGG Vorbis support in the audio GLTFDocumentExtension.
if env["module_vorbis_enabled"]:
thirdparty_dir = "#thirdparty/libvorbis/"
env_modules.Prepend(CPPPATH=[thirdparty_dir])
if env["builtin_libogg"]:
env_modules.Prepend(CPPPATH=["#thirdparty/libogg"])
Copy link
Member Author

@aaronfranke aaronfranke Feb 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could use some assistance from buildsystem gurus here. Right now this code adds these path includes into the whole of the modules environment, which seems wrong. If I move env_gltf = env_modules.Clone() above these lines, and change env_modules here to env_gltf, it does not compile.

[ 28%] In file included from modules/gltf/extensions/audio/gltf_document_extension_audio.cpp:40:
./modules/minimp3/audio_stream_mp3.h:37:10: fatal error: 'minimp3_ex.h' file not found
#include <minimp3_ex.h>
         ^~~~~~~~~~~~~~
[ 28%] 1 error generated.
[ 93%] scons: *** [modules/gltf/extensions/audio/gltf_document_extension_audio.macos.editor.arm64.o] Error 1

Comment on lines +114 to +120
} else if (uri.ends_with(".mp3")) {
return "audio/mpeg";
} else if (uri.ends_with(".wav")) {
return "audio/wav";
} else if (uri.ends_with(".ogg")) {
return "audio/ogg";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The decision, at least for now, is that the spec only requires support for the MP3 and WAV audio MIME types. Supporting Ogg Vorbis here is a superset of the spec. There is no harm in having this here, but I wanted to mention it. Additionally, with this PR, Ogg Vorbis cannot be exported, so Godot will only ever export valid to-spec files.

Comment on lines +391 to +408
void _copy_audio_stream_properties_to_audio_source(const Ref<AudioStream> p_audio_stream, Dictionary &p_audio_source) {
Ref<AudioStreamWAV> audio_stream_wav = p_audio_stream;
#ifdef MODULE_MINIMP3_ENABLED
Ref<AudioStreamMP3> audio_stream_mp3 = p_audio_stream;
#endif // MODULE_MINIMP3_ENABLED
#ifdef MODULE_VORBIS_ENABLED
Ref<AudioStreamOggVorbis> audio_stream_ogg = p_audio_stream;
#endif // MODULE_VORBIS_ENABLED
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The audio formats are conditionally compiled, so you can compile the glTF module without MiniMP3, without Vorbis, or without both if you want to. I tested that this works.

@fire
Copy link
Member

fire commented Sep 1, 2024

We are waiting for the ratification of the various gltf extension standards.

@aaronfranke
Copy link
Member Author

Marking as draft to prevent accidental merging before ratification. However, the code is complete and ready for review.

@aaronfranke aaronfranke marked this pull request as draft September 28, 2024 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

2 participants