LimiterCommandVersion1.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Renderer.Dsp.State;
  18. using Ryujinx.Audio.Renderer.Parameter.Effect;
  19. using System;
  20. using System.Diagnostics;
  21. using System.Runtime.CompilerServices;
  22. namespace Ryujinx.Audio.Renderer.Dsp.Command
  23. {
  24. public class LimiterCommandVersion1 : ICommand
  25. {
  26. public bool Enabled { get; set; }
  27. public int NodeId { get; }
  28. public CommandType CommandType => CommandType.LimiterVersion1;
  29. public ulong EstimatedProcessingTime { get; set; }
  30. public LimiterParameter Parameter => _parameter;
  31. public Memory<LimiterState> State { get; }
  32. public ulong WorkBuffer { get; }
  33. public ushort[] OutputBufferIndices { get; }
  34. public ushort[] InputBufferIndices { get; }
  35. public bool IsEffectEnabled { get; }
  36. private LimiterParameter _parameter;
  37. public LimiterCommandVersion1(uint bufferOffset, LimiterParameter parameter, Memory<LimiterState> state, bool isEnabled, ulong workBuffer, int nodeId)
  38. {
  39. Enabled = true;
  40. NodeId = nodeId;
  41. _parameter = parameter;
  42. State = state;
  43. WorkBuffer = workBuffer;
  44. IsEffectEnabled = isEnabled;
  45. InputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  46. OutputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  47. for (int i = 0; i < Parameter.ChannelCount; i++)
  48. {
  49. InputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Input[i]);
  50. OutputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Output[i]);
  51. }
  52. }
  53. public void Process(CommandList context)
  54. {
  55. ref LimiterState state = ref State.Span[0];
  56. if (IsEffectEnabled)
  57. {
  58. if (Parameter.Status == Server.Effect.UsageState.Invalid)
  59. {
  60. state = new LimiterState(ref _parameter, WorkBuffer);
  61. }
  62. else if (Parameter.Status == Server.Effect.UsageState.New)
  63. {
  64. state.UpdateParameter(ref _parameter);
  65. }
  66. }
  67. ProcessLimiter(context, ref state);
  68. }
  69. private unsafe void ProcessLimiter(CommandList context, ref LimiterState state)
  70. {
  71. Debug.Assert(Parameter.IsChannelCountValid());
  72. if (IsEffectEnabled && Parameter.IsChannelCountValid())
  73. {
  74. Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  75. Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  76. for (int i = 0; i < Parameter.ChannelCount; i++)
  77. {
  78. inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
  79. outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
  80. }
  81. for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
  82. {
  83. for (int sampleIndex = 0; sampleIndex < context.SampleCount; sampleIndex++)
  84. {
  85. float rawInputSample = *((float*)inputBuffers[channelIndex] + sampleIndex);
  86. float inputSample = (rawInputSample / short.MaxValue) * Parameter.InputGain;
  87. float sampleInputMax = Math.Abs(inputSample);
  88. float inputCoefficient = Parameter.ReleaseCoefficient;
  89. if (sampleInputMax > state.DectectorAverage[channelIndex])
  90. {
  91. inputCoefficient = Parameter.AttackCoefficient;
  92. }
  93. state.DectectorAverage[channelIndex] += inputCoefficient * (sampleInputMax - state.DectectorAverage[channelIndex]);
  94. float attenuation = 1.0f;
  95. if (state.DectectorAverage[channelIndex] > Parameter.Threshold)
  96. {
  97. attenuation = Parameter.Threshold / state.DectectorAverage[channelIndex];
  98. }
  99. float outputCoefficient = Parameter.ReleaseCoefficient;
  100. if (state.CompressionGain[channelIndex] > attenuation)
  101. {
  102. outputCoefficient = Parameter.AttackCoefficient;
  103. }
  104. state.CompressionGain[channelIndex] += outputCoefficient * (attenuation - state.CompressionGain[channelIndex]);
  105. ref float delayedSample = ref state.DelayedSampleBuffer[channelIndex * Parameter.DelayBufferSampleCountMax + state.DelayedSampleBufferPosition[channelIndex]];
  106. float outputSample = delayedSample * state.CompressionGain[channelIndex] * Parameter.OutputGain;
  107. *((float*)outputBuffers[channelIndex] + sampleIndex) = outputSample * short.MaxValue;
  108. delayedSample = inputSample;
  109. state.DelayedSampleBufferPosition[channelIndex]++;
  110. while (state.DelayedSampleBufferPosition[channelIndex] >= Parameter.DelayBufferSampleCountMin)
  111. {
  112. state.DelayedSampleBufferPosition[channelIndex] -= Parameter.DelayBufferSampleCountMin;
  113. }
  114. }
  115. }
  116. }
  117. else
  118. {
  119. for (int i = 0; i < Parameter.ChannelCount; i++)
  120. {
  121. if (InputBufferIndices[i] != OutputBufferIndices[i])
  122. {
  123. context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }