CompatLayerHardwareDeviceSession.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.Common;
  19. using System;
  20. using System.Runtime.InteropServices;
  21. namespace Ryujinx.Audio.Backends.CompatLayer
  22. {
  23. class CompatLayerHardwareDeviceSession : HardwareDeviceSessionOutputBase
  24. {
  25. private HardwareDeviceSessionOutputBase _realSession;
  26. private uint _userChannelCount;
  27. public CompatLayerHardwareDeviceSession(HardwareDeviceSessionOutputBase realSession, uint userChannelCount) : base(realSession.MemoryManager, realSession.RequestedSampleFormat, realSession.RequestedSampleRate, userChannelCount)
  28. {
  29. _realSession = realSession;
  30. _userChannelCount = userChannelCount;
  31. }
  32. public override void Dispose()
  33. {
  34. _realSession.Dispose();
  35. }
  36. public override ulong GetPlayedSampleCount()
  37. {
  38. return _realSession.GetPlayedSampleCount();
  39. }
  40. public override float GetVolume()
  41. {
  42. return _realSession.GetVolume();
  43. }
  44. public override void PrepareToClose()
  45. {
  46. _realSession.PrepareToClose();
  47. }
  48. public override void QueueBuffer(AudioBuffer buffer)
  49. {
  50. _realSession.QueueBuffer(buffer);
  51. }
  52. public override bool RegisterBuffer(AudioBuffer buffer, byte[] samples)
  53. {
  54. if (RequestedSampleFormat != SampleFormat.PcmInt16)
  55. {
  56. throw new NotImplementedException("Downmixing formats other than PCM16 is not supported.");
  57. }
  58. if (samples == null)
  59. {
  60. return false;
  61. }
  62. short[] downmixedBufferPCM16;
  63. ReadOnlySpan<short> samplesPCM16 = MemoryMarshal.Cast<byte, short>(samples);
  64. if (_userChannelCount == 6)
  65. {
  66. downmixedBufferPCM16 = Downmixing.DownMixSurroundToStereo(samplesPCM16);
  67. if (_realSession.RequestedChannelCount == 1)
  68. {
  69. downmixedBufferPCM16 = Downmixing.DownMixStereoToMono(downmixedBufferPCM16);
  70. }
  71. }
  72. else if (_userChannelCount == 2 && _realSession.RequestedChannelCount == 1)
  73. {
  74. downmixedBufferPCM16 = Downmixing.DownMixStereoToMono(samplesPCM16);
  75. }
  76. else
  77. {
  78. throw new NotImplementedException($"Downmixing from {_userChannelCount} to {_realSession.RequestedChannelCount} not implemented.");
  79. }
  80. byte[] downmixedBuffer = MemoryMarshal.Cast<short, byte>(downmixedBufferPCM16).ToArray();
  81. AudioBuffer fakeBuffer = new AudioBuffer
  82. {
  83. BufferTag = buffer.BufferTag,
  84. DataPointer = buffer.DataPointer,
  85. DataSize = (ulong)downmixedBuffer.Length
  86. };
  87. bool result = _realSession.RegisterBuffer(fakeBuffer, downmixedBuffer);
  88. if (result)
  89. {
  90. buffer.Data = fakeBuffer.Data;
  91. buffer.DataSize = fakeBuffer.DataSize;
  92. }
  93. return result;
  94. }
  95. public override void SetVolume(float volume)
  96. {
  97. _realSession.SetVolume(volume);
  98. }
  99. public override void Start()
  100. {
  101. _realSession.Start();
  102. }
  103. public override void Stop()
  104. {
  105. _realSession.Stop();
  106. }
  107. public override void UnregisterBuffer(AudioBuffer buffer)
  108. {
  109. _realSession.UnregisterBuffer(buffer);
  110. }
  111. public override bool WasBufferFullyConsumed(AudioBuffer buffer)
  112. {
  113. return _realSession.WasBufferFullyConsumed(buffer);
  114. }
  115. }
  116. }