SoundIoHardwareDeviceDriver.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using Ryujinx.Audio.Backends.SoundIo.Native;
  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.Threading;
  8. using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
  9. using static Ryujinx.Audio.Backends.SoundIo.Native.SoundIo;
  10. namespace Ryujinx.Audio.Backends.SoundIo
  11. {
  12. public class SoundIoHardwareDeviceDriver : IHardwareDeviceDriver
  13. {
  14. private readonly SoundIoContext _audioContext;
  15. private readonly SoundIoDeviceContext _audioDevice;
  16. private readonly ManualResetEvent _updateRequiredEvent;
  17. private readonly ManualResetEvent _pauseEvent;
  18. private readonly ConcurrentDictionary<SoundIoHardwareDeviceSession, byte> _sessions;
  19. private int _disposeState;
  20. public SoundIoHardwareDeviceDriver()
  21. {
  22. _audioContext = SoundIoContext.Create();
  23. _updateRequiredEvent = new ManualResetEvent(false);
  24. _pauseEvent = new ManualResetEvent(true);
  25. _sessions = new ConcurrentDictionary<SoundIoHardwareDeviceSession, byte>();
  26. _audioContext.Connect();
  27. _audioContext.FlushEvents();
  28. _audioDevice = FindValidAudioDevice(_audioContext, true);
  29. }
  30. public static bool IsSupported => IsSupportedInternal();
  31. private static bool IsSupportedInternal()
  32. {
  33. SoundIoContext context = null;
  34. SoundIoDeviceContext device = null;
  35. SoundIoOutStreamContext stream = null;
  36. bool backendDisconnected = false;
  37. try
  38. {
  39. context = SoundIoContext.Create();
  40. context.OnBackendDisconnect = err =>
  41. {
  42. backendDisconnected = true;
  43. };
  44. context.Connect();
  45. context.FlushEvents();
  46. if (backendDisconnected)
  47. {
  48. return false;
  49. }
  50. if (context.OutputDeviceCount == 0)
  51. {
  52. return false;
  53. }
  54. device = FindValidAudioDevice(context);
  55. if (device == null || backendDisconnected)
  56. {
  57. return false;
  58. }
  59. stream = device.CreateOutStream();
  60. if (stream == null || backendDisconnected)
  61. {
  62. return false;
  63. }
  64. return true;
  65. }
  66. catch
  67. {
  68. return false;
  69. }
  70. finally
  71. {
  72. stream?.Dispose();
  73. context?.Dispose();
  74. }
  75. }
  76. private static SoundIoDeviceContext FindValidAudioDevice(SoundIoContext audioContext, bool fallback = false)
  77. {
  78. SoundIoDeviceContext defaultAudioDevice = audioContext.GetOutputDevice(audioContext.DefaultOutputDeviceIndex);
  79. if (!defaultAudioDevice.IsRaw)
  80. {
  81. return defaultAudioDevice;
  82. }
  83. for (int i = 0; i < audioContext.OutputDeviceCount; i++)
  84. {
  85. SoundIoDeviceContext audioDevice = audioContext.GetOutputDevice(i);
  86. if (audioDevice.Id == defaultAudioDevice.Id && !audioDevice.IsRaw)
  87. {
  88. return audioDevice;
  89. }
  90. }
  91. return fallback ? defaultAudioDevice : null;
  92. }
  93. public ManualResetEvent GetUpdateRequiredEvent()
  94. {
  95. return _updateRequiredEvent;
  96. }
  97. public ManualResetEvent GetPauseEvent()
  98. {
  99. return _pauseEvent;
  100. }
  101. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount, float volume)
  102. {
  103. if (channelCount == 0)
  104. {
  105. channelCount = 2;
  106. }
  107. if (sampleRate == 0)
  108. {
  109. sampleRate = Constants.TargetSampleRate;
  110. }
  111. volume = Math.Clamp(volume, 0, 1);
  112. if (direction != Direction.Output)
  113. {
  114. throw new NotImplementedException("Input direction is currently not implemented on SoundIO backend!");
  115. }
  116. SoundIoHardwareDeviceSession session = new SoundIoHardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount, volume);
  117. _sessions.TryAdd(session, 0);
  118. return session;
  119. }
  120. internal bool Unregister(SoundIoHardwareDeviceSession session)
  121. {
  122. return _sessions.TryRemove(session, out _);
  123. }
  124. public static SoundIoFormat GetSoundIoFormat(SampleFormat format)
  125. {
  126. return format switch
  127. {
  128. SampleFormat.PcmInt8 => SoundIoFormat.S8,
  129. SampleFormat.PcmInt16 => SoundIoFormat.S16LE,
  130. SampleFormat.PcmInt24 => SoundIoFormat.S24LE,
  131. SampleFormat.PcmInt32 => SoundIoFormat.S32LE,
  132. SampleFormat.PcmFloat => SoundIoFormat.Float32LE,
  133. _ => throw new ArgumentException ($"Unsupported sample format {format}"),
  134. };
  135. }
  136. internal SoundIoOutStreamContext OpenStream(SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount)
  137. {
  138. SoundIoFormat driverSampleFormat = GetSoundIoFormat(requestedSampleFormat);
  139. if (!_audioDevice.SupportsSampleRate((int)requestedSampleRate))
  140. {
  141. throw new ArgumentException($"This sound device does not support a sample rate of {requestedSampleRate}Hz");
  142. }
  143. if (!_audioDevice.SupportsFormat(driverSampleFormat))
  144. {
  145. throw new ArgumentException($"This sound device does not support {requestedSampleFormat}");
  146. }
  147. if (!_audioDevice.SupportsChannelCount((int)requestedChannelCount))
  148. {
  149. throw new ArgumentException($"This sound device does not support channel count {requestedChannelCount}");
  150. }
  151. SoundIoOutStreamContext result = _audioDevice.CreateOutStream();
  152. result.Name = "Ryujinx";
  153. result.Layout = SoundIoChannelLayout.GetDefaultValue((int)requestedChannelCount);
  154. result.Format = driverSampleFormat;
  155. result.SampleRate = (int)requestedSampleRate;
  156. return result;
  157. }
  158. internal void FlushContextEvents()
  159. {
  160. _audioContext.FlushEvents();
  161. }
  162. public void Dispose()
  163. {
  164. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  165. {
  166. Dispose(true);
  167. }
  168. }
  169. protected virtual void Dispose(bool disposing)
  170. {
  171. if (disposing)
  172. {
  173. foreach (SoundIoHardwareDeviceSession session in _sessions.Keys)
  174. {
  175. session.Dispose();
  176. }
  177. _audioContext.Disconnect();
  178. _audioContext.Dispose();
  179. _pauseEvent.Dispose();
  180. }
  181. }
  182. public bool SupportsSampleRate(uint sampleRate)
  183. {
  184. return _audioDevice.SupportsSampleRate((int)sampleRate);
  185. }
  186. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  187. {
  188. return _audioDevice.SupportsFormat(GetSoundIoFormat(sampleFormat));
  189. }
  190. public bool SupportsChannelCount(uint channelCount)
  191. {
  192. return _audioDevice.SupportsChannelCount((int)channelCount);
  193. }
  194. public bool SupportsDirection(Direction direction)
  195. {
  196. // TODO: add direction input when supported.
  197. return direction == Direction.Output;
  198. }
  199. }
  200. }