From 374d032a6b63347f4a81e0f029412a336511cc43 Mon Sep 17 00:00:00 2001 From: James R Date: Fri, 7 Aug 2020 12:28:15 -0700 Subject: [PATCH] Clean up stuff, make volume work again --- src/doomdef.h | 5 ++--- src/i_sound.h | 4 ++-- src/s_sound.h | 6 ++++++ src/sdl/mixer_sound.c | 22 +++++++++++++++------- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/doomdef.h b/src/doomdef.h index 26072b705..fbea62c7e 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -676,8 +676,7 @@ extern const char *compdate, *comptime, *comprevision, *compbranch; /// Hardware renderer: OpenGL #define GL_SHADERS -/// Default volume for songs that don't have "volume" set via musicdef -/// TODO: Remove this once all songs are ported -#define DEFAULT_MUSICDEF_VOLUME 25 +/// Divide volume of music and sounds by this much (loudest sounds on earth) +#define VOLUME_DIVIDER 4 #endif // __DOOMDEF__ diff --git a/src/i_sound.h b/src/i_sound.h index 93e3f6dd0..f418b598f 100644 --- a/src/i_sound.h +++ b/src/i_sound.h @@ -118,7 +118,7 @@ void I_UpdateSoundParams(INT32 handle, UINT8 vol, UINT8 sep, UINT8 pitch); \return void */ -void I_SetSfxVolume(UINT8 volume); +void I_SetSfxVolume(int volume); /// ------------------------ // MUSIC SYSTEM @@ -227,7 +227,7 @@ void I_ResumeSong(void); \return void */ -void I_SetMusicVolume(UINT8 volume); +void I_SetMusicVolume(int volume); boolean I_SetSongTrack(INT32 track); diff --git a/src/s_sound.h b/src/s_sound.h index 332f93324..1fccd97fc 100644 --- a/src/s_sound.h +++ b/src/s_sound.h @@ -23,6 +23,12 @@ // mask used to indicate sound origin is player item pickup #define PICKUP_SOUND 0x8000 +// +#define SOUND_VOLUME_RANGE 256 +#define MAX_SOUND_VOLUME 255 + +#define DEFAULT_MUSICDEF_VOLUME ( 100 / VOLUME_DIVIDER ) + extern consvar_t stereoreverse; extern consvar_t cv_soundvolume, cv_digmusicvolume;//, cv_midimusicvolume; extern consvar_t cv_numChannels; diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 0821a22c9..54e63f8fe 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -84,7 +84,9 @@ static UINT32 stutter_threshold_user; static UINT32 stutter_threshold; static Mix_Music *music; -static UINT8 music_volume, sfx_volume, internal_volume; +static int music_volume; +static int sfx_volume; +static int internal_volume; static float loop_point; static float song_length; // length in seconds static boolean songpaused; @@ -494,7 +496,9 @@ void I_FreeSfx(sfxinfo_t *sfx) INT32 I_StartSound(sfxenum_t id, UINT8 vol, UINT8 sep, UINT8 pitch, UINT8 priority, INT32 channel) { //UINT8 volume = (((UINT16)vol + 1) * (UINT16)sfx_volume) / 62; // (256 * 31) / 62 == 127 - int volume = ( vol + 1 ) / 8;/* reduce to 1/4, 256 / 8 = 128 / 4 = 32 */ + const int scale = SOUND_VOLUME_RANGE / MIX_MAX_VOLUME; + const int divider = VOLUME_DIVIDER * scale; + const int volume = ( vol + 1 ) / divider * sfx_volume / 100; INT32 handle = Mix_PlayChannel(channel, S_sfx[id].data, 0); Mix_Volume(handle, volume); Mix_SetPanning(handle, min((UINT16)(0xff-sep)<<1, 0xff), min((UINT16)(sep)<<1, 0xff)); @@ -521,7 +525,7 @@ void I_UpdateSoundParams(INT32 handle, UINT8 vol, UINT8 sep, UINT8 pitch) (void)pitch; } -void I_SetSfxVolume(UINT8 volume) +void I_SetSfxVolume(int volume) { sfx_volume = volume; } @@ -530,9 +534,8 @@ void I_SetSfxVolume(UINT8 volume) /// Music Utilities /// ------------------------ -static UINT32 get_real_volume(UINT8 volume) +static UINT32 get_real_volume(int volume) { - (void)volume; #ifdef _WIN32 if (I_SongType() == MU_MID) // HACK: Until we stop using native MIDI, @@ -540,10 +543,15 @@ static UINT32 get_real_volume(UINT8 volume) return ((UINT32)31*128/31); // volume = 31 else #endif + { // convert volume to mixer's 128 scale // then apply internal_volume as a percentage //return ((UINT32)volume*128/31) * (UINT32)internal_volume / 100; - return MIX_MAX_VOLUME * musicdef_volume / 100 * internal_volume / 100; + return MIX_MAX_VOLUME + * musicdef_volume / 100 + * internal_volume / 100 + * volume / 100; + } } static UINT32 get_adjusted_position(UINT32 position) @@ -1237,7 +1245,7 @@ void I_ResumeSong(void) songpaused = false; } -void I_SetMusicVolume(UINT8 volume) +void I_SetMusicVolume(int volume) { if (!I_SongPlaying()) return;