SDL2HardwareDeviceDriver.cs 5.2 KB

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