LimiterCommandVersion1.cs 5.7 KB

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