CSoundHandler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * CSoundHandler.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 "ISoundPlayer.h"
  13. #include "../lib/CConfigHandler.h"
  14. struct Mix_Chunk;
  15. class CSoundHandler final : public CAudioBase, public ISoundPlayer
  16. {
  17. private:
  18. struct MixChunkDeleter
  19. {
  20. void operator ()(Mix_Chunk *);
  21. };
  22. using MixChunkPtr = std::unique_ptr<Mix_Chunk, MixChunkDeleter>;
  23. //update volume on configuration change
  24. SettingsListener listener;
  25. void onVolumeChange(const JsonNode & volumeNode);
  26. using CachedChunk = std::pair<MixChunkPtr, std::unique_ptr<ui8[]>>;
  27. /// List of all permanently cached sound chunks
  28. std::map<AudioPath, CachedChunk> soundChunks;
  29. /// List of all currently playing chunks that are currently playing
  30. /// and should be deallocated once channel playback is over
  31. /// indexed by channel ID
  32. std::map<int, MixChunkPtr> uncachedPlayingChunks;
  33. MixChunkPtr getSoundChunk(const AudioPath & sound);
  34. Mix_Chunk * getSoundChunkCached(const AudioPath & sound);
  35. MixChunkPtr getSoundChunk(std::pair<std::unique_ptr<ui8[]>, si64> & data);
  36. /// have entry for every currently active channel
  37. /// vector will be empty if callback was not set
  38. std::map<int, std::vector<std::function<void()>>> callbacks;
  39. /// Protects access to callbacks member to avoid data races:
  40. /// SDL calls sound finished callbacks from audio thread
  41. std::mutex mutexCallbacks;
  42. int ambientDistToVolume(int distance) const;
  43. void ambientStopSound(const AudioPath & soundId);
  44. void updateChannelVolume(int channel);
  45. const JsonNode ambientConfig;
  46. std::mutex mutex;
  47. std::map<AudioPath, int> ambientChannels;
  48. std::map<int, int> channelVolumes;
  49. int volume = 0;
  50. void initCallback(int channel, const std::function<void()> & function);
  51. void initCallback(int channel);
  52. void storeChunk(int channel, MixChunkPtr chunk);
  53. int playSoundImpl(const AudioPath & sound, int repeats, bool useCache);
  54. public:
  55. CSoundHandler();
  56. ~CSoundHandler();
  57. ui32 getVolume() const final;
  58. void setVolume(ui32 percent) final;
  59. void setChannelVolume(int channel, ui32 percent);
  60. // Sounds
  61. uint32_t getSoundDurationMilliseconds(const AudioPath & sound) final;
  62. int playSound(soundBase::soundID soundID) final;
  63. int playSound(const AudioPath & sound) final;
  64. int playSoundLooped(const AudioPath & sound) final;
  65. int playSound(std::pair<std::unique_ptr<ui8[]>, si64> & data) final;
  66. int playSoundFromSet(std::vector<soundBase::soundID> & sound_vec) final;
  67. void stopSound(int handler) final;
  68. void pauseSound(int handler) final;
  69. void resumeSound(int handler) final;
  70. void setCallback(int channel, std::function<void()> function) final;
  71. void resetCallback(int channel) final;
  72. void soundFinishedCallback(int channel) final;
  73. int ambientGetRange() const final;
  74. void ambientUpdateChannels(std::map<AudioPath, int> currentSounds) final;
  75. void ambientStopAllChannels() final;
  76. };