SoundIoHardwareDeviceDriver.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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, float volume)
  107. {
  108. if (channelCount == 0)
  109. {
  110. channelCount = 2;
  111. }
  112. if (sampleRate == 0)
  113. {
  114. sampleRate = Constants.TargetSampleRate;
  115. }
  116. volume = Math.Clamp(volume, 0, 1);
  117. if (direction != Direction.Output)
  118. {
  119. throw new NotImplementedException("Input direction is currently not implemented on SoundIO backend!");
  120. }
  121. SoundIoHardwareDeviceSession session = new SoundIoHardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount, volume);
  122. _sessions.TryAdd(session, 0);
  123. return session;
  124. }
  125. internal bool Unregister(SoundIoHardwareDeviceSession session)
  126. {
  127. return _sessions.TryRemove(session, out _);
  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. foreach (SoundIoHardwareDeviceSession session in _sessions.Keys)
  179. {
  180. session.Dispose();
  181. }
  182. _audioContext.Disconnect();
  183. _audioContext.Dispose();
  184. _pauseEvent.Dispose();
  185. }
  186. }
  187. public bool SupportsSampleRate(uint sampleRate)
  188. {
  189. return _audioDevice.SupportsSampleRate((int)sampleRate);
  190. }
  191. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  192. {
  193. return _audioDevice.SupportsFormat(GetSoundIoFormat(sampleFormat));
  194. }
  195. public bool SupportsChannelCount(uint channelCount)
  196. {
  197. return _audioDevice.SupportsChannelCount((int)channelCount);
  198. }
  199. public bool SupportsDirection(Direction direction)
  200. {
  201. // TODO: add direction input when supported.
  202. return direction == Direction.Output;
  203. }
  204. }
  205. }