LimiterCommandVersion2.cs 7.4 KB

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