SDL2HardwareDeviceDriver.cs 5.5 KB

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