DepopForMixBuffersCommand.cs 3.1 KB

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