audio_manager.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <atomic>
  6. #include <functional>
  7. #include <mutex>
  8. #include <thread>
  9. #include "common/polyfill_thread.h"
  10. #include "audio_core/audio_event.h"
  11. union Result;
  12. namespace AudioCore {
  13. /**
  14. * The AudioManager's main purpose is to wait for buffer events for the audio in and out managers,
  15. * and call an associated callback to release buffers.
  16. *
  17. * Execution pattern is:
  18. * Buffers appended ->
  19. * Buffers queued and played by the backend stream ->
  20. * When consumed, set the corresponding manager event and signal the audio manager ->
  21. * Consumed buffers are released, game is signalled ->
  22. * Game appends more buffers.
  23. *
  24. * This is only used by audio in and audio out.
  25. */
  26. class AudioManager {
  27. using BufferEventFunc = std::function<void()>;
  28. public:
  29. explicit AudioManager();
  30. /**
  31. * Shutdown the audio manager.
  32. */
  33. void Shutdown();
  34. /**
  35. * Register the out manager, keeping a function to be called when the out event is signalled.
  36. *
  37. * @param buffer_func - Function to be called on signal.
  38. * @return Result code.
  39. */
  40. Result SetOutManager(BufferEventFunc buffer_func);
  41. /**
  42. * Register the in manager, keeping a function to be called when the in event is signalled.
  43. *
  44. * @param buffer_func - Function to be called on signal.
  45. * @return Result code.
  46. */
  47. Result SetInManager(BufferEventFunc buffer_func);
  48. /**
  49. * Set an event to signalled, and signal the thread.
  50. *
  51. * @param type - Manager type to set.
  52. * @param signalled - Set the event to true or false?
  53. */
  54. void SetEvent(Event::Type type, bool signalled);
  55. private:
  56. /**
  57. * Main thread, waiting on a manager signal and calling the registered function.
  58. */
  59. void ThreadFunc();
  60. /// Is the main thread running?
  61. std::atomic<bool> running{};
  62. /// Unused
  63. bool needs_update{};
  64. /// Events to be set and signalled
  65. Event events{};
  66. /// Callbacks for each manager
  67. std::array<BufferEventFunc, 3> buffer_events{};
  68. /// General lock
  69. std::mutex lock{};
  70. /// Main thread for waiting and callbacks
  71. std::jthread thread;
  72. };
  73. } // namespace AudioCore