DownMixSurroundToStereoCommand.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 System;
  18. using System.Runtime.CompilerServices;
  19. namespace Ryujinx.Audio.Renderer.Dsp.Command
  20. {
  21. public class DownMixSurroundToStereoCommand : ICommand
  22. {
  23. public bool Enabled { get; set; }
  24. public int NodeId { get; }
  25. public CommandType CommandType => CommandType.DownMixSurroundToStereo;
  26. public ulong EstimatedProcessingTime { get; set; }
  27. public ushort[] InputBufferIndices { get; }
  28. public ushort[] OutputBufferIndices { get; }
  29. public float[] Coefficients { get; }
  30. public DownMixSurroundToStereoCommand(uint bufferOffset, Span<byte> inputBufferOffset, Span<byte> outputBufferOffset, float[] downMixParameter, int nodeId)
  31. {
  32. Enabled = true;
  33. NodeId = nodeId;
  34. InputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  35. OutputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  36. for (int i = 0; i < Constants.VoiceChannelCountMax; i++)
  37. {
  38. InputBufferIndices[i] = (ushort)(bufferOffset + inputBufferOffset[i]);
  39. OutputBufferIndices[i] = (ushort)(bufferOffset + outputBufferOffset[i]);
  40. }
  41. Coefficients = downMixParameter;
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. private static float DownMixSurroundToStereo(ReadOnlySpan<float> coefficients, float back, float lfe, float center, float front)
  45. {
  46. return FloatingPointHelper.RoundUp(coefficients[3] * back + coefficients[2] * lfe + coefficients[1] * center + coefficients[0] * front);
  47. }
  48. public void Process(CommandList context)
  49. {
  50. ReadOnlySpan<float> frontLeft = context.GetBuffer(InputBufferIndices[0]);
  51. ReadOnlySpan<float> frontRight = context.GetBuffer(InputBufferIndices[1]);
  52. ReadOnlySpan<float> frontCenter = context.GetBuffer(InputBufferIndices[2]);
  53. ReadOnlySpan<float> lowFrequency = context.GetBuffer(InputBufferIndices[3]);
  54. ReadOnlySpan<float> backLeft = context.GetBuffer(InputBufferIndices[4]);
  55. ReadOnlySpan<float> backRight = context.GetBuffer(InputBufferIndices[5]);
  56. Span<float> stereoLeft = context.GetBuffer(OutputBufferIndices[0]);
  57. Span<float> stereoRight = context.GetBuffer(OutputBufferIndices[1]);
  58. for (int i = 0; i < context.SampleCount; i++)
  59. {
  60. stereoLeft[i] = DownMixSurroundToStereo(Coefficients, backLeft[i], lowFrequency[i], frontCenter[i], frontLeft[i]);
  61. stereoRight[i] = DownMixSurroundToStereo(Coefficients, backRight[i], lowFrequency[i], frontCenter[i], frontRight[i]);
  62. }
  63. context.ClearBuffer(OutputBufferIndices[2]);
  64. context.ClearBuffer(OutputBufferIndices[3]);
  65. context.ClearBuffer(OutputBufferIndices[4]);
  66. context.ClearBuffer(OutputBufferIndices[5]);
  67. }
  68. }
  69. }