OpenALHardwareDeviceDriver.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using OpenTK.Audio;
  2. using Ryujinx.Audio.Common;
  3. using Ryujinx.Audio.Integration;
  4. using Ryujinx.Memory;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
  9. namespace Ryujinx.Audio.Backends.OpenAL
  10. {
  11. public class OpenALHardwareDeviceDriver : IHardwareDeviceDriver
  12. {
  13. private object _lock = new object();
  14. private AudioContext _context;
  15. private ManualResetEvent _updateRequiredEvent;
  16. private List<OpenALHardwareDeviceSession> _sessions;
  17. private bool _stillRunning;
  18. private Thread _updaterThread;
  19. public OpenALHardwareDeviceDriver()
  20. {
  21. _context = new AudioContext();
  22. _updateRequiredEvent = new ManualResetEvent(false);
  23. _sessions = new List<OpenALHardwareDeviceSession>();
  24. _stillRunning = true;
  25. _updaterThread = new Thread(Update)
  26. {
  27. Name = "HardwareDeviceDriver.OpenAL"
  28. };
  29. _updaterThread.Start();
  30. }
  31. public static bool IsSupported
  32. {
  33. get
  34. {
  35. try
  36. {
  37. return AudioContext.AvailableDevices.Count > 0;
  38. }
  39. catch
  40. {
  41. return false;
  42. }
  43. }
  44. }
  45. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount)
  46. {
  47. if (channelCount == 0)
  48. {
  49. channelCount = 2;
  50. }
  51. if (sampleRate == 0)
  52. {
  53. sampleRate = Constants.TargetSampleRate;
  54. }
  55. if (direction != Direction.Output)
  56. {
  57. throw new ArgumentException($"{direction}");
  58. }
  59. else if (!SupportsChannelCount(channelCount))
  60. {
  61. throw new ArgumentException($"{channelCount}");
  62. }
  63. lock (_lock)
  64. {
  65. OpenALHardwareDeviceSession session = new OpenALHardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount);
  66. _sessions.Add(session);
  67. return session;
  68. }
  69. }
  70. internal void Unregister(OpenALHardwareDeviceSession session)
  71. {
  72. lock (_lock)
  73. {
  74. _sessions.Remove(session);
  75. }
  76. }
  77. public ManualResetEvent GetUpdateRequiredEvent()
  78. {
  79. return _updateRequiredEvent;
  80. }
  81. private void Update()
  82. {
  83. while (_stillRunning)
  84. {
  85. bool updateRequired = false;
  86. lock (_lock)
  87. {
  88. foreach (OpenALHardwareDeviceSession session in _sessions)
  89. {
  90. if (session.Update())
  91. {
  92. updateRequired = true;
  93. }
  94. }
  95. }
  96. if (updateRequired)
  97. {
  98. _updateRequiredEvent.Set();
  99. }
  100. // If it's not slept it will waste cycles.
  101. Thread.Sleep(10);
  102. }
  103. }
  104. public void Dispose()
  105. {
  106. Dispose(true);
  107. }
  108. protected virtual void Dispose(bool disposing)
  109. {
  110. if (disposing)
  111. {
  112. lock (_lock)
  113. {
  114. _stillRunning = false;
  115. _updaterThread.Join();
  116. // Loop against all sessions to dispose them (they will unregister themself)
  117. while (_sessions.Count > 0)
  118. {
  119. OpenALHardwareDeviceSession session = _sessions[0];
  120. session.Dispose();
  121. }
  122. }
  123. _context.Dispose();
  124. }
  125. }
  126. public bool SupportsSampleRate(uint sampleRate)
  127. {
  128. return true;
  129. }
  130. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  131. {
  132. return true;
  133. }
  134. public bool SupportsChannelCount(uint channelCount)
  135. {
  136. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  137. }
  138. public bool SupportsDirection(Direction direction)
  139. {
  140. return direction == Direction.Output;
  141. }
  142. }
  143. }