SoundIoHardwareDeviceDriver.cs 8.1 KB

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