OpenALHardwareDeviceDriver.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using OpenTK.Audio.OpenAL;
  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.Linq;
  8. using System.Threading;
  9. using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
  10. namespace Ryujinx.Audio.Backends.OpenAL
  11. {
  12. public class OpenALHardwareDeviceDriver : IHardwareDeviceDriver
  13. {
  14. private object _lock = new object();
  15. private ALDevice _device;
  16. private ALContext _context;
  17. private ManualResetEvent _updateRequiredEvent;
  18. private List<OpenALHardwareDeviceSession> _sessions;
  19. private bool _stillRunning;
  20. private Thread _updaterThread;
  21. public OpenALHardwareDeviceDriver()
  22. {
  23. _device = ALC.OpenDevice("");
  24. _context = ALC.CreateContext(_device, new ALContextAttributes());
  25. _updateRequiredEvent = new ManualResetEvent(false);
  26. _sessions = new List<OpenALHardwareDeviceSession>();
  27. _stillRunning = true;
  28. _updaterThread = new Thread(Update)
  29. {
  30. Name = "HardwareDeviceDriver.OpenAL"
  31. };
  32. _updaterThread.Start();
  33. }
  34. public static bool IsSupported
  35. {
  36. get
  37. {
  38. try
  39. {
  40. return ALC.GetStringList(GetEnumerationStringList.DeviceSpecifier).Any();
  41. }
  42. catch
  43. {
  44. return false;
  45. }
  46. }
  47. }
  48. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount)
  49. {
  50. if (channelCount == 0)
  51. {
  52. channelCount = 2;
  53. }
  54. if (sampleRate == 0)
  55. {
  56. sampleRate = Constants.TargetSampleRate;
  57. }
  58. if (direction != Direction.Output)
  59. {
  60. throw new ArgumentException($"{direction}");
  61. }
  62. else if (!SupportsChannelCount(channelCount))
  63. {
  64. throw new ArgumentException($"{channelCount}");
  65. }
  66. lock (_lock)
  67. {
  68. OpenALHardwareDeviceSession session = new OpenALHardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount);
  69. _sessions.Add(session);
  70. return session;
  71. }
  72. }
  73. internal void Unregister(OpenALHardwareDeviceSession session)
  74. {
  75. lock (_lock)
  76. {
  77. _sessions.Remove(session);
  78. }
  79. }
  80. public ManualResetEvent GetUpdateRequiredEvent()
  81. {
  82. return _updateRequiredEvent;
  83. }
  84. private void Update()
  85. {
  86. ALC.MakeContextCurrent(_context);
  87. while (_stillRunning)
  88. {
  89. bool updateRequired = false;
  90. lock (_lock)
  91. {
  92. foreach (OpenALHardwareDeviceSession session in _sessions)
  93. {
  94. if (session.Update())
  95. {
  96. updateRequired = true;
  97. }
  98. }
  99. }
  100. if (updateRequired)
  101. {
  102. _updateRequiredEvent.Set();
  103. }
  104. // If it's not slept it will waste cycles.
  105. Thread.Sleep(10);
  106. }
  107. }
  108. public void Dispose()
  109. {
  110. Dispose(true);
  111. }
  112. protected virtual void Dispose(bool disposing)
  113. {
  114. if (disposing)
  115. {
  116. _stillRunning = false;
  117. int sessionCount = 0;
  118. // NOTE: This is done in a way to avoid possible situations when the OpenALHardwareDeviceSession is already being dispose in another thread but doesn't hold the lock and tries to Unregister.
  119. do
  120. {
  121. lock (_lock)
  122. {
  123. if (_sessions.Count == 0)
  124. {
  125. break;
  126. }
  127. OpenALHardwareDeviceSession session = _sessions[_sessions.Count - 1];
  128. session.Dispose();
  129. sessionCount = _sessions.Count;
  130. }
  131. }
  132. while (sessionCount > 0);
  133. ALC.DestroyContext(_context);
  134. ALC.CloseDevice(_device);
  135. }
  136. }
  137. public bool SupportsSampleRate(uint sampleRate)
  138. {
  139. return true;
  140. }
  141. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  142. {
  143. return true;
  144. }
  145. public bool SupportsChannelCount(uint channelCount)
  146. {
  147. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  148. }
  149. public bool SupportsDirection(Direction direction)
  150. {
  151. return direction == Direction.Output;
  152. }
  153. }
  154. }