OpenALHardwareDeviceDriver.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.Concurrent;
  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 readonly ALDevice _device;
  15. private readonly ALContext _context;
  16. private readonly ManualResetEvent _updateRequiredEvent;
  17. private readonly ManualResetEvent _pauseEvent;
  18. private readonly ConcurrentDictionary<OpenALHardwareDeviceSession, byte> _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. _pauseEvent = new ManualResetEvent(true);
  27. _sessions = new ConcurrentDictionary<OpenALHardwareDeviceSession, byte>();
  28. _stillRunning = true;
  29. _updaterThread = new Thread(Update)
  30. {
  31. Name = "HardwareDeviceDriver.OpenAL"
  32. };
  33. _updaterThread.Start();
  34. }
  35. public static bool IsSupported
  36. {
  37. get
  38. {
  39. try
  40. {
  41. return ALC.GetStringList(GetEnumerationStringList.DeviceSpecifier).Any();
  42. }
  43. catch
  44. {
  45. return false;
  46. }
  47. }
  48. }
  49. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount, float volume)
  50. {
  51. if (channelCount == 0)
  52. {
  53. channelCount = 2;
  54. }
  55. if (sampleRate == 0)
  56. {
  57. sampleRate = Constants.TargetSampleRate;
  58. }
  59. if (direction != Direction.Output)
  60. {
  61. throw new ArgumentException($"{direction}");
  62. }
  63. else if (!SupportsChannelCount(channelCount))
  64. {
  65. throw new ArgumentException($"{channelCount}");
  66. }
  67. OpenALHardwareDeviceSession session = new OpenALHardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount, volume);
  68. _sessions.TryAdd(session, 0);
  69. return session;
  70. }
  71. internal bool Unregister(OpenALHardwareDeviceSession session)
  72. {
  73. return _sessions.TryRemove(session, out _);
  74. }
  75. public ManualResetEvent GetUpdateRequiredEvent()
  76. {
  77. return _updateRequiredEvent;
  78. }
  79. public ManualResetEvent GetPauseEvent()
  80. {
  81. return _pauseEvent;
  82. }
  83. private void Update()
  84. {
  85. ALC.MakeContextCurrent(_context);
  86. while (_stillRunning)
  87. {
  88. bool updateRequired = false;
  89. foreach (OpenALHardwareDeviceSession session in _sessions.Keys)
  90. {
  91. if (session.Update())
  92. {
  93. updateRequired = true;
  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. _stillRunning = false;
  113. foreach (OpenALHardwareDeviceSession session in _sessions.Keys)
  114. {
  115. session.Dispose();
  116. }
  117. ALC.DestroyContext(_context);
  118. ALC.CloseDevice(_device);
  119. _pauseEvent.Dispose();
  120. }
  121. }
  122. public bool SupportsSampleRate(uint sampleRate)
  123. {
  124. return true;
  125. }
  126. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  127. {
  128. return true;
  129. }
  130. public bool SupportsChannelCount(uint channelCount)
  131. {
  132. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  133. }
  134. public bool SupportsDirection(Direction direction)
  135. {
  136. return direction == Direction.Output;
  137. }
  138. }
  139. }