1
0
mirror of https://github.com/godotengine/godot.git synced 2025-12-30 18:30:54 +00:00

Merge pull request #96547 from DeeJayLSP/drmp3

Replace `minimp3` with `dr_mp3`
This commit is contained in:
Thaddeus Crews
2025-12-10 18:10:31 -06:00
22 changed files with 5561 additions and 3562 deletions

View File

@@ -1,17 +0,0 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
Import("env_modules")
env_minimp3 = env_modules.Clone()
thirdparty_dir = "#thirdparty/minimp3/"
env_minimp3.Prepend(CPPPATH=[thirdparty_dir])
if not env["minimp3_extra_formats"]:
env_minimp3.Append(CPPDEFINES=["MINIMP3_ONLY_MP3"])
# Godot source files
env_minimp3.add_source_files(env.modules_sources, "*.cpp")

13
modules/mp3/SCsub Normal file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
Import("env_modules")
env_mp3 = env_modules.Clone()
if not env["mp3_extra_formats"]:
env_mp3.Append(CPPDEFINES=["DR_MP3_ONLY_MP3"])
# Godot source files
env_mp3.add_source_files(env.modules_sources, "*.cpp")

View File

@@ -28,13 +28,15 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#define MINIMP3_FLOAT_OUTPUT
#define MINIMP3_IMPLEMENTATION
#define MINIMP3_NO_STDIO
#define DR_MP3_FLOAT_OUTPUT
#define DR_MP3_IMPLEMENTATION
#define DR_MP3_NO_STDIO
#include "audio_stream_mp3.h"
#include "core/io/file_access.h"
#include "thirdparty/dr_libs/dr_bridge.h"
int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
if (!active) {
return 0;
@@ -53,13 +55,12 @@ int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
}
while (todo && active) {
mp3dec_frame_info_t frame_info;
mp3d_sample_t *buf_frame = nullptr;
drmp3d_sample_t buf_frame[2];
int samples_mixed = mp3dec_ex_read_frame(&mp3d, &buf_frame, &frame_info, mp3_stream->channels);
int samples_mixed = drmp3_read_pcm_frames_f32(&mp3d, 1, buf_frame);
if (samples_mixed) {
p_buffer[p_frames - todo] = AudioFrame(buf_frame[0], buf_frame[samples_mixed - 1]);
p_buffer[p_frames - todo] = AudioFrame(buf_frame[0], buf_frame[mp3d.channels - 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++;
@@ -69,8 +70,8 @@ int AudioStreamPlaybackMP3::_mix_internal(AudioFrame *p_buffer, int p_frames) {
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]);
samples_mixed = drmp3_read_pcm_frames_f32(&mp3d, 1, buf_frame);
loop_fade[i] = AudioFrame(buf_frame[0], buf_frame[mp3d.channels - 1]);
if (!samples_mixed) {
break;
}
@@ -137,7 +138,7 @@ void AudioStreamPlaybackMP3::seek(double p_time) {
}
frames_mixed = uint32_t(mp3_stream->sample_rate * p_time);
mp3dec_ex_seek(&mp3d, (uint64_t)frames_mixed * mp3_stream->channels);
drmp3_seek_to_pcm_frame(&mp3d, (uint64_t)frames_mixed);
}
void AudioStreamPlaybackMP3::tag_used_streams() {
@@ -183,7 +184,7 @@ Variant AudioStreamPlaybackMP3::get_parameter(const StringName &p_name) const {
}
AudioStreamPlaybackMP3::~AudioStreamPlaybackMP3() {
mp3dec_ex_close(&mp3d);
drmp3_uninit(&mp3d);
}
Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() {
@@ -197,15 +198,13 @@ Ref<AudioStreamPlayback> AudioStreamMP3::instantiate_playback() {
mp3s.instantiate();
mp3s->mp3_stream = Ref<AudioStreamMP3>(this);
int errorcode = mp3dec_ex_open_buf(&mp3s->mp3d, data.ptr(), data_len, MP3D_SEEK_TO_SAMPLE);
int success = drmp3_init_memory(&mp3s->mp3d, data.ptr(), data_len, (drmp3_allocation_callbacks *)&dr_alloc_calls);
mp3s->frames_mixed = 0;
mp3s->active = false;
mp3s->loops = 0;
if (errorcode) {
ERR_FAIL_COND_V(errorcode, Ref<AudioStreamPlaybackMP3>());
}
ERR_FAIL_COND_V(!success, Ref<AudioStreamPlaybackMP3>());
return mp3s;
}
@@ -221,18 +220,18 @@ void AudioStreamMP3::clear_data() {
void AudioStreamMP3::set_data(const Vector<uint8_t> &p_data) {
int src_data_len = p_data.size();
mp3dec_ex_t *mp3d = memnew(mp3dec_ex_t);
int err = mp3dec_ex_open_buf(mp3d, p_data.ptr(), src_data_len, MP3D_SEEK_TO_SAMPLE);
if (err || mp3d->info.hz == 0) {
drmp3 *mp3d = memnew(drmp3);
int success = drmp3_init_memory(mp3d, p_data.ptr(), src_data_len, (drmp3_allocation_callbacks *)&dr_alloc_calls);
if (!success || mp3d->sampleRate == 0) {
memdelete(mp3d);
ERR_FAIL_MSG("Failed to decode mp3 file. Make sure it is a valid mp3 audio file.");
}
channels = mp3d->info.channels;
sample_rate = mp3d->info.hz;
length = float(mp3d->samples) / (sample_rate * float(channels));
channels = mp3d->channels;
sample_rate = mp3d->sampleRate;
length = float(drmp3_get_pcm_frame_count(mp3d)) / (mp3d->sampleRate);
mp3dec_ex_close(mp3d);
drmp3_uninit(mp3d);
memdelete(mp3d);
data = p_data;

View File

@@ -32,7 +32,7 @@
#include "servers/audio/audio_stream.h"
#include <minimp3_ex.h>
#include "thirdparty/dr_libs/dr_mp3.h"
class AudioStreamMP3;
@@ -47,7 +47,7 @@ class AudioStreamPlaybackMP3 : public AudioStreamPlaybackResampled {
bool looping_override = false;
bool looping = false;
mp3dec_ex_t mp3d = {};
drmp3 mp3d = {};
uint32_t frames_mixed = 0;
bool active = false;
int loops = 0;

View File

@@ -6,7 +6,7 @@ def get_opts(platform):
from SCons.Variables import BoolVariable
return [
BoolVariable("minimp3_extra_formats", "Build minimp3 with MP1/MP2 decoding support", False),
BoolVariable("mp3_extra_formats", "Build mp3 module with MP1/MP2 decoding support", False),
]

View File

@@ -44,7 +44,7 @@ static void _editor_init() {
}
#endif
void initialize_minimp3_module(ModuleInitializationLevel p_level) {
void initialize_mp3_module(ModuleInitializationLevel p_level) {
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
GDREGISTER_CLASS(AudioStreamMP3);
}
@@ -58,5 +58,5 @@ void initialize_minimp3_module(ModuleInitializationLevel p_level) {
#endif
}
void uninitialize_minimp3_module(ModuleInitializationLevel p_level) {
void uninitialize_mp3_module(ModuleInitializationLevel p_level) {
}

View File

@@ -32,5 +32,5 @@
#include "modules/register_module_types.h"
void initialize_minimp3_module(ModuleInitializationLevel p_level);
void uninitialize_minimp3_module(ModuleInitializationLevel p_level);
void initialize_mp3_module(ModuleInitializationLevel p_level);
void uninitialize_mp3_module(ModuleInitializationLevel p_level);

View File

@@ -46,7 +46,7 @@ String ResourceImporterMP3::get_visible_name() const {
}
void ResourceImporterMP3::get_recognized_extensions(List<String> *p_extensions) const {
#ifndef MINIMP3_ONLY_MP3
#ifndef DR_MP3_ONLY_MP3
p_extensions->push_back("mp1");
p_extensions->push_back("mp2");
#endif