SoundIoHardwareDeviceDriver.cs 7.7 KB

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