ALL: Use CLIP to clip volumes

This commit is contained in:
Adrian Frühwirth 2018-05-03 21:54:58 +02:00
parent c4edac23b2
commit 49116b4ae7
9 changed files with 13 additions and 46 deletions

View file

@ -471,10 +471,7 @@ void MixerImpl::setVolumeForSoundType(SoundType type, int volume) {
assert(0 <= (int)type && (int)type < ARRAYSIZE(_soundTypeSettings)); assert(0 <= (int)type && (int)type < ARRAYSIZE(_soundTypeSettings));
// Check range // Check range
if (volume > kMaxMixerVolume) volume = CLIP<int>(volume, 0, kMaxMixerVolume);
volume = kMaxMixerVolume;
else if (volume < 0)
volume = 0;
// TODO: Maybe we should do logarithmic (not linear) volume // TODO: Maybe we should do logarithmic (not linear) volume
// scaling? See also Player_V2::setMasterVolume // scaling? See also Player_V2::setMasterVolume

View file

@ -482,20 +482,14 @@ void MidiPlayer::pause(bool b) {
} }
void MidiPlayer::setVolume(int musicVol, int sfxVol) { void MidiPlayer::setVolume(int musicVol, int sfxVol) {
if (musicVol < 0) musicVol = CLIP(musicVol, 0, 255);
musicVol = 0; sfxVol = CLIP(sfxVol, 0, 255);
else if (musicVol > 255)
musicVol = 255;
if (sfxVol < 0)
sfxVol = 0;
else if (sfxVol > 255)
sfxVol = 255;
if (_musicVolume == musicVol && _sfxVolume == sfxVol) if (_musicVolume == musicVol && _sfxVolume == sfxVol)
return; return;
_musicVolume = musicVol; _musicVolume = musicVol;
_sfxVolume = sfxVol; _sfxVolume = sfxVol;
// Now tell all the channels this. // Now tell all the channels this.
Common::StackLock lock(_mutex); Common::StackLock lock(_mutex);

View file

@ -299,11 +299,7 @@ void AdLibSoundDriver::setUpdateCallback(UpdateCallback upCb, void *ref) {
void AdLibSoundDriver::setupChannel(int channel, const byte *data, int instrument, int volume) { void AdLibSoundDriver::setupChannel(int channel, const byte *data, int instrument, int volume) {
assert(channel < 4); assert(channel < 4);
if (data) { if (data) {
if (volume > 80) { volume = CLIP(volume, 0, 80);
volume = 80;
} else if (volume < 0) {
volume = 0;
}
volume += volume / 4; volume += volume / 4;
_channelsVolumeTable[channel] = volume; _channelsVolumeTable[channel] = volume;

View file

@ -334,11 +334,7 @@ void AdLibSoundDriver::syncSounds() {
void AdLibSoundDriver::adjustVolume(int channel, int volume) { void AdLibSoundDriver::adjustVolume(int channel, int volume) {
_channelsVolumeTable[channel].original = volume; _channelsVolumeTable[channel].original = volume;
if (volume > 80) { volume = CLIP(volume, 0, 80);
volume = 80;
} else if (volume < 0) {
volume = 0;
}
volume += volume / 4; volume += volume / 4;
// The higher possible value for volume is 100 // The higher possible value for volume is 100

View file

@ -30,6 +30,8 @@
#include "audio/decoders/vorbis.h" #include "audio/decoders/vorbis.h"
#include "audio/decoders/flac.h" #include "audio/decoders/flac.h"
#include "common/util.h"
namespace Kyra { namespace Kyra {
class KyraAudioStream : public Audio::SeekableAudioStream { class KyraAudioStream : public Audio::SeekableAudioStream {
@ -203,11 +205,7 @@ int AUDStream::readBuffer(int16 *buffer, const int numSamples) {
} }
inline int16 clip8BitSample(int16 sample) { inline int16 clip8BitSample(int16 sample) {
if (sample > 255) return CLIP<int16>(sample, 0, 255);
return 255;
if (sample < 0)
return 0;
return sample;
} }
int AUDStream::readChunk(int16 *buffer, const int maxSamples) { int AUDStream::readChunk(int16 *buffer, const int maxSamples) {

View file

@ -649,10 +649,7 @@ MidiMusic::~MidiMusic() {
} }
void MidiMusic::setVolume(int volume) { void MidiMusic::setVolume(int volume) {
if (volume < 0) volume = CLIP(volume, 0, 255);
volume = 0;
else if (volume > 255)
volume = 255;
if (_volume == volume) if (_volume == volume)
return; return;

View file

@ -102,10 +102,7 @@ MidiMusic::~MidiMusic() {
} }
void MidiMusic::setVolume(int volume) { void MidiMusic::setVolume(int volume) {
if (volume < 0) volume = CLIP(volume, 0, 255);
volume = 0;
else if (volume > 255)
volume = 255;
if (_masterVolume == volume) if (_masterVolume == volume)
return; return;

View file

@ -1029,11 +1029,7 @@ top:
} }
_mchan[x].volume += _mchan[x].voldelta; _mchan[x].volume += _mchan[x].voldelta;
_mchan[x].volume = CLIP(_mchan[x].volume, 0, MAXVOLUME);
if (_mchan[x].volume < 0)
_mchan[x].volume = 0;
if (_mchan[x].volume > MAXVOLUME)
_mchan[x].volume = MAXVOLUME;
APU_writeChannel(x, 0, (_mchan[x].volume >> 3) | _mchan[x].envflags); APU_writeChannel(x, 0, (_mchan[x].volume >> 3) | _mchan[x].envflags);
} }

View file

@ -412,11 +412,7 @@ void FPSfx::setPause(bool pause) {
* *
*/ */
void FPSfx::setVolume(int volume) { void FPSfx::setVolume(int volume) {
if (volume > 63) volume = CLIP(volume, 0, 63);
volume = 63;
if (volume < 0)
volume = 0;
_lastVolume = volume; _lastVolume = volume;