SDL2HardwareDeviceDriver.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.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. Logger.Error?.Print(LogClass.Application,
  94. $"SDL2 open audio device initialization failed with error \"{SDL_GetError()}\"");
  95. return 0;
  96. }
  97. bool isValid = got.format == desired.format && got.freq == desired.freq && got.channels == desired.channels;
  98. if (!isValid)
  99. {
  100. Logger.Error?.Print(LogClass.Application, "SDL2 open audio device is not valid");
  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. _pauseEvent.Dispose();
  120. }
  121. }
  122. public bool SupportsSampleRate(uint sampleRate)
  123. {
  124. return true;
  125. }
  126. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  127. {
  128. return sampleFormat != SampleFormat.PcmInt24;
  129. }
  130. public bool SupportsChannelCount(uint channelCount)
  131. {
  132. return true;
  133. }
  134. public bool SupportsDirection(Direction direction)
  135. {
  136. // TODO: add direction input when supported.
  137. return direction == Direction.Output;
  138. }
  139. }
  140. }