SDL2HardwareDeviceDriver.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Ryujinx.Audio.Common;
  2. using Ryujinx.Audio.Integration;
  3. using Ryujinx.Memory;
  4. using Ryujinx.SDL2.Common;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
  10. using static SDL2.SDL;
  11. namespace Ryujinx.Audio.Backends.SDL2
  12. {
  13. public class SDL2HardwareDeviceDriver : IHardwareDeviceDriver
  14. {
  15. private readonly ManualResetEvent _updateRequiredEvent;
  16. private readonly ConcurrentDictionary<SDL2HardwareDeviceSession, byte> _sessions;
  17. public SDL2HardwareDeviceDriver()
  18. {
  19. _updateRequiredEvent = new ManualResetEvent(false);
  20. _sessions = new ConcurrentDictionary<SDL2HardwareDeviceSession, byte>();
  21. SDL2Driver.Instance.Initialize();
  22. }
  23. public static bool IsSupported => IsSupportedInternal();
  24. private static bool IsSupportedInternal()
  25. {
  26. uint device = OpenStream(SampleFormat.PcmInt16, Constants.TargetSampleRate, Constants.ChannelCountMax, Constants.TargetSampleCount, null);
  27. if (device != 0)
  28. {
  29. SDL_CloseAudioDevice(device);
  30. }
  31. return device != 0;
  32. }
  33. public ManualResetEvent GetUpdateRequiredEvent()
  34. {
  35. return _updateRequiredEvent;
  36. }
  37. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount)
  38. {
  39. if (channelCount == 0)
  40. {
  41. channelCount = 2;
  42. }
  43. if (sampleRate == 0)
  44. {
  45. sampleRate = Constants.TargetSampleRate;
  46. }
  47. if (direction != Direction.Output)
  48. {
  49. throw new NotImplementedException("Input direction is currently not implemented on SDL2 backend!");
  50. }
  51. SDL2HardwareDeviceSession session = new SDL2HardwareDeviceSession(this, memoryManager, sampleFormat, sampleRate, channelCount);
  52. _sessions.TryAdd(session, 0);
  53. return session;
  54. }
  55. internal bool Unregister(SDL2HardwareDeviceSession session)
  56. {
  57. return _sessions.TryRemove(session, out _);
  58. }
  59. private static SDL_AudioSpec GetSDL2Spec(SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, uint sampleCount)
  60. {
  61. return new SDL_AudioSpec
  62. {
  63. channels = (byte)requestedChannelCount,
  64. format = GetSDL2Format(requestedSampleFormat),
  65. freq = (int)requestedSampleRate,
  66. samples = (ushort)sampleCount
  67. };
  68. }
  69. internal static ushort GetSDL2Format(SampleFormat format)
  70. {
  71. return format switch
  72. {
  73. SampleFormat.PcmInt8 => AUDIO_S8,
  74. SampleFormat.PcmInt16 => AUDIO_S16,
  75. SampleFormat.PcmInt32 => AUDIO_S32,
  76. SampleFormat.PcmFloat => AUDIO_F32,
  77. _ => throw new ArgumentException($"Unsupported sample format {format}"),
  78. };
  79. }
  80. // TODO: Fix this in SDL2-CS.
  81. [DllImport("SDL2", EntryPoint = "SDL_OpenAudioDevice", CallingConvention = CallingConvention.Cdecl)]
  82. private static extern uint SDL_OpenAudioDevice_Workaround(
  83. IntPtr name,
  84. int iscapture,
  85. ref SDL_AudioSpec desired,
  86. out SDL_AudioSpec obtained,
  87. uint allowed_changes
  88. );
  89. internal static uint OpenStream(SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, uint sampleCount, SDL_AudioCallback callback)
  90. {
  91. SDL_AudioSpec desired = GetSDL2Spec(requestedSampleFormat, requestedSampleRate, requestedChannelCount, sampleCount);
  92. desired.callback = callback;
  93. uint device = SDL_OpenAudioDevice_Workaround(IntPtr.Zero, 0, ref desired, out SDL_AudioSpec got, 0);
  94. if (device == 0)
  95. {
  96. return 0;
  97. }
  98. bool isValid = got.format == desired.format && got.freq == desired.freq && got.channels == desired.channels;
  99. if (!isValid)
  100. {
  101. SDL_CloseAudioDevice(device);
  102. return 0;
  103. }
  104. return device;
  105. }
  106. public void Dispose()
  107. {
  108. Dispose(true);
  109. }
  110. protected virtual void Dispose(bool disposing)
  111. {
  112. if (disposing)
  113. {
  114. foreach (SDL2HardwareDeviceSession session in _sessions.Keys)
  115. {
  116. session.Dispose();
  117. }
  118. SDL2Driver.Instance.Dispose();
  119. }
  120. }
  121. public bool SupportsSampleRate(uint sampleRate)
  122. {
  123. return true;
  124. }
  125. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  126. {
  127. return sampleFormat != SampleFormat.PcmInt24;
  128. }
  129. public bool SupportsChannelCount(uint channelCount)
  130. {
  131. return true;
  132. }
  133. public bool SupportsDirection(Direction direction)
  134. {
  135. // TODO: add direction input when supported.
  136. return direction == Direction.Output;
  137. }
  138. }
  139. }