|
@@ -22,7 +22,7 @@
|
|
|
#define VCMI_SOUND_FILE(y) #y,
|
|
|
|
|
|
// sounds mapped to soundBase enum
|
|
|
-static const std::string sounds[] = {
|
|
|
+static const std::string soundsList[] = {
|
|
|
"", // invalid
|
|
|
"", // todo
|
|
|
VCMI_SOUND_LIST
|
|
@@ -30,10 +30,9 @@ static const std::string sounds[] = {
|
|
|
#undef VCMI_SOUND_NAME
|
|
|
#undef VCMI_SOUND_FILE
|
|
|
|
|
|
-
|
|
|
-void CSoundHandler::onVolumeChange(const JsonNode &volumeNode)
|
|
|
+void CSoundHandler::onVolumeChange(const JsonNode & volumeNode)
|
|
|
{
|
|
|
- setVolume((ui32)volumeNode.Float());
|
|
|
+ setVolume(volumeNode.Integer());
|
|
|
}
|
|
|
|
|
|
CSoundHandler::CSoundHandler():
|
|
@@ -43,9 +42,9 @@ CSoundHandler::CSoundHandler():
|
|
|
listener(std::bind(&CSoundHandler::onVolumeChange, this, _1));
|
|
|
|
|
|
if(ambientConfig["allocateChannels"].isNumber())
|
|
|
- Mix_AllocateChannels((int)ambientConfig["allocateChannels"].Integer());
|
|
|
+ Mix_AllocateChannels(ambientConfig["allocateChannels"].Integer());
|
|
|
|
|
|
- if (isInitialized())
|
|
|
+ if(isInitialized())
|
|
|
{
|
|
|
Mix_ChannelFinished([](int channel)
|
|
|
{
|
|
@@ -56,60 +55,60 @@ CSoundHandler::CSoundHandler():
|
|
|
|
|
|
CSoundHandler::~CSoundHandler()
|
|
|
{
|
|
|
- if (isInitialized())
|
|
|
+ if(isInitialized())
|
|
|
{
|
|
|
Mix_HaltChannel(-1);
|
|
|
|
|
|
- for (auto &chunk : soundChunks)
|
|
|
+ for(auto & chunk : soundChunks)
|
|
|
{
|
|
|
- if (chunk.second.first)
|
|
|
+ if(chunk.second.first)
|
|
|
Mix_FreeChunk(chunk.second.first);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Allocate an SDL chunk and cache it.
|
|
|
-Mix_Chunk *CSoundHandler::GetSoundChunk(const AudioPath & sound, bool cache)
|
|
|
+Mix_Chunk * CSoundHandler::GetSoundChunk(const AudioPath & sound, bool cache)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- if (cache && soundChunks.find(sound) != soundChunks.end())
|
|
|
+ if(cache && soundChunks.find(sound) != soundChunks.end())
|
|
|
return soundChunks[sound].first;
|
|
|
|
|
|
auto data = CResourceHandler::get()->load(sound.addPrefix("SOUNDS/"))->readAll();
|
|
|
- SDL_RWops *ops = SDL_RWFromMem(data.first.get(), (int)data.second);
|
|
|
- Mix_Chunk *chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
|
|
+ SDL_RWops * ops = SDL_RWFromMem(data.first.get(), data.second);
|
|
|
+ Mix_Chunk * chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
|
|
|
|
|
- if (cache)
|
|
|
- soundChunks.insert({sound, std::make_pair (chunk, std::move (data.first))});
|
|
|
+ if(cache)
|
|
|
+ soundChunks.insert({sound, std::make_pair(chunk, std::move(data.first))});
|
|
|
|
|
|
return chunk;
|
|
|
}
|
|
|
- catch(std::exception &e)
|
|
|
+ catch(std::exception & e)
|
|
|
{
|
|
|
logGlobal->warn("Cannot get sound %s chunk: %s", sound.getOriginalName(), e.what());
|
|
|
return nullptr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-Mix_Chunk *CSoundHandler::GetSoundChunk(std::pair<std::unique_ptr<ui8 []>, si64> & data, bool cache)
|
|
|
+Mix_Chunk * CSoundHandler::GetSoundChunk(std::pair<std::unique_ptr<ui8[]>, si64> & data, bool cache)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- std::vector<ui8> startBytes = std::vector<ui8>(data.first.get(), data.first.get() + std::min((si64)100, data.second));
|
|
|
+ std::vector<ui8> startBytes = std::vector<ui8>(data.first.get(), data.first.get() + std::min(static_cast<si64>(100), data.second));
|
|
|
|
|
|
- if (cache && soundChunksRaw.find(startBytes) != soundChunksRaw.end())
|
|
|
+ if(cache && soundChunksRaw.find(startBytes) != soundChunksRaw.end())
|
|
|
return soundChunksRaw[startBytes].first;
|
|
|
|
|
|
- SDL_RWops *ops = SDL_RWFromMem(data.first.get(), (int)data.second);
|
|
|
- Mix_Chunk *chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
|
|
+ SDL_RWops * ops = SDL_RWFromMem(data.first.get(), data.second);
|
|
|
+ Mix_Chunk * chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
|
|
|
|
|
- if (cache)
|
|
|
- soundChunksRaw.insert({startBytes, std::make_pair (chunk, std::move (data.first))});
|
|
|
+ if(cache)
|
|
|
+ soundChunksRaw.insert({startBytes, std::make_pair(chunk, std::move(data.first))});
|
|
|
|
|
|
return chunk;
|
|
|
}
|
|
|
- catch(std::exception &e)
|
|
|
+ catch(std::exception & e)
|
|
|
{
|
|
|
logGlobal->warn("Cannot get sound chunk: %s", e.what());
|
|
|
return nullptr;
|
|
@@ -123,8 +122,8 @@ int CSoundHandler::ambientDistToVolume(int distance) const
|
|
|
if(distance >= distancesVector.size())
|
|
|
return 0;
|
|
|
|
|
|
- int volume = static_cast<int>(distancesVector[distance].Integer());
|
|
|
- return volume * (int)ambientConfig["volume"].Integer() / 100;
|
|
|
+ int volumeByDistance = static_cast<int>(distancesVector[distance].Integer());
|
|
|
+ return volumeByDistance * ambientConfig["volume"].Integer() / 100;
|
|
|
}
|
|
|
|
|
|
void CSoundHandler::ambientStopSound(const AudioPath & soundId)
|
|
@@ -135,22 +134,22 @@ void CSoundHandler::ambientStopSound(const AudioPath & soundId)
|
|
|
|
|
|
uint32_t CSoundHandler::getSoundDurationMilliseconds(const AudioPath & sound)
|
|
|
{
|
|
|
- if (!isInitialized() || sound.empty())
|
|
|
+ if(!isInitialized() || sound.empty())
|
|
|
return 0;
|
|
|
|
|
|
auto resourcePath = sound.addPrefix("SOUNDS/");
|
|
|
|
|
|
- if (!CResourceHandler::get()->existsResource(resourcePath))
|
|
|
+ if(!CResourceHandler::get()->existsResource(resourcePath))
|
|
|
return 0;
|
|
|
|
|
|
auto data = CResourceHandler::get()->load(resourcePath)->readAll();
|
|
|
|
|
|
SDL_AudioSpec spec;
|
|
|
uint32_t audioLen;
|
|
|
- uint8_t *audioBuf;
|
|
|
+ uint8_t * audioBuf;
|
|
|
uint32_t miliseconds = 0;
|
|
|
|
|
|
- if(SDL_LoadWAV_RW(SDL_RWFromMem(data.first.get(), (int)data.second), 1, &spec, &audioBuf, &audioLen) != nullptr)
|
|
|
+ if(SDL_LoadWAV_RW(SDL_RWFromMem(data.first.get(), data.second), 1, &spec, &audioBuf, &audioLen) != nullptr)
|
|
|
{
|
|
|
SDL_FreeWAV(audioBuf);
|
|
|
uint32_t sampleSize = SDL_AUDIO_BITSIZE(spec.format) / 8;
|
|
@@ -159,14 +158,14 @@ uint32_t CSoundHandler::getSoundDurationMilliseconds(const AudioPath & sound)
|
|
|
miliseconds = 1000 * sampleLen / spec.freq;
|
|
|
}
|
|
|
|
|
|
- return miliseconds ;
|
|
|
+ return miliseconds;
|
|
|
}
|
|
|
|
|
|
// Plays a sound, and return its channel so we can fade it out later
|
|
|
int CSoundHandler::playSound(soundBase::soundID soundID, int repeats)
|
|
|
{
|
|
|
assert(soundID < soundBase::sound_after_last);
|
|
|
- auto sound = AudioPath::builtin(sounds[soundID]);
|
|
|
+ auto sound = AudioPath::builtin(soundsList[soundID]);
|
|
|
logGlobal->trace("Attempt to play sound %d with file name %s with cache", soundID, sound.getOriginalName());
|
|
|
|
|
|
return playSound(sound, repeats, true);
|
|
@@ -174,22 +173,22 @@ int CSoundHandler::playSound(soundBase::soundID soundID, int repeats)
|
|
|
|
|
|
int CSoundHandler::playSound(const AudioPath & sound, int repeats, bool cache)
|
|
|
{
|
|
|
- if (!isInitialized() || sound.empty())
|
|
|
+ if(!isInitialized() || sound.empty())
|
|
|
return -1;
|
|
|
|
|
|
int channel;
|
|
|
- Mix_Chunk *chunk = GetSoundChunk(sound, cache);
|
|
|
+ Mix_Chunk * chunk = GetSoundChunk(sound, cache);
|
|
|
|
|
|
- if (chunk)
|
|
|
+ if(chunk)
|
|
|
{
|
|
|
channel = Mix_PlayChannel(-1, chunk, repeats);
|
|
|
- if (channel == -1)
|
|
|
+ if(channel == -1)
|
|
|
{
|
|
|
logGlobal->error("Unable to play sound file %s , error %s", sound.getOriginalName(), Mix_GetError());
|
|
|
- if (!cache)
|
|
|
+ if(!cache)
|
|
|
Mix_FreeChunk(chunk);
|
|
|
}
|
|
|
- else if (cache)
|
|
|
+ else if(cache)
|
|
|
initCallback(channel);
|
|
|
else
|
|
|
initCallback(channel, [chunk](){ Mix_FreeChunk(chunk);});
|
|
@@ -200,19 +199,19 @@ int CSoundHandler::playSound(const AudioPath & sound, int repeats, bool cache)
|
|
|
return channel;
|
|
|
}
|
|
|
|
|
|
-int CSoundHandler::playSound(std::pair<std::unique_ptr<ui8 []>, si64> & data, int repeats, bool cache)
|
|
|
+int CSoundHandler::playSound(std::pair<std::unique_ptr<ui8[]>, si64> & data, int repeats, bool cache)
|
|
|
{
|
|
|
int channel = -1;
|
|
|
- if (Mix_Chunk *chunk = GetSoundChunk(data, cache))
|
|
|
+ if(Mix_Chunk * chunk = GetSoundChunk(data, cache))
|
|
|
{
|
|
|
channel = Mix_PlayChannel(-1, chunk, repeats);
|
|
|
- if (channel == -1)
|
|
|
+ if(channel == -1)
|
|
|
{
|
|
|
logGlobal->error("Unable to play sound, error %s", Mix_GetError());
|
|
|
- if (!cache)
|
|
|
+ if(!cache)
|
|
|
Mix_FreeChunk(chunk);
|
|
|
}
|
|
|
- else if (cache)
|
|
|
+ else if(cache)
|
|
|
initCallback(channel);
|
|
|
else
|
|
|
initCallback(channel, [chunk](){ Mix_FreeChunk(chunk);});
|
|
@@ -221,14 +220,14 @@ int CSoundHandler::playSound(std::pair<std::unique_ptr<ui8 []>, si64> & data, in
|
|
|
}
|
|
|
|
|
|
// Helper. Randomly select a sound from an array and play it
|
|
|
-int CSoundHandler::playSoundFromSet(std::vector<soundBase::soundID> &sound_vec)
|
|
|
+int CSoundHandler::playSoundFromSet(std::vector<soundBase::soundID> & sound_vec)
|
|
|
{
|
|
|
return playSound(*RandomGeneratorUtil::nextItem(sound_vec, CRandomGenerator::getDefault()));
|
|
|
}
|
|
|
|
|
|
void CSoundHandler::stopSound(int handler)
|
|
|
{
|
|
|
- if (isInitialized() && handler != -1)
|
|
|
+ if(isInitialized() && handler != -1)
|
|
|
Mix_HaltChannel(handler);
|
|
|
}
|
|
|
|
|
@@ -242,18 +241,18 @@ void CSoundHandler::setVolume(ui32 percent)
|
|
|
{
|
|
|
volume = std::min(100u, percent);
|
|
|
|
|
|
- if (isInitialized())
|
|
|
+ if(isInitialized())
|
|
|
{
|
|
|
setChannelVolume(-1, volume);
|
|
|
|
|
|
- for (auto const & channel : channelVolumes)
|
|
|
+ for(const auto & channel : channelVolumes)
|
|
|
updateChannelVolume(channel.first);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void CSoundHandler::updateChannelVolume(int channel)
|
|
|
{
|
|
|
- if (channelVolumes.count(channel))
|
|
|
+ if(channelVolumes.count(channel))
|
|
|
setChannelVolume(channel, getVolume() * channelVolumes[channel] / 100);
|
|
|
else
|
|
|
setChannelVolume(channel, getVolume());
|
|
@@ -262,7 +261,7 @@ void CSoundHandler::updateChannelVolume(int channel)
|
|
|
// Sets the sound volume, from 0 (mute) to 100
|
|
|
void CSoundHandler::setChannelVolume(int channel, ui32 percent)
|
|
|
{
|
|
|
- Mix_Volume(channel, (MIX_MAX_VOLUME * percent)/100);
|
|
|
+ Mix_Volume(channel, (MIX_MAX_VOLUME * percent) / 100);
|
|
|
}
|
|
|
|
|
|
void CSoundHandler::setCallback(int channel, std::function<void()> function)
|
|
@@ -289,7 +288,7 @@ void CSoundHandler::soundFinishedCallback(int channel)
|
|
|
{
|
|
|
boost::mutex::scoped_lock lockGuard(mutexCallbacks);
|
|
|
|
|
|
- if (callbacks.count(channel) == 0)
|
|
|
+ if(callbacks.count(channel) == 0)
|
|
|
return;
|
|
|
|
|
|
// store callbacks from container locally - SDL might reuse this channel for another sound
|
|
@@ -297,12 +296,15 @@ void CSoundHandler::soundFinishedCallback(int channel)
|
|
|
auto callback = callbacks.at(channel);
|
|
|
callbacks.erase(channel);
|
|
|
|
|
|
- if (!callback.empty())
|
|
|
+ if(!callback.empty())
|
|
|
{
|
|
|
- GH.dispatchMainThread([callback](){
|
|
|
- for (auto entry : callback)
|
|
|
- entry();
|
|
|
- });
|
|
|
+ GH.dispatchMainThread(
|
|
|
+ [callback]()
|
|
|
+ {
|
|
|
+ for(const auto & entry : callback)
|
|
|
+ entry();
|
|
|
+ }
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -322,7 +324,7 @@ void CSoundHandler::initCallback(int channel, const std::function<void()> & func
|
|
|
|
|
|
int CSoundHandler::ambientGetRange() const
|
|
|
{
|
|
|
- return static_cast<int>(ambientConfig["range"].Integer());
|
|
|
+ return ambientConfig["range"].Integer();
|
|
|
}
|
|
|
|
|
|
void CSoundHandler::ambientUpdateChannels(std::map<AudioPath, int> soundsArg)
|
|
@@ -330,7 +332,7 @@ void CSoundHandler::ambientUpdateChannels(std::map<AudioPath, int> soundsArg)
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
|
|
|
|
|
std::vector<AudioPath> stoppedSounds;
|
|
|
- for(auto & pair : ambientChannels)
|
|
|
+ for(const auto & pair : ambientChannels)
|
|
|
{
|
|
|
const auto & soundId = pair.first;
|
|
|
const int channel = pair.second;
|
|
@@ -342,18 +344,18 @@ void CSoundHandler::ambientUpdateChannels(std::map<AudioPath, int> soundsArg)
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- int volume = ambientDistToVolume(soundsArg[soundId]);
|
|
|
- channelVolumes[channel] = volume;
|
|
|
+ int channelVolume = ambientDistToVolume(soundsArg[soundId]);
|
|
|
+ channelVolumes[channel] = channelVolume;
|
|
|
updateChannelVolume(channel);
|
|
|
}
|
|
|
}
|
|
|
- for(auto soundId : stoppedSounds)
|
|
|
+ for(const auto & soundId : stoppedSounds)
|
|
|
{
|
|
|
channelVolumes.erase(ambientChannels[soundId]);
|
|
|
ambientChannels.erase(soundId);
|
|
|
}
|
|
|
|
|
|
- for(auto & pair : soundsArg)
|
|
|
+ for(const auto & pair : soundsArg)
|
|
|
{
|
|
|
const auto & soundId = pair.first;
|
|
|
const int distance = pair.second;
|
|
@@ -361,8 +363,8 @@ void CSoundHandler::ambientUpdateChannels(std::map<AudioPath, int> soundsArg)
|
|
|
if(!vstd::contains(ambientChannels, soundId))
|
|
|
{
|
|
|
int channel = playSound(soundId, -1);
|
|
|
- int volume = ambientDistToVolume(distance);
|
|
|
- channelVolumes[channel] = volume;
|
|
|
+ int channelVolume = ambientDistToVolume(distance);
|
|
|
+ channelVolumes[channel] = channelVolume;
|
|
|
|
|
|
updateChannelVolume(channel);
|
|
|
ambientChannels[soundId] = channel;
|
|
@@ -374,7 +376,7 @@ void CSoundHandler::ambientStopAllChannels()
|
|
|
{
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
|
|
|
|
|
- for(auto ch : ambientChannels)
|
|
|
+ for(const auto & ch : ambientChannels)
|
|
|
{
|
|
|
ambientStopSound(ch.first);
|
|
|
}
|