SoundIoHardwareDeviceDriver.cs 8.1 KB

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