CMusicHandler.h 2.7 KB

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