Reverb3dState.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using Ryujinx.Audio.Renderer.Dsp.Effect;
  18. using Ryujinx.Audio.Renderer.Parameter.Effect;
  19. using System;
  20. namespace Ryujinx.Audio.Renderer.Dsp.State
  21. {
  22. public class Reverb3dState
  23. {
  24. private readonly float[] FdnDelayMinTimes = new float[4] { 5.0f, 6.0f, 13.0f, 14.0f };
  25. private readonly float[] FdnDelayMaxTimes = new float[4] { 45.704f, 82.782f, 149.94f, 271.58f };
  26. private readonly float[] DecayDelayMaxTimes1 = new float[4] { 17.0f, 13.0f, 9.0f, 7.0f };
  27. private readonly float[] DecayDelayMaxTimes2 = new float[4] { 19.0f, 11.0f, 10.0f, 6.0f };
  28. 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 };
  29. 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 };
  30. public IDelayLine[] FdnDelayLines { get; }
  31. public DecayDelay[] DecayDelays1 { get; }
  32. public DecayDelay[] DecayDelays2 { get; }
  33. public IDelayLine PreDelayLine { get; }
  34. public IDelayLine BackLeftDelayLine { get; }
  35. public float DryGain { get; private set; }
  36. public uint[] EarlyDelayTime { get; private set; }
  37. public float PreviousPreDelayValue { get; set; }
  38. public float PreviousPreDelayGain { get; private set; }
  39. public float TargetPreDelayGain { get; private set; }
  40. public float EarlyReflectionsGain { get; private set; }
  41. public float LateReverbGain { get; private set; }
  42. public uint ReflectionDelayTime { get; private set; }
  43. public float EchoLateReverbDecay { get; private set; }
  44. public float[] DecayDirectFdnGain { get; private set; }
  45. public float[] DecayCurrentFdnGain { get; private set; }
  46. public float[] DecayCurrentOutputGain { get; private set; }
  47. public float[] PreviousFeedbackOutputDecayed { get; private set; }
  48. public Reverb3dState(ref Reverb3dParameter parameter, ulong workBuffer)
  49. {
  50. FdnDelayLines = new IDelayLine[4];
  51. DecayDelays1 = new DecayDelay[4];
  52. DecayDelays2 = new DecayDelay[4];
  53. DecayDirectFdnGain = new float[4];
  54. DecayCurrentFdnGain = new float[4];
  55. DecayCurrentOutputGain = new float[4];
  56. PreviousFeedbackOutputDecayed = new float[4];
  57. uint sampleRate = parameter.SampleRate / 1000;
  58. for (int i = 0; i < 4; i++)
  59. {
  60. FdnDelayLines[i] = new DelayLine3d(sampleRate, FdnDelayMaxTimes[i]);
  61. DecayDelays1[i] = new DecayDelay(new DelayLine3d(sampleRate, DecayDelayMaxTimes1[i]));
  62. DecayDelays2[i] = new DecayDelay(new DelayLine3d(sampleRate, DecayDelayMaxTimes2[i]));
  63. }
  64. PreDelayLine = new DelayLine3d(sampleRate, 400);
  65. BackLeftDelayLine = new DelayLine3d(sampleRate, 5);
  66. UpdateParameter(ref parameter);
  67. }
  68. public void UpdateParameter(ref Reverb3dParameter parameter)
  69. {
  70. uint sampleRate = parameter.SampleRate / 1000;
  71. EarlyDelayTime = new uint[20];
  72. DryGain = parameter.DryGain;
  73. PreviousFeedbackOutputDecayed.AsSpan().Fill(0);
  74. PreviousPreDelayValue = 0;
  75. EarlyReflectionsGain = FloatingPointHelper.Pow10(Math.Min(parameter.RoomGain + parameter.ReflectionsGain, 5000.0f) / 2000.0f);
  76. LateReverbGain = FloatingPointHelper.Pow10(Math.Min(parameter.RoomGain + parameter.ReverbGain, 5000.0f) / 2000.0f);
  77. float highFrequencyRoomGain = FloatingPointHelper.Pow10(parameter.RoomHf / 2000.0f);
  78. if (highFrequencyRoomGain < 1.0f)
  79. {
  80. float tempA = 1.0f - highFrequencyRoomGain;
  81. float tempB = 2.0f - ((2.0f * highFrequencyRoomGain) * FloatingPointHelper.Cos(256.0f * parameter.HfReference / parameter.SampleRate));
  82. float tempC = MathF.Sqrt(MathF.Pow(tempB, 2) - (4.0f * (1.0f - highFrequencyRoomGain) * (1.0f - highFrequencyRoomGain)));
  83. PreviousPreDelayGain = (tempB - tempC) / (2.0f * tempA);
  84. TargetPreDelayGain = 1.0f - PreviousPreDelayGain;
  85. }
  86. else
  87. {
  88. PreviousPreDelayGain = 0.0f;
  89. TargetPreDelayGain = 1.0f;
  90. }
  91. ReflectionDelayTime = IDelayLine.GetSampleCount(sampleRate, 1000.0f * (parameter.ReflectionDelay + parameter.ReverbDelayTime));
  92. EchoLateReverbDecay = 0.6f * parameter.Diffusion * 0.01f;
  93. for (int i = 0; i < FdnDelayLines.Length; i++)
  94. {
  95. FdnDelayLines[i].SetDelay(FdnDelayMinTimes[i] + (parameter.Density / 100 * (FdnDelayMaxTimes[i] - FdnDelayMinTimes[i])));
  96. uint tempSampleCount = FdnDelayLines[i].CurrentSampleCount + DecayDelays1[i].CurrentSampleCount + DecayDelays2[i].CurrentSampleCount;
  97. float tempA = (-60.0f * tempSampleCount) / (parameter.DecayTime * parameter.SampleRate);
  98. float tempB = tempA / parameter.HfDecayRatio;
  99. float tempC = FloatingPointHelper.Cos(128.0f * 0.5f * parameter.HfReference / parameter.SampleRate) / FloatingPointHelper.Sin(128.0f * 0.5f * parameter.HfReference / parameter.SampleRate);
  100. float tempD = FloatingPointHelper.Pow10((tempB - tempA) / 40.0f);
  101. float tempE = FloatingPointHelper.Pow10((tempB + tempA) / 40.0f) * 0.7071f;
  102. DecayDirectFdnGain[i] = tempE * ((tempD * tempC) + 1.0f) / (tempC + tempD);
  103. DecayCurrentFdnGain[i] = tempE * (1.0f - (tempD * tempC)) / (tempC + tempD);
  104. DecayCurrentOutputGain[i] = (tempC - tempD) / (tempC + tempD);
  105. DecayDelays1[i].SetDecayRate(EchoLateReverbDecay);
  106. DecayDelays2[i].SetDecayRate(EchoLateReverbDecay * -0.9f);
  107. }
  108. for (int i = 0; i < EarlyDelayTime.Length; i++)
  109. {
  110. uint sampleCount = Math.Min(IDelayLine.GetSampleCount(sampleRate, (parameter.ReflectionDelay * 1000.0f) + (EarlyDelayTimes[i] * 1000.0f * ((parameter.ReverbDelayTime * 0.9998f) + 0.02f))), PreDelayLine.SampleCountMax);
  111. EarlyDelayTime[i] = sampleCount;
  112. }
  113. }
  114. }
  115. }