CompatLayerHardwareDeviceDriver.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using Ryujinx.Audio.Backends.Common;
  18. using Ryujinx.Audio.Backends.Dummy;
  19. using Ryujinx.Audio.Common;
  20. using Ryujinx.Audio.Integration;
  21. using Ryujinx.Common.Logging;
  22. using Ryujinx.Memory;
  23. using System;
  24. using System.Threading;
  25. using static Ryujinx.Audio.Integration.IHardwareDeviceDriver;
  26. namespace Ryujinx.Audio.Backends.CompatLayer
  27. {
  28. public class CompatLayerHardwareDeviceDriver : IHardwareDeviceDriver
  29. {
  30. private IHardwareDeviceDriver _realDriver;
  31. public CompatLayerHardwareDeviceDriver(IHardwareDeviceDriver realDevice)
  32. {
  33. _realDriver = realDevice;
  34. }
  35. public void Dispose()
  36. {
  37. _realDriver.Dispose();
  38. }
  39. public ManualResetEvent GetUpdateRequiredEvent()
  40. {
  41. return _realDriver.GetUpdateRequiredEvent();
  42. }
  43. public ManualResetEvent GetPauseEvent()
  44. {
  45. return _realDriver.GetPauseEvent();
  46. }
  47. private uint SelectHardwareChannelCount(uint targetChannelCount)
  48. {
  49. if (_realDriver.SupportsChannelCount(targetChannelCount))
  50. {
  51. return targetChannelCount;
  52. }
  53. return targetChannelCount switch
  54. {
  55. 6 => SelectHardwareChannelCount(2),
  56. 2 => SelectHardwareChannelCount(1),
  57. 1 => throw new ArgumentException("No valid channel configuration found!"),
  58. _ => throw new ArgumentException($"Invalid targetChannelCount {targetChannelCount}")
  59. };
  60. }
  61. public IHardwareDeviceSession OpenDeviceSession(Direction direction, IVirtualMemoryManager memoryManager, SampleFormat sampleFormat, uint sampleRate, uint channelCount)
  62. {
  63. if (channelCount == 0)
  64. {
  65. channelCount = 2;
  66. }
  67. if (sampleRate == 0)
  68. {
  69. sampleRate = Constants.TargetSampleRate;
  70. }
  71. if (!_realDriver.SupportsDirection(direction))
  72. {
  73. if (direction == Direction.Input)
  74. {
  75. Logger.Warning?.Print(LogClass.Audio, "The selected audio backend doesn't support audio input, fallback to dummy...");
  76. return new DummyHardwareDeviceSessionInput(this, memoryManager, sampleFormat, sampleRate, channelCount);
  77. }
  78. throw new NotImplementedException();
  79. }
  80. uint hardwareChannelCount = SelectHardwareChannelCount(channelCount);
  81. IHardwareDeviceSession realSession = _realDriver.OpenDeviceSession(direction, memoryManager, sampleFormat, sampleRate, hardwareChannelCount);
  82. if (hardwareChannelCount == channelCount)
  83. {
  84. return realSession;
  85. }
  86. if (direction == Direction.Input)
  87. {
  88. Logger.Warning?.Print(LogClass.Audio, $"The selected audio backend doesn't support the requested audio input configuration, fallback to dummy...");
  89. // TODO: We currently don't support audio input upsampling/downsampling, implement this.
  90. realSession.Dispose();
  91. return new DummyHardwareDeviceSessionInput(this, memoryManager, sampleFormat, sampleRate, channelCount);
  92. }
  93. // It must be a HardwareDeviceSessionOutputBase.
  94. if (realSession is not HardwareDeviceSessionOutputBase realSessionOutputBase)
  95. {
  96. throw new InvalidOperationException($"Real driver session class type isn't based on {typeof(HardwareDeviceSessionOutputBase).Name}.");
  97. }
  98. // If we need to do post processing before sending to the hardware device, wrap around it.
  99. return new CompatLayerHardwareDeviceSession(realSessionOutputBase, channelCount);
  100. }
  101. public bool SupportsChannelCount(uint channelCount)
  102. {
  103. return channelCount == 1 || channelCount == 2 || channelCount == 6;
  104. }
  105. public bool SupportsSampleFormat(SampleFormat sampleFormat)
  106. {
  107. // TODO: More formats.
  108. return sampleFormat == SampleFormat.PcmInt16;
  109. }
  110. public bool SupportsSampleRate(uint sampleRate)
  111. {
  112. // TODO: More sample rates.
  113. return sampleRate == Constants.TargetSampleRate;
  114. }
  115. public IHardwareDeviceDriver GetRealDeviceDriver()
  116. {
  117. return _realDriver;
  118. }
  119. public bool SupportsDirection(Direction direction)
  120. {
  121. return direction == Direction.Input || direction == Direction.Output;
  122. }
  123. }
  124. }