LimiterCommandVersion2.cs 6.6 KB

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