CompatLayerHardwareDeviceDriver.cs 5.1 KB

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