Skip to content

Commit 2026096

Browse files
committed
Strip AudioStream WAV/Ogg/MP3 data using placeholders for dedicated server exports
1 parent 5d72153 commit 2026096

13 files changed

+523
-0
lines changed

doc/classes/AudioStreamWAV.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
1212
</tutorials>
1313
<methods>
14+
<method name="create_placeholder" qualifiers="const">
15+
<return type="Resource" />
16+
<description>
17+
Creates a placeholder version of this resource ([PlaceholderAudioStream]).
18+
</description>
19+
</method>
1420
<method name="load_from_buffer" qualifiers="static">
1521
<return type="AudioStreamWAV" />
1622
<param index="0" name="stream_data" type="PackedByteArray" />
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="PlaceholderAudioStream" inherits="AudioStream" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3+
<brief_description>
4+
Placeholder class for WAV, Ogg Vorbis or MP3 audio.
5+
</brief_description>
6+
<description>
7+
This class is used when loading a project that uses [AudioStreamWAV], [AudioStreamOggVorbis] or [AudioStreamMP3], if the project was exported in dedicated server mode. In this mode, only the resource reference is kept, with no actual audio data. This allows reducing the exported PCK's size significantly.
8+
[b]Note:[/b] This is not intended to be used as an actual resource for audio playback.
9+
</description>
10+
<tutorials>
11+
</tutorials>
12+
<members>
13+
<member name="length" type="float" setter="set_length" getter="get_length" default="0.0">
14+
The audio stream's length in seconds.
15+
</member>
16+
<member name="loop_begin" type="int" setter="set_loop_begin" getter="get_loop_begin" default="0">
17+
The loop start point (in number of samples, relative to the beginning of the stream).
18+
</member>
19+
<member name="loop_end" type="int" setter="set_loop_end" getter="get_loop_end" default="0">
20+
The loop end point (in number of samples, relative to the beginning of the stream).
21+
</member>
22+
<member name="loop_mode" type="int" setter="set_loop_mode" getter="get_loop_mode" enum="PlaceholderAudioStream.LoopMode" default="0">
23+
The loop mode.
24+
</member>
25+
<member name="tags" type="Dictionary" setter="set_tags" getter="get_tags" default="{}">
26+
Contains user-defined tags if found in the WAV or Ogg Vorbis data.
27+
Commonly used tags include [code]title[/code], [code]artist[/code], [code]album[/code], [code]tracknumber[/code], and [code]date[/code] ([code]date[/code] does not have a standard date format).
28+
[b]Note:[/b] No tag is [i]guaranteed[/i] to be present in every file, so make sure to account for the keys not always existing.
29+
[b]Note:[/b] Only WAV files using a [code]LIST[/code] chunk with an identifier of [code]INFO[/code] to encode the tags are currently supported.
30+
</member>
31+
</members>
32+
<constants>
33+
<constant name="LOOP_DISABLED" value="0" enum="LoopMode">
34+
Audio does not loop.
35+
</constant>
36+
<constant name="LOOP_FORWARD" value="1" enum="LoopMode">
37+
Audio loops the data between [member loop_begin] and [member loop_end], playing forward only.
38+
</constant>
39+
<constant name="LOOP_PINGPONG" value="2" enum="LoopMode">
40+
Audio loops the data between [member loop_begin] and [member loop_end], playing back and forth.
41+
</constant>
42+
<constant name="LOOP_BACKWARD" value="3" enum="LoopMode">
43+
Audio loops the data between [member loop_begin] and [member loop_end], playing backward only.
44+
</constant>
45+
</constants>
46+
</class>

modules/minimp3/audio_stream_mp3.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "audio_stream_mp3.h"
3636
#include "core/io/file_access.h"
37+
#include "scene/resources/placeholder_audio_stream.h"
3738

3839
int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
3940
if (!active) {
@@ -327,6 +328,14 @@ Ref<AudioStreamMP3> AudioStreamMP3::load_from_file(const String &p_path) {
327328
return load_from_buffer(stream_data);
328329
}
329330

331+
Ref<Resource> AudioStreamMP3::create_placeholder() const {
332+
Ref<PlaceholderAudioStream> placeholder;
333+
placeholder.instantiate();
334+
placeholder->set_length(get_length());
335+
placeholder->set_tags(get_tags());
336+
return placeholder;
337+
}
338+
330339
void AudioStreamMP3::_bind_methods() {
331340
ClassDB::bind_static_method("AudioStreamMP3", D_METHOD("load_from_buffer", "stream_data"), &AudioStreamMP3::load_from_buffer);
332341
ClassDB::bind_static_method("AudioStreamMP3", D_METHOD("load_from_file", "path"), &AudioStreamMP3::load_from_file);
@@ -349,6 +358,8 @@ void AudioStreamMP3::_bind_methods() {
349358
ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamMP3::set_bar_beats);
350359
ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamMP3::get_bar_beats);
351360

361+
ClassDB::bind_method(D_METHOD("create_placeholder"), &AudioStreamMP3::create_placeholder);
362+
352363
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
353364
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm");
354365
ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count");

modules/minimp3/audio_stream_mp3.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class AudioStreamMP3 : public AudioStream {
147147

148148
virtual void get_parameter_list(List<Parameter> *r_parameters) override;
149149

150+
virtual Ref<Resource> create_placeholder() const;
151+
150152
AudioStreamMP3();
151153
virtual ~AudioStreamMP3();
152154
};

modules/minimp3/doc_classes/AudioStreamMP3.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
<tutorials>
1111
</tutorials>
1212
<methods>
13+
<method name="create_placeholder" qualifiers="const">
14+
<return type="Resource" />
15+
<description>
16+
Creates a placeholder version of this resource ([PlaceholderAudioStream]).
17+
</description>
18+
</method>
1319
<method name="load_from_buffer" qualifiers="static">
1420
<return type="AudioStreamMP3" />
1521
<param index="0" name="stream_data" type="PackedByteArray" />

modules/vorbis/audio_stream_ogg_vorbis.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "audio_stream_ogg_vorbis.h"
3232
#include "core/io/file_access.h"
33+
#include "scene/resources/placeholder_audio_stream.h"
3334

3435
#include "core/templates/rb_map.h"
3536

@@ -703,6 +704,14 @@ Ref<AudioStreamOggVorbis> AudioStreamOggVorbis::load_from_file(const String &p_p
703704
return load_from_buffer(stream_data);
704705
}
705706

707+
Ref<Resource> AudioStreamOggVorbis::create_placeholder() const {
708+
Ref<PlaceholderAudioStream> placeholder;
709+
placeholder.instantiate();
710+
placeholder->set_length(get_length());
711+
placeholder->set_tags(get_tags());
712+
return placeholder;
713+
}
714+
706715
void AudioStreamOggVorbis::_bind_methods() {
707716
ClassDB::bind_static_method("AudioStreamOggVorbis", D_METHOD("load_from_buffer", "stream_data"), &AudioStreamOggVorbis::load_from_buffer);
708717
ClassDB::bind_static_method("AudioStreamOggVorbis", D_METHOD("load_from_file", "path"), &AudioStreamOggVorbis::load_from_file);
@@ -728,6 +737,8 @@ void AudioStreamOggVorbis::_bind_methods() {
728737
ClassDB::bind_method(D_METHOD("set_tags", "tags"), &AudioStreamOggVorbis::set_tags);
729738
ClassDB::bind_method(D_METHOD("get_tags"), &AudioStreamOggVorbis::get_tags);
730739

740+
ClassDB::bind_method(D_METHOD("create_placeholder"), &AudioStreamOggVorbis::create_placeholder);
741+
731742
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "packet_sequence", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_packet_sequence", "get_packet_sequence");
732743
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm");
733744
ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count");

modules/vorbis/audio_stream_ogg_vorbis.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <vorbis/codec.h>
3939

4040
class AudioStreamOggVorbis;
41+
class PlaceholderAudioStream;
4142

4243
class AudioStreamPlaybackOggVorbis : public AudioStreamPlaybackResampled {
4344
GDCLASS(AudioStreamPlaybackOggVorbis, AudioStreamPlaybackResampled);
@@ -177,6 +178,8 @@ class AudioStreamOggVorbis : public AudioStream {
177178
}
178179
virtual Ref<AudioSample> generate_sample() const override;
179180

181+
virtual Ref<Resource> create_placeholder() const;
182+
180183
AudioStreamOggVorbis();
181184
virtual ~AudioStreamOggVorbis();
182185
};

modules/vorbis/doc_classes/AudioStreamOggVorbis.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
1111
</tutorials>
1212
<methods>
13+
<method name="create_placeholder" qualifiers="const">
14+
<return type="Resource" />
15+
<description>
16+
Creates a placeholder version of this resource ([PlaceholderAudioStream]).
17+
</description>
18+
</method>
1319
<method name="load_from_buffer" qualifiers="static">
1420
<return type="AudioStreamOggVorbis" />
1521
<param index="0" name="stream_data" type="PackedByteArray" />

scene/register_scene_types.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
#include "scene/resources/mesh_data_tool.h"
133133
#include "scene/resources/mesh_texture.h"
134134
#include "scene/resources/multimesh.h"
135+
#include "scene/resources/placeholder_audio_stream.h"
135136
#if !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
136137
#include "scene/resources/navigation_mesh.h"
137138
#endif // !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
@@ -1116,6 +1117,7 @@ void register_scene_types() {
11161117
GDREGISTER_CLASS(AudioStreamPlayer);
11171118
GDREGISTER_CLASS(AudioStreamWAV);
11181119
GDREGISTER_CLASS(AudioStreamPolyphonic);
1120+
GDREGISTER_CLASS(PlaceholderAudioStream);
11191121
GDREGISTER_ABSTRACT_CLASS(AudioStreamPlaybackPolyphonic);
11201122

11211123
OS::get_singleton()->yield(); // may take time to init

scene/resources/audio_stream_wav.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/io/file_access_memory.h"
3434
#include "core/io/marshalls.h"
35+
#include "scene/resources/placeholder_audio_stream.h"
3536

3637
const float TRIM_DB_LIMIT = -50;
3738
const int TRIM_FADE_OUT_FRAMES = 500;
@@ -1204,6 +1205,14 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_file(const String &p_path, const D
12041205
return load_from_buffer(stream_data, p_options);
12051206
}
12061207

1208+
Ref<Resource> AudioStreamWAV::create_placeholder() const {
1209+
Ref<PlaceholderAudioStream> placeholder;
1210+
placeholder.instantiate();
1211+
placeholder->set_length(get_length());
1212+
placeholder->set_tags(get_tags());
1213+
return placeholder;
1214+
}
1215+
12071216
void AudioStreamWAV::_bind_methods() {
12081217
ClassDB::bind_static_method("AudioStreamWAV", D_METHOD("load_from_buffer", "stream_data", "options"), &AudioStreamWAV::load_from_buffer, DEFVAL(Dictionary()));
12091218
ClassDB::bind_static_method("AudioStreamWAV", D_METHOD("load_from_file", "path", "options"), &AudioStreamWAV::load_from_file, DEFVAL(Dictionary()));
@@ -1234,6 +1243,8 @@ void AudioStreamWAV::_bind_methods() {
12341243

12351244
ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamWAV::save_to_wav);
12361245

1246+
ClassDB::bind_method(D_METHOD("create_placeholder"), &AudioStreamWAV::create_placeholder);
1247+
12371248
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
12381249
ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA ADPCM,Quite OK Audio"), "set_format", "get_format");
12391250
ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode");

0 commit comments

Comments
 (0)