CompatLayerHardwareDeviceDriver.cs 6.5 KB

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