CSound.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <memory>
  3. #include <optional>
  4. #include <glm/vec2.hpp>
  5. #include <glm/vec3.hpp>
  6. #include "WallpaperEngine/Audio/AudioStream.h"
  7. #include "WallpaperEngine/Audio/Drivers/AudioDriver.h"
  8. #include "WallpaperEngine/Scripting/ScriptableObject.h"
  9. using namespace WallpaperEngine;
  10. namespace WallpaperEngine::Render::Wallpapers {
  11. class CScene;
  12. }
  13. namespace WallpaperEngine::Render::Objects {
  14. using namespace WallpaperEngine::Data::Model;
  15. // WE's sound object (2.8.42, off_140490AE8): one voice per file, play() stops all and starts a random one; loop moves
  16. // on to another random file when one ends, random waits mintime..maxtime between files, single plays once. OpenAL
  17. // Soft 1.21.1 panning and attenuation reproduced for mono files
  18. class CSound final : public Scripting::ScriptableObject {
  19. public:
  20. CSound (Wallpapers::CScene& scene, const Sound& sound);
  21. ~CSound () override;
  22. /** Timers, volume changes and the spatial position, every frame for visible and hidden sounds (sub_1401F4F50) */
  23. void update (float dt);
  24. /** Overrides the volume (0-128) this sound's streams mix at instead of the global volume; nullopt = global */
  25. void setVolumeOverride (std::optional<int> volume);
  26. /** Script-facing playback control, thisScene.getLayer(<sound name>).play()/stop()/pause()/isPlaying() */
  27. void play ();
  28. void stop ();
  29. void pause ();
  30. [[nodiscard]] bool isPlaying () const override;
  31. void applyPlayback (Playback playback) override;
  32. private:
  33. enum class VoiceState { Stopped, Paused, Playing };
  34. struct SourceParams {
  35. float gain;
  36. glm::vec3 direction;
  37. float distance;
  38. bool operator== (const SourceParams&) const = default;
  39. };
  40. /** What applyOutput last handed the driver, so an unchanged voice skips the HRTF coefficients and the lock */
  41. struct AppliedOutput {
  42. int streamId = -1;
  43. const Audio::Hrtf::Store* hrtf = nullptr;
  44. SourceParams source {};
  45. };
  46. struct Voice {
  47. std::string file;
  48. std::unique_ptr<Audio::AudioStream> stream;
  49. int streamId = -1;
  50. float duration = 0.0f;
  51. bool mono = false;
  52. VoiceState state = VoiceState::Stopped;
  53. mutable AppliedOutput applied;
  54. };
  55. void loadVoices ();
  56. void playVoice (Voice& voice, bool loop);
  57. void pauseVoice (Voice& voice);
  58. void stopVoice (Voice& voice);
  59. void stopAllVoices ();
  60. /** Random voice, like WE's rand () / 32767 * count clamped to the last one */
  61. [[nodiscard]] Voice& pickVoice ();
  62. [[nodiscard]] bool hasActiveVoice () const;
  63. void volumeChanged ();
  64. /** WE's master volume (renderer +728), 0-1 */
  65. [[nodiscard]] float masterVolume () const;
  66. /** AL_GAIN WE gives every voice: volume squared times the master volume */
  67. [[nodiscard]] float sourceGain () const;
  68. /** Where WE puts the sound in OpenAL's listener space (sub_1401F5460) */
  69. [[nodiscard]] glm::vec3 spatialPosition () const;
  70. /** What OpenAL's source update works out for a voice: the clamped gain, and for mono files direction and distance
  71. */
  72. [[nodiscard]] SourceParams sourceParams (const Voice& voice) const;
  73. /** Left and right gain of a voice: AL_GAIN, and for mono files OpenAL's distance attenuation and panning */
  74. [[nodiscard]] glm::vec2 outputGains (const Voice& voice) const;
  75. /** The voice's HRTF filters while the output renders for headphones (OpenAL's "full" HRTF mode) */
  76. [[nodiscard]] Audio::Drivers::StreamHrtf hrtfOutput (const Voice& voice, const Audio::Hrtf::Store& hrtf) const;
  77. void applyOutput (const Voice& voice) const;
  78. void applyOutput () const;
  79. std::vector<Voice> m_voices = {};
  80. const Sound& m_sound;
  81. /** Screen-level mute/ambient-volume policy from CScene::setAudioPolicy; nullopt = no override */
  82. std::optional<int> m_screenVolumeOverride;
  83. /** +764: countdown to the next play () (next file in loop mode, next play after the random delay) */
  84. float m_timer = 0.0f;
  85. /** +768: time left of the file that was started last */
  86. float m_remaining = 0.0f;
  87. /** +784 flags 0x40000000, 0x80000000 and 2 */
  88. bool m_paused = false;
  89. bool m_stopped = true;
  90. bool m_startSilent = false;
  91. float m_lastVolume = 0.0f;
  92. float m_lastMaster = 0.0f;
  93. /** startsilent changes rewrite the flag even after play () cleared it (sub_14019B6B0) */
  94. bool m_lastStartSilent = false;
  95. /** OpenAL source position, the origin (the listener) until spatialization moves it */
  96. glm::vec3 m_position = glm::vec3 (0.0f);
  97. /** only set by play () on spatialized sounds */
  98. float m_reference = 1.0f;
  99. float m_rolloff = 1.0f;
  100. };
  101. } // namespace WallpaperEngine::Render::Objects