SDLAudioDriver.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <atomic>
  3. #include <map>
  4. #include <memory>
  5. #include <mutex>
  6. #include <vector>
  7. #include "WallpaperEngine/Audio/AudioStream.h"
  8. #include "WallpaperEngine/Audio/Drivers/AudioDriver.h"
  9. #include "WallpaperEngine/Audio/Drivers/Detectors/HeadphonesDetector.h"
  10. #include "WallpaperEngine/Audio/Hrtf.h"
  11. #include "WallpaperEngine/Debugging/ThreadSafety.h"
  12. #include <SDL.h>
  13. namespace WallpaperEngine::Audio::Drivers {
  14. struct SDLAudioBuffer {
  15. AudioStream* stream = nullptr;
  16. /** Per-stream volume override (0-128), -1 = use the driver's global volume */
  17. std::atomic<int> volume { -1 };
  18. std::atomic<float> gainLeft { 1.0f };
  19. std::atomic<float> gainRight { 1.0f };
  20. std::atomic<bool> paused { false };
  21. /** stopStream: one more update fading to silence, then finished and freed by update () with the stream */
  22. std::atomic<bool> stopping { false };
  23. std::atomic<bool> finished { false };
  24. std::unique_ptr<AudioStream> owned;
  25. /** audio callback only: the file ran out and its stopping update was mixed, the voice stays silent from here */
  26. bool over = false;
  27. /** audio callback only: OpenAL's voice state for the gain path, set after a voice's first update */
  28. bool mixed = false;
  29. float currentLeft = 1.0f;
  30. float currentRight = 1.0f;
  31. /** the update a pause faded out, played again on resume (a stopping OpenAL voice keeps its position) */
  32. bool pauseFaded = false;
  33. std::vector<float> replay;
  34. Debugging::Mutex hrtfMutex;
  35. bool hrtfSet LWE_GUARDED_BY (hrtfMutex) = false;
  36. StreamHrtf hrtf LWE_GUARDED_BY (hrtfMutex) {};
  37. /** audio callback only: the filters of this callback, OpenAL's voice state and the decoded input */
  38. StreamHrtf hrtfMixing {};
  39. std::array<Hrtf::Channel, 2> hrtfChannels {};
  40. bool hrtfFading = false;
  41. int hrtfCounter = 0;
  42. std::vector<int> hrtfPasses;
  43. unsigned hrtfGeneration = 0;
  44. std::array<std::vector<float>, 2> hrtfSamples {};
  45. };
  46. class SDLAudioDriver final : public AudioDriver {
  47. public:
  48. SDLAudioDriver (
  49. Application::ApplicationContext& applicationContext, Detectors::AudioPlayingDetector& detector,
  50. Recorders::PlaybackRecorder& recorder
  51. );
  52. ~SDLAudioDriver () override;
  53. int addStream (AudioStream* stream, int volume, float left, float right) override;
  54. void removeStream (int streamId) override;
  55. void setStreamVolume (int streamId, int volume) override;
  56. void setStreamGains (int streamId, float left, float right) override;
  57. void setStreamPaused (int streamId, bool paused) override;
  58. void stopStream (int streamId, std::unique_ptr<AudioStream> stream) override;
  59. void setStreamHrtf (int streamId, const StreamHrtf* hrtf) override;
  60. [[nodiscard]] const Hrtf::Store* getHrtf () const override;
  61. void update (float dt) override;
  62. void render (float* out, int frames);
  63. [[nodiscard]] AVSampleFormat getFormat () const override;
  64. [[nodiscard]] int getSampleRate () const override;
  65. [[nodiscard]] int getChannels () const override;
  66. private:
  67. SDL_mutex* m_streamListMutex;
  68. int m_lastStreamID = 0;
  69. SDL_AudioDeviceID m_deviceID = 0;
  70. bool m_initialized = false;
  71. SDL_AudioSpec m_audioSpec {};
  72. std::map<int, SDLAudioBuffer*> m_streams {};
  73. [[nodiscard]] Hrtf::Mixer* getHrtfMixer () const;
  74. /** Changes every time HRTF is turned on, voices and the mixer start over then */
  75. [[nodiscard]] unsigned getHrtfGeneration () const;
  76. void mixUpdate (float* out, int frames);
  77. void mixHrtf (const std::vector<SDLAudioBuffer*>& voices, float* out, int frames);
  78. // frames WE's OpenAL Soft mixes per update: its 20 ms default (882 at 44.1 kHz) as a multiple of the WASAPI period
  79. // at the device rate (sub_180186400)
  80. int m_updateFrames = 960;
  81. std::vector<float> m_pending;
  82. /** audio callback only */
  83. std::vector<float> m_update;
  84. std::vector<SDLAudioBuffer*> m_hrtfVoices;
  85. std::array<std::vector<float>, 2> m_hrtfOutput;
  86. unsigned m_hrtfMixerGeneration = 0;
  87. void releaseFinishedStreams ();
  88. void updateHrtf ();
  89. std::unique_ptr<Detectors::HeadphonesDetector> m_headphonesDetector;
  90. /** loaded the first time HRTF turns on and kept, the callback may still be using them */
  91. std::unique_ptr<Hrtf::Store> m_hrtfStore;
  92. std::unique_ptr<Hrtf::Mixer> m_hrtfMixer;
  93. bool m_hrtfFailed = false;
  94. std::atomic<bool> m_hrtfEnabled { false };
  95. std::atomic<unsigned> m_hrtfGeneration { 0 };
  96. };
  97. }