2024-05-02 14:55:20 +02:00
|
|
|
/*
|
|
|
|
* CMusicHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CSoundHandler.h"
|
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
|
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
|
|
|
#include "../lib/CRandomGenerator.h"
|
|
|
|
|
|
|
|
#include <SDL_mixer.h>
|
|
|
|
|
|
|
|
#define VCMI_SOUND_NAME(x)
|
|
|
|
#define VCMI_SOUND_FILE(y) #y,
|
|
|
|
|
|
|
|
// sounds mapped to soundBase enum
|
2024-05-17 18:01:08 +02:00
|
|
|
static const std::string soundsList[] = {
|
2024-05-02 14:55:20 +02:00
|
|
|
"", // invalid
|
|
|
|
"", // todo
|
|
|
|
VCMI_SOUND_LIST
|
|
|
|
};
|
|
|
|
#undef VCMI_SOUND_NAME
|
|
|
|
#undef VCMI_SOUND_FILE
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
void CSoundHandler::onVolumeChange(const JsonNode & volumeNode)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
setVolume(volumeNode.Integer());
|
2024-05-02 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSoundHandler::CSoundHandler():
|
|
|
|
listener(settings.listen["general"]["sound"]),
|
|
|
|
ambientConfig(JsonPath::builtin("config/ambientSounds.json"))
|
|
|
|
{
|
|
|
|
listener(std::bind(&CSoundHandler::onVolumeChange, this, _1));
|
|
|
|
|
|
|
|
if(ambientConfig["allocateChannels"].isNumber())
|
2024-05-17 18:01:08 +02:00
|
|
|
Mix_AllocateChannels(ambientConfig["allocateChannels"].Integer());
|
2024-05-02 14:55:20 +02:00
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(isInitialized())
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
Mix_ChannelFinished([](int channel)
|
|
|
|
{
|
|
|
|
CCS->soundh->soundFinishedCallback(channel);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CSoundHandler::~CSoundHandler()
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
if(isInitialized())
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
Mix_HaltChannel(-1);
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
for(auto & chunk : soundChunks)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
if(chunk.second.first)
|
2024-05-02 14:55:20 +02:00
|
|
|
Mix_FreeChunk(chunk.second.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate an SDL chunk and cache it.
|
2024-05-17 18:01:08 +02:00
|
|
|
Mix_Chunk * CSoundHandler::GetSoundChunk(const AudioPath & sound, bool cache)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
if(cache && soundChunks.find(sound) != soundChunks.end())
|
2024-05-02 14:55:20 +02:00
|
|
|
return soundChunks[sound].first;
|
|
|
|
|
|
|
|
auto data = CResourceHandler::get()->load(sound.addPrefix("SOUNDS/"))->readAll();
|
2024-05-17 18:01:08 +02:00
|
|
|
SDL_RWops * ops = SDL_RWFromMem(data.first.get(), data.second);
|
|
|
|
Mix_Chunk * chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
2024-05-02 14:55:20 +02:00
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(cache)
|
|
|
|
soundChunks.insert({sound, std::make_pair(chunk, std::move(data.first))});
|
2024-05-02 14:55:20 +02:00
|
|
|
|
|
|
|
return chunk;
|
|
|
|
}
|
2024-05-17 18:01:08 +02:00
|
|
|
catch(std::exception & e)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
logGlobal->warn("Cannot get sound %s chunk: %s", sound.getOriginalName(), e.what());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
Mix_Chunk * CSoundHandler::GetSoundChunk(std::pair<std::unique_ptr<ui8[]>, si64> & data, bool cache)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
std::vector<ui8> startBytes = std::vector<ui8>(data.first.get(), data.first.get() + std::min(static_cast<si64>(100), data.second));
|
2024-05-02 14:55:20 +02:00
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(cache && soundChunksRaw.find(startBytes) != soundChunksRaw.end())
|
2024-05-02 14:55:20 +02:00
|
|
|
return soundChunksRaw[startBytes].first;
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
SDL_RWops * ops = SDL_RWFromMem(data.first.get(), data.second);
|
|
|
|
Mix_Chunk * chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
2024-05-02 14:55:20 +02:00
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(cache)
|
|
|
|
soundChunksRaw.insert({startBytes, std::make_pair(chunk, std::move(data.first))});
|
2024-05-02 14:55:20 +02:00
|
|
|
|
|
|
|
return chunk;
|
|
|
|
}
|
2024-05-17 18:01:08 +02:00
|
|
|
catch(std::exception & e)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
logGlobal->warn("Cannot get sound chunk: %s", e.what());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSoundHandler::ambientDistToVolume(int distance) const
|
|
|
|
{
|
|
|
|
const auto & distancesVector = ambientConfig["distances"].Vector();
|
|
|
|
|
|
|
|
if(distance >= distancesVector.size())
|
|
|
|
return 0;
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
int volumeByDistance = static_cast<int>(distancesVector[distance].Integer());
|
|
|
|
return volumeByDistance * ambientConfig["volume"].Integer() / 100;
|
2024-05-02 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::ambientStopSound(const AudioPath & soundId)
|
|
|
|
{
|
|
|
|
stopSound(ambientChannels[soundId]);
|
|
|
|
setChannelVolume(ambientChannels[soundId], volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CSoundHandler::getSoundDurationMilliseconds(const AudioPath & sound)
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
if(!isInitialized() || sound.empty())
|
2024-05-02 14:55:20 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
auto resourcePath = sound.addPrefix("SOUNDS/");
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(!CResourceHandler::get()->existsResource(resourcePath))
|
2024-05-02 14:55:20 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
auto data = CResourceHandler::get()->load(resourcePath)->readAll();
|
|
|
|
|
|
|
|
SDL_AudioSpec spec;
|
|
|
|
uint32_t audioLen;
|
2024-05-17 18:01:08 +02:00
|
|
|
uint8_t * audioBuf;
|
2024-06-24 03:23:26 +02:00
|
|
|
uint32_t milliseconds = 0;
|
2024-05-02 14:55:20 +02:00
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(SDL_LoadWAV_RW(SDL_RWFromMem(data.first.get(), data.second), 1, &spec, &audioBuf, &audioLen) != nullptr)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
SDL_FreeWAV(audioBuf);
|
|
|
|
uint32_t sampleSize = SDL_AUDIO_BITSIZE(spec.format) / 8;
|
|
|
|
uint32_t sampleCount = audioLen / sampleSize;
|
|
|
|
uint32_t sampleLen = sampleCount / spec.channels;
|
2024-06-24 03:23:26 +02:00
|
|
|
milliseconds = 1000 * sampleLen / spec.freq;
|
2024-05-02 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
return milliseconds;
|
2024-05-02 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2024-05-17 18:01:08 +02:00
|
|
|
auto sound = AudioPath::builtin(soundsList[soundID]);
|
2024-05-02 14:55:20 +02:00
|
|
|
logGlobal->trace("Attempt to play sound %d with file name %s with cache", soundID, sound.getOriginalName());
|
|
|
|
|
|
|
|
return playSound(sound, repeats, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSoundHandler::playSound(const AudioPath & sound, int repeats, bool cache)
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
if(!isInitialized() || sound.empty())
|
2024-05-02 14:55:20 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
int channel;
|
2024-05-17 18:01:08 +02:00
|
|
|
Mix_Chunk * chunk = GetSoundChunk(sound, cache);
|
2024-05-02 14:55:20 +02:00
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(chunk)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
channel = Mix_PlayChannel(-1, chunk, repeats);
|
2024-05-17 18:01:08 +02:00
|
|
|
if(channel == -1)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
logGlobal->error("Unable to play sound file %s , error %s", sound.getOriginalName(), Mix_GetError());
|
2024-05-17 18:01:08 +02:00
|
|
|
if(!cache)
|
2024-05-02 14:55:20 +02:00
|
|
|
Mix_FreeChunk(chunk);
|
|
|
|
}
|
2024-05-17 18:01:08 +02:00
|
|
|
else if(cache)
|
2024-05-02 14:55:20 +02:00
|
|
|
initCallback(channel);
|
|
|
|
else
|
|
|
|
initCallback(channel, [chunk](){ Mix_FreeChunk(chunk);});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
channel = -1;
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
int CSoundHandler::playSound(std::pair<std::unique_ptr<ui8[]>, si64> & data, int repeats, bool cache)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
int channel = -1;
|
2024-05-17 18:01:08 +02:00
|
|
|
if(Mix_Chunk * chunk = GetSoundChunk(data, cache))
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
channel = Mix_PlayChannel(-1, chunk, repeats);
|
2024-05-17 18:01:08 +02:00
|
|
|
if(channel == -1)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
logGlobal->error("Unable to play sound, error %s", Mix_GetError());
|
2024-05-17 18:01:08 +02:00
|
|
|
if(!cache)
|
2024-05-02 14:55:20 +02:00
|
|
|
Mix_FreeChunk(chunk);
|
|
|
|
}
|
2024-05-17 18:01:08 +02:00
|
|
|
else if(cache)
|
2024-05-02 14:55:20 +02:00
|
|
|
initCallback(channel);
|
|
|
|
else
|
|
|
|
initCallback(channel, [chunk](){ Mix_FreeChunk(chunk);});
|
|
|
|
}
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper. Randomly select a sound from an array and play it
|
2024-05-17 18:01:08 +02:00
|
|
|
int CSoundHandler::playSoundFromSet(std::vector<soundBase::soundID> & sound_vec)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
return playSound(*RandomGeneratorUtil::nextItem(sound_vec, CRandomGenerator::getDefault()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::stopSound(int handler)
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
if(isInitialized() && handler != -1)
|
2024-05-02 14:55:20 +02:00
|
|
|
Mix_HaltChannel(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui32 CSoundHandler::getVolume() const
|
|
|
|
{
|
|
|
|
return volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the sound volume, from 0 (mute) to 100
|
|
|
|
void CSoundHandler::setVolume(ui32 percent)
|
|
|
|
{
|
|
|
|
volume = std::min(100u, percent);
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(isInitialized())
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
setChannelVolume(-1, volume);
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
for(const auto & channel : channelVolumes)
|
2024-05-02 14:55:20 +02:00
|
|
|
updateChannelVolume(channel.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::updateChannelVolume(int channel)
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
if(channelVolumes.count(channel))
|
2024-05-02 14:55:20 +02:00
|
|
|
setChannelVolume(channel, getVolume() * channelVolumes[channel] / 100);
|
|
|
|
else
|
|
|
|
setChannelVolume(channel, getVolume());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the sound volume, from 0 (mute) to 100
|
|
|
|
void CSoundHandler::setChannelVolume(int channel, ui32 percent)
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
Mix_Volume(channel, (MIX_MAX_VOLUME * percent) / 100);
|
2024-05-02 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::setCallback(int channel, std::function<void()> function)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lockGuard(mutexCallbacks);
|
|
|
|
|
|
|
|
auto iter = callbacks.find(channel);
|
|
|
|
|
|
|
|
//channel not found. It may have finished so fire callback now
|
|
|
|
if(iter == callbacks.end())
|
|
|
|
function();
|
|
|
|
else
|
|
|
|
iter->second.push_back(function);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::resetCallback(int channel)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lockGuard(mutexCallbacks);
|
|
|
|
|
|
|
|
callbacks.erase(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::soundFinishedCallback(int channel)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lockGuard(mutexCallbacks);
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(callbacks.count(channel) == 0)
|
2024-05-02 14:55:20 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// store callbacks from container locally - SDL might reuse this channel for another sound
|
2024-06-24 03:23:26 +02:00
|
|
|
// but do actually execution in separate thread, to avoid potential deadlocks in case if callback requires locks of its own
|
2024-05-02 14:55:20 +02:00
|
|
|
auto callback = callbacks.at(channel);
|
|
|
|
callbacks.erase(channel);
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
if(!callback.empty())
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
GH.dispatchMainThread(
|
|
|
|
[callback]()
|
|
|
|
{
|
|
|
|
for(const auto & entry : callback)
|
|
|
|
entry();
|
|
|
|
}
|
|
|
|
);
|
2024-05-02 14:55:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::initCallback(int channel)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lockGuard(mutexCallbacks);
|
|
|
|
assert(callbacks.count(channel) == 0);
|
|
|
|
callbacks[channel] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::initCallback(int channel, const std::function<void()> & function)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lockGuard(mutexCallbacks);
|
|
|
|
assert(callbacks.count(channel) == 0);
|
|
|
|
callbacks[channel].push_back(function);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSoundHandler::ambientGetRange() const
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
return ambientConfig["range"].Integer();
|
2024-05-02 14:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::ambientUpdateChannels(std::map<AudioPath, int> soundsArg)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
|
|
|
|
|
|
|
std::vector<AudioPath> stoppedSounds;
|
2024-05-17 18:01:08 +02:00
|
|
|
for(const auto & pair : ambientChannels)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
const auto & soundId = pair.first;
|
|
|
|
const int channel = pair.second;
|
|
|
|
|
|
|
|
if(!vstd::contains(soundsArg, soundId))
|
|
|
|
{
|
|
|
|
ambientStopSound(soundId);
|
|
|
|
stoppedSounds.push_back(soundId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-17 18:01:08 +02:00
|
|
|
int channelVolume = ambientDistToVolume(soundsArg[soundId]);
|
|
|
|
channelVolumes[channel] = channelVolume;
|
2024-05-02 14:55:20 +02:00
|
|
|
updateChannelVolume(channel);
|
|
|
|
}
|
|
|
|
}
|
2024-05-17 18:01:08 +02:00
|
|
|
for(const auto & soundId : stoppedSounds)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
channelVolumes.erase(ambientChannels[soundId]);
|
|
|
|
ambientChannels.erase(soundId);
|
|
|
|
}
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
for(const auto & pair : soundsArg)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
const auto & soundId = pair.first;
|
|
|
|
const int distance = pair.second;
|
|
|
|
|
|
|
|
if(!vstd::contains(ambientChannels, soundId))
|
|
|
|
{
|
|
|
|
int channel = playSound(soundId, -1);
|
2024-05-17 18:01:08 +02:00
|
|
|
int channelVolume = ambientDistToVolume(distance);
|
|
|
|
channelVolumes[channel] = channelVolume;
|
2024-05-02 14:55:20 +02:00
|
|
|
|
|
|
|
updateChannelVolume(channel);
|
|
|
|
ambientChannels[soundId] = channel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::ambientStopAllChannels()
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
|
|
|
|
2024-05-17 18:01:08 +02:00
|
|
|
for(const auto & ch : ambientChannels)
|
2024-05-02 14:55:20 +02:00
|
|
|
{
|
|
|
|
ambientStopSound(ch.first);
|
|
|
|
}
|
|
|
|
channelVolumes.clear();
|
|
|
|
ambientChannels.clear();
|
|
|
|
}
|