CompatLayerHardwareDeviceDriver.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Ryujinx.Audio.Backends.Common;
  2. using Ryujinx.Audio.Backends.Dummy;
  3. using Ryujinx.Audio.Common;
  4. using Ryujinx.Audio.Integration;
  5. using Ryujinx.Common.Logging;
  6. using Ryujinx.Memory;
  7. using System;
  8. using System.Threading;
  9. using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
  10. namespace Ryujinx.Audio.Backends.CompatLayer
  11. {
  12. public class CompatLayerHardwareDeviceDriver : IHardwareDeviceDriver
  13. {
  14. private IHardwareDeviceDriver _realDriver;
  15. public static bool IsSupported => true;
  16. public CompatLayerHardwareDeviceDriver(IHardwareDeviceDriver realDevice)
  17. {
  18. _realDriver = realDevice;
  19. }
  20. public void Dispose()
  21. {
  22. _realDriver.Dispose();
  23. }
  24. public ManualResetEvent GetUpdateRequiredEvent()
  25. {
  26. return _realDriver.GetUpdateRequiredEvent();
  27. }
  28. public ManualResetEvent GetPauseEvent()
  29. {
  30. return _realDriver.GetPauseEvent();
  31. }
  32. private uint SelectHardwareChannelCount(uint targetChannelCount)
  33. {
  34. if (_realDriver.SupportsChannelCount(targetChannelCount))
  35. {
  36. return targetChannelCount;
  37. }
  38. return targetChannelCount switch
  39. {
  40. 6 => SelectHardwareChannelCount(2),
  41. 2 => SelectHardwareChannelCount(1),
  42. 1 => throw new ArgumentException("No valid channel configuration found!"),
  43. _ => throw new ArgumentException($"Invalid targetChannelCount {targetChannelCount}")
  44. };
  45. }
  46. private SampleFormat SelectHardwareSampleFormat(SampleFormat targetSampleFormat)
  47. {
  48. if (_realDriver.SupportsSampleFormat(targetSampleFormat))
  49. {
  50. return targetSampleFormat;
  51. }
  52. // Attempt conversion from PCM16.
  53. if (targetSampleFormat == SampleFormat.PcmInt16)
  54. {
  55. // Prefer PCM32 if we need to convert.
  56. if (_realDriver.SupportsSampleFormat(SampleFormat.PcmInt32))
  57. {
  58. return SampleFormat.PcmInt32;
  59. }
  60. // If not supported, PCM float provides the best quality with a cost lower than PCM24.
  61. if (_realDriver.SupportsSampleFormat(SampleFormat.PcmFloat))
  62. {
  63. return SampleFormat.PcmFloat;
  64. }
  65. if (_realDriver.SupportsSampleFormat(SampleFormat.PcmInt24))
  66. {
  67. return SampleFormat.PcmInt24;
  68. }
  69. // If nothing is truly supported, attempt PCM8 at the cost of losing quality.
  70. if (_realDriver.SupportsSampleFormat(SampleFormat.PcmInt8))
  71. {
  72. return SampleFormat.PcmInt8;
  73. }
  74. }
  75. throw new ArgumentException("No valid sample format configuration found!");
  76. }
  77. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount, float volume)
  78. {
  79. if (channelCount == 0)
  80. {
  81. channelCount = 2;
  82. }
  83. if (sampleRate == 0)
  84. {
  85. sampleRate = Constants.TargetSampleRate;
  86. }
  87. volume = Math.Clamp(volume, 0, 1);
  88. if (!_realDriver.SupportsDirection(direction))
  89. {
  90. if (direction == Direction.Input)
  91. {
  92. Logger.Warning?.Print(LogClass.Audio, "The selected audio backend doesn't support audio input, fallback to dummy...");
  93. return new DummyHardwareDeviceSessionInput(this, memoryManager, sampleFormat, sampleRate, channelCount);
  94. }
  95. throw new NotImplementedException();
  96. }
  97. SampleFormat hardwareSampleFormat = SelectHardwareSampleFormat(sampleFormat);
  98. uint hardwareChannelCount = SelectHardwareChannelCount(channelCount);
  99. IHardwareDeviceSession realSession = _realDriver.OpenDeviceSession(direction, memoryManager, hardwareSampleFormat, sampleRate, hardwareChannelCount, volume);
  100. if (hardwareChannelCount == channelCount && hardwareSampleFormat == sampleFormat)
  101. {
  102. return realSession;
  103. }
  104. if (hardwareSampleFormat != sampleFormat)
  105. {
  106. Logger.Warning?.Print(LogClass.Audio, $"{sampleFormat} isn't supported by the audio device, conversion to {hardwareSampleFormat} will happen.");
  107. if (hardwareSampleFormat < sampleFormat)
  108. {
  109. Logger.Warning?.Print(LogClass.Audio, $"{hardwareSampleFormat} has lower quality than {sampleFormat}, expect some loss in audio fidelity.");
  110. }
  111. }
  112. if (direction == Direction.Input)
  113. {
  114. Logger.Warning?.Print(LogClass.Audio, $"The selected audio backend doesn't support the requested audio input configuration, fallback to dummy...");
  115. // TODO: We currently don't support audio input upsampling/downsampling, implement this.
  116. realSession.Dispose();
  117. return new DummyHardwareDeviceSessionInput(this, memoryManager, sampleFormat, sampleRate, channelCount);
  118. }
  119. // It must be a HardwareDeviceSessionOutputBase.
  120. if (realSession is not HardwareDeviceSessionOutputBase realSessionOutputBase)
  121. {
  122. throw new InvalidOperationException($"Real driver session class type isn't based on {typeof(HardwareDeviceSessionOutputBase).Name}.");
  123. }
  124. // If we need to do post processing before sending to the hardware device, wrap around it.
  125. return new CompatLayerHardwareDeviceSession(realSessionOutputBase, sampleFormat, channelCount);
  126. }
  127. public bool SupportsChannelCount(uint channelCount)
  128. {
  129. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  130. }
  131. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  132. {
  133. // TODO: More formats.
  134. return sampleFormat == SampleFormat.PcmInt16;
  135. }
  136. public bool SupportsSampleRate(uint sampleRate)
  137. {
  138. // TODO: More sample rates.
  139. return sampleRate == Constants.TargetSampleRate;
  140. }
  141. public IHardwareDeviceDriver GetRealDeviceDriver()
  142. {
  143. return _realDriver;
  144. }
  145. public bool SupportsDirection(Direction direction)
  146. {
  147. return direction == Direction.Input || direction == Direction.Output;
  148. }
  149. }
  150. }