2017-07-13 10:26:03 +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
|
|
|
|
*
|
|
|
|
*/
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2009-04-22 21:48:56 +03:00
|
|
|
#include <SDL_mixer.h>
|
2022-11-13 14:05:51 +02:00
|
|
|
#include <SDL.h>
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2007-06-20 15:00:18 +03:00
|
|
|
#include "CMusicHandler.h"
|
2014-08-04 21:33:59 +03:00
|
|
|
#include "CGameInfo.h"
|
2016-01-30 23:34:46 +02:00
|
|
|
#include "SDLRWwrapper.h"
|
2011-08-20 04:15:37 +03:00
|
|
|
#include "../lib/JsonNode.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "../lib/GameConstants.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2013-12-28 21:57:08 +03:00
|
|
|
#include "../lib/StringConstants.h"
|
2014-04-10 20:11:09 +03:00
|
|
|
#include "../lib/CRandomGenerator.h"
|
2017-11-11 22:16:11 +02:00
|
|
|
#include "../lib/VCMIDirs.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "../lib/TerrainHandler.h"
|
2007-06-20 15:00:18 +03:00
|
|
|
|
2013-02-24 13:05:56 +03:00
|
|
|
#define VCMI_SOUND_NAME(x)
|
|
|
|
#define VCMI_SOUND_FILE(y) #y,
|
|
|
|
|
|
|
|
// sounds mapped to soundBase enum
|
|
|
|
static std::string sounds[] = {
|
2020-10-06 01:27:04 +02:00
|
|
|
"", // invalid
|
|
|
|
"", // todo
|
2013-02-24 13:05:56 +03:00
|
|
|
VCMI_SOUND_LIST
|
|
|
|
};
|
|
|
|
#undef VCMI_SOUND_NAME
|
|
|
|
#undef VCMI_SOUND_FILE
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
void CAudioBase::init()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1)
|
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->error("Mix_OpenAudio error: %s", Mix_GetError());
|
2009-05-22 07:14:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioBase::release()
|
|
|
|
{
|
2019-04-21 21:09:14 +02:00
|
|
|
if(!(CCS->soundh->initialized && CCS->musich->initialized))
|
2009-05-22 07:14:59 +03:00
|
|
|
Mix_CloseAudio();
|
2019-04-21 21:09:14 +02:00
|
|
|
|
|
|
|
initialized = false;
|
2009-05-22 07:14:59 +03:00
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
void CAudioBase::setVolume(ui32 percent)
|
2009-05-22 07:14:59 +03:00
|
|
|
{
|
|
|
|
if (percent > 100)
|
|
|
|
percent = 100;
|
2012-02-16 20:10:58 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
volume = percent;
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
void CSoundHandler::onVolumeChange(const JsonNode &volumeNode)
|
2009-04-30 13:53:06 +03:00
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
setVolume((ui32)volumeNode.Float());
|
2012-01-12 18:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CSoundHandler::CSoundHandler():
|
2017-09-13 02:35:58 +02:00
|
|
|
listener(settings.listen["general"]["sound"]),
|
|
|
|
ambientConfig(JsonNode(ResourceID("config/ambientSounds.json")))
|
2012-01-12 18:23:00 +03:00
|
|
|
{
|
2017-09-13 02:35:58 +02:00
|
|
|
allTilesSource = ambientConfig["allTilesSource"].Bool();
|
2014-08-04 21:33:59 +03:00
|
|
|
listener(std::bind(&CSoundHandler::onVolumeChange, this, _1));
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2009-04-22 21:50:22 +03:00
|
|
|
// Vectors for helper(s)
|
2016-01-30 23:34:46 +02:00
|
|
|
pickupSounds =
|
2014-10-02 18:43:46 +03:00
|
|
|
{
|
|
|
|
soundBase::pickup01, soundBase::pickup02, soundBase::pickup03,
|
|
|
|
soundBase::pickup04, soundBase::pickup05, soundBase::pickup06, soundBase::pickup07
|
|
|
|
};
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
battleIntroSounds =
|
|
|
|
{
|
|
|
|
soundBase::battle00, soundBase::battle01,
|
2020-10-06 01:27:04 +02:00
|
|
|
soundBase::battle02, soundBase::battle03, soundBase::battle04,
|
|
|
|
soundBase::battle05, soundBase::battle06, soundBase::battle07
|
2014-10-02 18:43:46 +03:00
|
|
|
};
|
2022-09-21 13:43:00 +02:00
|
|
|
}
|
2009-04-22 21:50:22 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
void CSoundHandler::init()
|
|
|
|
{
|
|
|
|
CAudioBase::init();
|
2017-09-13 02:35:58 +02:00
|
|
|
if(ambientConfig["allocateChannels"].isNumber())
|
2020-10-01 10:38:06 +02:00
|
|
|
Mix_AllocateChannels((int)ambientConfig["allocateChannels"].Integer());
|
2009-05-22 07:14:59 +03:00
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
if (initialized)
|
|
|
|
{
|
2022-11-21 16:16:23 +02:00
|
|
|
Mix_ChannelFinished([](int channel)
|
|
|
|
{
|
|
|
|
CCS->soundh->soundFinishedCallback(channel);
|
|
|
|
});
|
2011-08-07 22:14:46 +03:00
|
|
|
}
|
2009-05-06 05:32:36 +03:00
|
|
|
}
|
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
void CSoundHandler::release()
|
2009-05-06 05:32:36 +03:00
|
|
|
{
|
2011-08-19 22:50:24 +03:00
|
|
|
if (initialized)
|
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
Mix_HaltChannel(-1);
|
|
|
|
|
2015-02-15 21:51:33 +02:00
|
|
|
for (auto &chunk : soundChunks)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2016-08-30 23:04:16 +02:00
|
|
|
if (chunk.second.first)
|
|
|
|
Mix_FreeChunk(chunk.second.first);
|
2009-05-22 07:14:59 +03:00
|
|
|
}
|
2009-05-06 05:32:36 +03:00
|
|
|
}
|
2009-05-22 07:14:59 +03:00
|
|
|
|
|
|
|
CAudioBase::release();
|
2007-06-20 15:00:18 +03:00
|
|
|
}
|
|
|
|
|
2009-04-30 13:53:06 +03:00
|
|
|
// Allocate an SDL chunk and cache it.
|
2015-02-15 21:51:33 +02:00
|
|
|
Mix_Chunk *CSoundHandler::GetSoundChunk(std::string &sound, bool cache)
|
2007-06-20 15:00:18 +03:00
|
|
|
{
|
2012-08-09 10:40:47 +03:00
|
|
|
try
|
|
|
|
{
|
2015-02-15 21:51:33 +02:00
|
|
|
if (cache && soundChunks.find(sound) != soundChunks.end())
|
2016-08-30 23:04:16 +02:00
|
|
|
return soundChunks[sound].first;
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2015-02-15 21:51:33 +02:00
|
|
|
auto data = CResourceHandler::get()->load(ResourceID(std::string("SOUNDS/") + sound, EResType::SOUND))->readAll();
|
2020-10-01 10:38:06 +02:00
|
|
|
SDL_RWops *ops = SDL_RWFromMem(data.first.get(), (int)data.second);
|
2015-02-15 21:51:33 +02:00
|
|
|
Mix_Chunk *chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
2009-04-26 02:31:39 +03:00
|
|
|
|
2015-02-15 21:51:33 +02:00
|
|
|
if (cache)
|
2016-08-30 23:04:16 +02:00
|
|
|
soundChunks.insert(std::pair<std::string, CachedChunk>(sound, std::make_pair (chunk, std::move (data.first))));
|
2012-09-22 18:10:15 +03:00
|
|
|
|
2012-09-17 22:00:26 +03:00
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->warn("Cannot get sound %s chunk: %s", sound, e.what());
|
2012-09-17 22:00:26 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
int CSoundHandler::ambientDistToVolume(int distance) const
|
|
|
|
{
|
|
|
|
if(distance >= ambientConfig["distances"].Vector().size())
|
|
|
|
return 0;
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
int volume = static_cast<int>(ambientConfig["distances"].Vector()[distance].Integer());
|
|
|
|
return volume * (int)ambientConfig["volume"].Integer() * getVolume() / 10000;
|
2017-09-13 02:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::ambientStopSound(std::string soundId)
|
|
|
|
{
|
|
|
|
stopSound(ambientChannels[soundId]);
|
2019-05-07 05:30:44 +02:00
|
|
|
setChannelVolume(ambientChannels[soundId], volume);
|
2017-09-13 02:35:58 +02:00
|
|
|
}
|
|
|
|
|
2009-04-22 21:48:56 +03:00
|
|
|
// Plays a sound, and return its channel so we can fade it out later
|
2009-05-06 05:32:36 +03:00
|
|
|
int CSoundHandler::playSound(soundBase::soundID soundID, int repeats)
|
2009-04-22 21:48:56 +03:00
|
|
|
{
|
2013-03-14 23:44:00 +03:00
|
|
|
assert(soundID < soundBase::sound_after_last);
|
2015-02-15 21:51:33 +02:00
|
|
|
auto sound = sounds[soundID];
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->trace("Attempt to play sound %d with file name %s with cache", soundID, sound);
|
2008-08-02 18:08:03 +03:00
|
|
|
|
2015-02-15 21:51:33 +02:00
|
|
|
return playSound(sound, repeats, true);
|
2008-08-02 18:08:03 +03:00
|
|
|
}
|
2009-04-22 21:50:22 +03:00
|
|
|
|
2015-02-15 21:51:33 +02:00
|
|
|
int CSoundHandler::playSound(std::string sound, int repeats, bool cache)
|
2012-09-17 22:00:26 +03:00
|
|
|
{
|
2015-02-15 21:51:33 +02:00
|
|
|
if (!initialized || sound.empty())
|
2012-09-17 22:00:26 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
int channel;
|
2015-02-15 21:51:33 +02:00
|
|
|
Mix_Chunk *chunk = GetSoundChunk(sound, cache);
|
2012-09-17 22:00:26 +03:00
|
|
|
|
|
|
|
if (chunk)
|
|
|
|
{
|
|
|
|
channel = Mix_PlayChannel(-1, chunk, repeats);
|
|
|
|
if (channel == -1)
|
2015-02-15 21:51:33 +02:00
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->error("Unable to play sound file %s , error %s", sound, Mix_GetError());
|
2015-02-15 21:51:33 +02:00
|
|
|
if (!cache)
|
|
|
|
Mix_FreeChunk(chunk);
|
|
|
|
}
|
|
|
|
else if (cache)
|
|
|
|
callbacks[channel];
|
2012-09-17 22:00:26 +03:00
|
|
|
else
|
2017-07-17 14:35:57 +02:00
|
|
|
callbacks[channel] = [chunk](){ Mix_FreeChunk(chunk);};
|
2012-09-17 22:00:26 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
channel = -1;
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2009-04-22 21:50:22 +03:00
|
|
|
// Helper. Randomly select a sound from an array and play it
|
2009-05-06 05:32:36 +03:00
|
|
|
int CSoundHandler::playSoundFromSet(std::vector<soundBase::soundID> &sound_vec)
|
2009-04-22 21:50:22 +03:00
|
|
|
{
|
2014-04-10 20:11:09 +03:00
|
|
|
return playSound(*RandomGeneratorUtil::nextItem(sound_vec, CRandomGenerator::getDefault()));
|
2009-04-22 21:50:22 +03:00
|
|
|
}
|
2009-04-24 00:09:10 +03:00
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
void CSoundHandler::stopSound(int handler)
|
2009-04-24 00:09:10 +03:00
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
if (initialized && handler != -1)
|
2009-04-26 01:51:16 +03:00
|
|
|
Mix_HaltChannel(handler);
|
2009-05-01 02:25:17 +03:00
|
|
|
}
|
|
|
|
|
2009-05-06 06:13:46 +03:00
|
|
|
// Sets the sound volume, from 0 (mute) to 100
|
2011-12-14 00:23:17 +03:00
|
|
|
void CSoundHandler::setVolume(ui32 percent)
|
2009-05-06 06:13:46 +03:00
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
CAudioBase::setVolume(percent);
|
2009-05-06 06:13:46 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
if (initialized)
|
2017-09-13 02:35:58 +02:00
|
|
|
setChannelVolume(-1, volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the sound volume, from 0 (mute) to 100
|
|
|
|
void CSoundHandler::setChannelVolume(int channel, ui32 percent)
|
|
|
|
{
|
|
|
|
Mix_Volume(channel, (MIX_MAX_VOLUME * percent)/100);
|
2009-05-06 06:13:46 +03:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
void CSoundHandler::setCallback(int channel, std::function<void()> function)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
std::map<int, std::function<void()> >::iterator iter;
|
2011-08-19 22:50:24 +03:00
|
|
|
iter = callbacks.find(channel);
|
|
|
|
|
|
|
|
//channel not found. It may have finished so fire callback now
|
|
|
|
if(iter == callbacks.end())
|
|
|
|
function();
|
|
|
|
else
|
|
|
|
iter->second = function;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::soundFinishedCallback(int channel)
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
std::map<int, std::function<void()> >::iterator iter;
|
2011-08-19 22:50:24 +03:00
|
|
|
iter = callbacks.find(channel);
|
2022-07-20 15:48:32 +02:00
|
|
|
if (iter == callbacks.end())
|
|
|
|
return;
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2022-07-20 15:48:32 +02:00
|
|
|
auto callback = std::move(iter->second);
|
2011-08-19 22:50:24 +03:00
|
|
|
callbacks.erase(iter);
|
2022-07-20 15:48:32 +02:00
|
|
|
|
|
|
|
if (callback)
|
|
|
|
callback();
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
int CSoundHandler::ambientGetRange() const
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
return static_cast<int>(ambientConfig["range"].Integer());
|
2017-09-13 02:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CSoundHandler::ambientCheckVisitable() const
|
|
|
|
{
|
|
|
|
return !allTilesSource;
|
|
|
|
}
|
|
|
|
|
2018-10-29 17:34:03 +02:00
|
|
|
void CSoundHandler::ambientUpdateChannels(std::map<std::string, int> soundsArg)
|
2017-09-13 02:35:58 +02:00
|
|
|
{
|
2017-09-14 10:57:19 +02:00
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
2017-09-13 02:35:58 +02:00
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
std::vector<std::string> stoppedSounds;
|
|
|
|
for(auto & pair : ambientChannels)
|
|
|
|
{
|
2018-10-29 17:34:03 +02:00
|
|
|
if(!vstd::contains(soundsArg, pair.first))
|
2017-09-13 02:35:58 +02:00
|
|
|
{
|
|
|
|
ambientStopSound(pair.first);
|
|
|
|
stoppedSounds.push_back(pair.first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-29 17:34:03 +02:00
|
|
|
CCS->soundh->setChannelVolume(pair.second, ambientDistToVolume(soundsArg[pair.first]));
|
2017-09-13 02:35:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for(auto soundId : stoppedSounds)
|
|
|
|
ambientChannels.erase(soundId);
|
|
|
|
|
2018-10-29 17:34:03 +02:00
|
|
|
for(auto & pair : soundsArg)
|
2017-09-13 02:35:58 +02:00
|
|
|
{
|
|
|
|
if(!vstd::contains(ambientChannels, pair.first))
|
|
|
|
{
|
|
|
|
int channel = CCS->soundh->playSound(pair.first, -1);
|
|
|
|
CCS->soundh->setChannelVolume(channel, ambientDistToVolume(pair.second));
|
|
|
|
CCS->soundh->ambientChannels.insert(std::make_pair(pair.first, channel));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundHandler::ambientStopAllChannels()
|
|
|
|
{
|
2017-09-14 10:57:19 +02:00
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
2017-09-13 02:35:58 +02:00
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
for(auto ch : ambientChannels)
|
|
|
|
{
|
|
|
|
ambientStopSound(ch.first);
|
|
|
|
}
|
|
|
|
ambientChannels.clear();
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
void CMusicHandler::onVolumeChange(const JsonNode &volumeNode)
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
setVolume((ui32)volumeNode.Float());
|
2012-01-12 18:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMusicHandler::CMusicHandler():
|
|
|
|
listener(settings.listen["general"]["music"])
|
2009-05-06 05:32:36 +03:00
|
|
|
{
|
2014-08-04 21:33:59 +03:00
|
|
|
listener(std::bind(&CMusicHandler::onVolumeChange, this, _1));
|
2012-02-16 20:10:58 +03:00
|
|
|
|
2017-11-16 16:47:52 +02:00
|
|
|
auto mp3files = CResourceHandler::get()->getFilteredFiles([](const ResourceID & id) -> bool
|
2012-08-06 10:34:37 +03:00
|
|
|
{
|
2017-11-16 16:47:52 +02:00
|
|
|
if(id.getType() != EResType::MUSIC)
|
2017-11-11 22:16:11 +02:00
|
|
|
return false;
|
2017-11-16 16:47:52 +02:00
|
|
|
|
|
|
|
if(!boost::algorithm::istarts_with(id.getName(), "MUSIC/"))
|
2017-11-11 22:16:11 +02:00
|
|
|
return false;
|
2017-11-16 16:47:52 +02:00
|
|
|
|
|
|
|
logGlobal->trace("Found music file %s", id.getName());
|
2017-11-11 22:16:11 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2017-11-16 16:47:52 +02:00
|
|
|
for(const ResourceID & file : mp3files)
|
2017-11-11 22:16:11 +02:00
|
|
|
{
|
2017-11-16 16:47:52 +02:00
|
|
|
if(boost::algorithm::istarts_with(file.getName(), "MUSIC/Combat"))
|
2022-11-21 16:16:23 +02:00
|
|
|
addEntryToSet("battle", file.getName());
|
2017-11-16 16:47:52 +02:00
|
|
|
else if(boost::algorithm::istarts_with(file.getName(), "MUSIC/AITheme"))
|
2022-11-21 16:16:23 +02:00
|
|
|
addEntryToSet("enemy-turn", file.getName());
|
2017-11-11 22:16:11 +02:00
|
|
|
}
|
2013-12-28 21:57:08 +03:00
|
|
|
|
2022-09-21 13:43:00 +02:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
void CMusicHandler::loadTerrainMusicThemes()
|
2022-09-21 13:43:00 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
for (const auto & terrain : CGI->terrainTypeHandler->objects)
|
2013-12-28 21:57:08 +03:00
|
|
|
{
|
2023-01-01 22:42:28 +02:00
|
|
|
addEntryToSet("terrain_" + terrain->getJsonKey(), "Music/" + terrain->musicFilename);
|
2013-12-28 21:57:08 +03:00
|
|
|
}
|
2012-08-06 10:34:37 +03:00
|
|
|
}
|
2011-08-17 23:44:14 +03:00
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
void CMusicHandler::addEntryToSet(const std::string & set, const std::string & musicURI)
|
2012-08-06 10:34:37 +03:00
|
|
|
{
|
2022-11-21 16:16:23 +02:00
|
|
|
musicsSet[set].push_back(musicURI);
|
2009-05-06 05:32:36 +03:00
|
|
|
}
|
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
void CMusicHandler::init()
|
2009-05-06 05:32:36 +03:00
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
CAudioBase::init();
|
2009-05-06 05:32:36 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
if (initialized)
|
2022-11-21 16:16:23 +02:00
|
|
|
{
|
|
|
|
Mix_HookMusicFinished([]()
|
|
|
|
{
|
|
|
|
CCS->musich->musicFinishedCallback();
|
|
|
|
});
|
|
|
|
}
|
2009-05-22 07:14:59 +03:00
|
|
|
}
|
2009-05-06 05:32:36 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
void CMusicHandler::release()
|
|
|
|
{
|
2011-08-19 22:50:24 +03:00
|
|
|
if (initialized)
|
|
|
|
{
|
2017-09-14 10:57:19 +02:00
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
2009-05-22 07:14:59 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
Mix_HookMusicFinished(nullptr);
|
2009-05-22 07:14:59 +03:00
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
current.reset();
|
|
|
|
next.reset();
|
2009-05-22 07:14:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CAudioBase::release();
|
2009-05-06 05:32:36 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:05:51 +02:00
|
|
|
void CMusicHandler::playMusic(const std::string & musicURI, bool loop, bool fromStart)
|
2009-05-01 02:25:17 +03:00
|
|
|
{
|
2022-12-26 21:05:01 +02:00
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
if (current && current->isPlaying() && current->isTrack(musicURI))
|
2009-05-22 07:14:59 +03:00
|
|
|
return;
|
|
|
|
|
2022-11-13 14:24:15 +02:00
|
|
|
queueNext(this, "", musicURI, loop, fromStart);
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
void CMusicHandler::playMusicFromSet(const std::string & musicSet, const std::string & entryID, bool loop, bool fromStart)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2022-11-21 16:16:23 +02:00
|
|
|
playMusicFromSet(musicSet + "_" + entryID, loop, fromStart);
|
2012-08-06 10:34:37 +03:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
void CMusicHandler::playMusicFromSet(const std::string & whichSet, bool loop, bool fromStart)
|
2012-08-06 10:34:37 +03:00
|
|
|
{
|
2022-12-26 21:05:01 +02:00
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
auto selectedSet = musicsSet.find(whichSet);
|
|
|
|
if (selectedSet == musicsSet.end())
|
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->error("Error: playing music from non-existing set: %s", whichSet);
|
2012-08-06 10:34:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
if (current && current->isPlaying() && current->isSet(whichSet))
|
2012-08-10 11:49:18 +03:00
|
|
|
return;
|
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
// in this mode - play random track from set
|
|
|
|
queueNext(this, whichSet, "", loop, fromStart);
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
void CMusicHandler::queueNext(std::unique_ptr<MusicEntry> queued)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
return;
|
|
|
|
|
2013-08-01 16:52:01 +03:00
|
|
|
next = std::move(queued);
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2012-08-27 23:45:58 +03:00
|
|
|
if (current.get() == nullptr || !current->stop(1000))
|
2009-05-20 12:02:50 +03:00
|
|
|
{
|
2012-02-17 23:34:34 +03:00
|
|
|
current.reset(next.release());
|
2011-08-19 22:50:24 +03:00
|
|
|
current->play();
|
2009-05-01 02:25:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 14:05:51 +02:00
|
|
|
void CMusicHandler::queueNext(CMusicHandler *owner, const std::string & setName, const std::string & musicURI, bool looped, bool fromStart)
|
2013-08-01 16:52:01 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
queueNext(std::make_unique<MusicEntry>(owner, setName, musicURI, looped, fromStart));
|
2013-08-01 16:52:01 +03:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->error("Failed to queue music. setName=%s\tmusicURI=%s", setName, musicURI);
|
|
|
|
logGlobal->error("Exception: %s", e.what());
|
2013-08-01 16:52:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-01 02:25:17 +03:00
|
|
|
void CMusicHandler::stopMusic(int fade_ms)
|
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
if (!initialized)
|
|
|
|
return;
|
|
|
|
|
2017-09-14 10:57:19 +02:00
|
|
|
boost::mutex::scoped_lock guard(mutex);
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
if (current.get() != nullptr)
|
2011-08-19 22:50:24 +03:00
|
|
|
current->stop(fade_ms);
|
|
|
|
next.reset();
|
2009-05-01 02:25:17 +03:00
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
void CMusicHandler::setVolume(ui32 percent)
|
2009-05-06 06:13:46 +03:00
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
CAudioBase::setVolume(percent);
|
2009-05-06 06:13:46 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
if (initialized)
|
|
|
|
Mix_VolumeMusic((MIX_MAX_VOLUME * volume)/100);
|
2009-05-06 06:13:46 +03:00
|
|
|
}
|
|
|
|
|
2018-01-13 10:43:26 +02:00
|
|
|
void CMusicHandler::musicFinishedCallback()
|
2009-05-01 02:25:17 +03:00
|
|
|
{
|
2022-12-26 21:05:01 +02:00
|
|
|
// boost::mutex::scoped_lock guard(mutex);
|
|
|
|
// FIXME: WORKAROUND FOR A POTENTIAL DEADLOCK
|
|
|
|
// It is possible for:
|
|
|
|
// 1) SDL thread to call this method on end of playback
|
|
|
|
// 2) VCMI code to call queueNext() method to queue new file
|
|
|
|
// this leads to:
|
2022-12-27 00:52:28 +02:00
|
|
|
// 1) SDL thread waiting to acquire music lock in this method (while keeping internal SDL mutex locked)
|
|
|
|
// 2) VCMI thread waiting to acquire internal SDL mutex (while keeping music mutex locked)
|
2022-12-26 21:05:01 +02:00
|
|
|
// Because of that (and lack of clear way to fix that)
|
|
|
|
// We will try to acquire lock here and if failed - do nothing
|
|
|
|
// This may break music playback till next song is enqued but won't deadlock the game
|
|
|
|
|
|
|
|
if (!mutex.try_lock())
|
|
|
|
{
|
2022-12-27 00:52:28 +02:00
|
|
|
logGlobal->error("Failed to acquire mutex! Unable to restart music!");
|
2022-12-26 21:05:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
if (current.get() != nullptr)
|
2009-05-20 12:02:50 +03:00
|
|
|
{
|
2022-11-21 16:16:23 +02:00
|
|
|
// if music is looped, play it again
|
2011-08-19 22:50:24 +03:00
|
|
|
if (current->play())
|
2022-12-26 21:05:01 +02:00
|
|
|
{
|
|
|
|
mutex.unlock();
|
2011-08-19 22:50:24 +03:00
|
|
|
return;
|
2022-12-26 21:05:01 +02:00
|
|
|
}
|
2011-08-19 22:50:24 +03:00
|
|
|
else
|
|
|
|
current.reset();
|
2009-05-01 02:25:17 +03:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
if (current.get() == nullptr && next.get() != nullptr)
|
2009-05-20 12:02:50 +03:00
|
|
|
{
|
2012-02-17 23:34:34 +03:00
|
|
|
current.reset(next.release());
|
2011-08-19 22:50:24 +03:00
|
|
|
current->play();
|
2009-05-01 02:25:17 +03:00
|
|
|
}
|
2022-12-26 21:05:01 +02:00
|
|
|
mutex.unlock();
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2022-11-13 14:05:51 +02:00
|
|
|
MusicEntry::MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped, bool fromStart):
|
2012-08-06 10:34:37 +03:00
|
|
|
owner(owner),
|
|
|
|
music(nullptr),
|
2022-11-21 16:16:23 +02:00
|
|
|
playing(false),
|
2022-11-13 14:59:28 +02:00
|
|
|
startTime(uint32_t(-1)),
|
2022-11-13 14:24:15 +02:00
|
|
|
startPosition(0),
|
2012-08-27 23:45:58 +03:00
|
|
|
loop(looped ? -1 : 1),
|
2022-11-13 14:24:15 +02:00
|
|
|
fromStart(fromStart),
|
2020-10-06 01:27:04 +02:00
|
|
|
setName(std::move(setName))
|
2009-06-10 22:44:09 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
if (!musicURI.empty())
|
2016-01-30 23:34:46 +02:00
|
|
|
load(std::move(musicURI));
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
|
|
|
MusicEntry::~MusicEntry()
|
|
|
|
{
|
2022-12-31 17:55:22 +02:00
|
|
|
if (playing)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
logGlobal->error("Attempt to delete music while playing!");
|
|
|
|
Mix_HaltMusic();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loop == 0 && Mix_FadingMusic() != MIX_NO_FADING)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
logGlobal->error("Attempt to delete music while fading out!");
|
|
|
|
Mix_HaltMusic();
|
|
|
|
}
|
|
|
|
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->trace("Del-ing music file %s", currentName);
|
2011-08-19 22:50:24 +03:00
|
|
|
if (music)
|
|
|
|
Mix_FreeMusic(music);
|
|
|
|
}
|
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
void MusicEntry::load(std::string musicURI)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2012-06-13 16:04:06 +03:00
|
|
|
if (music)
|
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->trace("Del-ing music file %s", currentName);
|
2012-06-13 16:04:06 +03:00
|
|
|
Mix_FreeMusic(music);
|
2013-08-06 14:20:28 +03:00
|
|
|
music = nullptr;
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
currentName = musicURI;
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->trace("Loading music file %s", musicURI);
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2016-01-30 23:34:46 +02:00
|
|
|
auto musicFile = MakeSDLRWops(CResourceHandler::get()->load(ResourceID(std::move(musicURI), EResType::MUSIC)));
|
|
|
|
|
|
|
|
music = Mix_LoadMUS_RW(musicFile, SDL_TRUE);
|
2014-05-21 19:04:34 +03:00
|
|
|
|
|
|
|
if(!music)
|
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->warn("Warning: Cannot open %s: %s", currentName, Mix_GetError());
|
2014-05-21 19:04:34 +03:00
|
|
|
return;
|
|
|
|
}
|
2009-06-10 22:44:09 +03:00
|
|
|
}
|
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
bool MusicEntry::play()
|
2009-06-10 22:44:09 +03:00
|
|
|
{
|
2012-08-27 23:45:58 +03:00
|
|
|
if (!(loop--) && music) //already played once - return
|
2011-08-19 22:50:24 +03:00
|
|
|
return false;
|
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
if (!setName.empty())
|
|
|
|
{
|
2022-06-20 16:39:50 +02:00
|
|
|
const auto & set = owner->musicsSet[setName];
|
2022-11-21 16:16:23 +02:00
|
|
|
const auto & iter = RandomGeneratorUtil::nextItem(set, CRandomGenerator::getDefault());
|
|
|
|
load(*iter);
|
2012-08-06 10:34:37 +03:00
|
|
|
}
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->trace("Playing music file %s", currentName);
|
2022-11-13 14:05:51 +02:00
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
if (!fromStart && owner->trackPositions.count(currentName) > 0 && owner->trackPositions[currentName] > 0)
|
2022-11-13 14:24:15 +02:00
|
|
|
{
|
|
|
|
float timeToStart = owner->trackPositions[currentName];
|
|
|
|
startPosition = std::round(timeToStart * 1000);
|
2022-11-13 14:05:51 +02:00
|
|
|
|
2022-11-20 23:38:09 +02:00
|
|
|
// erase stored position:
|
|
|
|
// if music track will be interrupted again - new position will be written in stop() method
|
|
|
|
// if music track is not interrupted and will finish by timeout/end of file - it will restart from begginning as it should
|
2022-11-21 16:16:23 +02:00
|
|
|
owner->trackPositions.erase(owner->trackPositions.find(currentName));
|
2022-11-20 23:38:09 +02:00
|
|
|
|
2022-11-13 14:24:15 +02:00
|
|
|
if (Mix_FadeInMusicPos(music, 1, 1000, timeToStart) == -1)
|
|
|
|
{
|
|
|
|
logGlobal->error("Unable to play music (%s)", Mix_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 16:16:23 +02:00
|
|
|
else
|
2022-11-13 14:24:15 +02:00
|
|
|
{
|
2022-11-21 16:16:23 +02:00
|
|
|
startPosition = 0;
|
|
|
|
|
|
|
|
if(Mix_PlayMusic(music, 1) == -1)
|
|
|
|
{
|
|
|
|
logGlobal->error("Unable to play music (%s)", Mix_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-13 14:24:15 +02:00
|
|
|
}
|
2022-11-13 14:05:51 +02:00
|
|
|
|
2022-11-13 14:59:28 +02:00
|
|
|
startTime = SDL_GetTicks();
|
2022-11-21 16:16:23 +02:00
|
|
|
playing = true;
|
2022-11-13 14:24:15 +02:00
|
|
|
return true;
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
2009-06-10 22:44:09 +03:00
|
|
|
|
2012-08-27 23:45:58 +03:00
|
|
|
bool MusicEntry::stop(int fade_ms)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2022-12-31 17:55:22 +02:00
|
|
|
if (Mix_PlayingMusic())
|
2012-08-27 23:45:58 +03:00
|
|
|
{
|
2022-11-21 16:16:23 +02:00
|
|
|
playing = false;
|
2012-08-27 23:45:58 +03:00
|
|
|
loop = 0;
|
2022-11-13 14:59:28 +02:00
|
|
|
uint32_t endTime = SDL_GetTicks();
|
|
|
|
assert(startTime != uint32_t(-1));
|
2022-11-13 14:24:15 +02:00
|
|
|
float playDuration = (endTime - startTime + startPosition) / 1000.f;
|
|
|
|
owner->trackPositions[currentName] = playDuration;
|
|
|
|
logGlobal->info("Stopping music file %s at %f", currentName, playDuration);
|
2022-11-13 14:05:51 +02:00
|
|
|
|
2012-08-27 23:45:58 +03:00
|
|
|
Mix_FadeOutMusic(fade_ms);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
bool MusicEntry::isPlaying()
|
|
|
|
{
|
|
|
|
return playing;
|
|
|
|
}
|
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
bool MusicEntry::isSet(std::string set)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
return !setName.empty() && set == setName;
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
bool MusicEntry::isTrack(std::string track)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
return setName.empty() && track == currentName;
|
2011-08-20 04:15:37 +03:00
|
|
|
}
|