SDLAudioDriver.h 4.2 KB

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