BiquadFilterHelper.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Ryujinx.Audio.Renderer.Dsp.State;
  2. using Ryujinx.Audio.Renderer.Parameter;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace Ryujinx.Audio.Renderer.Dsp
  6. {
  7. public static class BiquadFilterHelper
  8. {
  9. private const int FixedPointPrecisionForParameter = 14;
  10. /// <summary>
  11. /// Apply a single biquad filter.
  12. /// </summary>
  13. /// <remarks>This is implemented with a direct form 2.</remarks>
  14. /// <param name="parameter">The biquad filter parameter</param>
  15. /// <param name="state">The biquad filter state</param>
  16. /// <param name="outputBuffer">The output buffer to write the result</param>
  17. /// <param name="inputBuffer">The input buffer to write the result</param>
  18. /// <param name="sampleCount">The count of samples to process</param>
  19. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  20. public static void ProcessBiquadFilter(ref BiquadFilterParameter parameter, ref BiquadFilterState state, Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, uint sampleCount)
  21. {
  22. float a0 = FixedPointHelper.ToFloat(parameter.Numerator[0], FixedPointPrecisionForParameter);
  23. float a1 = FixedPointHelper.ToFloat(parameter.Numerator[1], FixedPointPrecisionForParameter);
  24. float a2 = FixedPointHelper.ToFloat(parameter.Numerator[2], FixedPointPrecisionForParameter);
  25. float b1 = FixedPointHelper.ToFloat(parameter.Denominator[0], FixedPointPrecisionForParameter);
  26. float b2 = FixedPointHelper.ToFloat(parameter.Denominator[1], FixedPointPrecisionForParameter);
  27. for (int i = 0; i < sampleCount; i++)
  28. {
  29. float input = inputBuffer[i];
  30. float output = input * a0 + state.State0;
  31. state.State0 = input * a1 + output * b1 + state.State1;
  32. state.State1 = input * a2 + output * b2;
  33. outputBuffer[i] = output;
  34. }
  35. }
  36. /// <summary>
  37. /// Apply multiple biquad filter.
  38. /// </summary>
  39. /// <remarks>This is implemented with a direct form 1.</remarks>
  40. /// <param name="parameters">The biquad filter parameter</param>
  41. /// <param name="states">The biquad filter state</param>
  42. /// <param name="outputBuffer">The output buffer to write the result</param>
  43. /// <param name="inputBuffer">The input buffer to write the result</param>
  44. /// <param name="sampleCount">The count of samples to process</param>
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public static void ProcessBiquadFilter(ReadOnlySpan<BiquadFilterParameter> parameters, Span<BiquadFilterState> states, Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, uint sampleCount)
  47. {
  48. for (int stageIndex = 0; stageIndex < parameters.Length; stageIndex++)
  49. {
  50. BiquadFilterParameter parameter = parameters[stageIndex];
  51. ref BiquadFilterState state = ref states[stageIndex];
  52. float a0 = FixedPointHelper.ToFloat(parameter.Numerator[0], FixedPointPrecisionForParameter);
  53. float a1 = FixedPointHelper.ToFloat(parameter.Numerator[1], FixedPointPrecisionForParameter);
  54. float a2 = FixedPointHelper.ToFloat(parameter.Numerator[2], FixedPointPrecisionForParameter);
  55. float b1 = FixedPointHelper.ToFloat(parameter.Denominator[0], FixedPointPrecisionForParameter);
  56. float b2 = FixedPointHelper.ToFloat(parameter.Denominator[1], FixedPointPrecisionForParameter);
  57. for (int i = 0; i < sampleCount; i++)
  58. {
  59. float input = inputBuffer[i];
  60. float output = input * a0 + state.State0 * a1 + state.State1 * a2 + state.State2 * b1 + state.State3 * b2;
  61. state.State1 = state.State0;
  62. state.State0 = input;
  63. state.State3 = state.State2;
  64. state.State2 = output;
  65. outputBuffer[i] = output;
  66. }
  67. }
  68. }
  69. }
  70. }