CMusicHandler.h 4.9 KB

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