From 49116b4ae7dd31fd736a33913a2969c0653a06cb Mon Sep 17 00:00:00 2001 From: Adrian Frühwirth Date: Thu, 3 May 2018 21:54:58 +0200 Subject: ALL: Use CLIP to clip volumes --- audio/mixer.cpp | 5 +---- engines/agos/midi.cpp | 12 +++--------- engines/cine/sound.cpp | 6 +----- engines/cruise/sound.cpp | 6 +----- engines/kyra/sound_digital.cpp | 8 +++----- engines/lure/sound.cpp | 5 +---- engines/queen/music.cpp | 5 +---- engines/scumm/players/player_nes.cpp | 6 +----- engines/tony/sound.cpp | 6 +----- 9 files changed, 13 insertions(+), 46 deletions(-) diff --git a/audio/mixer.cpp b/audio/mixer.cpp index 16cf5640eb..274f8e9de6 100644 --- a/audio/mixer.cpp +++ b/audio/mixer.cpp @@ -471,10 +471,7 @@ void MixerImpl::setVolumeForSoundType(SoundType type, int volume) { assert(0 <= (int)type && (int)type < ARRAYSIZE(_soundTypeSettings)); // Check range - if (volume > kMaxMixerVolume) - volume = kMaxMixerVolume; - else if (volume < 0) - volume = 0; + volume = CLIP(volume, 0, kMaxMixerVolume); // TODO: Maybe we should do logarithmic (not linear) volume // scaling? See also Player_V2::setMasterVolume diff --git a/engines/agos/midi.cpp b/engines/agos/midi.cpp index 3a7158a203..5e28654c41 100644 --- a/engines/agos/midi.cpp +++ b/engines/agos/midi.cpp @@ -482,20 +482,14 @@ void MidiPlayer::pause(bool b) { } void MidiPlayer::setVolume(int musicVol, int sfxVol) { - if (musicVol < 0) - musicVol = 0; - else if (musicVol > 255) - musicVol = 255; - if (sfxVol < 0) - sfxVol = 0; - else if (sfxVol > 255) - sfxVol = 255; + musicVol = CLIP(musicVol, 0, 255); + sfxVol = CLIP(sfxVol, 0, 255); if (_musicVolume == musicVol && _sfxVolume == sfxVol) return; _musicVolume = musicVol; - _sfxVolume = sfxVol; + _sfxVolume = sfxVol; // Now tell all the channels this. Common::StackLock lock(_mutex); diff --git a/engines/cine/sound.cpp b/engines/cine/sound.cpp index a8b4c085ff..70ce8fec98 100644 --- a/engines/cine/sound.cpp +++ b/engines/cine/sound.cpp @@ -299,11 +299,7 @@ void AdLibSoundDriver::setUpdateCallback(UpdateCallback upCb, void *ref) { void AdLibSoundDriver::setupChannel(int channel, const byte *data, int instrument, int volume) { assert(channel < 4); if (data) { - if (volume > 80) { - volume = 80; - } else if (volume < 0) { - volume = 0; - } + volume = CLIP(volume, 0, 80); volume += volume / 4; _channelsVolumeTable[channel] = volume; diff --git a/engines/cruise/sound.cpp b/engines/cruise/sound.cpp index 57dcfcea8f..8d6ca571d1 100644 --- a/engines/cruise/sound.cpp +++ b/engines/cruise/sound.cpp @@ -334,11 +334,7 @@ void AdLibSoundDriver::syncSounds() { void AdLibSoundDriver::adjustVolume(int channel, int volume) { _channelsVolumeTable[channel].original = volume; - if (volume > 80) { - volume = 80; - } else if (volume < 0) { - volume = 0; - } + volume = CLIP(volume, 0, 80); volume += volume / 4; // The higher possible value for volume is 100 diff --git a/engines/kyra/sound_digital.cpp b/engines/kyra/sound_digital.cpp index a1600f6464..551d79cc55 100644 --- a/engines/kyra/sound_digital.cpp +++ b/engines/kyra/sound_digital.cpp @@ -30,6 +30,8 @@ #include "audio/decoders/vorbis.h" #include "audio/decoders/flac.h" +#include "common/util.h" + namespace Kyra { class KyraAudioStream : public Audio::SeekableAudioStream { @@ -203,11 +205,7 @@ int AUDStream::readBuffer(int16 *buffer, const int numSamples) { } inline int16 clip8BitSample(int16 sample) { - if (sample > 255) - return 255; - if (sample < 0) - return 0; - return sample; + return CLIP(sample, 0, 255); } int AUDStream::readChunk(int16 *buffer, const int maxSamples) { diff --git a/engines/lure/sound.cpp b/engines/lure/sound.cpp index 25d68c762d..5acd6d81a0 100644 --- a/engines/lure/sound.cpp +++ b/engines/lure/sound.cpp @@ -649,10 +649,7 @@ MidiMusic::~MidiMusic() { } void MidiMusic::setVolume(int volume) { - if (volume < 0) - volume = 0; - else if (volume > 255) - volume = 255; + volume = CLIP(volume, 0, 255); if (_volume == volume) return; diff --git a/engines/queen/music.cpp b/engines/queen/music.cpp index 9f74aab915..c65f2de5ef 100644 --- a/engines/queen/music.cpp +++ b/engines/queen/music.cpp @@ -102,10 +102,7 @@ MidiMusic::~MidiMusic() { } void MidiMusic::setVolume(int volume) { - if (volume < 0) - volume = 0; - else if (volume > 255) - volume = 255; + volume = CLIP(volume, 0, 255); if (_masterVolume == volume) return; diff --git a/engines/scumm/players/player_nes.cpp b/engines/scumm/players/player_nes.cpp index 3b5adc4550..b2f6eb0d40 100644 --- a/engines/scumm/players/player_nes.cpp +++ b/engines/scumm/players/player_nes.cpp @@ -1029,11 +1029,7 @@ top: } _mchan[x].volume += _mchan[x].voldelta; - - if (_mchan[x].volume < 0) - _mchan[x].volume = 0; - if (_mchan[x].volume > MAXVOLUME) - _mchan[x].volume = MAXVOLUME; + _mchan[x].volume = CLIP(_mchan[x].volume, 0, MAXVOLUME); APU_writeChannel(x, 0, (_mchan[x].volume >> 3) | _mchan[x].envflags); } diff --git a/engines/tony/sound.cpp b/engines/tony/sound.cpp index fed51dacf4..07d15e71c9 100644 --- a/engines/tony/sound.cpp +++ b/engines/tony/sound.cpp @@ -412,11 +412,7 @@ void FPSfx::setPause(bool pause) { * */ void FPSfx::setVolume(int volume) { - if (volume > 63) - volume = 63; - - if (volume < 0) - volume = 0; + volume = CLIP(volume, 0, 63); _lastVolume = volume; -- cgit v1.2.3