DownMixSurroundToStereoCommand.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // Copyright (c) 2019-2020 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, ReadOnlySpan<float> downMixParameter, int nodeId)
  31. {
  32. Enabled = true;
  33. NodeId = nodeId;
  34. InputBufferIndices = new ushort[RendererConstants.VoiceChannelCountMax];
  35. OutputBufferIndices = new ushort[RendererConstants.VoiceChannelCountMax];
  36. for (int i = 0; i < RendererConstants.VoiceChannelCountMax; i++)
  37. {
  38. InputBufferIndices[i] = (ushort)(bufferOffset + inputBufferOffset[i]);
  39. OutputBufferIndices[i] = (ushort)(bufferOffset + outputBufferOffset[i]);
  40. }
  41. Coefficients = downMixParameter.ToArray();
  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. Span<float> unused2 = context.GetBuffer(OutputBufferIndices[2]);
  59. Span<float> unused3 = context.GetBuffer(OutputBufferIndices[3]);
  60. Span<float> unused4 = context.GetBuffer(OutputBufferIndices[4]);
  61. Span<float> unused5 = context.GetBuffer(OutputBufferIndices[5]);
  62. for (int i = 0; i < context.SampleCount; i++)
  63. {
  64. stereoLeft[i] = DownMixSurroundToStereo(Coefficients, backLeft[i], lowFrequency[i], frontCenter[i], frontLeft[i]);
  65. stereoRight[i] = DownMixSurroundToStereo(Coefficients, backRight[i], lowFrequency[i], frontCenter[i], frontRight[i]);
  66. }
  67. unused2.Fill(0);
  68. unused3.Fill(0);
  69. unused4.Fill(0);
  70. unused5.Fill(0);
  71. }
  72. }
  73. }