1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Fixed volume of ambient sounds

This commit is contained in:
Ivan Savenko
2023-03-28 18:56:44 +03:00
parent 238de8a5d7
commit 9062d67f80
2 changed files with 44 additions and 14 deletions

View File

@@ -142,11 +142,13 @@ Mix_Chunk *CSoundHandler::GetSoundChunk(std::string &sound, bool cache)
int CSoundHandler::ambientDistToVolume(int distance) const int CSoundHandler::ambientDistToVolume(int distance) const
{ {
if(distance >= ambientConfig["distances"].Vector().size()) const auto & distancesVector = ambientConfig["distances"].Vector();
if(distance >= distancesVector.size())
return 0; return 0;
int volume = static_cast<int>(ambientConfig["distances"].Vector()[distance].Integer()); int volume = static_cast<int>(distancesVector[distance].Integer());
return volume * (int)ambientConfig["volume"].Integer() * getVolume() / 10000; return volume * (int)ambientConfig["volume"].Integer() / 100;
} }
void CSoundHandler::ambientStopSound(std::string soundId) void CSoundHandler::ambientStopSound(std::string soundId)
@@ -211,7 +213,20 @@ void CSoundHandler::setVolume(ui32 percent)
CAudioBase::setVolume(percent); CAudioBase::setVolume(percent);
if (initialized) if (initialized)
{
setChannelVolume(-1, volume); setChannelVolume(-1, volume);
for (auto const & channel : channelVolumes)
updateChannelVolume(channel.first);
}
}
void CSoundHandler::updateChannelVolume(int channel)
{
if (channelVolumes.count(channel))
setChannelVolume(channel, getVolume() * channelVolumes[channel] / 100);
else
setChannelVolume(channel, getVolume());
} }
// Sets the sound volume, from 0 (mute) to 100 // Sets the sound volume, from 0 (mute) to 100
@@ -258,29 +273,40 @@ void CSoundHandler::ambientUpdateChannels(std::map<std::string, int> soundsArg)
std::vector<std::string> stoppedSounds; std::vector<std::string> stoppedSounds;
for(auto & pair : ambientChannels) for(auto & pair : ambientChannels)
{ {
if(!vstd::contains(soundsArg, pair.first)) const std::string & soundId = pair.first;
const int channel = pair.second;
if(!vstd::contains(soundsArg, soundId))
{ {
ambientStopSound(pair.first); ambientStopSound(soundId);
stoppedSounds.push_back(pair.first); stoppedSounds.push_back(soundId);
} }
else else
{ {
int volume = ambientDistToVolume(soundsArg[pair.first]); int volume = ambientDistToVolume(soundsArg[soundId]);
CCS->soundh->setChannelVolume(pair.second, volume); channelVolumes[channel] = volume;
updateChannelVolume(channel);
} }
} }
for(auto soundId : stoppedSounds) for(auto soundId : stoppedSounds)
{
channelVolumes.erase(ambientChannels[soundId]);
ambientChannels.erase(soundId); ambientChannels.erase(soundId);
}
for(auto & pair : soundsArg) for(auto & pair : soundsArg)
{ {
if(!vstd::contains(ambientChannels, pair.first)) const std::string & soundId = pair.first;
{ const int distance = pair.second;
int channel = CCS->soundh->playSound(pair.first, -1);
int volume = ambientDistToVolume(pair.second);
CCS->soundh->setChannelVolume(channel, volume); if(!vstd::contains(ambientChannels, soundId))
CCS->soundh->ambientChannels.insert(std::make_pair(pair.first, channel)); {
int channel = playSound(soundId, -1);
int volume = ambientDistToVolume(distance);
channelVolumes[channel] = volume;
updateChannelVolume(channel);
ambientChannels[soundId] = channel;
} }
} }
} }
@@ -293,6 +319,7 @@ void CSoundHandler::ambientStopAllChannels()
{ {
ambientStopSound(ch.first); ambientStopSound(ch.first);
} }
channelVolumes.clear();
ambientChannels.clear(); ambientChannels.clear();
} }

View File

@@ -51,9 +51,12 @@ private:
int ambientDistToVolume(int distance) const; int ambientDistToVolume(int distance) const;
void ambientStopSound(std::string soundId); void ambientStopSound(std::string soundId);
void updateChannelVolume(int channel);
const JsonNode ambientConfig; const JsonNode ambientConfig;
std::map<std::string, int> ambientChannels; std::map<std::string, int> ambientChannels;
std::map<int, int> channelVolumes;
public: public:
CSoundHandler(); CSoundHandler();