CompatLayerHardwareDeviceDriver.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount, float volume)
  46. {
  47. if (channelCount == 0)
  48. {
  49. channelCount = 2;
  50. }
  51. if (sampleRate == 0)
  52. {
  53. sampleRate = Constants.TargetSampleRate;
  54. }
  55. volume = Math.Clamp(volume, 0, 1);
  56. if (!_realDriver.SupportsDirection(direction))
  57. {
  58. if (direction == Direction.Input)
  59. {
  60. Logger.Warning?.Print(LogClass.Audio, "The selected audio backend doesn't support audio input, fallback to dummy...");
  61. return new DummyHardwareDeviceSessionInput(this, memoryManager, sampleFormat, sampleRate, channelCount);
  62. }
  63. throw new NotImplementedException();
  64. }
  65. uint hardwareChannelCount = SelectHardwareChannelCount(channelCount);
  66. IHardwareDeviceSession realSession = _realDriver.OpenDeviceSession(direction, memoryManager, sampleFormat, sampleRate, hardwareChannelCount, volume);
  67. if (hardwareChannelCount == channelCount)
  68. {
  69. return realSession;
  70. }
  71. if (direction == Direction.Input)
  72. {
  73. Logger.Warning?.Print(LogClass.Audio, $"The selected audio backend doesn't support the requested audio input configuration, fallback to dummy...");
  74. // TODO: We currently don't support audio input upsampling/downsampling, implement this.
  75. realSession.Dispose();
  76. return new DummyHardwareDeviceSessionInput(this, memoryManager, sampleFormat, sampleRate, channelCount);
  77. }
  78. // It must be a HardwareDeviceSessionOutputBase.
  79. if (realSession is not HardwareDeviceSessionOutputBase realSessionOutputBase)
  80. {
  81. throw new InvalidOperationException($"Real driver session class type isn't based on {typeof(HardwareDeviceSessionOutputBase).Name}.");
  82. }
  83. // If we need to do post processing before sending to the hardware device, wrap around it.
  84. return new CompatLayerHardwareDeviceSession(realSessionOutputBase, channelCount);
  85. }
  86. public bool SupportsChannelCount(uint channelCount)
  87. {
  88. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  89. }
  90. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  91. {
  92. // TODO: More formats.
  93. return sampleFormat == SampleFormat.PcmInt16;
  94. }
  95. public bool SupportsSampleRate(uint sampleRate)
  96. {
  97. // TODO: More sample rates.
  98. return sampleRate == Constants.TargetSampleRate;
  99. }
  100. public IHardwareDeviceDriver GetRealDeviceDriver()
  101. {
  102. return _realDriver;
  103. }
  104. public bool SupportsDirection(Direction direction)
  105. {
  106. return direction == Direction.Input || direction == Direction.Output;
  107. }
  108. }
  109. }