[Scummvm-git-logs] scummvm master -> 67429ea7449ae51c55c222c13dd4f0b351d88f09

aquadran noreply at scummvm.org
Wed Sep 24 18:16:04 UTC 2025


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://api.github.com/repos/scummvm/scummvm .

Summary:
67429ea744 WINTERMUTE: Synced with original code, and reimplement sound volume code


Commit: 67429ea7449ae51c55c222c13dd4f0b351d88f09
    https://github.com/scummvm/scummvm/commit/67429ea7449ae51c55c222c13dd4f0b351d88f09
Author: Paweł Kołodziejski (aquadran at gmail.com)
Date: 2025-09-24T20:15:59+02:00

Commit Message:
WINTERMUTE: Synced with original code, and reimplement sound volume code

Changed paths:
    engines/wintermute/base/base_game.cpp
    engines/wintermute/base/base_object.cpp
    engines/wintermute/base/sound/base_sound.cpp
    engines/wintermute/base/sound/base_sound.h
    engines/wintermute/base/sound/base_sound_buffer.cpp
    engines/wintermute/base/sound/base_sound_buffer.h
    engines/wintermute/base/sound/base_sound_manager.cpp
    engines/wintermute/base/sound/base_sound_manager.h


diff --git a/engines/wintermute/base/base_game.cpp b/engines/wintermute/base/base_game.cpp
index e82eb34993b..4e90d2939ce 100644
--- a/engines/wintermute/base/base_game.cpp
+++ b/engines/wintermute/base/base_game.cpp
@@ -1479,7 +1479,7 @@ bool BaseGame::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack
 		if (channel < 0 || channel >= NUM_MUSIC_CHANNELS || !_music[channel]) {
 			stack->pushBool(false);
 		} else {
-			if (DID_FAIL(_music[channel]->setVolumePercent(volume))) {
+			if (DID_FAIL(_music[channel]->setVolume(volume))) {
 				stack->pushBool(false);
 			} else {
 				stack->pushBool(true);
@@ -1503,7 +1503,7 @@ bool BaseGame::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack
 		if (channel < 0 || channel >= NUM_MUSIC_CHANNELS || !_music[channel]) {
 			stack->pushInt(0);
 		} else {
-			stack->pushInt(_music[channel]->getVolumePercent());
+			stack->pushInt(_music[channel]->getVolume());
 		}
 
 		return STATUS_OK;
@@ -5242,17 +5242,17 @@ bool BaseGame::updateMusicCrossfade() {
 
 		if (_musicCrossfadeVolume2 == 0) {
 			_music[_musicCrossfadeChannel2]->stop();
-			_music[_musicCrossfadeChannel2]->setVolumePercent(100);
+			_music[_musicCrossfadeChannel2]->setVolume(100);
 		} else {
-			_music[_musicCrossfadeChannel2]->setVolumePercent(_musicCrossfadeVolume2);
+			_music[_musicCrossfadeChannel2]->setVolume(_musicCrossfadeVolume2);
 		}
 
 		if (_musicCrossfadeChannel1 != _musicCrossfadeChannel2) {
 			if (_musicCrossfadeVolume1 == 0) {
 				_music[_musicCrossfadeChannel1]->stop();
-				_music[_musicCrossfadeChannel1]->setVolumePercent(100);
+				_music[_musicCrossfadeChannel1]->setVolume(100);
 			} else {
-				_music[_musicCrossfadeChannel1]->setVolumePercent(_musicCrossfadeVolume1);
+				_music[_musicCrossfadeChannel1]->setVolume(_musicCrossfadeVolume1);
 			}
 		}
 
@@ -5270,11 +5270,11 @@ bool BaseGame::updateMusicCrossfade() {
 	} else {
 		float progress = (float)currentTime / (float)_musicCrossfadeLength;
 		int volumeDelta = (int)((_musicCrossfadeVolume1 - _musicCrossfadeVolume2)*progress);
-		_music[_musicCrossfadeChannel2]->setVolumePercent(_musicCrossfadeVolume1 - volumeDelta);
+		_music[_musicCrossfadeChannel2]->setVolume(_musicCrossfadeVolume1 - volumeDelta);
 		BaseEngine::LOG(0, "Setting music channel %d volume to %d", _musicCrossfadeChannel2, _musicCrossfadeVolume1 - volumeDelta);
 
 		if (_musicCrossfadeChannel1 != _musicCrossfadeChannel2) {
-			_music[_musicCrossfadeChannel1]->setVolumePercent(_musicCrossfadeVolume2 + volumeDelta);
+			_music[_musicCrossfadeChannel1]->setVolume(_musicCrossfadeVolume2 + volumeDelta);
 			BaseEngine::LOG(0, "Setting music channel %d volume to %d", _musicCrossfadeChannel1, _musicCrossfadeVolume2 + volumeDelta);
 		}
 	}
diff --git a/engines/wintermute/base/base_object.cpp b/engines/wintermute/base/base_object.cpp
index ccde9701762..45173d45407 100644
--- a/engines/wintermute/base/base_object.cpp
+++ b/engines/wintermute/base/base_object.cpp
@@ -507,7 +507,7 @@ bool BaseObject::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSta
 		if (!_sFX) {
 			stack->pushInt(_sFXVolume);
 		} else {
-			stack->pushInt(_sFX->getVolumePercent());
+			stack->pushInt(_sFX->getVolume());
 		}
 		return STATUS_OK;
 	}
@@ -1210,7 +1210,7 @@ bool BaseObject::playSFX(const char *filename, bool looping, bool playNow, const
 	// just play loaded sound
 	if (filename == nullptr && _sFX) {
 		if (_game->_editorMode || _sFXStart) {
-			_sFX->setVolumePercent(_sFXVolume);
+			_sFX->setVolume(_sFXVolume);
 			_sFX->setPositionTime(_sFXStart);
 			if (!_game->_editorMode) {
 				_sFXStart = 0;
@@ -1236,7 +1236,7 @@ bool BaseObject::playSFX(const char *filename, bool looping, bool playNow, const
 
 	_sFX = new BaseSound(_game);
 	if (_sFX && DID_SUCCEED(_sFX->setSound(filename, TSoundType::SOUND_SFX, true))) {
-		_sFX->setVolumePercent(_sFXVolume);
+		_sFX->setVolume(_sFXVolume);
 		if (_sFXStart) {
 			_sFX->setPositionTime(_sFXStart);
 			_sFXStart = 0;
@@ -1307,7 +1307,7 @@ bool BaseObject::setSFXTime(uint32 time) {
 bool BaseObject::setSFXVolume(int volume) {
 	_sFXVolume = volume;
 	if (_sFX) {
-		return _sFX->setVolumePercent(volume);
+		return _sFX->setVolume(volume);
 	} else {
 		return STATUS_OK;
 	}
diff --git a/engines/wintermute/base/sound/base_sound.cpp b/engines/wintermute/base/sound/base_sound.cpp
index 41891e04a36..749bde8a9f7 100644
--- a/engines/wintermute/base/sound/base_sound.cpp
+++ b/engines/wintermute/base/sound/base_sound.cpp
@@ -65,14 +65,14 @@ BaseSound::~BaseSound() {
 }
 
 //////////////////////////////////////////////////////////////////////////
-bool BaseSound::setSound(const char *filename, TSoundType type, bool streamed) {
+bool BaseSound::setSound(const char *filename, TSoundType type, bool streamed, uint32 initialPrivateVolume) {
 	if (_sound) {
 		_game->_soundMgr->removeSound(_sound);
 		_sound = nullptr;
 	}
 	SAFE_DELETE_ARRAY(_soundFilename);
 
-	_sound = _game->_soundMgr->addSound(filename, type, streamed);
+	_sound = _game->_soundMgr->addSound(filename, type, streamed, initialPrivateVolume);
 	if (_sound) {
 		size_t nameSize = strlen(filename) + 1;
 		_soundFilename = new char[nameSize];
@@ -244,7 +244,7 @@ bool BaseSound::setPrivateVolume(int volume) {
 	if (!_sound) {
 		return STATUS_FAILED;
 	} else {
-		return _sound->_privateVolume = volume;
+		_sound->_privateVolume = volume;
 		return STATUS_OK;
 	}
 }
@@ -297,23 +297,4 @@ bool BaseSound::applyFX(TSFXType type, float param1, float param2, float param3,
 	return STATUS_OK;
 }
 
-//////////////////////////////////////////////////////////////////////////
-int BaseSound::getVolumePercent() {
-	if (!_sound) {
-		return 0;
-	} else {
-		return _sound->getPrivateVolume() * 100 / 255;
-	}
-}
-
-//////////////////////////////////////////////////////////////////////////
-bool BaseSound::setVolumePercent(int percent) {
-	if (!_sound) {
-		return STATUS_FAILED;
-	} else {
-		return _sound->setPrivateVolume(percent * 255 / 100);
-	}
-}
-
-
 } // End of namespace Wintermute
diff --git a/engines/wintermute/base/sound/base_sound.h b/engines/wintermute/base/sound/base_sound.h
index 08af2c2ce02..ea42c6f770b 100644
--- a/engines/wintermute/base/sound/base_sound.h
+++ b/engines/wintermute/base/sound/base_sound.h
@@ -41,8 +41,6 @@ public:
 	bool setPan(float pan);
 	int32 _soundPrivateVolume;
 	int getVolume();
-	int getVolumePercent();
-	bool setVolumePercent(int percent);
 	bool setVolume(int volume);
 	bool setPrivateVolume(int volume);
 	bool setLoopStart(uint32 pos);
@@ -66,7 +64,7 @@ public:
 	TSoundType _soundType;
 	char *_soundFilename;
 	bool setSoundSimple();
-	bool setSound(const char *filename, TSoundType type = TSoundType::SOUND_SFX, bool streamed = false);
+	bool setSound(const char *filename, TSoundType type = TSoundType::SOUND_SFX, bool streamed = false, uint32 initialPrivateVolume = 100);
 	BaseSound(BaseGame *inGame);
 	~BaseSound() override;
 
diff --git a/engines/wintermute/base/sound/base_sound_buffer.cpp b/engines/wintermute/base/sound/base_sound_buffer.cpp
index ba1e46436a4..75f93e080ee 100644
--- a/engines/wintermute/base/sound/base_sound_buffer.cpp
+++ b/engines/wintermute/base/sound/base_sound_buffer.cpp
@@ -49,8 +49,6 @@ namespace Wintermute {
 // Construction/Destruction
 //////////////////////////////////////////////////////////////////////
 
-#define MAX_NONSTREAMED_FILE_SIZE (1024 * 1024)
-
 //////////////////////////////////////////////////////////////////////////
 BaseSoundBuffer::BaseSoundBuffer(BaseGame *inGame) : BaseClass(inGame) {
 	_stream = nullptr;
@@ -58,7 +56,7 @@ BaseSoundBuffer::BaseSoundBuffer(BaseGame *inGame) : BaseClass(inGame) {
 
 	_streamed = false;
 	_filename = nullptr;
-	_privateVolume = 255;
+	_privateVolume = 100;
 	_volume = 255;
 	_pan = 0;
 
@@ -236,10 +234,10 @@ void BaseSoundBuffer::setType(TSoundType type) {
 
 //////////////////////////////////////////////////////////////////////////
 bool BaseSoundBuffer::setVolume(int volume) {
-	_volume = volume * _game->_soundMgr->getMasterVolume() / 255;
+	volume = (float)volume / 100.0f * _privateVolume;
+	_volume = (volume * Audio::Mixer::kMaxChannelVolume) / 100;
 	if (_stream && _handle) {
-		byte vol = (byte)(_volume);
-		g_system->getMixer()->setChannelVolume(*_handle, vol);
+		g_system->getMixer()->setChannelVolume(*_handle, (byte)(_volume));
 	}
 	return STATUS_OK;
 }
@@ -248,7 +246,7 @@ bool BaseSoundBuffer::setVolume(int volume) {
 //////////////////////////////////////////////////////////////////////////
 bool BaseSoundBuffer::setPrivateVolume(int volume) {
 	_privateVolume = volume;
-	return setVolume(_privateVolume);
+	return setVolume(volume);
 }
 
 
@@ -321,12 +319,4 @@ bool BaseSoundBuffer::applyFX(TSFXType type, float param1, float param2, float p
 	return STATUS_OK;
 }
 
-int32 BaseSoundBuffer::getPrivateVolume() const {
-	return _privateVolume;
-}
-
-void BaseSoundBuffer::updateVolume() {
-	setVolume(_privateVolume);
-}
-
 } // End of namespace Wintermute
diff --git a/engines/wintermute/base/sound/base_sound_buffer.h b/engines/wintermute/base/sound/base_sound_buffer.h
index c0d9a75c0ca..937c8718ad5 100644
--- a/engines/wintermute/base/sound/base_sound_buffer.h
+++ b/engines/wintermute/base/sound/base_sound_buffer.h
@@ -66,14 +66,12 @@ public:
 	bool setPan(float pan);
 	bool setPrivateVolume(int colume);
 	bool setVolume(int colume);
-	void updateVolume();
 
 	void setType(TSoundType type);
 
 	bool loadFromFile(const char *filename, bool forceReload = false);
 	void setStreaming(bool streamed, uint32 numBlocks = 0, uint32 blockSize = 0);
 	bool applyFX(TSFXType type, float param1, float param2, float param3, float param4);
-	int32 getPrivateVolume() const;
 
 	bool _freezePaused;
 	uint32 _loopStart;
diff --git a/engines/wintermute/base/sound/base_sound_manager.cpp b/engines/wintermute/base/sound/base_sound_manager.cpp
index c396e8128a4..70715f037c8 100644
--- a/engines/wintermute/base/sound/base_sound_manager.cpp
+++ b/engines/wintermute/base/sound/base_sound_manager.cpp
@@ -49,8 +49,8 @@ namespace Wintermute {
 //////////////////////////////////////////////////////////////////////////
 BaseSoundMgr::BaseSoundMgr(BaseGame *inGame) : BaseClass(inGame) {
 	_soundAvailable = false;
-	_volumeMaster = 255;
-	_volumeMasterPercent = 100;
+
+	_volumeMaster = 100;
 }
 
 
@@ -73,7 +73,7 @@ bool BaseSoundMgr::cleanup() {
 //////////////////////////////////////////////////////////////////////////
 void BaseSoundMgr::saveSettings() {
 	if (_soundAvailable) {
-		ConfMan.setInt("master_volume_percent", _volumeMasterPercent);
+		ConfMan.setInt("master_volume_percent", _volumeMaster);
 	}
 }
 
@@ -84,11 +84,15 @@ bool BaseSoundMgr::initialize() {
 	if (!g_system->getMixer()->isReady()) {
 		return STATUS_FAILED;
 	}
-	byte volumeMasterPercent = (ConfMan.hasKey("master_volume_percent") ? ConfMan.getInt("master_volume_percent") : 100);
-	setMasterVolumePercent(volumeMasterPercent);
-	_soundAvailable = true;
 
 	g_engine->syncSoundSettings();
+
+	_volumeMaster = (ConfMan.hasKey("master_volume_percent") ? ConfMan.getInt("master_volume_percent") : 100);
+
+	_soundAvailable = true;
+
+	setMasterVolumePercent(_volumeMaster);
+
 	return STATUS_OK;
 }
 
@@ -102,7 +106,7 @@ bool BaseSoundMgr::initLoop() {
 }
 
 //////////////////////////////////////////////////////////////////////////
-BaseSoundBuffer *BaseSoundMgr::addSound(const char *filename, TSoundType type, bool streamed) {
+BaseSoundBuffer *BaseSoundMgr::addSound(const char *filename, TSoundType type, bool streamed, uint32 initialPrivateVolume) {
 	if (!_soundAvailable) {
 		return nullptr;
 	}
@@ -142,8 +146,8 @@ BaseSoundBuffer *BaseSoundMgr::addSound(const char *filename, TSoundType type, b
 		return nullptr;
 	}
 
-	// Make sure the master-volume is applied to the sound.
-	sound->updateVolume();
+	// sound starts with user defined instead of 100% volume (of the global setting)
+	sound->setPrivateVolume(initialPrivateVolume);
 
 	// register sound
 	_sounds.add(sound);
@@ -157,9 +161,6 @@ bool BaseSoundMgr::addSound(BaseSoundBuffer *sound, TSoundType type) {
 		return STATUS_FAILED;
 	}
 
-	// Make sure the master-volume is applied to the sound.
-	sound->updateVolume();
-
 	// register sound
 	_sounds.add(sound);
 
@@ -188,26 +189,27 @@ bool BaseSoundMgr::setVolume(TSoundType type, int volume) {
 
 	switch (type) {
 	case TSoundType::SOUND_SFX:
-		ConfMan.setInt("sfx_volume", volume);
+		g_system->getMixer()->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, (volume * Audio::Mixer::kMaxChannelVolume) / 100);
+		ConfMan.setInt("sfx_volume", g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kSFXSoundType));
 		break;
 	case TSoundType::SOUND_SPEECH:
-		ConfMan.setInt("speech_volume", volume);
+		g_system->getMixer()->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, (volume * Audio::Mixer::kMaxChannelVolume) / 100);
+		ConfMan.setInt("speech_volume", g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kSpeechSoundType));
 		break;
 	case TSoundType::SOUND_MUSIC:
-		ConfMan.setInt("music_volume", volume);
+		g_system->getMixer()->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, (volume * Audio::Mixer::kMaxChannelVolume) / 100);
+		ConfMan.setInt("music_volume", g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kMusicSoundType));
 		break;
 	default:
 		break;
 	}
 
-	g_engine->syncSoundSettings();
-
 	return STATUS_OK;
 }
 
 //////////////////////////////////////////////////////////////////////////
 bool BaseSoundMgr::setVolumePercent(TSoundType type, byte percent) {
-	return setVolume(type, percent * 255 / 100);
+	return setVolume(type, percent);
 }
 
 
@@ -217,33 +219,35 @@ byte BaseSoundMgr::getVolumePercent(TSoundType type) {
 
 	switch (type) {
 	case TSoundType::SOUND_SFX:
-		volume = g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kSFXSoundType);
+		volume = ConfMan.getInt("sfx_volume") * 100 / Audio::Mixer::kMaxChannelVolume;
 		break;
 	case TSoundType::SOUND_SPEECH:
-		volume = g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kSpeechSoundType);
+		volume = ConfMan.getInt("speech_volume") * 100 / Audio::Mixer::kMaxChannelVolume;
 		break;
 	case TSoundType::SOUND_MUSIC:
-		volume = g_system->getMixer()->getVolumeForSoundType(Audio::Mixer::kMusicSoundType);
+		volume = ConfMan.getInt("music_volume") * 100 / Audio::Mixer::kMaxChannelVolume;
 		break;
 	default:
 		break;
 	}
 
-	return (byte)(volume * 100 / 255);
+	return (byte)volume;
 }
 
 
 //////////////////////////////////////////////////////////////////////////
 bool BaseSoundMgr::setMasterVolumePercent(byte percent) {
-	_volumeMasterPercent = percent;
-	setMasterVolume((int)ceil(percent * 255.0 / 100.0));
+	_volumeMaster = percent;
+	for (int32 i = 0; i < _sounds.getSize(); i++) {
+		_sounds[i]->setVolume(percent);
+	}
 	return STATUS_OK;
 }
 
 
 //////////////////////////////////////////////////////////////////////////
 byte BaseSoundMgr::getMasterVolumePercent() {
-	return _volumeMasterPercent;
+	return _volumeMaster;
 }
 
 
@@ -303,23 +307,4 @@ float BaseSoundMgr::posToPan(int x, int y) {
 	return minPan + relPos * (maxPan - minPan);
 }
 
-//////////////////////////////////////////////////////////////////////////
-bool BaseSoundMgr::setMasterVolume(byte value) {
-	// This function intentionally doesn't touch _volumeMasterPercent,
-	// as that variable keeps track of what the game actually wanted,
-	// and this gives a close approximation, while letting the game
-	// be none the wiser about round-off-errors. This function should thus
-	// ONLY be called by setMasterVolumePercent.
-	_volumeMaster = value;
-	for (int32 i = 0; i < _sounds.getSize(); i++) {
-		_sounds[i]->updateVolume();
-	}
-	return STATUS_OK;
-}
-
-//////////////////////////////////////////////////////////////////////////
-byte BaseSoundMgr::getMasterVolume() {
-	return (byte)_volumeMaster;
-}
-
 } // End of namespace Wintermute
diff --git a/engines/wintermute/base/sound/base_sound_manager.h b/engines/wintermute/base/sound/base_sound_manager.h
index 3b07082449d..339d14ba035 100644
--- a/engines/wintermute/base/sound/base_sound_manager.h
+++ b/engines/wintermute/base/sound/base_sound_manager.h
@@ -50,7 +50,7 @@ public:
 	bool setVolume(TSoundType type, int volume);
 	int32 _volumeMaster;
 	bool removeSound(BaseSoundBuffer *sound);
-	BaseSoundBuffer *addSound(const char *filename, TSoundType type = TSoundType::SOUND_SFX, bool streamed = false);
+	BaseSoundBuffer *addSound(const char *filename, TSoundType type = TSoundType::SOUND_SFX, bool streamed = false, uint32 initialPrivateVolume = 100);
 	bool addSound(BaseSoundBuffer *sound, TSoundType type = TSoundType::SOUND_SFX);
 	bool initLoop();
 	bool initialize();
@@ -59,9 +59,6 @@ public:
 	~BaseSoundMgr() override;
 	BaseArray<BaseSoundBuffer *> _sounds;
 	void saveSettings();
-private:
-	int32 _volumeMasterPercent; // Necessary to avoid round-offs.
-	bool setMasterVolume(byte percent);
 };
 
 } // End of namespace Wintermute




More information about the Scummvm-git-logs mailing list