From fa2398109edad38f50f0af3f1c4cc223099a0eb0 Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Fri, 9 Dec 2022 17:11:09 +0700 Subject: [PATCH] OPL output gain (#829) * crude OPL volume gain * replace SDL_MixAudioFormat with MixAudioFormat with added gain * remove unnecessary code path, set default gain = 2 * fix gain calculation * apply gain only to `src1` * cosmetic changes --- opl/opl_sdl.c | 36 ++++++++++++++++++++++++++++++++++-- src/m_misc.c | 8 ++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/opl/opl_sdl.c b/opl/opl_sdl.c index ad90b80e..2e017861 100644 --- a/opl/opl_sdl.c +++ b/opl/opl_sdl.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "SDL.h" #include "SDL_mixer.h" @@ -154,6 +155,38 @@ static void AdvanceTime(unsigned int nsamples) SDL_UnlockMutex(callback_queue_mutex); } +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + #define OPL_SHORT SDL_SwapLE16 +#else + #define OPL_SHORT SDL_SwapBE16 +#endif + +int opl_gain = 200; + +static void MixAudioFormat(Uint8 *dst, const Uint8 *src, Uint32 len) +{ + Sint16 src1, src2; + int dst_sample; + + while (len--) + { + src1 = OPL_SHORT(*(Sint16 *)src); + src2 = OPL_SHORT(*(Sint16 *)dst); + src += 2; + dst_sample = src1 * opl_gain / 100 + src2; + if (dst_sample > SHRT_MAX) + { + dst_sample = SHRT_MAX; + } + else if (dst_sample < SHRT_MIN) + { + dst_sample = SHRT_MIN; + } + *(Sint16 *)dst = OPL_SHORT(dst_sample); + dst += 2; + } +} + // Call the OPL emulator code to fill the specified buffer. static void FillBuffer(uint8_t *buffer, unsigned int nsamples) @@ -166,8 +199,7 @@ static void FillBuffer(uint8_t *buffer, unsigned int nsamples) // OPL output is generated into temporary buffer and then mixed // (to avoid overflows etc.) OPL3_GenerateStream(&opl_chip, (Bit16s *) mix_buffer, nsamples); - SDL_MixAudioFormat(buffer, mix_buffer, AUDIO_S16SYS, nsamples * 4, - SDL_MIX_MAXVOLUME); + MixAudioFormat(buffer, mix_buffer, nsamples * 2); } // Callback function to fill a new sound buffer: diff --git a/src/m_misc.c b/src/m_misc.c index f69c8588..9d90370c 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -100,6 +100,7 @@ extern int winmm_reset_delay; extern int winmm_reverb_level; extern int winmm_chorus_level; #endif +extern int opl_gain; extern boolean demobar; extern boolean smoothlight; extern boolean brightmaps; @@ -2321,6 +2322,13 @@ default_t defaults[] = { }, #endif + { + "opl_gain", + (config_t *) &opl_gain, NULL, + {200}, {100, 1000}, number, ss_none, wad_no, + "fine tune OPL emulation output level (default 200%)" + }, + #if defined(_WIN32) { "winmm_reset_type",