2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2009-04-22 21:48:56 +03:00
|
|
|
#include <SDL_mixer.h>
|
|
|
|
|
2007-06-20 15:00:18 +03:00
|
|
|
#include "CMusicHandler.h"
|
2014-08-04 21:33:59 +03:00
|
|
|
#include "CGameInfo.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "../lib/CCreatureHandler.h"
|
|
|
|
#include "../lib/CSpellHandler.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"
|
2007-06-20 15:00:18 +03:00
|
|
|
|
2009-04-15 17:03:31 +03: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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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[] = {
|
|
|
|
"", // invalid
|
|
|
|
"", // todo
|
|
|
|
VCMI_SOUND_LIST
|
|
|
|
};
|
|
|
|
#undef VCMI_SOUND_NAME
|
|
|
|
#undef VCMI_SOUND_FILE
|
2009-05-01 02:25:17 +03:00
|
|
|
|
|
|
|
// Not pretty, but there's only one music handler object in the game.
|
2011-08-19 22:50:24 +03:00
|
|
|
static void soundFinishedCallbackC(int channel)
|
|
|
|
{
|
|
|
|
CCS->soundh->soundFinishedCallback(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void musicFinishedCallbackC(void)
|
|
|
|
{
|
2010-12-19 16:39:56 +02:00
|
|
|
CCS->musich->musicFinishedCallback();
|
2009-05-01 02:25:17 +03:00
|
|
|
}
|
2009-04-30 13:53:06 +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)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Mix_OpenAudio error: " << Mix_GetError();
|
2009-05-22 07:14:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAudioBase::release()
|
|
|
|
{
|
2011-08-19 22:50:24 +03:00
|
|
|
if (initialized)
|
2012-08-06 10:34:37 +03:00
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
Mix_CloseAudio();
|
|
|
|
initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
setVolume(volumeNode.Float());
|
|
|
|
}
|
|
|
|
|
|
|
|
CSoundHandler::CSoundHandler():
|
|
|
|
listener(settings.listen["general"]["sound"])
|
|
|
|
{
|
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)
|
2014-10-02 18:43:46 +03:00
|
|
|
pickupSounds =
|
|
|
|
{
|
|
|
|
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
|
|
|
horseSounds = // must be the same order as terrains (see ETerrainType);
|
|
|
|
{
|
2009-04-23 22:50:27 +03:00
|
|
|
soundBase::horseDirt, soundBase::horseSand, soundBase::horseGrass,
|
|
|
|
soundBase::horseSnow, soundBase::horseSwamp, soundBase::horseRough,
|
|
|
|
soundBase::horseSubterranean, soundBase::horseLava,
|
2014-10-02 18:43:46 +03:00
|
|
|
soundBase::horseWater, soundBase::horseRock
|
|
|
|
};
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
battleIntroSounds =
|
|
|
|
{
|
|
|
|
soundBase::battle00, soundBase::battle01,
|
2011-08-19 22:50:24 +03:00
|
|
|
soundBase::battle02, soundBase::battle03, soundBase::battle04,
|
2014-10-02 18:43:46 +03:00
|
|
|
soundBase::battle05, soundBase::battle06, soundBase::battle07
|
|
|
|
};
|
2009-05-22 07:14:59 +03:00
|
|
|
};
|
2009-04-22 21:50:22 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
void CSoundHandler::init()
|
|
|
|
{
|
|
|
|
CAudioBase::init();
|
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
if (initialized)
|
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
// Load sounds
|
2011-08-19 22:50:24 +03:00
|
|
|
Mix_ChannelFinished(soundFinishedCallbackC);
|
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);
|
|
|
|
|
|
|
|
std::map<soundBase::soundID, Mix_Chunk *>::iterator it;
|
2011-08-19 22:50:24 +03:00
|
|
|
for (it=soundChunks.begin(); it != soundChunks.end(); it++)
|
|
|
|
{
|
2009-05-22 07:14:59 +03:00
|
|
|
if (it->second)
|
|
|
|
Mix_FreeChunk(it->second);
|
|
|
|
}
|
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.
|
2009-05-06 05:32:36 +03:00
|
|
|
Mix_Chunk *CSoundHandler::GetSoundChunk(soundBase::soundID soundID)
|
2007-06-20 15:00:18 +03:00
|
|
|
{
|
2009-04-30 13:53:06 +03:00
|
|
|
// Find its name
|
2013-02-24 13:05:56 +03:00
|
|
|
auto fname = sounds[soundID];
|
|
|
|
if (fname.empty())
|
2012-08-09 10:40:47 +03:00
|
|
|
return nullptr;
|
2008-03-20 16:23:54 +02:00
|
|
|
|
2009-04-30 13:53:06 +03:00
|
|
|
// Load and insert
|
2012-08-09 10:40:47 +03:00
|
|
|
try
|
|
|
|
{
|
2013-07-28 17:49:50 +03:00
|
|
|
auto data = CResourceHandler::get()->load(ResourceID(std::string("SOUNDS/") + fname, EResType::SOUND))->readAll();
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2012-08-09 10:40:47 +03:00
|
|
|
SDL_RWops *ops = SDL_RWFromMem(data.first.release(), data.second);
|
|
|
|
Mix_Chunk *chunk;
|
|
|
|
chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
|
|
|
soundChunks.insert(std::pair<soundBase::soundID, Mix_Chunk *>(soundID, chunk));
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->warnStream() << "Cannot get sound " << soundID << " chunk: " << e.what();
|
2012-08-09 10:40:47 +03:00
|
|
|
return nullptr;
|
2009-04-30 13:53:06 +03:00
|
|
|
}
|
2009-04-22 21:48:56 +03:00
|
|
|
}
|
2009-04-26 02:31:39 +03:00
|
|
|
|
2012-09-17 22:00:26 +03:00
|
|
|
Mix_Chunk *CSoundHandler::GetSoundChunk(std::string &sound)
|
|
|
|
{
|
2012-09-22 18:10:15 +03:00
|
|
|
if (sound.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
2012-09-17 22:00:26 +03:00
|
|
|
// Load and insert
|
|
|
|
try
|
|
|
|
{
|
2013-07-28 17:49:50 +03:00
|
|
|
auto data = CResourceHandler::get()->load(ResourceID(std::string("SOUNDS/") + sound, EResType::SOUND))->readAll();
|
2012-09-17 22:00:26 +03:00
|
|
|
|
|
|
|
SDL_RWops *ops = SDL_RWFromMem(data.first.release(), data.second);
|
|
|
|
Mix_Chunk *chunk;
|
|
|
|
chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->warnStream() << "Cannot get sound " << sound << " chunk: " << e.what();
|
2012-09-17 22:00:26 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2009-05-22 07:14:59 +03:00
|
|
|
if (!initialized)
|
2009-04-22 21:48:56 +03:00
|
|
|
return -1;
|
2008-08-02 18:08:03 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
int channel;
|
|
|
|
Mix_Chunk *chunk = GetSoundChunk(soundID);
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2009-04-30 13:53:06 +03:00
|
|
|
if (chunk)
|
2008-03-20 11:55:33 +02:00
|
|
|
{
|
2009-04-30 13:53:06 +03:00
|
|
|
channel = Mix_PlayChannel(-1, chunk, repeats);
|
|
|
|
if (channel == -1)
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Unable to play sound file " << soundID << " , error " << Mix_GetError();
|
2011-08-19 22:50:24 +03:00
|
|
|
else
|
|
|
|
callbacks[channel];//insert empty callback
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-30 13:53:06 +03:00
|
|
|
channel = -1;
|
2008-03-20 11:55:33 +02:00
|
|
|
}
|
2009-04-22 21:48:56 +03:00
|
|
|
|
|
|
|
return channel;
|
2008-08-02 18:08:03 +03:00
|
|
|
}
|
2009-04-22 21:50:22 +03:00
|
|
|
|
2012-09-17 22:00:26 +03:00
|
|
|
int CSoundHandler::playSound(std::string sound, int repeats)
|
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int channel;
|
|
|
|
Mix_Chunk *chunk = GetSoundChunk(sound);
|
|
|
|
|
|
|
|
if (chunk)
|
|
|
|
{
|
|
|
|
channel = Mix_PlayChannel(-1, chunk, repeats);
|
|
|
|
if (channel == -1)
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Unable to play sound file " << sound << " , error " << Mix_GetError();
|
2012-09-17 22:00:26 +03:00
|
|
|
else
|
|
|
|
callbacks[channel];//insert empty callback
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2009-05-06 05:32:36 +03: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)
|
|
|
|
Mix_Volume(-1, (MIX_MAX_VOLUME * volume)/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);
|
|
|
|
|
|
|
|
assert(iter != callbacks.end());
|
|
|
|
|
|
|
|
if (iter->second)
|
|
|
|
iter->second();
|
|
|
|
|
|
|
|
callbacks.erase(iter);
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
void CMusicHandler::onVolumeChange(const JsonNode &volumeNode)
|
|
|
|
{
|
|
|
|
setVolume(volumeNode.Float());
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2009-05-06 05:32:36 +03:00
|
|
|
// Map music IDs
|
2010-08-18 17:24:30 +03:00
|
|
|
// Vectors for helper
|
2012-08-06 10:34:37 +03:00
|
|
|
const std::string setEnemy[] = {"AITheme0", "AITheme1", "AITheme2"};
|
|
|
|
const std::string setBattle[] = {"Combat01", "Combat02", "Combat03", "Combat04"};
|
2012-02-16 20:10:58 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
auto fillSet = [=](std::string setName, const std::string list[], size_t amount)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i < amount; i++)
|
2013-12-28 21:57:08 +03:00
|
|
|
addEntryToSet(setName, i, "music/" + list[i]);
|
2012-08-06 10:34:37 +03:00
|
|
|
};
|
|
|
|
fillSet("enemy-turn", setEnemy, ARRAY_COUNT(setEnemy));
|
|
|
|
fillSet("battle", setBattle, ARRAY_COUNT(setBattle));
|
2013-12-28 21:57:08 +03:00
|
|
|
|
|
|
|
JsonNode terrains(ResourceID("config/terrains.json"));
|
|
|
|
for (auto entry : terrains.Struct())
|
|
|
|
{
|
|
|
|
int terrIndex = vstd::find_pos(GameConstants::TERRAIN_NAMES, entry.first);
|
|
|
|
addEntryToSet("terrain", terrIndex, "Music/" + entry.second["music"].String());
|
|
|
|
}
|
2012-08-06 10:34:37 +03:00
|
|
|
}
|
2011-08-17 23:44:14 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
void CMusicHandler::addEntryToSet(std::string set, int musicID, std::string musicURI)
|
|
|
|
{
|
|
|
|
musicsSet[set][musicID] = 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)
|
|
|
|
Mix_HookMusicFinished(musicFinishedCallbackC);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock guard(musicMutex);
|
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
|
|
|
}
|
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
void CMusicHandler::playMusic(std::string musicURI, bool loop)
|
2009-05-01 02:25:17 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
if (current && current->isTrack( musicURI))
|
2009-05-22 07:14:59 +03:00
|
|
|
return;
|
|
|
|
|
2013-08-01 16:52:01 +03:00
|
|
|
queueNext(this, "", musicURI, loop);
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
void CMusicHandler::playMusicFromSet(std::string whichSet, bool loop)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
auto selectedSet = musicsSet.find(whichSet);
|
|
|
|
if (selectedSet == musicsSet.end())
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Error: playing music from non-existing set: " << whichSet;
|
2011-08-19 22:50:24 +03:00
|
|
|
return;
|
2012-08-06 10:34:37 +03:00
|
|
|
}
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
if (current && current->isSet(whichSet))
|
|
|
|
return;
|
|
|
|
|
2013-08-20 13:47:40 +03:00
|
|
|
// in this mode - play random track from set
|
2013-08-01 16:52:01 +03:00
|
|
|
queueNext(this, whichSet, "", loop);
|
2012-08-06 10:34:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMusicHandler::playMusicFromSet(std::string whichSet, int entryID, bool loop)
|
|
|
|
{
|
|
|
|
auto selectedSet = musicsSet.find(whichSet);
|
|
|
|
if (selectedSet == musicsSet.end())
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Error: playing music from non-existing set: " << whichSet;
|
2012-08-06 10:34:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto selectedEntry = selectedSet->second.find(entryID);
|
|
|
|
if (selectedEntry == selectedSet->second.end())
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Error: playing non-existing entry " << entryID << " from set: " << whichSet;
|
2012-08-06 10:34:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2012-08-10 11:49:18 +03:00
|
|
|
|
|
|
|
if (current && current->isTrack( selectedEntry->second))
|
|
|
|
return;
|
|
|
|
|
2013-08-20 13:47:40 +03:00
|
|
|
// in this mode - play specific track from set
|
|
|
|
queueNext(this, "", selectedEntry->second, loop);
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
|
|
|
|
2013-08-01 16:52:01 +03:00
|
|
|
void CMusicHandler::queueNext(unique_ptr<MusicEntry> queued)
|
2011-08-19 22:50:24 +03:00
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock guard(musicMutex);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 16:52:01 +03:00
|
|
|
void CMusicHandler::queueNext(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
queueNext(make_unique<MusicEntry>(owner, setName, musicURI, looped));
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2014-03-07 16:21:09 +03:00
|
|
|
logGlobal->errorStream() << "Failed to queue music. setName=" << setName << "\tmusicURI=" << musicURI;
|
2013-08-01 16:52:01 +03:00
|
|
|
logGlobal->errorStream() << "Exception: " << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
boost::mutex::scoped_lock guard(musicMutex);
|
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
|
|
|
}
|
|
|
|
|
2009-05-01 02:25:17 +03:00
|
|
|
void CMusicHandler::musicFinishedCallback(void)
|
|
|
|
{
|
2011-08-19 22:50:24 +03:00
|
|
|
boost::mutex::scoped_lock guard(musicMutex);
|
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
|
|
|
{
|
2011-08-19 22:50:24 +03:00
|
|
|
//return if current music still not finished
|
|
|
|
if (current->play())
|
|
|
|
return;
|
|
|
|
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
|
|
|
}
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
MusicEntry::MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped):
|
|
|
|
owner(owner),
|
|
|
|
music(nullptr),
|
2013-07-31 14:36:42 +03:00
|
|
|
musicFile(nullptr),
|
2012-08-27 23:45:58 +03:00
|
|
|
loop(looped ? -1 : 1),
|
2012-08-06 10:34:37 +03:00
|
|
|
setName(setName)
|
2009-06-10 22:44:09 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
if (!musicURI.empty())
|
|
|
|
load(musicURI);
|
2011-08-19 22:50:24 +03:00
|
|
|
}
|
|
|
|
MusicEntry::~MusicEntry()
|
|
|
|
{
|
2013-07-31 14:36:42 +03:00
|
|
|
logGlobal->traceStream()<<"Del-ing music file "<<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)
|
|
|
|
{
|
2013-07-31 14:36:42 +03:00
|
|
|
logGlobal->traceStream()<<"Del-ing music file "<<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
|
|
|
|
2013-07-31 14:36:42 +03:00
|
|
|
logGlobal->traceStream()<<"Loading music file "<<musicURI;
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2013-12-19 22:43:16 +03:00
|
|
|
data = CResourceHandler::get()->load(ResourceID(musicURI, EResType::MUSIC))->readAll();
|
|
|
|
musicFile = SDL_RWFromConstMem(data.first.get(), data.second);
|
2014-05-23 19:46:54 +03:00
|
|
|
|
|
|
|
#ifdef VCMI_SDL1
|
2013-07-31 14:36:42 +03:00
|
|
|
music = Mix_LoadMUS_RW(musicFile);
|
2011-08-17 23:44:14 +03:00
|
|
|
|
2011-09-19 23:50:25 +03:00
|
|
|
if(!music)
|
|
|
|
{
|
2013-07-31 14:36:42 +03:00
|
|
|
SDL_FreeRW(musicFile);
|
2013-08-06 14:20:28 +03:00
|
|
|
musicFile = nullptr;
|
2013-07-31 14:36:42 +03:00
|
|
|
logGlobal->warnStream() << "Warning: Cannot open " << currentName << ": " << Mix_GetError();
|
2011-09-19 23:50:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-21 19:04:34 +03:00
|
|
|
#else
|
|
|
|
music = Mix_LoadMUS_RW(musicFile, SDL_FALSE);
|
|
|
|
|
|
|
|
if(!music)
|
|
|
|
{
|
|
|
|
SDL_FreeRW(musicFile);
|
|
|
|
musicFile = nullptr;
|
|
|
|
logGlobal->warnStream() << "Warning: Cannot open " << currentName << ": " << Mix_GetError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // 0
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
auto set = owner->musicsSet[setName];
|
2014-04-10 20:11:09 +03:00
|
|
|
load(RandomGeneratorUtil::nextItem(set, CRandomGenerator::getDefault())->second);
|
2012-08-06 10:34:37 +03:00
|
|
|
}
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->traceStream()<<"Playing music file "<<currentName;
|
2011-08-19 22:50:24 +03:00
|
|
|
if(Mix_PlayMusic(music, 1) == -1)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Unable to play music (" << Mix_GetError() << ")";
|
2011-08-19 22:50:24 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
{
|
2012-08-27 23:45:58 +03:00
|
|
|
if (Mix_PlayingMusic())
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->traceStream()<<"Stoping music file "<<currentName;
|
2012-08-27 23:45:58 +03:00
|
|
|
loop = 0;
|
|
|
|
Mix_FadeOutMusic(fade_ms);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|
2011-08-19 22:50:24 +03:00
|
|
|
|
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
|
|
|
}
|