CMusicHandler.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "CAudioBase.h"
  12. #include "IMusicPlayer.h"
  13. #include "../lib/CConfigHandler.h"
  14. #include <SDL_mixer.h>
  15. class CMusicHandler;
  16. //Class for handling one music file
  17. class MusicEntry : boost::noncopyable
  18. {
  19. CMusicHandler * owner;
  20. Mix_Music * music;
  21. //if not null - set from which music will be randomly selected
  22. std::string setName;
  23. AudioPath currentName;
  24. uint32_t startTime;
  25. uint32_t startPosition;
  26. int loop; // -1 = indefinite
  27. bool fromStart;
  28. bool playing;
  29. void load(const AudioPath & musicURI);
  30. public:
  31. MusicEntry(CMusicHandler * owner, std::string setName, const AudioPath & musicURI, bool looped, bool fromStart);
  32. ~MusicEntry();
  33. bool isSet(const std::string & setName);
  34. bool isTrack(const AudioPath & trackName);
  35. bool isPlaying() const;
  36. bool play();
  37. bool stop(int fade_ms = 0);
  38. };
  39. class CMusicHandler final : public CAudioBase, public IMusicPlayer
  40. {
  41. private:
  42. //update volume on configuration change
  43. SettingsListener listener;
  44. void onVolumeChange(const JsonNode & volumeNode);
  45. std::unique_ptr<MusicEntry> current;
  46. std::unique_ptr<MusicEntry> next;
  47. std::mutex mutex;
  48. int volume = 0; // from 0 (mute) to 100
  49. void queueNext(CMusicHandler * owner, const std::string & setName, const AudioPath & musicURI, bool looped, bool fromStart);
  50. void queueNext(std::unique_ptr<MusicEntry> queued);
  51. void musicFinishedCallback() final;
  52. /// map <set name> -> <list of URI's to tracks belonging to the said set>
  53. std::map<std::string, std::vector<AudioPath>> musicsSet;
  54. /// stored position, in seconds at which music player should resume playing this track
  55. std::map<AudioPath, float> trackPositions;
  56. public:
  57. CMusicHandler();
  58. ~CMusicHandler();
  59. /// add entry with URI musicURI in set. Track will have ID musicID
  60. void addEntryToSet(const std::string & set, const AudioPath & musicURI);
  61. void loadTerrainMusicThemes() final;
  62. void setVolume(ui32 percent) final;
  63. ui32 getVolume() const final;
  64. /// play track by URI, if loop = true music will be looped
  65. void playMusic(const AudioPath & musicURI, bool loop, bool fromStart) final;
  66. /// play random track from this set
  67. void playMusicFromSet(const std::string & musicSet, bool loop, bool fromStart) final;
  68. /// play random track from set (musicSet, entryID)
  69. void playMusicFromSet(const std::string & musicSet, const std::string & entryID, bool loop, bool fromStart) final;
  70. /// stops currently playing music by fading out it over fade_ms and starts next scheduled track, if any
  71. void stopMusic(int fade_ms) final;
  72. friend class MusicEntry;
  73. };