DepopForMixBuffersCommand.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.Diagnostics;
  19. using System.Runtime.CompilerServices;
  20. namespace Ryujinx.Audio.Renderer.Dsp.Command
  21. {
  22. public class DepopForMixBuffersCommand : ICommand
  23. {
  24. public bool Enabled { get; set; }
  25. public int NodeId { get; }
  26. public CommandType CommandType => CommandType.DepopForMixBuffers;
  27. public ulong EstimatedProcessingTime { get; set; }
  28. public uint MixBufferOffset { get; }
  29. public uint MixBufferCount { get; }
  30. public float Decay { get; }
  31. public Memory<float> DepopBuffer { get; }
  32. public DepopForMixBuffersCommand(Memory<float> depopBuffer, uint bufferOffset, uint mixBufferCount, int nodeId, uint sampleRate)
  33. {
  34. Enabled = true;
  35. NodeId = nodeId;
  36. MixBufferOffset = bufferOffset;
  37. MixBufferCount = mixBufferCount;
  38. DepopBuffer = depopBuffer;
  39. if (sampleRate == 48000)
  40. {
  41. Decay = 0.962189f;
  42. }
  43. else // if (sampleRate == 32000)
  44. {
  45. Decay = 0.943695f;
  46. }
  47. }
  48. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. private unsafe float ProcessDepopMix(float* buffer, float depopValue, uint sampleCount)
  50. {
  51. if (depopValue < 0)
  52. {
  53. depopValue = -depopValue;
  54. for (int i = 0; i < sampleCount; i++)
  55. {
  56. depopValue = FloatingPointHelper.MultiplyRoundDown(Decay, depopValue);
  57. buffer[i] -= depopValue;
  58. }
  59. return -depopValue;
  60. }
  61. else
  62. {
  63. for (int i = 0; i < sampleCount; i++)
  64. {
  65. depopValue = FloatingPointHelper.MultiplyRoundDown(Decay, depopValue);
  66. buffer[i] += depopValue;
  67. }
  68. return depopValue;
  69. }
  70. }
  71. public void Process(CommandList context)
  72. {
  73. Span<float> depopBuffer = DepopBuffer.Span;
  74. uint bufferCount = Math.Min(MixBufferOffset + MixBufferCount, context.BufferCount);
  75. for (int i = (int)MixBufferOffset; i < bufferCount; i++)
  76. {
  77. float depopValue = depopBuffer[i];
  78. if (depopValue != 0)
  79. {
  80. unsafe
  81. {
  82. float* buffer = (float*)context.GetBufferPointer(i);
  83. depopBuffer[i] = ProcessDepopMix(buffer, depopValue, context.SampleCount);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }