PlaybackRecorder.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #pragma once
  2. #include <functional>
  3. #include <map>
  4. #include <mutex>
  5. #include "WallpaperEngine/Audio/SpectrumProcessor.h"
  6. namespace WallpaperEngine::Audio::Drivers::Recorders {
  7. class PlaybackRecorder {
  8. // declared first, audio16/32/64 below point into it
  9. Audio::SpectrumProcessor m_processor;
  10. public:
  11. /** Called with the 64 band spectrum every time a fresh one has been computed, from the recorder's capture thread */
  12. using SpectrumListener = std::function<void (const float* audio64)>;
  13. virtual ~PlaybackRecorder () = default;
  14. /**
  15. * Runs once per rendered frame, turns the latest captured spectrum into audio16/32/64
  16. *
  17. * @param dt Seconds since the previous frame, scaled by the playback speed
  18. */
  19. virtual void update (float dt);
  20. /** Guards the captured spectrum for recorders that fill it from a background capture thread; no-op by default */
  21. virtual void lock () const { }
  22. virtual void unlock () const { }
  23. /**
  24. * Registers a callback to be pushed every new spectrum instead of polling audio64 once per rendered frame,
  25. * which at low --fps values adds up to a whole frame of latency. Does nothing for recorders that don't capture.
  26. *
  27. * @return An id for removeSpectrumListener. After that call returns the callback is guaranteed not to be running
  28. */
  29. int addSpectrumListener (SpectrumListener listener);
  30. void removeSpectrumListener (int id);
  31. // [left | right | average], only written from update() on the render thread
  32. const float* audio16 = m_processor.audio16;
  33. const float* audio32 = m_processor.audio32;
  34. const float* audio64 = m_processor.audio64;
  35. protected:
  36. void notifySpectrumListeners (const float* audio64);
  37. /** [left 64 | right 64] linear band levels from the capture thread, guarded by lock()/unlock() */
  38. float m_captured[128] = { 0 };
  39. private:
  40. std::mutex m_listenersMutex;
  41. std::map<int, SpectrumListener> m_listeners;
  42. int m_nextListenerId = 0;
  43. };
  44. } // namespace WallpaperEngine::Audio::Drivers::Recorders