2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CMusicHandler.h, 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
|
|
|
|
*
|
2009-04-16 14:14:13 +03:00
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../lib/CConfigHandler.h"
|
|
|
|
#include "../lib/CSoundBase.h"
|
2022-08-09 07:54:32 +02:00
|
|
|
#include "../lib/Terrain.h"
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2009-05-01 02:25:17 +03:00
|
|
|
struct _Mix_Music;
|
2013-07-31 14:36:42 +03:00
|
|
|
struct SDL_RWops;
|
2009-05-01 02:25:17 +03:00
|
|
|
typedef struct _Mix_Music Mix_Music;
|
|
|
|
struct Mix_Chunk;
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
class CAudioBase {
|
|
|
|
protected:
|
2017-09-14 10:57:19 +02:00
|
|
|
boost::mutex mutex;
|
2009-05-22 07:14:59 +03:00
|
|
|
bool initialized;
|
|
|
|
int volume; // from 0 (mute) to 100
|
|
|
|
|
|
|
|
public:
|
|
|
|
CAudioBase(): initialized(false), volume(0) {};
|
|
|
|
virtual void init() = 0;
|
|
|
|
virtual void release() = 0;
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
virtual void setVolume(ui32 percent);
|
2017-09-13 02:35:58 +02:00
|
|
|
ui32 getVolume() const { return volume; };
|
2009-05-22 07:14:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class CSoundHandler: public CAudioBase
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
2009-04-22 21:48:56 +03:00
|
|
|
private:
|
2013-02-24 13:05:56 +03:00
|
|
|
//soundBase::soundID getSoundID(const std::string &fileName);
|
2012-01-12 18:23:00 +03:00
|
|
|
//update volume on configuration change
|
|
|
|
SettingsListener listener;
|
|
|
|
void onVolumeChange(const JsonNode &volumeNode);
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2016-08-30 23:04:16 +02:00
|
|
|
using CachedChunk = std::pair<Mix_Chunk *, std::unique_ptr<ui8[]>>;
|
|
|
|
std::map<std::string, CachedChunk> soundChunks;
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2015-02-15 21:51:33 +02:00
|
|
|
Mix_Chunk *GetSoundChunk(std::string &sound, bool cache);
|
2009-04-22 21:48:56 +03:00
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
//have entry for every currently active channel
|
2013-06-26 14:18:27 +03:00
|
|
|
//std::function will be nullptr if callback was not set
|
|
|
|
std::map<int, std::function<void()> > callbacks;
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
int ambientDistToVolume(int distance) const;
|
|
|
|
void ambientStopSound(std::string soundId);
|
|
|
|
|
|
|
|
const JsonNode ambientConfig;
|
|
|
|
bool allTilesSource;
|
|
|
|
std::map<std::string, int> ambientChannels;
|
|
|
|
|
2009-04-16 14:14:13 +03:00
|
|
|
public:
|
2009-05-22 07:14:59 +03:00
|
|
|
CSoundHandler();
|
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void init() override;
|
|
|
|
void release() override;
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void setVolume(ui32 percent) override;
|
2017-09-13 02:35:58 +02:00
|
|
|
void setChannelVolume(int channel, ui32 percent);
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2009-04-22 21:48:56 +03:00
|
|
|
// Sounds
|
2009-04-30 13:53:06 +03:00
|
|
|
int playSound(soundBase::soundID soundID, int repeats=0);
|
2015-02-15 21:51:33 +02:00
|
|
|
int playSound(std::string sound, int repeats=0, bool cache=false);
|
2009-04-30 13:53:06 +03:00
|
|
|
int playSoundFromSet(std::vector<soundBase::soundID> &sound_vec);
|
2009-04-24 00:09:10 +03:00
|
|
|
void stopSound(int handler);
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
void setCallback(int channel, std::function<void()> function);
|
2011-08-19 22:50:24 +03:00
|
|
|
void soundFinishedCallback(int channel);
|
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
int ambientGetRange() const;
|
|
|
|
bool ambientCheckVisitable() const;
|
|
|
|
void ambientUpdateChannels(std::map<std::string, int> currentSounds);
|
|
|
|
void ambientStopAllChannels();
|
|
|
|
|
2009-04-22 21:50:22 +03:00
|
|
|
// Sets
|
2009-05-06 05:39:03 +03:00
|
|
|
std::vector<soundBase::soundID> pickupSounds;
|
2011-08-19 22:50:24 +03:00
|
|
|
std::vector<soundBase::soundID> battleIntroSounds;
|
2022-06-20 16:39:50 +02:00
|
|
|
std::map<Terrain, soundBase::soundID> horseSounds;
|
2009-05-06 05:32:36 +03:00
|
|
|
};
|
|
|
|
|
2012-09-17 22:00:26 +03:00
|
|
|
// Helper //now it looks somewhat useless
|
|
|
|
#define battle_sound(creature,what_sound) creature->sounds.what_sound
|
2009-10-12 08:00:28 +03:00
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
class CMusicHandler;
|
|
|
|
|
|
|
|
//Class for handling one music file
|
|
|
|
class MusicEntry
|
|
|
|
{
|
|
|
|
CMusicHandler *owner;
|
|
|
|
Mix_Music *music;
|
2013-07-31 14:36:42 +03:00
|
|
|
|
2012-08-27 23:45:58 +03:00
|
|
|
int loop; // -1 = indefinite
|
2012-08-06 10:34:37 +03:00
|
|
|
//if not null - set from which music will be randomly selected
|
|
|
|
std::string setName;
|
|
|
|
std::string currentName;
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
|
|
|
|
void load(std::string musicURI);
|
2011-08-19 22:50:24 +03:00
|
|
|
|
|
|
|
public:
|
2012-08-06 10:34:37 +03:00
|
|
|
bool isSet(std::string setName);
|
|
|
|
bool isTrack(std::string trackName);
|
2011-08-19 22:50:24 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped);
|
2011-08-19 22:50:24 +03:00
|
|
|
~MusicEntry();
|
|
|
|
|
|
|
|
bool play();
|
2012-08-27 23:45:58 +03:00
|
|
|
bool stop(int fade_ms=0);
|
2011-08-19 22:50:24 +03:00
|
|
|
};
|
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
class CMusicHandler: public CAudioBase
|
2009-05-06 05:32:36 +03:00
|
|
|
{
|
|
|
|
private:
|
2022-06-20 16:39:50 +02:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
//update volume on configuration change
|
|
|
|
SettingsListener listener;
|
|
|
|
void onVolumeChange(const JsonNode &volumeNode);
|
2009-05-06 05:32:36 +03:00
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
std::unique_ptr<MusicEntry> current;
|
|
|
|
std::unique_ptr<MusicEntry> next;
|
2016-01-30 23:34:46 +02:00
|
|
|
|
2022-06-20 16:39:50 +02:00
|
|
|
void queueNext(CMusicHandler *owner, const std::string & setName, const std::string & musicURI, bool looped);
|
2015-12-29 04:43:33 +02:00
|
|
|
void queueNext(std::unique_ptr<MusicEntry> queued);
|
2012-08-06 10:34:37 +03:00
|
|
|
|
2022-06-20 16:39:50 +02:00
|
|
|
std::map<std::string, std::map<std::string, std::string>> musicsSet;
|
2009-05-06 05:32:36 +03:00
|
|
|
public:
|
2022-06-20 16:39:50 +02:00
|
|
|
|
2009-05-22 07:14:59 +03:00
|
|
|
CMusicHandler();
|
2009-05-06 05:32:36 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
/// add entry with URI musicURI in set. Track will have ID musicID
|
2022-06-20 16:39:50 +02:00
|
|
|
void addEntryToSet(const std::string & set, const std::string & entryID, const std::string & musicURI);
|
2012-08-06 10:34:37 +03:00
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void init() override;
|
|
|
|
void release() override;
|
|
|
|
void setVolume(ui32 percent) override;
|
2009-05-01 02:25:17 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
/// play track by URI, if loop = true music will be looped
|
2022-06-20 16:39:50 +02:00
|
|
|
void playMusic(const std::string & musicURI, bool loop);
|
2012-08-06 10:34:37 +03:00
|
|
|
/// play random track from this set
|
2022-06-20 16:39:50 +02:00
|
|
|
void playMusicFromSet(const std::string & musicSet, bool loop);
|
2012-08-06 10:34:37 +03:00
|
|
|
/// play specific track from set
|
2022-06-20 16:39:50 +02:00
|
|
|
void playMusicFromSet(const std::string & musicSet, const std::string & entryID, bool loop);
|
2009-05-01 02:25:17 +03:00
|
|
|
void stopMusic(int fade_ms=1000);
|
2018-01-13 10:43:26 +02:00
|
|
|
void musicFinishedCallback();
|
2012-08-06 10:34:37 +03:00
|
|
|
|
|
|
|
friend class MusicEntry;
|
2009-04-22 21:48:56 +03:00
|
|
|
};
|