OpenALHardwareDeviceDriver.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 ConcurrentDictionary<OpenALHardwareDeviceSession, byte> _sessions;
  18. private bool _stillRunning;
  19. private Thread _updaterThread;
  20. public OpenALHardwareDeviceDriver()
  21. {
  22. _device = ALC.OpenDevice("");
  23. _context = ALC.CreateContext(_device, new ALContextAttributes());
  24. _updateRequiredEvent = new ManualResetEvent(false);
  25. _sessions = new ConcurrentDictionary<OpenALHardwareDeviceSession, byte>();
  26. _stillRunning = true;
  27. _updaterThread = new Thread(Update)
  28. {
  29. Name = "HardwareDeviceDriver.OpenAL"
  30. };
  31. _updaterThread.Start();
  32. }
  33. public static bool IsSupported
  34. {
  35. get
  36. {
  37. try
  38. {
  39. return ALC.GetStringList(GetEnumerationStringList.DeviceSpecifier).Any();
  40. }
  41. catch
  42. {
  43. return false;
  44. }
  45. }
  46. }
  47. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount)
  48. {
  49. if (channelCount == 0)
  50. {
  51. channelCount = 2;
  52. }
  53. if (sampleRate == 0)
  54. {
  55. sampleRate = Constants.TargetSampleRate;
  56. }
  57. if (direction != Direction.Output)
  58. {
  59. throw new ArgumentException($"{direction}");
  60. }
  61. else if (!SupportsChannelCount(channelCount))
  62. {
  63. throw new ArgumentException($"{channelCount}");
  64. }
  65. OpenALHardwareDeviceSession session = new OpenALHardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount);
  66. _sessions.TryAdd(session, 0);
  67. return session;
  68. }
  69. internal bool Unregister(OpenALHardwareDeviceSession session)
  70. {
  71. return _sessions.TryRemove(session, out _);
  72. }
  73. public ManualResetEvent GetUpdateRequiredEvent()
  74. {
  75. return _updateRequiredEvent;
  76. }
  77. private void Update()
  78. {
  79. ALC.MakeContextCurrent(_context);
  80. while (_stillRunning)
  81. {
  82. bool updateRequired = false;
  83. foreach (OpenALHardwareDeviceSession session in _sessions.Keys)
  84. {
  85. if (session.Update())
  86. {
  87. updateRequired = true;
  88. }
  89. }
  90. if (updateRequired)
  91. {
  92. _updateRequiredEvent.Set();
  93. }
  94. // If it's not slept it will waste cycles.
  95. Thread.Sleep(10);
  96. }
  97. }
  98. public void Dispose()
  99. {
  100. Dispose(true);
  101. }
  102. protected virtual void Dispose(bool disposing)
  103. {
  104. if (disposing)
  105. {
  106. _stillRunning = false;
  107. foreach (OpenALHardwareDeviceSession session in _sessions.Keys)
  108. {
  109. session.Dispose();
  110. }
  111. ALC.DestroyContext(_context);
  112. ALC.CloseDevice(_device);
  113. }
  114. }
  115. public bool SupportsSampleRate(uint sampleRate)
  116. {
  117. return true;
  118. }
  119. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  120. {
  121. return true;
  122. }
  123. public bool SupportsChannelCount(uint channelCount)
  124. {
  125. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  126. }
  127. public bool SupportsDirection(Direction direction)
  128. {
  129. return direction == Direction.Output;
  130. }
  131. }
  132. }