LimiterCommandVersion2.cs 7.3 KB

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