Skip to content

Commit 114b80e

Browse files
committed
Add audio support to the GLTF module
1 parent c2732ae commit 114b80e

File tree

9 files changed

+1461
-0
lines changed

9 files changed

+1461
-0
lines changed

modules/gltf/SCsub

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ from misc.utility.scons_hints import *
44
Import("env")
55
Import("env_modules")
66

7+
# For MP3 support in the GLTFDocumentExtensionAudio.
8+
if env["module_minimp3_enabled"]:
9+
thirdparty_dir = "#thirdparty/minimp3/"
10+
if not env.msvc:
11+
env_modules.Append(CPPFLAGS=["-isystem", Dir(thirdparty_dir).path])
12+
else:
13+
env_modules.Prepend(CPPPATH=[thirdparty_dir])
14+
if not env["minimp3_extra_formats"]:
15+
env_modules.Append(CPPDEFINES=["MINIMP3_ONLY_MP3"])
16+
17+
# For OGG Vorbis support in the GLTFDocumentExtensionAudio.
18+
if env["module_vorbis_enabled"]:
19+
thirdparty_dir = "#thirdparty/libvorbis/"
20+
env_modules.Prepend(CPPPATH=[thirdparty_dir])
21+
if env["builtin_libogg"]:
22+
env_modules.Prepend(CPPPATH=["#thirdparty/libogg"])
23+
724
env_gltf = env_modules.Clone()
825

926
# Godot source files

modules/gltf/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def get_doc_classes():
1313
"EditorSceneFormatImporterGLTF",
1414
"GLTFAccessor",
1515
"GLTFAnimation",
16+
"GLTFAudioPlayer",
1617
"GLTFBufferView",
1718
"GLTFCamera",
1819
"GLTFDocument",
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="GLTFAudioPlayer" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
3+
<brief_description>
4+
Represents a GLTF audio player.
5+
</brief_description>
6+
<description>
7+
GLTFAudioPlayer is an intermediary between GLTF audio and Godot's audio player nodes.
8+
The KHR_audio_emitter GLTF extension includes MP3 and WAV formats in the base spec. Godot supports saving and loading both of these formats. The MP3 format is available when Godot is compiled with the MiniMP3 module enabled (default). Additionally, Godot can load Ogg Vorbis audio files but not save them when Godot is compiled with the Vorbis module enabled (default).
9+
</description>
10+
<tutorials>
11+
<link title="KHR_audio_emitter GLTF extension">https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/KHR_audio_emitter</link>
12+
</tutorials>
13+
<methods>
14+
<method name="from_dictionary" qualifiers="static">
15+
<return type="GLTFAudioPlayer" />
16+
<param index="0" name="dictionary" type="Dictionary" />
17+
<description>
18+
Create a new GLTFAudioPlayer instance from the given [Dictionary] containing GLTF audio emitter data as defined by KHR_audio_emitter.
19+
</description>
20+
</method>
21+
<method name="from_node" qualifiers="static">
22+
<return type="GLTFAudioPlayer" />
23+
<param index="0" name="audio_node" type="Node" />
24+
<description>
25+
Create a new GLTFAudioPlayer instance from the given Godot [AudioStreamPlayer] or [AudioStreamPlayer3D] node.
26+
</description>
27+
</method>
28+
<method name="from_node_0d" qualifiers="static">
29+
<return type="GLTFAudioPlayer" />
30+
<param index="0" name="audio_node" type="AudioStreamPlayer" />
31+
<description>
32+
Create a new GLTFAudioPlayer instance from the given Godot [AudioStreamPlayer] node.
33+
</description>
34+
</method>
35+
<method name="from_node_3d" qualifiers="static">
36+
<return type="GLTFAudioPlayer" />
37+
<param index="0" name="audio_node" type="AudioStreamPlayer3D" />
38+
<description>
39+
Create a new GLTFAudioPlayer instance from the given Godot [AudioStreamPlayer3D] node.
40+
</description>
41+
</method>
42+
<method name="to_dictionary" qualifiers="const">
43+
<return type="Dictionary" />
44+
<description>
45+
Converts this GLTFAudioPlayer to a [Dictionary] containing GLTF audio emitter data as defined by KHR_audio_emitter.
46+
</description>
47+
</method>
48+
<method name="to_node">
49+
<return type="Node" />
50+
<description>
51+
Converts this GLTFAudioPlayer to a Godot [AudioStreamPlayer] or [AudioStreamPlayer3D] node.
52+
</description>
53+
</method>
54+
<method name="to_node_0d">
55+
<return type="AudioStreamPlayer" />
56+
<description>
57+
Converts this GLTFAudioPlayer to a Godot [AudioStreamPlayer] node.
58+
</description>
59+
</method>
60+
<method name="to_node_3d">
61+
<return type="AudioStreamPlayer3D" />
62+
<description>
63+
Converts this GLTFAudioPlayer to a Godot [AudioStreamPlayer3D] node.
64+
</description>
65+
</method>
66+
</methods>
67+
<members>
68+
<member name="audio_sources" type="PackedInt32Array" setter="set_audio_sources" getter="get_audio_sources" default="PackedInt32Array()">
69+
Indices of the audio sources in the GLTF file that are used by this player. This property is used by the [Dictionary] conversion methods, but not the [Node] conversion methods.
70+
</member>
71+
<member name="audio_stream" type="AudioStream" setter="set_audio_stream" getter="get_audio_stream">
72+
The audio stream used by this player. This property is used by the [Node] conversion methods, but not the [Dictionary] conversion methods.
73+
</member>
74+
<member name="auto_play" type="bool" setter="set_auto_play" getter="get_auto_play" default="false">
75+
If [code]true[/code], the audio will automatically start playing when the audio player node is added to the scene tree. This corresponds to the [code]autoPlay[/code] property of the audio source in the GLTF file (not the audio emitter).
76+
</member>
77+
<member name="cone_inner_angle" type="float" setter="set_cone_inner_angle" getter="get_cone_inner_angle" default="6.28319">
78+
The inner angle of the audio cone's angular diameter in radians. An angle of [constant @GDScript.TAU] or greater means the audio is emitted in all directions. This corresponds to the [code]coneInnerAngle[/code] property of the audio emitter in the GLTF file.
79+
</member>
80+
<member name="cone_outer_angle" type="float" setter="set_cone_outer_angle" getter="get_cone_outer_angle" default="6.28319">
81+
The outer angle of the audio cone's angular diameter in radians. This corresponds to the [code]coneOuterAngle[/code] property of the audio emitter in the GLTF file.
82+
</member>
83+
<member name="cone_outer_gain" type="float" setter="set_cone_outer_gain" getter="get_cone_outer_gain" default="0.0">
84+
The linear volume gain multiplier of the audio applied when outside the outer cone angle. This is multiplied with [member volume_gain]. This corresponds to the [code]coneOuterGain[/code] property of the audio emitter in the GLTF file.
85+
</member>
86+
<member name="distance_model" type="String" setter="set_distance_model" getter="get_distance_model" default="&quot;inverse&quot;">
87+
The distance model used to calculate the volume of the audio. Godot only supports the [code]"inverse"[/code] distance model. This corresponds to the [code]distanceModel[/code] property of the audio emitter in the GLTF file.
88+
</member>
89+
<member name="emitter_type" type="int" setter="set_emitter_type" getter="get_emitter_type" enum="GLTFAudioPlayer.EmitterType" default="1">
90+
The emitter type of audio player. This corresponds to the [code]type[/code] property of the audio emitter in the GLTF file.
91+
</member>
92+
<member name="max_distance" type="float" setter="set_max_distance" getter="get_max_distance" default="0.0">
93+
The maximum distance from the audio source, beyond which the audio cannot be heard. This corresponds to the [code]maxDistance[/code] property of the audio emitter in the GLTF file.
94+
</member>
95+
<member name="pitch_speed" type="float" setter="set_pitch_speed" getter="get_pitch_speed" default="1.0">
96+
The combined pitch and playback speed without resampling, as a multiplier of the audio sample's sample rate. This corresponds to the [code]pitchSpeed[/code] property of the audio source in the GLTF file (not the audio emitter), and the [member AudioStreamPlayer.pitch_scale] and [member AudioStreamPlayer3D.pitch_scale] properties in Godot.
97+
</member>
98+
<member name="rolloff_factor" type="float" setter="set_rolloff_factor" getter="get_rolloff_factor" default="1.0">
99+
The rate at which the volume decreases between [member unit_distance] and [member max_distance]. Godot only supports values of [code]0.0[/code] (no distance attenuation), [code]1.0[/code] (inverse distance), and [code]2.0[/code] (inverse squared distance). This corresponds to the [code]rolloffFactor[/code] property of the audio emitter in the GLTF file.
100+
</member>
101+
<member name="shape_type" type="String" setter="set_shape_type" getter="get_shape_type" default="&quot;omnidirectional&quot;">
102+
The shape of the audio emitter. May be [code]"omnidirectional"[/code] or [code]"cone"[/code]. This corresponds to the [code]shapeType[/code] property of the audio emitter in the GLTF file.
103+
</member>
104+
<member name="unit_distance" type="float" setter="set_unit_distance" getter="get_unit_distance" default="1.0">
105+
The distance in meters where the volume is heard at 100% of its original volume. If closer than this distance, the volume will be [member volume_gain]. If between this distance and [member max_distance], the volume will decrease at a rate determined by [member rolloff_factor]. This corresponds to the [code]refDistance[/code] property of the audio emitter in the GLTF file.
106+
</member>
107+
<member name="volume_gain" type="float" setter="set_volume_gain" getter="get_volume_gain" default="1.0">
108+
The linear volume gain multiplier of the audio. This value is linear, a value of [code]0.0[/code] means silence, [code]1.0[/code] is the original volume, [code]2.0[/code] is twice the volume, etc. This corresponds to the [code]gain[/code] property of the audio source in the GLTF file (not the audio emitter).
109+
</member>
110+
</members>
111+
<constants>
112+
<constant name="EMITTER_TYPE_GLOBAL" value="0" enum="EmitterType">
113+
Global emitter type, played everywhere. Audio players with the global emitter type will be imported as [AudioStreamPlayer] nodes, or exported as [code]"type": "global"[/code] in the GLTF file.
114+
</constant>
115+
<constant name="EMITTER_TYPE_POSITIONAL" value="1" enum="EmitterType">
116+
Positional emitter type, played at a specific position, either omnidirectionally or in a cone. Audio players with the positional emitter type will be imported as [AudioStreamPlayer3D] nodes, or exported as [code]"type": "positional"[/code] in the GLTF file.
117+
</constant>
118+
</constants>
119+
</class>

modules/gltf/extensions/SCsub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ env_gltf = env_modules.Clone()
99
# Godot source files
1010

1111
env_gltf.add_source_files(env.modules_sources, "*.cpp")
12+
env_gltf.add_source_files(env.modules_sources, "audio/*.cpp")
1213
env_gltf.add_source_files(env.modules_sources, "physics/*.cpp")

0 commit comments

Comments
 (0)