LimiterCommandVersion1.cs 6.3 KB

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