Reverb3dState.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Ryujinx.Audio.Renderer.Dsp.Effect;
  2. using Ryujinx.Audio.Renderer.Parameter.Effect;
  3. using System;
  4. namespace Ryujinx.Audio.Renderer.Dsp.State
  5. {
  6. public class Reverb3dState
  7. {
  8. private readonly float[] FdnDelayMinTimes = new float[4] { 5.0f, 6.0f, 13.0f, 14.0f };
  9. private readonly float[] FdnDelayMaxTimes = new float[4] { 45.704f, 82.782f, 149.94f, 271.58f };
  10. private readonly float[] DecayDelayMaxTimes1 = new float[4] { 17.0f, 13.0f, 9.0f, 7.0f };
  11. private readonly float[] DecayDelayMaxTimes2 = new float[4] { 19.0f, 11.0f, 10.0f, 6.0f };
  12. private readonly float[] EarlyDelayTimes = new float[20] { 0.017136f, 0.059154f, 0.16173f, 0.39019f, 0.42526f, 0.45541f, 0.68974f, 0.74591f, 0.83384f, 0.8595f, 0.0f, 0.075024f, 0.16879f, 0.2999f, 0.33744f, 0.3719f, 0.59901f, 0.71674f, 0.81786f, 0.85166f };
  13. public readonly float[] EarlyGain = new float[20] { 0.67096f, 0.61027f, 1.0f, 0.35680f, 0.68361f, 0.65978f, 0.51939f, 0.24712f, 0.45945f, 0.45021f, 0.64196f, 0.54879f, 0.92925f, 0.38270f, 0.72867f, 0.69794f, 0.5464f, 0.24563f, 0.45214f, 0.44042f };
  14. public IDelayLine[] FdnDelayLines { get; }
  15. public DecayDelay[] DecayDelays1 { get; }
  16. public DecayDelay[] DecayDelays2 { get; }
  17. public IDelayLine PreDelayLine { get; }
  18. public IDelayLine FrontCenterDelayLine { get; }
  19. public float DryGain { get; private set; }
  20. public uint[] EarlyDelayTime { get; private set; }
  21. public float PreviousPreDelayValue { get; set; }
  22. public float PreviousPreDelayGain { get; private set; }
  23. public float TargetPreDelayGain { get; private set; }
  24. public float EarlyReflectionsGain { get; private set; }
  25. public float LateReverbGain { get; private set; }
  26. public uint ReflectionDelayTime { get; private set; }
  27. public float EchoLateReverbDecay { get; private set; }
  28. public float[] DecayDirectFdnGain { get; private set; }
  29. public float[] DecayCurrentFdnGain { get; private set; }
  30. public float[] DecayCurrentOutputGain { get; private set; }
  31. public float[] PreviousFeedbackOutputDecayed { get; private set; }
  32. public Reverb3dState(ref Reverb3dParameter parameter, ulong workBuffer)
  33. {
  34. FdnDelayLines = new IDelayLine[4];
  35. DecayDelays1 = new DecayDelay[4];
  36. DecayDelays2 = new DecayDelay[4];
  37. DecayDirectFdnGain = new float[4];
  38. DecayCurrentFdnGain = new float[4];
  39. DecayCurrentOutputGain = new float[4];
  40. PreviousFeedbackOutputDecayed = new float[4];
  41. uint sampleRate = parameter.SampleRate / 1000;
  42. for (int i = 0; i < 4; i++)
  43. {
  44. FdnDelayLines[i] = new DelayLine3d(sampleRate, FdnDelayMaxTimes[i]);
  45. DecayDelays1[i] = new DecayDelay(new DelayLine3d(sampleRate, DecayDelayMaxTimes1[i]));
  46. DecayDelays2[i] = new DecayDelay(new DelayLine3d(sampleRate, DecayDelayMaxTimes2[i]));
  47. }
  48. PreDelayLine = new DelayLine3d(sampleRate, 400);
  49. FrontCenterDelayLine = new DelayLine3d(sampleRate, 5);
  50. UpdateParameter(ref parameter);
  51. }
  52. public void UpdateParameter(ref Reverb3dParameter parameter)
  53. {
  54. uint sampleRate = parameter.SampleRate / 1000;
  55. EarlyDelayTime = new uint[20];
  56. DryGain = parameter.DryGain;
  57. PreviousFeedbackOutputDecayed.AsSpan().Fill(0);
  58. PreviousPreDelayValue = 0;
  59. EarlyReflectionsGain = FloatingPointHelper.Pow10(Math.Min(parameter.RoomGain + parameter.ReflectionsGain, 5000.0f) / 2000.0f);
  60. LateReverbGain = FloatingPointHelper.Pow10(Math.Min(parameter.RoomGain + parameter.ReverbGain, 5000.0f) / 2000.0f);
  61. float highFrequencyRoomGain = FloatingPointHelper.Pow10(parameter.RoomHf / 2000.0f);
  62. if (highFrequencyRoomGain < 1.0f)
  63. {
  64. float tempA = 1.0f - highFrequencyRoomGain;
  65. float tempB = 2.0f - ((2.0f * highFrequencyRoomGain) * FloatingPointHelper.Cos(256.0f * parameter.HfReference / parameter.SampleRate));
  66. float tempC = MathF.Sqrt(MathF.Pow(tempB, 2) - (4.0f * (1.0f - highFrequencyRoomGain) * (1.0f - highFrequencyRoomGain)));
  67. PreviousPreDelayGain = (tempB - tempC) / (2.0f * tempA);
  68. TargetPreDelayGain = 1.0f - PreviousPreDelayGain;
  69. }
  70. else
  71. {
  72. PreviousPreDelayGain = 0.0f;
  73. TargetPreDelayGain = 1.0f;
  74. }
  75. ReflectionDelayTime = IDelayLine.GetSampleCount(sampleRate, 1000.0f * (parameter.ReflectionDelay + parameter.ReverbDelayTime));
  76. EchoLateReverbDecay = 0.6f * parameter.Diffusion * 0.01f;
  77. for (int i = 0; i < FdnDelayLines.Length; i++)
  78. {
  79. FdnDelayLines[i].SetDelay(FdnDelayMinTimes[i] + (parameter.Density / 100 * (FdnDelayMaxTimes[i] - FdnDelayMinTimes[i])));
  80. uint tempSampleCount = FdnDelayLines[i].CurrentSampleCount + DecayDelays1[i].CurrentSampleCount + DecayDelays2[i].CurrentSampleCount;
  81. float tempA = (-60.0f * tempSampleCount) / (parameter.DecayTime * parameter.SampleRate);
  82. float tempB = tempA / parameter.HfDecayRatio;
  83. float tempC = FloatingPointHelper.Cos(128.0f * 0.5f * parameter.HfReference / parameter.SampleRate) / FloatingPointHelper.Sin(128.0f * 0.5f * parameter.HfReference / parameter.SampleRate);
  84. float tempD = FloatingPointHelper.Pow10((tempB - tempA) / 40.0f);
  85. float tempE = FloatingPointHelper.Pow10((tempB + tempA) / 40.0f) * 0.7071f;
  86. DecayDirectFdnGain[i] = tempE * ((tempD * tempC) + 1.0f) / (tempC + tempD);
  87. DecayCurrentFdnGain[i] = tempE * (1.0f - (tempD * tempC)) / (tempC + tempD);
  88. DecayCurrentOutputGain[i] = (tempC - tempD) / (tempC + tempD);
  89. DecayDelays1[i].SetDecayRate(EchoLateReverbDecay);
  90. DecayDelays2[i].SetDecayRate(EchoLateReverbDecay * -0.9f);
  91. }
  92. for (int i = 0; i < EarlyDelayTime.Length; i++)
  93. {
  94. uint sampleCount = Math.Min(IDelayLine.GetSampleCount(sampleRate, (parameter.ReflectionDelay * 1000.0f) + (EarlyDelayTimes[i] * 1000.0f * ((parameter.ReverbDelayTime * 0.9998f) + 0.02f))), PreDelayLine.SampleCountMax);
  95. EarlyDelayTime[i] = sampleCount;
  96. }
  97. }
  98. }
  99. }