LimiterCommandVersion2.cs 6.7 KB

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