SDLAudioDriver.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. #include <map>
  3. #include <vector>
  4. #include "WallpaperEngine/Audio/AudioStream.h"
  5. #include "WallpaperEngine/Audio/Drivers/AudioDriver.h"
  6. #include <SDL.h>
  7. #define MAX_AUDIO_FRAME_SIZE 192000
  8. namespace WallpaperEngine::Audio::Drivers {
  9. /**
  10. * Audio output buffers for streams being played under SDL
  11. */
  12. struct SDLAudioBuffer {
  13. AudioStream* stream = nullptr;
  14. uint8_t audio_buf [(MAX_AUDIO_FRAME_SIZE * 3) / 2] = {0};
  15. unsigned int audio_buf_size = 0;
  16. unsigned int audio_buf_index = 0;
  17. };
  18. /**
  19. * SDL's audio driver implementation
  20. */
  21. class SDLAudioDriver final : public AudioDriver {
  22. public:
  23. SDLAudioDriver (
  24. Application::ApplicationContext& applicationContext, Detectors::AudioPlayingDetector& detector,
  25. Recorders::PlaybackRecorder& recorder);
  26. ~SDLAudioDriver () override;
  27. /** @inheritdoc */
  28. void addStream (AudioStream* stream) override;
  29. /**
  30. * @return All the registered audio streams
  31. */
  32. const std::vector<SDLAudioBuffer*>& getStreams ();
  33. /** @inheritdoc */
  34. [[nodiscard]] AVSampleFormat getFormat () const override;
  35. /** @inheritdoc */
  36. [[nodiscard]] int getSampleRate () const override;
  37. /** @inheritdoc */
  38. [[nodiscard]] int getChannels () const override;
  39. /**
  40. * @return The SDL's audio driver settings
  41. */
  42. [[nodiscard]] const SDL_AudioSpec& getSpec () const;
  43. private:
  44. /** The device's ID */
  45. SDL_AudioDeviceID m_deviceID;
  46. /** If the driver is initialized or not */
  47. bool m_initialized = false;
  48. /** The sound output configuration */
  49. SDL_AudioSpec m_audioSpec {};
  50. /** All the playable steams */
  51. std::vector<SDLAudioBuffer*> m_streams {};
  52. };
  53. } // namespace WallpaperEngine::Audio::Drivers