SDL2HardwareDeviceDriver.cs 5.2 KB

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