SDL2HardwareDeviceDriver.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Ryujinx.Audio.Common;
  2. using Ryujinx.Audio.Integration;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Memory;
  5. using Ryujinx.SDL2.Common;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
  11. using static SDL2.SDL;
  12. namespace Ryujinx.Audio.Backends.SDL2
  13. {
  14. public class SDL2HardwareDeviceDriver : IHardwareDeviceDriver
  15. {
  16. private readonly ManualResetEvent _updateRequiredEvent;
  17. private readonly ManualResetEvent _pauseEvent;
  18. private readonly ConcurrentDictionary<SDL2HardwareDeviceSession, byte> _sessions;
  19. private readonly bool _supportSurroundConfiguration;
  20. public float Volume { get; set; }
  21. // TODO: Add this to SDL2-CS
  22. // NOTE: We use a DllImport here because of marshaling issue for spec.
  23. #pragma warning disable SYSLIB1054
  24. [DllImport("SDL2")]
  25. private static extern int SDL_GetDefaultAudioInfo(nint name, out SDL_AudioSpec spec, int isCapture);
  26. #pragma warning restore SYSLIB1054
  27. public SDL2HardwareDeviceDriver()
  28. {
  29. _updateRequiredEvent = new ManualResetEvent(false);
  30. _pauseEvent = new ManualResetEvent(true);
  31. _sessions = new ConcurrentDictionary<SDL2HardwareDeviceSession, byte>();
  32. SDL2Driver.Instance.Initialize();
  33. int res = SDL_GetDefaultAudioInfo(nint.Zero, out var spec, 0);
  34. if (res != 0)
  35. {
  36. Logger.Error?.Print(LogClass.Application,
  37. $"SDL_GetDefaultAudioInfo failed with error \"{SDL_GetError()}\"");
  38. _supportSurroundConfiguration = true;
  39. }
  40. else
  41. {
  42. _supportSurroundConfiguration = spec.channels >= 6;
  43. }
  44. Volume = 1f;
  45. }
  46. public static bool IsSupported => IsSupportedInternal();
  47. private static bool IsSupportedInternal()
  48. {
  49. uint device = OpenStream(SampleFormat.PcmInt16, Constants.TargetSampleRate, Constants.ChannelCountMax, Constants.TargetSampleCount, null);
  50. if (device != 0)
  51. {
  52. SDL_CloseAudioDevice(device);
  53. }
  54. return device != 0;
  55. }
  56. public ManualResetEvent GetUpdateRequiredEvent()
  57. {
  58. return _updateRequiredEvent;
  59. }
  60. public ManualResetEvent GetPauseEvent()
  61. {
  62. return _pauseEvent;
  63. }
  64. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount)
  65. {
  66. if (channelCount == 0)
  67. {
  68. channelCount = 2;
  69. }
  70. if (sampleRate == 0)
  71. {
  72. sampleRate = Constants.TargetSampleRate;
  73. }
  74. if (direction != Direction.Output)
  75. {
  76. throw new NotImplementedException("Input direction is currently not implemented on SDL2 backend!");
  77. }
  78. SDL2HardwareDeviceSession session = new(this, memoryManager, sampleFormat, sampleRate, channelCount);
  79. _sessions.TryAdd(session, 0);
  80. return session;
  81. }
  82. internal bool Unregister(SDL2HardwareDeviceSession session)
  83. {
  84. return _sessions.TryRemove(session, out _);
  85. }
  86. private static SDL_AudioSpec GetSDL2Spec(SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, uint sampleCount)
  87. {
  88. return new SDL_AudioSpec
  89. {
  90. channels = (byte)requestedChannelCount,
  91. format = GetSDL2Format(requestedSampleFormat),
  92. freq = (int)requestedSampleRate,
  93. samples = (ushort)sampleCount,
  94. };
  95. }
  96. internal static ushort GetSDL2Format(SampleFormat format)
  97. {
  98. return format switch
  99. {
  100. SampleFormat.PcmInt8 => AUDIO_S8,
  101. SampleFormat.PcmInt16 => AUDIO_S16,
  102. SampleFormat.PcmInt32 => AUDIO_S32,
  103. SampleFormat.PcmFloat => AUDIO_F32,
  104. _ => throw new ArgumentException($"Unsupported sample format {format}"),
  105. };
  106. }
  107. internal static uint OpenStream(SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, uint sampleCount, SDL_AudioCallback callback)
  108. {
  109. SDL_AudioSpec desired = GetSDL2Spec(requestedSampleFormat, requestedSampleRate, requestedChannelCount, sampleCount);
  110. desired.callback = callback;
  111. uint device = SDL_OpenAudioDevice(nint.Zero, 0, ref desired, out SDL_AudioSpec got, 0);
  112. if (device == 0)
  113. {
  114. Logger.Error?.Print(LogClass.Application, $"SDL2 open audio device initialization failed with error \"{SDL_GetError()}\"");
  115. return 0;
  116. }
  117. bool isValid = got.format == desired.format && got.freq == desired.freq && got.channels == desired.channels;
  118. if (!isValid)
  119. {
  120. Logger.Error?.Print(LogClass.Application, "SDL2 open audio device is not valid");
  121. SDL_CloseAudioDevice(device);
  122. return 0;
  123. }
  124. return device;
  125. }
  126. public void Dispose()
  127. {
  128. GC.SuppressFinalize(this);
  129. Dispose(true);
  130. }
  131. protected virtual void Dispose(bool disposing)
  132. {
  133. if (disposing)
  134. {
  135. foreach (SDL2HardwareDeviceSession session in _sessions.Keys)
  136. {
  137. session.Dispose();
  138. }
  139. SDL2Driver.Instance.Dispose();
  140. _pauseEvent.Dispose();
  141. }
  142. }
  143. public bool SupportsSampleRate(uint sampleRate)
  144. {
  145. return true;
  146. }
  147. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  148. {
  149. return sampleFormat != SampleFormat.PcmInt24;
  150. }
  151. public bool SupportsChannelCount(uint channelCount)
  152. {
  153. if (channelCount == 6)
  154. {
  155. return _supportSurroundConfiguration;
  156. }
  157. return true;
  158. }
  159. public bool SupportsDirection(Direction direction)
  160. {
  161. // TODO: add direction input when supported.
  162. return direction == Direction.Output;
  163. }
  164. }
  165. }