audio_event.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <chrono>
  7. #include <condition_variable>
  8. #include <mutex>
  9. namespace AudioCore {
  10. /**
  11. * Responsible for the input/output events, set by the stream backend when buffers are consumed, and
  12. * waited on by the audio manager. These callbacks signal the game's events to keep the audio buffer
  13. * recycling going.
  14. * In a real Switch this is not a separate class, and exists entirely within the audio manager.
  15. * On the Switch it's implemented more simply through a MultiWaitEventHolder, where it can
  16. * wait on multiple events at once, and the events are not needed by the backend.
  17. */
  18. class Event {
  19. public:
  20. enum class Type {
  21. AudioInManager,
  22. AudioOutManager,
  23. FinalOutputRecorderManager,
  24. Max,
  25. };
  26. /**
  27. * Convert a manager type to an index.
  28. *
  29. * @param type - The manager type to convert
  30. * @return The index of the type.
  31. */
  32. size_t GetManagerIndex(Type type) const;
  33. /**
  34. * Set an audio event to true or false.
  35. *
  36. * @param type - The manager type to signal.
  37. * @param signalled - Its signal state.
  38. */
  39. void SetAudioEvent(Type type, bool signalled);
  40. /**
  41. * Check if the given manager type is signalled.
  42. *
  43. * @param type - The manager type to check.
  44. * @return True if the event is signalled, otherwise false.
  45. */
  46. bool CheckAudioEventSet(Type type) const;
  47. /**
  48. * Get the lock for audio events.
  49. *
  50. * @return Reference to the lock.
  51. */
  52. std::mutex& GetAudioEventLock();
  53. /**
  54. * Get the manager event, this signals the audio manager to release buffers and signal the game
  55. * for more.
  56. *
  57. * @return Reference to the condition variable.
  58. */
  59. std::condition_variable_any& GetAudioEvent();
  60. /**
  61. * Wait on the manager_event.
  62. *
  63. * @param l - Lock held by the wait.
  64. * @param timeout - Timeout for the wait. This is 2 seconds by default.
  65. * @return True if the wait timed out, otherwise false if signalled.
  66. */
  67. bool Wait(std::unique_lock<std::mutex>& l, std::chrono::seconds timeout);
  68. /**
  69. * Reset all manager events.
  70. */
  71. void ClearEvents();
  72. private:
  73. /// Lock, used by the audio manager
  74. std::mutex event_lock;
  75. /// Array of events, one per system type (see Type), last event is used to terminate
  76. std::array<std::atomic<bool>, 4> events_signalled;
  77. /// Event to signal the audio manager
  78. std::condition_variable_any manager_event;
  79. };
  80. } // namespace AudioCore