OpenALHardwareDeviceDriver.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. lock (_lock)
  117. {
  118. _stillRunning = false;
  119. _updaterThread.Join();
  120. // Loop against all sessions to dispose them (they will unregister themself)
  121. while (_sessions.Count > 0)
  122. {
  123. OpenALHardwareDeviceSession session = _sessions[0];
  124. session.Dispose();
  125. }
  126. }
  127. ALC.DestroyContext(_context);
  128. ALC.CloseDevice(_device);
  129. }
  130. }
  131. public bool SupportsSampleRate(uint sampleRate)
  132. {
  133. return true;
  134. }
  135. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  136. {
  137. return true;
  138. }
  139. public bool SupportsChannelCount(uint channelCount)
  140. {
  141. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  142. }
  143. public bool SupportsDirection(Direction direction)
  144. {
  145. return direction == Direction.Output;
  146. }
  147. }
  148. }