AudioDriver.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include <vector>
  3. #include "WallpaperEngine/Application/ApplicationContext.h"
  4. #include "WallpaperEngine/Audio/AudioStream.h"
  5. #include "WallpaperEngine/Audio/Drivers/Detectors/AudioPlayingDetector.h"
  6. #include "WallpaperEngine/Audio/Drivers/Recorders/PlaybackRecorder.h"
  7. #include "WallpaperEngine/Audio/Hrtf.h"
  8. namespace WallpaperEngine {
  9. namespace Application {
  10. class ApplicationContext;
  11. }
  12. namespace Audio {
  13. class AudioStream;
  14. namespace Drivers {
  15. namespace Detectors {
  16. class AudioPlayingDetector;
  17. }
  18. namespace Recorders {
  19. class PulseAudioPlaybackRecorder;
  20. }
  21. /** HRTF filters of a stream, one per input channel it uses: a mono file's first channel, a stereo file's two */
  22. struct StreamHrtf {
  23. int channels = 1;
  24. std::array<Hrtf::Filter, 2> filters {};
  25. };
  26. class AudioDriver {
  27. public:
  28. explicit AudioDriver (
  29. Application::ApplicationContext& applicationContext, Detectors::AudioPlayingDetector& detector,
  30. Recorders::PlaybackRecorder& recorder
  31. );
  32. virtual ~AudioDriver () = default;
  33. /**
  34. * Registers the given stream in the driver for playing
  35. *
  36. * @param volume Initial volume override (0-128), -1 = the global volume
  37. * @param left Initial gain of the left channel
  38. * @param right Initial gain of the right channel
  39. */
  40. virtual int addStream (AudioStream* stream, int volume = -1, float left = 1.0f, float right = 1.0f) = 0;
  41. virtual void removeStream (int streamId) = 0;
  42. /**
  43. * Overrides the volume used to mix a single stream, instead of the driver's global volume
  44. *
  45. * @param streamId The stream to change
  46. * @param volume 0-128, or a negative value to go back to using the global volume
  47. */
  48. virtual void setStreamVolume (int streamId, int volume) = 0;
  49. /**
  50. * Scales each output channel of a stream, on top of its volume
  51. *
  52. * @param left Gain of the left channel
  53. * @param right Gain of the right channel
  54. */
  55. virtual void setStreamGains (int streamId, float left, float right) = 0;
  56. /**
  57. * A paused stream keeps its position and isn't decoded until it is resumed
  58. */
  59. virtual void setStreamPaused (int streamId, bool paused) = 0;
  60. /**
  61. * Stops a stream the way OpenAL stops a source: one more update without new audio (only an HRTF voice's
  62. * delayed samples fade out), then it is removed; the driver keeps the stream alive until then
  63. */
  64. virtual void stopStream (int streamId, std::unique_ptr<AudioStream> stream) {
  65. this->removeStream (streamId);
  66. }
  67. /**
  68. * Mixes the stream through the HRTF renderer with these filters while HRTF is on, nullptr goes back to the
  69. * channel gains
  70. */
  71. virtual void setStreamHrtf (int streamId, const StreamHrtf* hrtf) { }
  72. /**
  73. * @return The HRTF data set at the output rate while headphones rendering is on, nullptr otherwise
  74. */
  75. [[nodiscard]] virtual const Hrtf::Store* getHrtf () const { return nullptr; }
  76. /**
  77. * Updates status of the different audio settings
  78. *
  79. * @param dt Seconds since the previous frame, scaled by the playback speed
  80. */
  81. virtual void update (float dt);
  82. /**
  83. * TODO: MAYBE THIS SHOULD BE OUR OWN DEFINITIONS INSTEAD OF LIBRARY SPECIFIC ONES?
  84. *
  85. * @return The audio format the driver supports
  86. */
  87. [[nodiscard]] virtual AVSampleFormat getFormat () const = 0;
  88. [[nodiscard]] virtual int getSampleRate () const = 0;
  89. [[nodiscard]] virtual int getChannels () const = 0;
  90. Application::ApplicationContext& getApplicationContext () const;
  91. /**
  92. * @return The audio playing detector to use to stop playing sound when something else starts playing
  93. */
  94. [[nodiscard]] Detectors::AudioPlayingDetector& getAudioDetector () const;
  95. [[nodiscard]] Recorders::PlaybackRecorder& getRecorder () const;
  96. void setRecorder (Recorders::PlaybackRecorder& recorder);
  97. private:
  98. Application::ApplicationContext& m_applicationContext;
  99. Detectors::AudioPlayingDetector& m_detector;
  100. Recorders::PlaybackRecorder* m_recorder;
  101. };
  102. } // namespace Drivers
  103. } // namespace Audio
  104. } // namespace WallpaperEngine