summaryrefslogtreecommitdiff
path: root/modules/minimp3
diff options
context:
space:
mode:
Diffstat (limited to 'modules/minimp3')
-rw-r--r--modules/minimp3/audio_stream_mp3.cpp71
-rw-r--r--modules/minimp3/audio_stream_mp3.h25
-rw-r--r--modules/minimp3/doc_classes/AudioStreamMP3.xml31
-rw-r--r--modules/minimp3/resource_importer_mp3.cpp48
-rw-r--r--modules/minimp3/resource_importer_mp3.h6
5 files changed, 170 insertions, 11 deletions
diff --git a/modules/minimp3/audio_stream_mp3.cpp b/modules/minimp3/audio_stream_mp3.cpp
index c37bea519f..98bcdea8f4 100644
--- a/modules/minimp3/audio_stream_mp3.cpp
+++ b/modules/minimp3/audio_stream_mp3.cpp
@@ -46,6 +46,12 @@ int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
int frames_mixed_this_step = p_frames;
+ int beat_length_frames = -1;
+ bool beat_loop = mp3_stream->has_loop() && mp3_stream->get_bpm() > 0 && mp3_stream->get_beat_count() > 0;
+ if (beat_loop) {
+ beat_length_frames = mp3_stream->get_beat_count() * mp3_stream->sample_rate * 60 / mp3_stream->get_bpm();
+ }
+
while (todo && active) {
mp3dec_frame_info_t frame_info;
mp3d_sample_t *buf_frame = nullptr;
@@ -54,8 +60,25 @@ int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
if (samples_mixed) {
p_buffer[p_frames - todo] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
+ if (loop_fade_remaining < FADE_SIZE) {
+ p_buffer[p_frames - todo] += loop_fade[loop_fade_remaining] * (float(FADE_SIZE - loop_fade_remaining) / float(FADE_SIZE));
+ loop_fade_remaining++;
+ }
--todo;
++frames_mixed;
+
+ if (beat_loop && (int)frames_mixed >= beat_length_frames) {
+ for (int i = 0; i < FADE_SIZE; i++) {
+ samples_mixed = mp3dec_ex_read_frame(mp3d, &buf_frame, &frame_info, mp3_stream->channels);
+ loop_fade[i] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
+ if (!samples_mixed) {
+ break;
+ }
+ }
+ loop_fade_remaining = 0;
+ seek(mp3_stream->loop_offset);
+ loops++;
+ }
}
else {
@@ -117,6 +140,10 @@ void AudioStreamPlaybackMP3::seek(float p_time) {
mp3dec_ex_seek(mp3d, (uint64_t)frames_mixed * mp3_stream->channels);
}
+void AudioStreamPlaybackMP3::tag_used_streams() {
+ mp3_stream->tag_used(get_playback_position());
+}
+
AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() {
if (mp3d) {
mp3dec_ex_close(mp3d);
@@ -124,7 +151,7 @@ AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() {
}
}
-Ref<AudioStreamPlayback> AudioStreamMP3::instance_playback() {
+Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() {
Ref<AudioStreamPlaybackMP3> mp3s;
ERR_FAIL_COND_V_MSG(data.is_empty(), mp3s,
@@ -206,6 +233,36 @@ bool AudioStreamMP3::is_monophonic() const {
return false;
}
+void AudioStreamMP3::set_bpm(double p_bpm) {
+ ERR_FAIL_COND(p_bpm < 0);
+ bpm = p_bpm;
+ emit_changed();
+}
+
+double AudioStreamMP3::get_bpm() const {
+ return bpm;
+}
+
+void AudioStreamMP3::set_beat_count(int p_beat_count) {
+ ERR_FAIL_COND(p_beat_count < 0);
+ beat_count = p_beat_count;
+ emit_changed();
+}
+
+int AudioStreamMP3::get_beat_count() const {
+ return beat_count;
+}
+
+void AudioStreamMP3::set_bar_beats(int p_bar_beats) {
+ ERR_FAIL_COND(p_bar_beats < 0);
+ bar_beats = p_bar_beats;
+ emit_changed();
+}
+
+int AudioStreamMP3::get_bar_beats() const {
+ return bar_beats;
+}
+
void AudioStreamMP3::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamMP3::set_data);
ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamMP3::get_data);
@@ -216,7 +273,19 @@ void AudioStreamMP3::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_loop_offset", "seconds"), &AudioStreamMP3::set_loop_offset);
ClassDB::bind_method(D_METHOD("get_loop_offset"), &AudioStreamMP3::get_loop_offset);
+ ClassDB::bind_method(D_METHOD("set_bpm", "bpm"), &AudioStreamMP3::set_bpm);
+ ClassDB::bind_method(D_METHOD("get_bpm"), &AudioStreamMP3::get_bpm);
+
+ ClassDB::bind_method(D_METHOD("set_beat_count", "count"), &AudioStreamMP3::set_beat_count);
+ ClassDB::bind_method(D_METHOD("get_beat_count"), &AudioStreamMP3::get_beat_count);
+
+ ClassDB::bind_method(D_METHOD("set_bar_beats", "count"), &AudioStreamMP3::set_bar_beats);
+ ClassDB::bind_method(D_METHOD("get_bar_beats"), &AudioStreamMP3::get_bar_beats);
+
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), "set_bpm", "get_bpm");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,1,or_greater"), "set_beat_count", "get_beat_count");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,1,or_greater"), "set_bar_beats", "get_bar_beats");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_offset"), "set_loop_offset", "get_loop_offset");
}
diff --git a/modules/minimp3/audio_stream_mp3.h b/modules/minimp3/audio_stream_mp3.h
index c1a60ddccb..428ac1240e 100644
--- a/modules/minimp3/audio_stream_mp3.h
+++ b/modules/minimp3/audio_stream_mp3.h
@@ -41,6 +41,12 @@ class AudioStreamMP3;
class AudioStreamPlaybackMP3 : public AudioStreamPlaybackResampled {
GDCLASS(AudioStreamPlaybackMP3, AudioStreamPlaybackResampled);
+ enum {
+ FADE_SIZE = 256
+ };
+ AudioFrame loop_fade[FADE_SIZE];
+ int loop_fade_remaining = FADE_SIZE;
+
mp3dec_ex_t *mp3d = nullptr;
uint32_t frames_mixed = 0;
bool active = false;
@@ -64,6 +70,8 @@ public:
virtual float get_playback_position() const override;
virtual void seek(float p_time) override;
+ virtual void tag_used_streams() override;
+
AudioStreamPlaybackMP3() {}
~AudioStreamPlaybackMP3();
};
@@ -85,17 +93,30 @@ class AudioStreamMP3 : public AudioStream {
float loop_offset = 0.0;
void clear_data();
+ double bpm = 0;
+ int beat_count = 0;
+ int bar_beats = 4;
+
protected:
static void _bind_methods();
public:
void set_loop(bool p_enable);
- bool has_loop() const;
+ virtual bool has_loop() const override;
void set_loop_offset(float p_seconds);
float get_loop_offset() const;
- virtual Ref<AudioStreamPlayback> instance_playback() override;
+ void set_bpm(double p_bpm);
+ virtual double get_bpm() const override;
+
+ void set_beat_count(int p_beat_count);
+ virtual int get_beat_count() const override;
+
+ void set_bar_beats(int p_bar_beats);
+ virtual int get_bar_beats() const override;
+
+ virtual Ref<AudioStreamPlayback> instantiate_playback() override;
virtual String get_stream_name() const override;
void set_data(const Vector<uint8_t> &p_data);
diff --git a/modules/minimp3/doc_classes/AudioStreamMP3.xml b/modules/minimp3/doc_classes/AudioStreamMP3.xml
index f5f7d3ef17..8f03681c06 100644
--- a/modules/minimp3/doc_classes/AudioStreamMP3.xml
+++ b/modules/minimp3/doc_classes/AudioStreamMP3.xml
@@ -4,13 +4,42 @@
MP3 audio stream driver.
</brief_description>
<description>
- MP3 audio stream driver.
+ MP3 audio stream driver. See [member data] if you want to load an MP3 file at run-time.
</description>
<tutorials>
</tutorials>
<members>
+ <member name="bar_beats" type="int" setter="set_bar_beats" getter="get_bar_beats" default="4">
+ </member>
+ <member name="beat_count" type="int" setter="set_beat_count" getter="get_beat_count" default="0">
+ </member>
+ <member name="bpm" type="float" setter="set_bpm" getter="get_bpm" default="0.0">
+ </member>
<member name="data" type="PackedByteArray" setter="set_data" getter="get_data" default="PackedByteArray()">
Contains the audio data in bytes.
+ You can load a file without having to import it beforehand using the code snippet below. Keep in mind that this snippet loads the whole file into memory and may not be ideal for huge files (hundreds of megabytes or more).
+ [codeblocks]
+ [gdscript]
+ func load_mp3(path):
+ var file = File.new()
+ file.open(path, File.READ)
+ var sound = AudioStreamMP3.new()
+ sound.data = file.get_buffer(file.get_length())
+ file.close()
+ return sound
+ [/gdscript]
+ [csharp]
+ public AudioStreamMP3 LoadMP3(string path)
+ {
+ var file = new File();
+ file.Open(path, File.READ);
+ var sound = new AudioStreamMP3();
+ sound.Data = file.GetBuffer(file.GetLength());
+ file.Close();
+ return sound;
+ }
+ [/csharp]
+ [/codeblocks]
</member>
<member name="loop" type="bool" setter="set_loop" getter="has_loop" default="false">
If [code]true[/code], the stream will automatically loop when it reaches the end.
diff --git a/modules/minimp3/resource_importer_mp3.cpp b/modules/minimp3/resource_importer_mp3.cpp
index e03940f963..7492533094 100644
--- a/modules/minimp3/resource_importer_mp3.cpp
+++ b/modules/minimp3/resource_importer_mp3.cpp
@@ -34,6 +34,10 @@
#include "core/io/resource_saver.h"
#include "scene/resources/texture.h"
+#ifdef TOOLS_ENABLED
+#include "editor/import/audio_stream_import_settings.h"
+#endif
+
String ResourceImporterMP3::get_importer_name() const {
return "mp3";
}
@@ -69,14 +73,26 @@ String ResourceImporterMP3::get_preset_name(int p_idx) const {
void ResourceImporterMP3::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "loop"), true));
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "loop_offset"), 0));
+ r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "bpm", PROPERTY_HINT_RANGE, "0,400,0.01,or_greater"), 0));
+ r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "beat_count", PROPERTY_HINT_RANGE, "0,512,or_greater"), 0));
+ r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "bar_beats", PROPERTY_HINT_RANGE, "2,32,or_greater"), 4));
}
-Error ResourceImporterMP3::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
- bool loop = p_options["loop"];
- float loop_offset = p_options["loop_offset"];
+#ifdef TOOLS_ENABLED
+bool ResourceImporterMP3::has_advanced_options() const {
+ return true;
+}
+void ResourceImporterMP3::show_advanced_options(const String &p_path) {
+ Ref<AudioStreamMP3> mp3_stream = import_mp3(p_path);
+ if (mp3_stream.is_valid()) {
+ AudioStreamImportSettings::get_singleton()->edit(p_path, "mp3", mp3_stream);
+ }
+}
+#endif
- Ref<FileAccess> f = FileAccess::open(p_source_file, FileAccess::READ);
- ERR_FAIL_COND_V(f.is_null(), ERR_CANT_OPEN);
+Ref<AudioStreamMP3> ResourceImporterMP3::import_mp3(const String &p_path) {
+ Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
+ ERR_FAIL_COND_V(f.is_null(), Ref<AudioStreamMP3>());
uint64_t len = f->get_length();
@@ -90,11 +106,29 @@ Error ResourceImporterMP3::import(const String &p_source_file, const String &p_s
mp3_stream.instantiate();
mp3_stream->set_data(data);
- ERR_FAIL_COND_V(!mp3_stream->get_data().size(), ERR_FILE_CORRUPT);
+ ERR_FAIL_COND_V(!mp3_stream->get_data().size(), Ref<AudioStreamMP3>());
+
+ return mp3_stream;
+}
+
+Error ResourceImporterMP3::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
+ bool loop = p_options["loop"];
+ float loop_offset = p_options["loop_offset"];
+ double bpm = p_options["bpm"];
+ float beat_count = p_options["beat_count"];
+ float bar_beats = p_options["bar_beats"];
+
+ Ref<AudioStreamMP3> mp3_stream = import_mp3(p_source_file);
+ if (mp3_stream.is_null()) {
+ return ERR_CANT_OPEN;
+ }
mp3_stream->set_loop(loop);
mp3_stream->set_loop_offset(loop_offset);
+ mp3_stream->set_bpm(bpm);
+ mp3_stream->set_beat_count(beat_count);
+ mp3_stream->set_bar_beats(bar_beats);
- return ResourceSaver::save(p_save_path + ".mp3str", mp3_stream);
+ return ResourceSaver::save(mp3_stream, p_save_path + ".mp3str");
}
ResourceImporterMP3::ResourceImporterMP3() {
diff --git a/modules/minimp3/resource_importer_mp3.h b/modules/minimp3/resource_importer_mp3.h
index 678a3773bb..38729d68f8 100644
--- a/modules/minimp3/resource_importer_mp3.h
+++ b/modules/minimp3/resource_importer_mp3.h
@@ -50,6 +50,12 @@ public:
virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const override;
virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const override;
+#ifdef TOOLS_ENABLED
+ virtual bool has_advanced_options() const override;
+ virtual void show_advanced_options(const String &p_path) override;
+#endif
+ static Ref<AudioStreamMP3> import_mp3(const String &p_path);
+
virtual Error import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) override;
ResourceImporterMP3();