CMusicHandler.h 5.0 KB

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