CompatLayerHardwareDeviceDriver.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // TODO: Implement PCM24 conversion.
  66. // If nothing is truly supported, attempt PCM8 at the cost of loosing quality.
  67. if (_realDriver.SupportsSampleFormat(SampleFormat.PcmInt8))
  68. {
  69. return SampleFormat.PcmInt8;
  70. }
  71. }
  72. throw new ArgumentException("No valid sample format configuration found!");
  73. }
  74. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount, float volume)
  75. {
  76. if (channelCount == 0)
  77. {
  78. channelCount = 2;
  79. }
  80. if (sampleRate == 0)
  81. {
  82. sampleRate = Constants.TargetSampleRate;
  83. }
  84. volume = Math.Clamp(volume, 0, 1);
  85. if (!_realDriver.SupportsDirection(direction))
  86. {
  87. if (direction == Direction.Input)
  88. {
  89. Logger.Warning?.Print(LogClass.Audio, "The selected audio backend doesn't support audio input, fallback to dummy...");
  90. return new DummyHardwareDeviceSessionInput(this, memoryManager, sampleFormat, sampleRate, channelCount);
  91. }
  92. throw new NotImplementedException();
  93. }
  94. SampleFormat hardwareSampleFormat = SelectHardwareSampleFormat(sampleFormat);
  95. uint hardwareChannelCount = SelectHardwareChannelCount(channelCount);
  96. IHardwareDeviceSession realSession = _realDriver.OpenDeviceSession(direction, memoryManager, hardwareSampleFormat, sampleRate, hardwareChannelCount, volume);
  97. if (hardwareChannelCount == channelCount && hardwareSampleFormat == sampleFormat)
  98. {
  99. return realSession;
  100. }
  101. if (hardwareSampleFormat != sampleFormat)
  102. {
  103. Logger.Warning?.Print(LogClass.Audio, $"{sampleFormat} isn't supported by the audio device, conversion to {hardwareSampleFormat} will happen.");
  104. if (hardwareSampleFormat < sampleFormat)
  105. {
  106. Logger.Warning?.Print(LogClass.Audio, $"{hardwareSampleFormat} has lower quality than {sampleFormat}, expect some loss in audio fidelity.");
  107. }
  108. }
  109. if (direction == Direction.Input)
  110. {
  111. Logger.Warning?.Print(LogClass.Audio, $"The selected audio backend doesn't support the requested audio input configuration, fallback to dummy...");
  112. // TODO: We currently don't support audio input upsampling/downsampling, implement this.
  113. realSession.Dispose();
  114. return new DummyHardwareDeviceSessionInput(this, memoryManager, sampleFormat, sampleRate, channelCount);
  115. }
  116. // It must be a HardwareDeviceSessionOutputBase.
  117. if (realSession is not HardwareDeviceSessionOutputBase realSessionOutputBase)
  118. {
  119. throw new InvalidOperationException($"Real driver session class type isn't based on {typeof(HardwareDeviceSessionOutputBase).Name}.");
  120. }
  121. // If we need to do post processing before sending to the hardware device, wrap around it.
  122. return new CompatLayerHardwareDeviceSession(realSessionOutputBase, sampleFormat, channelCount);
  123. }
  124. public bool SupportsChannelCount(uint channelCount)
  125. {
  126. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  127. }
  128. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  129. {
  130. // TODO: More formats.
  131. return sampleFormat == SampleFormat.PcmInt16;
  132. }
  133. public bool SupportsSampleRate(uint sampleRate)
  134. {
  135. // TODO: More sample rates.
  136. return sampleRate == Constants.TargetSampleRate;
  137. }
  138. public IHardwareDeviceDriver GetRealDeviceDriver()
  139. {
  140. return _realDriver;
  141. }
  142. public bool SupportsDirection(Direction direction)
  143. {
  144. return direction == Direction.Input || direction == Direction.Output;
  145. }
  146. }
  147. }