From 164e797670a7e7c0036f36c8c4668b8ba7a76cfc Mon Sep 17 00:00:00 2001 From: Steel Titanium Date: Sat, 29 Jun 2019 20:35:31 -0400 Subject: [PATCH 1/3] Better loading code for modules --- src/sdl/mixer_sound.c | 59 +++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 3932d23d3..8127edd60 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -1112,6 +1112,32 @@ boolean I_LoadSong(char *data, size_t len) } #endif +#ifdef HAVE_OPENMPT + size_t probesize; + int result; + + if (len > openmpt_probe_file_header_get_recommended_size()) + probesize = openmpt_probe_file_header_get_recommended_size(); + else + probesize = len; + + result = openmpt_probe_file_header(OPENMPT_PROBE_FILE_HEADER_FLAGS_DEFAULT, data, probesize, len, NULL, NULL, NULL, NULL, NULL, NULL); + + if (result == OPENMPT_PROBE_FILE_HEADER_RESULT_SUCCESS) // We only cared if it succeeded, continue on if not. + { + openmpt_mhandle = openmpt_module_create_from_memory2(data, len, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + if (!openmpt_mhandle) + { + mod_err = openmpt_module_error_get_last(openmpt_mhandle); + mod_err_str = openmpt_error_string(mod_err); + CONS_Alert(CONS_ERROR, "openmpt_module_create_from_memory2: %s\n", mod_err_str); + return false; + } + else + return true; + } +#endif + rw = SDL_RWFromMem(data, len); if (rw != NULL) { @@ -1123,39 +1149,6 @@ boolean I_LoadSong(char *data, size_t len) return false; } -#ifdef HAVE_OPENMPT - switch(Mix_GetMusicType(music)) - { - case MUS_MODPLUG: - case MUS_MOD: - openmpt_mhandle = openmpt_module_create_from_memory2(data, len, NULL, NULL, NULL, NULL, NULL, NULL, NULL); - if (!openmpt_mhandle) - { - mod_err = openmpt_module_error_get_last(openmpt_mhandle); - mod_err_str = openmpt_error_string(mod_err); - CONS_Alert(CONS_ERROR, "openmpt_module_create_from_memory2: %s\n", mod_err_str); - Mix_FreeMusic(music); - music = NULL; - return false; - } - else - { - Mix_FreeMusic(music); - music = NULL; - return true; - } - break; - case MUS_WAV: - case MUS_MID: - case MUS_OGG: - case MUS_MP3: - case MUS_FLAC: - Mix_HookMusic(NULL, NULL); - break; - default: - break; - } -#endif // Find the OGG loop point. loop_point = 0.0f; From 21c6836fe070e2ea1dc810c0b036ee952b6d2f98 Mon Sep 17 00:00:00 2001 From: Steel Titanium Date: Tue, 9 Jul 2019 00:19:21 -0400 Subject: [PATCH 2/3] Some few comments --- src/sdl/mixer_sound.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 8127edd60..28cedcf27 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -1116,6 +1116,12 @@ boolean I_LoadSong(char *data, size_t len) size_t probesize; int result; + /* + If the size of the data to be checked is bigger than the recommended size (> 2048) + Let's just set the probe size to the recommended size + Otherwise let's give it the full data size + */ + if (len > openmpt_probe_file_header_get_recommended_size()) probesize = openmpt_probe_file_header_get_recommended_size(); else @@ -1126,7 +1132,7 @@ boolean I_LoadSong(char *data, size_t len) if (result == OPENMPT_PROBE_FILE_HEADER_RESULT_SUCCESS) // We only cared if it succeeded, continue on if not. { openmpt_mhandle = openmpt_module_create_from_memory2(data, len, NULL, NULL, NULL, NULL, NULL, NULL, NULL); - if (!openmpt_mhandle) + if (!openmpt_mhandle) // Failed to create module handle? Show error and return! { mod_err = openmpt_module_error_get_last(openmpt_mhandle); mod_err_str = openmpt_error_string(mod_err); @@ -1134,10 +1140,11 @@ boolean I_LoadSong(char *data, size_t len) return false; } else - return true; + return true; // All good and we're ready for music playback! } -#endif +#endif + // Let's see if Mixer is able to load this. rw = SDL_RWFromMem(data, len); if (rw != NULL) { @@ -1149,7 +1156,6 @@ boolean I_LoadSong(char *data, size_t len) return false; } - // Find the OGG loop point. loop_point = 0.0f; song_length = 0.0f; From 60cb2de6a07944430cedf1f4173f4d6b0a16b4c3 Mon Sep 17 00:00:00 2001 From: Steel Titanium Date: Tue, 9 Jul 2019 15:51:11 -0400 Subject: [PATCH 3/3] Seperate declarations from code --- src/sdl/mixer_sound.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 28cedcf27..883ba992a 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -111,6 +111,8 @@ static UINT16 current_track; int mod_err = OPENMPT_ERROR_OK; static const char *mod_err_str; static UINT16 current_subsong; +size_t probesize; +int result; #endif static void var_cleanup(void) @@ -1113,9 +1115,6 @@ boolean I_LoadSong(char *data, size_t len) #endif #ifdef HAVE_OPENMPT - size_t probesize; - int result; - /* If the size of the data to be checked is bigger than the recommended size (> 2048) Let's just set the probe size to the recommended size