diff options
author | Wohlstand <admin@wohlnet.ru> | 2017-02-17 10:57:23 +0300 |
---|---|---|
committer | Wohlstand <admin@wohlnet.ru> | 2017-02-17 10:57:23 +0300 |
commit | 3e66410de0bf006c4b90eeb259d59da7ff8e8355 (patch) | |
tree | 9159d3ec398485aef351a0b101e4811c341f05c0 /src | |
parent | 57911fca4a7179677174cea7449af9687232e473 (diff) | |
download | libADLMIDI-3e66410de0bf006c4b90eeb259d59da7ff8e8355.tar.gz libADLMIDI-3e66410de0bf006c4b90eeb259d59da7ff8e8355.tar.bz2 libADLMIDI-3e66410de0bf006c4b90eeb259d59da7ff8e8355.zip |
Better audio clipping while mixing
Diffstat (limited to 'src')
-rw-r--r-- | src/adlmidi.cpp | 5 | ||||
-rw-r--r-- | src/nukedopl3.c | 12 |
2 files changed, 14 insertions, 3 deletions
diff --git a/src/adlmidi.cpp b/src/adlmidi.cpp index d54ce9a..07efea3 100644 --- a/src/adlmidi.cpp +++ b/src/adlmidi.cpp @@ -274,6 +274,9 @@ ADLMIDI_EXPORT void adl_reset(ADL_MIDIPlayer *device) } #ifdef ADLMIDI_USE_DOSBOX_OPL + +#define ADLMIDI_CLAMP(V, MIN, MAX) std::max(std::min(V, (MAX)), (MIN)) + inline static void SendStereoAudio(ADL_MIDIPlayer *device, int &samples_requested, ssize_t &in_size, @@ -296,7 +299,7 @@ inline static void SendStereoAudio(ADL_MIDIPlayer *device, offset = pos + p * 2 + w; if(offset < samples_requested) - _out[offset] = static_cast<short>(out); + _out[offset] = static_cast<short>(ADLMIDI_CLAMP(out, INT16_MIN, INT16_MAX)); else { device->backup_samples[device->backup_samples_size] = static_cast<short>(out); diff --git a/src/nukedopl3.c b/src/nukedopl3.c index 3557df9..21877ab 100644 --- a/src/nukedopl3.c +++ b/src/nukedopl3.c @@ -1455,16 +1455,24 @@ void OPL3_GenerateStream(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples) } } +#define OPL3_MIN(A, B) (((A) > (B)) ? (B) : (A)) +#define OPL3_MAX(A, B) (((A) < (B)) ? (B) : (A)) +#define OPL3_CLAMP(V, MIN, MAX) OPL3_MAX(OPL3_MIN(V, MAX), MIN) + void OPL3_GenerateStreamMix(opl3_chip *chip, Bit16s *sndptr, Bit32u numsamples) { Bit32u i; Bit16s sample[2]; + Bit32s mix[2]; for(i = 0; i < numsamples; i++) { OPL3_GenerateResampledREAL(chip, sample); - sndptr[0] += sample[0]; - sndptr[1] += sample[1]; + mix[0] = sndptr[0] + sample[0]; + mix[1] = sndptr[1] + sample[1]; + sndptr[0] = OPL3_CLAMP(mix[0], INT16_MIN, INT16_MAX); + sndptr[1] = OPL3_CLAMP(mix[1], INT16_MIN, INT16_MAX); sndptr += 2; } } + |