#pragma once #include #include #include #include #include #include "WallpaperEngine/Audio/AudioStream.h" #include "WallpaperEngine/Audio/Drivers/AudioDriver.h" #include "WallpaperEngine/Audio/Drivers/Detectors/HeadphonesDetector.h" #include "WallpaperEngine/Audio/Hrtf.h" #include namespace WallpaperEngine::Audio::Drivers { struct SDLAudioBuffer { AudioStream* stream = nullptr; /** Per-stream volume override (0-128), -1 = use the driver's global volume */ std::atomic volume { -1 }; std::atomic gainLeft { 1.0f }; std::atomic gainRight { 1.0f }; std::atomic paused { false }; /** stopStream: one more update fading to silence, then finished and freed by update () with the stream */ std::atomic stopping { false }; std::atomic finished { false }; std::unique_ptr owned; /** audio callback only: the file ran out and its stopping update was mixed, the voice stays silent from here */ bool over = false; /** audio callback only: OpenAL's voice state for the gain path, set after a voice's first update */ bool mixed = false; float currentLeft = 1.0f; float currentRight = 1.0f; /** the update a pause faded out, played again on resume (a stopping OpenAL voice keeps its position) */ bool pauseFaded = false; std::vector replay; std::mutex hrtfMutex; bool hrtfSet = false; StreamHrtf hrtf {}; /** audio callback only: the filters of this callback, OpenAL's voice state and the decoded input */ StreamHrtf hrtfMixing {}; std::array hrtfChannels {}; bool hrtfFading = false; int hrtfCounter = 0; std::vector hrtfPasses; unsigned hrtfGeneration = 0; std::array, 2> hrtfSamples {}; }; class SDLAudioDriver final : public AudioDriver { public: SDLAudioDriver ( Application::ApplicationContext& applicationContext, Detectors::AudioPlayingDetector& detector, Recorders::PlaybackRecorder& recorder ); ~SDLAudioDriver () override; int addStream (AudioStream* stream, int volume, float left, float right) override; void removeStream (int streamId) override; void setStreamVolume (int streamId, int volume) override; void setStreamGains (int streamId, float left, float right) override; void setStreamPaused (int streamId, bool paused) override; void stopStream (int streamId, std::unique_ptr stream) override; void setStreamHrtf (int streamId, const StreamHrtf* hrtf) override; [[nodiscard]] const Hrtf::Store* getHrtf () const override; void update (float dt) override; void render (float* out, int frames); [[nodiscard]] AVSampleFormat getFormat () const override; [[nodiscard]] int getSampleRate () const override; [[nodiscard]] int getChannels () const override; private: SDL_mutex* m_streamListMutex; int m_lastStreamID = 0; SDL_AudioDeviceID m_deviceID = 0; bool m_initialized = false; SDL_AudioSpec m_audioSpec {}; std::map m_streams {}; [[nodiscard]] Hrtf::Mixer* getHrtfMixer () const; /** Changes every time HRTF is turned on, voices and the mixer start over then */ [[nodiscard]] unsigned getHrtfGeneration () const; void mixUpdate (float* out, int frames); void mixHrtf (const std::vector& voices, float* out, int frames); // frames WE's OpenAL Soft mixes per update: its 20 ms default (882 at 44.1 kHz) as a multiple of the WASAPI period // at the device rate (sub_180186400) int m_updateFrames = 960; std::vector m_pending; /** audio callback only */ std::vector m_update; std::vector m_hrtfVoices; std::array, 2> m_hrtfOutput; unsigned m_hrtfMixerGeneration = 0; void releaseFinishedStreams (); void updateHrtf (); std::unique_ptr m_headphonesDetector; /** loaded the first time HRTF turns on and kept, the callback may still be using them */ std::unique_ptr m_hrtfStore; std::unique_ptr m_hrtfMixer; bool m_hrtfFailed = false; std::atomic m_hrtfEnabled { false }; std::atomic m_hrtfGeneration { 0 }; }; }