From 8ec431acb29430cfb7b22ee65d38e01b3418310f Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 16 Nov 2025 05:06:04 +0100 Subject: [PATCH] mixer_sound/s_sound: remove all the pointless "else" + some tiny optimizations here and there --- src/s_sound.c | 40 +++++++----- src/sdl/mixer_sound.c | 148 +++++++++++++++++------------------------- 2 files changed, 84 insertions(+), 104 deletions(-) diff --git a/src/s_sound.c b/src/s_sound.c index 24743cc56..b21a16c4a 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -215,26 +215,33 @@ static INT32 S_getChannel(const void *origin, sfxinfo_t *sfxinfo) if (!channels[cnum].sfxinfo) break; + const boolean samesoundplaying = channels[cnum].sfxinfo == sfxinfo; + // Now checks if same sound is being played, rather // than just one sound per mobj - else if (sfxinfo == channels[cnum].sfxinfo && (sfxinfo->pitch & SF_NOMULTIPLESOUND)) + if (samesoundplaying && (sfxinfo->pitch & SF_NOMULTIPLESOUND)) { return -1; } - else if (sfxinfo == channels[cnum].sfxinfo && sfxinfo->singularity == true) + + if (samesoundplaying && sfxinfo->singularity == true) { S_StopChannel(cnum); break; } - else if (origin && channels[cnum].origin == origin && channels[cnum].sfxinfo == sfxinfo) + + const boolean sameorigin = origin && channels[cnum].origin == origin; + + if (sameorigin && samesoundplaying) { if (sfxinfo->pitch & SF_NOINTERRUPT) return -1; - else - S_StopChannel(cnum); + + S_StopChannel(cnum); break; } - else if (origin && channels[cnum].origin == origin + + if (sameorigin && channels[cnum].sfxinfo->name != sfxinfo->name && (channels[cnum].sfxinfo->pitch & SF_TOTALLYSINGLE) && (sfxinfo->pitch & SF_TOTALLYSINGLE)) { @@ -1280,8 +1287,7 @@ static musicdef_t *S_AddMusicCredit(void) struct cursongcredit cursongcredit = {0}; // Currently displayed song credit info -static boolean -ReadMusicDefFields (UINT16 wadnum, int line, char *stoken, musicdef_t **defp) +static boolean ReadMusicDefFields(UINT16 wadnum, int line, char *stoken, musicdef_t **defp) { musicdef_t *def; @@ -1492,7 +1498,7 @@ void S_InitMusicDefs(void) // musicdef_t *S_FindMusicCredit(const char *musname) { - UINT32 hash = quickncasehash (musname, 6); + UINT32 hash = quickncasehash(musname, 6); musicdef_t *def; for (INT32 i = 0; i < nummusicdefs; ++i) @@ -1651,7 +1657,8 @@ static lumpnum_t S_GetMusicLumpNum(const char *mname) { if (S_MusicExists(mname, false, true)) // check non midis first return W_GetNumForName(va("o_%s", mname)); - else if (S_MusicExists(mname, true, false)) + + if (S_MusicExists(mname, true, false)) { #ifdef NO_MIDI CONS_Alert(CONS_ERROR, "A MIDI music lump %.6s was found,\nbut SRB2Kart does not support MIDI output.\nWe apologise for the inconvenience.\n", mname); @@ -1691,8 +1698,8 @@ static boolean S_LoadMusic(const char *mname) music.data = mdata; return true; } - else - return false; + + return false; } static void S_UnloadMusic(void) @@ -1932,8 +1939,8 @@ boolean S_FadeMusicFromVolume(UINT8 target_volume, INT16 source_volume, UINT32 m { if (source_volume < 0) return I_FadeSong(target_volume, ms, NULL); - else - return I_FadeSongFromVolume(target_volume, source_volume, ms, NULL); + + return I_FadeSongFromVolume(target_volume, source_volume, ms, NULL); } boolean S_FadeOutStopMusic(UINT32 ms) @@ -2090,6 +2097,7 @@ void S_InitMapMusic(void) { S_ChangeMusicEx(mapmusic.name, mapmusic.flags, true, keepmusic.resume, 0, 500); } + return; } @@ -2103,7 +2111,7 @@ void S_InitMapMusic(void) return; if (leveltime < MUSICSTARTTIME) // SRB2Kart - S_ChangeMusicInternal((encoremode ? "estart" : "kstart"), false); //S_StopMusic(); + S_ChangeMusicInternal((encoremode ? "estart" : "kstart"), false); // S_StopMusic(); } void S_StartMapMusic(void) @@ -2371,7 +2379,7 @@ static void AmigaType_OnChange(void) openmpt_module_ctl_set_text(openmpt_mhandle, "render.resampler.emulate_amiga_type", cv_amigatype.string); if (sound_started) - S_RestartMusic(); //need to restart the music system or else it wont work + S_RestartMusic(); // need to restart the music system or else it wont work } #endif #endif diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 3239da9b2..6f8098813 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -181,7 +181,8 @@ void I_StartupSound(void) CONS_Debug(DBG_DETAILED, "SDL Audio already started\n"); return; } - else if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) + + if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { CONS_Alert(CONS_ERROR, "Error initializing SDL Audio: %s\n", SDL_GetError()); // call to start audio failed -- we do not have it @@ -606,8 +607,8 @@ static UINT32 get_adjusted_position(UINT32 position) UINT32 looppoint = I_GetSongLoopPoint(); return position >= length ? (position % (length-looppoint)) : position; } - else - return position; + + return position; } static void do_fading_callback(void) @@ -693,11 +694,13 @@ static UINT32 music_fade(UINT32 interval, void *param) do_fading_callback(); return 0; } - else if (songpaused) // don't decrement timer + + if (songpaused) // don't decrement timer { return interval; } - else if ((fading_timer -= 10) <= 0) + + if ((fading_timer -= 10) <= 0) { internal_volume = fading_target; Mix_VolumeMusic(get_real_volume(music_volume)); @@ -705,20 +708,18 @@ static UINT32 music_fade(UINT32 interval, void *param) do_fading_callback(); return 0; } - else - { - UINT8 delta = abs(fading_target - fading_source); - fixed_t factor = FixedDiv(fading_duration - fading_timer, fading_duration); - if (fading_target < fading_source) - internal_volume = max(min(internal_volume, fading_source - FixedMul(delta, factor)), fading_target); - else if (fading_target > fading_source) - internal_volume = min(max(internal_volume, fading_source + FixedMul(delta, factor)), fading_target); + UINT8 delta = abs(fading_target - fading_source); + fixed_t factor = FixedMul(delta, FixedDiv(fading_duration - fading_timer, fading_duration)); - Mix_VolumeMusic(get_real_volume(music_volume)); + if (fading_target < fading_source) + internal_volume = max(min(internal_volume, fading_source - factor), fading_target); + else if (fading_target > fading_source) + internal_volume = min(max(internal_volume, fading_source + factor), fading_target); - return interval; - } + Mix_VolumeMusic(get_real_volume(music_volume)); + + return interval; } #ifdef HAVE_LIBGME @@ -792,7 +793,6 @@ musictype_t I_SongType(void) #ifdef HAVE_LIBGME if (gme) return MU_GME; - else #endif #ifdef HAVE_OPENMPT if (openmpt_mhandle) @@ -822,10 +822,10 @@ boolean I_SongPlaying(void) { return ( #ifdef HAVE_LIBGME - (I_SongType() == MU_GME && gme) || + (gme && I_SongType() == MU_GME) || #endif #ifdef HAVE_OPENMPT - (I_SongType() == MU_MOD_EX && openmpt_mhandle) || + (openmpt_mhandle && I_SongType() == MU_MOD_EX) || #endif music != NULL ); @@ -844,7 +844,6 @@ boolean I_SetSongSpeed(float speed) { if (speed > 250.0f) speed = 250.0f; //limit speed up to 250x - #ifdef HAVE_LIBGME if (gme) { @@ -853,7 +852,6 @@ boolean I_SetSongSpeed(float speed) SDL_UnlockAudio(); return true; } - else #endif #ifdef HAVE_OPENMPT if (openmpt_mhandle) @@ -917,30 +915,26 @@ UINT32 I_GetSongLength(void) return max(length, 0); } - else #endif #ifdef HAVE_OPENMPT if (openmpt_mhandle) { return (UINT32)(openmpt_module_get_duration_seconds(openmpt_mhandle) * 1000.); } - else #endif if (!music || I_SongType() == MU_MOD || I_SongType() == MU_MID) { return 0; } - else - { - // VERY IMPORTANT to set your LENGTHMS= in your song files, folks! - // SDL mixer can't read music length itself. - length = (UINT32)(song_length*1000); - if (!length) - CONS_Debug(DBG_DETAILED, "Getting music length: music is missing LENGTHMS= tag. Needed for seeking.\n"); + // VERY IMPORTANT to set your LENGTHMS= in your song files, folks! + // SDL mixer can't read music length itself. + length = (UINT32)(song_length*1000); - return length; - } + if (!length) + CONS_Debug(DBG_DETAILED, "Getting music length: music is missing LENGTHMS= tag. Needed for seeking.\n"); + + return length; } boolean I_SetSongLoopPoint(UINT32 looppoint) @@ -980,18 +974,16 @@ UINT32 I_GetSongLoopPoint(void) looppoint = info->intro_length > 0 ? info->intro_length : 0; gme_free_info(info); + return max(looppoint, 0); } - else #endif if (!music || I_SongType() == MU_MOD || I_SongType() == MU_MID) { return 0; } - else - { - return (UINT32)(loop_point * 1000); - } + + return (UINT32)(loop_point * 1000); } boolean I_SetSongPosition(UINT32 position) @@ -1021,7 +1013,6 @@ boolean I_SetSongPosition(UINT32 position) // else // return true; } - else #endif #ifdef HAVE_OPENMPT if (openmpt_mhandle) @@ -1031,40 +1022,38 @@ boolean I_SetSongPosition(UINT32 position) openmpt_module_set_position_seconds(openmpt_mhandle, (double)(get_adjusted_position(position)/1000.0L)); // returns new position return true; } - else #endif if (!music || I_SongType() == MU_MID) { return false; } - else if (I_SongType() == MU_MOD) + + if (I_SongType() == MU_MOD) { return Mix_SetMusicPosition(position); // Goes by channels } + + // Because SDL mixer can't identify song length, if you have + // a position input greater than the real length, then + // music_bytes becomes inaccurate. + + length = I_GetSongLength(); // get it in MS + + if (length) + position = get_adjusted_position(position); + + Mix_RewindMusic(); // needed for mp3 + + if (Mix_SetMusicPosition((float)(position/1000.0L)) == 0) + music_bytes = (UINT32)(position/1000.0L*44100.0L*4); //assume 44.1khz, 4-byte length (see I_GetSongPosition) else - { - // Because SDL mixer can't identify song length, if you have - // a position input greater than the real length, then - // music_bytes becomes inaccurate. + // NOTE: This block fires on incorrect song format, + // NOT if position input is greater than song length. + music_bytes = 0; - length = I_GetSongLength(); // get it in MS + music_stutter_bytes = 0; - if (length) - position = get_adjusted_position(position); - - Mix_RewindMusic(); // needed for mp3 - - if (Mix_SetMusicPosition((float)(position/1000.0L)) == 0) - music_bytes = (UINT32)(position/1000.0L*44100.0L*4); //assume 44.1khz, 4-byte length (see I_GetSongPosition) - else - // NOTE: This block fires on incorrect song format, - // NOT if position input is greater than song length. - music_bytes = 0; - - music_stutter_bytes = 0; - - return true; - } + return true; } UINT32 I_GetSongPosition(void) @@ -1097,7 +1086,6 @@ UINT32 I_GetSongPosition(void) return max(position, 0); } - else #endif #ifdef HAVE_OPENMPT if (openmpt_mhandle) @@ -1105,19 +1093,16 @@ UINT32 I_GetSongPosition(void) // So return unadjusted. See note in SetMusicPosition: we adjust for that. return (UINT32)(openmpt_module_get_position_seconds(openmpt_mhandle)*1000.); //return get_adjusted_position((UINT32)(openmpt_module_get_position_seconds(openmpt_mhandle)*1000.)); - else #endif if (!music || I_SongType() == MU_MID) { return 0; } - else - { - return (UINT32)(music_bytes/44100.0L*1000.0L/4); //assume 44.1khz - // 4 = byte length for 16-bit samples (AUDIO_S16SYS), stereo (2-channel) - // This is hardcoded in I_StartupSound. Other formats for factor: - // 8M: 1 | 8S: 2 | 16M: 2 | 16S: 4 - } + + return (UINT32)(music_bytes/44100.0L*1000.0L/4); //assume 44.1khz + // 4 = byte length for 16-bit samples (AUDIO_S16SYS), stereo (2-channel) + // This is hardcoded in I_StartupSound. Other formats for factor: + // 8M: 1 | 8S: 2 | 16M: 2 | 16S: 4 } void I_UpdateSongLagThreshold(void) @@ -1235,8 +1220,8 @@ boolean I_LoadSong(char *data, size_t len) CONS_Alert(CONS_ERROR, "openmpt_module_create_from_memory2: %s\n", mod_err_str); return false; } - else - return true; // All good and we're ready for music playback! + + return true; // All good and we're ready for music playback! } #endif @@ -1331,31 +1316,20 @@ boolean I_PlaySong(boolean looping) return true; } - else #endif #ifdef HAVE_OPENMPT if (openmpt_mhandle) { openmpt_module_select_subsong(openmpt_mhandle, 0); - + openmpt_module_set_render_param(openmpt_mhandle, OPENMPT_MODULE_RENDER_STEREOSEPARATION_PERCENT, cv_stereosep.value); //have a feeling some might like it #if OPENMPT_API_VERSION_MAJOR < 1 && OPENMPT_API_VERSION_MINOR > 4 openmpt_module_ctl_set_text(openmpt_mhandle, "dither", "1"); + openmpt_module_ctl_set_boolean(openmpt_mhandle, "render.resampler.emulate_amiga", cv_amigafilter.value); + openmpt_module_ctl_set_text(openmpt_mhandle, "render.resampler.emulate_amiga_type", cv_amigatype.string); #else openmpt_module_ctl_set(openmpt_mhandle, "dither", "1"); -#endif - - openmpt_module_set_render_param(openmpt_mhandle, OPENMPT_MODULE_RENDER_STEREOSEPARATION_PERCENT, cv_stereosep.value); //have a feeling some might like it - -#if OPENMPT_API_VERSION_MAJOR < 1 && OPENMPT_API_VERSION_MINOR > 4 - openmpt_module_ctl_set_boolean(openmpt_mhandle, "render.resampler.emulate_amiga", cv_amigafilter.value); -#else openmpt_module_ctl_set(openmpt_mhandle, "render.resampler.emulate_amiga", cv_amigafilter.value ? "1" : "0"); #endif - -#if OPENMPT_API_VERSION_MAJOR < 1 && OPENMPT_API_VERSION_MINOR > 4 - openmpt_module_ctl_set_text(openmpt_mhandle, "render.resampler.emulate_amiga_type", cv_amigatype.string); -#endif - openmpt_module_set_render_param(openmpt_mhandle, OPENMPT_MODULE_RENDER_INTERPOLATIONFILTER_LENGTH, cv_modfilter.value); if (looping) openmpt_module_set_repeat_count(openmpt_mhandle, -1); // Always repeat @@ -1363,7 +1337,6 @@ boolean I_PlaySong(boolean looping) Mix_HookMusic(mix_openmpt, openmpt_mhandle); return true; } - else #endif if (!music) return false; @@ -1504,7 +1477,6 @@ boolean I_SetSongTrack(INT32 track) return false; } - else #endif #ifdef HAVE_OPENMPT if (openmpt_mhandle)