SoundIoHardwareDeviceDriver.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. public SoundIoHardwareDeviceDriver()
  19. {
  20. _audioContext = new SoundIO();
  21. _updateRequiredEvent = new ManualResetEvent(false);
  22. _sessions = new List<SoundIoHardwareDeviceSession>();
  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. lock (_lock)
  115. {
  116. SoundIoHardwareDeviceSession session = new SoundIoHardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount);
  117. _sessions.Add(session);
  118. return session;
  119. }
  120. }
  121. internal void Unregister(SoundIoHardwareDeviceSession session)
  122. {
  123. lock (_lock)
  124. {
  125. _sessions.Remove(session);
  126. }
  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. Dispose(true);
  169. }
  170. protected virtual void Dispose(bool disposing)
  171. {
  172. if (disposing)
  173. {
  174. while (_sessions.Count > 0)
  175. {
  176. SoundIoHardwareDeviceSession session = _sessions[_sessions.Count - 1];
  177. session.Dispose();
  178. }
  179. _audioContext.Disconnect();
  180. _audioContext.Dispose();
  181. }
  182. }
  183. public bool SupportsSampleRate(uint sampleRate)
  184. {
  185. return _audioDevice.SupportsSampleRate((int)sampleRate);
  186. }
  187. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  188. {
  189. return _audioDevice.SupportsFormat(GetSoundIoFormat(sampleFormat));
  190. }
  191. public bool SupportsChannelCount(uint channelCount)
  192. {
  193. return _audioDevice.SupportsChannelCount((int)channelCount);
  194. }
  195. public bool SupportsDirection(Direction direction)
  196. {
  197. // TODO: add direction input when supported.
  198. return direction == Direction.Output;
  199. }
  200. }
  201. }