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;
|
2022-09-21 13:43:00 +02:00
|
|
|
void loadHorseSounds();
|
2015-10-12 15:47:10 +02:00
|
|
|
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-09-29 11:44:46 +02:00
|
|
|
std::map<TerrainId, 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
|
2022-11-13 14:24:15 +02:00
|
|
|
bool fromStart;
|
2022-11-21 16:16:23 +02:00
|
|
|
bool playing;
|
2022-11-13 14:59:28 +02:00
|
|
|
uint32_t startTime;
|
|
|
|
uint32_t startPosition;
|
2022-11-13 14:24:15 +02:00
|
|
|
//if not null - set from which music will be randomly selected
|
2012-08-06 10:34:37 +03:00
|
|
|
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:
|
2022-11-13 14:24:15 +02:00
|
|
|
MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped, bool fromStart);
|
2011-08-19 22:50:24 +03:00
|
|
|
~MusicEntry();
|
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
bool isSet(std::string setName);
|
|
|
|
bool isTrack(std::string trackName);
|
|
|
|
bool isPlaying();
|
|
|
|
|
2011-08-19 22:50:24 +03:00
|
|
|
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:
|
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-11-13 14:24:15 +02:00
|
|
|
void queueNext(CMusicHandler *owner, const std::string & setName, const std::string & musicURI, bool looped, bool fromStart);
|
2015-12-29 04:43:33 +02:00
|
|
|
void queueNext(std::unique_ptr<MusicEntry> queued);
|
2022-11-21 16:16:23 +02:00
|
|
|
void musicFinishedCallback();
|
2012-08-06 10:34:37 +03:00
|
|
|
|
2022-11-21 16:16:23 +02:00
|
|
|
/// map <set name> -> <list of URI's to tracks belonging to the said set>
|
|
|
|
std::map<std::string, std::vector<std::string>> musicsSet;
|
|
|
|
/// stored position, in seconds at which music player should resume playing this track
|
2022-11-13 14:24:15 +02:00
|
|
|
std::map<std::string, float> trackPositions;
|
2022-11-21 16:16:23 +02:00
|
|
|
|
2009-05-06 05:32:36 +03:00
|
|
|
public:
|
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-11-21 16:16:23 +02:00
|
|
|
void addEntryToSet(const std::string & set, const std::string & musicURI);
|
2012-08-06 10:34:37 +03:00
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void init() override;
|
2022-11-21 16:16:23 +02:00
|
|
|
void loadTerrainMusicThemes();
|
2015-10-12 15:47:10 +02:00
|
|
|
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-11-13 14:24:15 +02:00
|
|
|
void playMusic(const std::string & musicURI, bool loop, bool fromStart);
|
2012-08-06 10:34:37 +03:00
|
|
|
/// play random track from this set
|
2022-11-13 14:24:15 +02:00
|
|
|
void playMusicFromSet(const std::string & musicSet, bool loop, bool fromStart);
|
2022-11-21 16:16:23 +02:00
|
|
|
/// play random track from set (musicSet, entryID)
|
2022-11-13 14:24:15 +02:00
|
|
|
void playMusicFromSet(const std::string & musicSet, const std::string & entryID, bool loop, bool fromStart);
|
2022-11-21 16:16:23 +02:00
|
|
|
/// stops currently playing music by fading out it over fade_ms and starts next scheduled track, if any
|
2009-05-01 02:25:17 +03:00
|
|
|
void stopMusic(int fade_ms=1000);
|
2012-08-06 10:34:37 +03:00
|
|
|
|
|
|
|
friend class MusicEntry;
|
2009-04-22 21:48:56 +03:00
|
|
|
};
|