CompatLayerHardwareDeviceDriver.cs 5.3 KB

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