CSoundHandler.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. //update volume on configuration change
  19. SettingsListener listener;
  20. void onVolumeChange(const JsonNode & volumeNode);
  21. using CachedChunk = std::pair<Mix_Chunk *, std::unique_ptr<ui8[]>>;
  22. std::map<AudioPath, CachedChunk> soundChunks;
  23. std::map<std::vector<ui8>, CachedChunk> soundChunksRaw;
  24. Mix_Chunk * GetSoundChunk(const AudioPath & sound, bool cache);
  25. Mix_Chunk * GetSoundChunk(std::pair<std::unique_ptr<ui8[]>, si64> & data, bool cache);
  26. /// have entry for every currently active channel
  27. /// vector will be empty if callback was not set
  28. std::map<int, std::vector<std::function<void()>>> callbacks;
  29. /// Protects access to callbacks member to avoid data races:
  30. /// SDL calls sound finished callbacks from audio thread
  31. boost::mutex mutexCallbacks;
  32. int ambientDistToVolume(int distance) const;
  33. void ambientStopSound(const AudioPath & soundId);
  34. void updateChannelVolume(int channel);
  35. const JsonNode ambientConfig;
  36. boost::mutex mutex;
  37. std::map<AudioPath, int> ambientChannels;
  38. std::map<int, int> channelVolumes;
  39. int volume = 0;
  40. void initCallback(int channel, const std::function<void()> & function);
  41. void initCallback(int channel);
  42. public:
  43. CSoundHandler();
  44. ~CSoundHandler();
  45. ui32 getVolume() const final;
  46. void setVolume(ui32 percent) final;
  47. void setChannelVolume(int channel, ui32 percent);
  48. // Sounds
  49. uint32_t getSoundDurationMilliseconds(const AudioPath & sound) final;
  50. int playSound(soundBase::soundID soundID, int repeats = 0) final;
  51. int playSound(const AudioPath & sound, int repeats = 0, bool cache = false) final;
  52. int playSound(std::pair<std::unique_ptr<ui8[]>, si64> & data, int repeats = 0, bool cache = false) final;
  53. int playSoundFromSet(std::vector<soundBase::soundID> & sound_vec) final;
  54. void stopSound(int handler) final;
  55. void pauseSound(int handler) final;
  56. void resumeSound(int handler) final;
  57. void setCallback(int channel, std::function<void()> function) final;
  58. void resetCallback(int channel) final;
  59. void soundFinishedCallback(int channel) final;
  60. int ambientGetRange() const final;
  61. void ambientUpdateChannels(std::map<AudioPath, int> currentSounds) final;
  62. void ambientStopAllChannels() final;
  63. };