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
This commit is contained in:
Roman Fomin 2022-12-09 17:11:09 +07:00 committed by GitHub
parent 72ddbe8ffc
commit fa2398109e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View File

@ -22,6 +22,7 @@
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#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:

View File

@ -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",