CMusicHandler.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * CMusicHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../lib/CConfigHandler.h"
  12. #include "../lib/CSoundBase.h"
  13. #include "../lib/Terrain.h"
  14. struct _Mix_Music;
  15. struct SDL_RWops;
  16. typedef struct _Mix_Music Mix_Music;
  17. struct Mix_Chunk;
  18. class CAudioBase {
  19. protected:
  20. boost::mutex mutex;
  21. bool initialized;
  22. int volume; // from 0 (mute) to 100
  23. public:
  24. CAudioBase(): initialized(false), volume(0) {};
  25. virtual void init() = 0;
  26. virtual void release() = 0;
  27. virtual void setVolume(ui32 percent);
  28. ui32 getVolume() const { return volume; };
  29. };
  30. class CSoundHandler: public CAudioBase
  31. {
  32. private:
  33. //soundBase::soundID getSoundID(const std::string &fileName);
  34. //update volume on configuration change
  35. SettingsListener listener;
  36. void onVolumeChange(const JsonNode &volumeNode);
  37. using CachedChunk = std::pair<Mix_Chunk *, std::unique_ptr<ui8[]>>;
  38. std::map<std::string, CachedChunk> soundChunks;
  39. Mix_Chunk *GetSoundChunk(std::string &sound, bool cache);
  40. //have entry for every currently active channel
  41. //std::function will be nullptr if callback was not set
  42. std::map<int, std::function<void()> > callbacks;
  43. int ambientDistToVolume(int distance) const;
  44. void ambientStopSound(std::string soundId);
  45. const JsonNode ambientConfig;
  46. bool allTilesSource;
  47. std::map<std::string, int> ambientChannels;
  48. public:
  49. CSoundHandler();
  50. void init() override;
  51. void loadHorseSounds();
  52. void release() override;
  53. void setVolume(ui32 percent) override;
  54. void setChannelVolume(int channel, ui32 percent);
  55. // Sounds
  56. int playSound(soundBase::soundID soundID, int repeats=0);
  57. int playSound(std::string sound, int repeats=0, bool cache=false);
  58. int playSoundFromSet(std::vector<soundBase::soundID> &sound_vec);
  59. void stopSound(int handler);
  60. void setCallback(int channel, std::function<void()> function);
  61. void soundFinishedCallback(int channel);
  62. int ambientGetRange() const;
  63. bool ambientCheckVisitable() const;
  64. void ambientUpdateChannels(std::map<std::string, int> currentSounds);
  65. void ambientStopAllChannels();
  66. // Sets
  67. std::vector<soundBase::soundID> pickupSounds;
  68. std::vector<soundBase::soundID> battleIntroSounds;
  69. std::map<TerrainId, soundBase::soundID> horseSounds;
  70. };
  71. // Helper //now it looks somewhat useless
  72. #define battle_sound(creature,what_sound) creature->sounds.what_sound
  73. class CMusicHandler;
  74. //Class for handling one music file
  75. class MusicEntry
  76. {
  77. CMusicHandler *owner;
  78. Mix_Music *music;
  79. int loop; // -1 = indefinite
  80. bool fromStart;
  81. bool playing;
  82. uint32_t startTime;
  83. uint32_t startPosition;
  84. //if not null - set from which music will be randomly selected
  85. std::string setName;
  86. std::string currentName;
  87. void load(std::string musicURI);
  88. public:
  89. MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped, bool fromStart);
  90. ~MusicEntry();
  91. bool isSet(std::string setName);
  92. bool isTrack(std::string trackName);
  93. bool isPlaying();
  94. bool play();
  95. bool stop(int fade_ms=0);
  96. };
  97. class CMusicHandler: public CAudioBase
  98. {
  99. private:
  100. //update volume on configuration change
  101. SettingsListener listener;
  102. void onVolumeChange(const JsonNode &volumeNode);
  103. std::unique_ptr<MusicEntry> current;
  104. std::unique_ptr<MusicEntry> next;
  105. void queueNext(CMusicHandler *owner, const std::string & setName, const std::string & musicURI, bool looped, bool fromStart);
  106. void queueNext(std::unique_ptr<MusicEntry> queued);
  107. void musicFinishedCallback();
  108. /// map <set name> -> <list of URI's to tracks belonging to the said set>
  109. std::map<std::string, std::vector<std::string>> musicsSet;
  110. /// stored position, in seconds at which music player should resume playing this track
  111. std::map<std::string, float> trackPositions;
  112. public:
  113. CMusicHandler();
  114. /// add entry with URI musicURI in set. Track will have ID musicID
  115. void addEntryToSet(const std::string & set, const std::string & musicURI);
  116. void init() override;
  117. void loadTerrainMusicThemes();
  118. void release() override;
  119. void setVolume(ui32 percent) override;
  120. /// play track by URI, if loop = true music will be looped
  121. void playMusic(const std::string & musicURI, bool loop, bool fromStart);
  122. /// play random track from this set
  123. void playMusicFromSet(const std::string & musicSet, bool loop, bool fromStart);
  124. /// play random track from set (musicSet, entryID)
  125. void playMusicFromSet(const std::string & musicSet, const std::string & entryID, bool loop, bool fromStart);
  126. /// stops currently playing music by fading out it over fade_ms and starts next scheduled track, if any
  127. void stopMusic(int fade_ms=1000);
  128. friend class MusicEntry;
  129. };