SoundIoHardwareDeviceDriver.cs 7.5 KB

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