ReverbState.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Ryujinx.Audio.Renderer.Common;
  2. using Ryujinx.Audio.Renderer.Dsp.Effect;
  3. using Ryujinx.Audio.Renderer.Parameter.Effect;
  4. using System;
  5. namespace Ryujinx.Audio.Renderer.Dsp.State
  6. {
  7. public class ReverbState
  8. {
  9. private static readonly float[] FdnDelayTimes = new float[20]
  10. {
  11. // Room
  12. 53.953247f, 79.192566f, 116.238770f, 130.615295f,
  13. // Hall
  14. 53.953247f, 79.192566f, 116.238770f, 170.615295f,
  15. // Plate
  16. 5f, 10f, 5f, 10f,
  17. // Cathedral
  18. 47.03f, 71f, 103f, 170f,
  19. // Max delay (Hall is the one with the highest values so identical to Hall)
  20. 53.953247f, 79.192566f, 116.238770f, 170.615295f,
  21. };
  22. private static readonly float[] DecayDelayTimes = new float[20]
  23. {
  24. // Room
  25. 7f, 9f, 13f, 17f,
  26. // Hall
  27. 7f, 9f, 13f, 17f,
  28. // Plate (no decay)
  29. 1f, 1f, 1f, 1f,
  30. // Cathedral
  31. 7f, 7f, 13f, 9f,
  32. // Max delay (Hall is the one with the highest values so identical to Hall)
  33. 7f, 9f, 13f, 17f,
  34. };
  35. private static readonly float[] EarlyDelayTimes = new float[50]
  36. {
  37. // Room
  38. 0.0f, 3.5f, 2.8f, 3.9f, 2.7f, 13.4f, 7.9f, 8.4f, 9.9f, 12.0f,
  39. // Chamber
  40. 0.0f, 11.8f, 5.5f, 11.2f, 10.4f, 38.1f, 22.2f, 29.6f, 21.2f, 24.8f,
  41. // Hall
  42. 0.0f, 41.5f, 20.5f, 41.3f, 0.0f, 29.5f, 33.8f, 45.2f, 46.8f, 0.0f,
  43. // Cathedral
  44. 33.1f, 43.3f, 22.8f, 37.9f, 14.9f, 35.3f, 17.9f, 34.2f, 0.0f, 43.3f,
  45. // Disabled
  46. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
  47. };
  48. private static readonly float[] EarlyGainBase = new float[50]
  49. {
  50. // Room
  51. 0.70f, 0.68f, 0.70f, 0.68f, 0.70f, 0.68f, 0.70f, 0.68f, 0.68f, 0.68f,
  52. // Chamber
  53. 0.70f, 0.68f, 0.70f, 0.68f, 0.70f, 0.68f, 0.68f, 0.68f, 0.68f, 0.68f,
  54. // Hall
  55. 0.50f, 0.70f, 0.70f, 0.68f, 0.50f, 0.68f, 0.68f, 0.70f, 0.68f, 0.00f,
  56. // Cathedral
  57. 0.93f, 0.92f, 0.87f, 0.86f, 0.94f, 0.81f, 0.80f, 0.77f, 0.76f, 0.65f,
  58. // Disabled
  59. 0.00f, 0.00f, 0.00f, 0.00f, 0.00f, 0.00f, 0.00f, 0.00f, 0.00f, 0.00f
  60. };
  61. private static readonly float[] PreDelayTimes = new float[5]
  62. {
  63. // Room
  64. 12.5f,
  65. // Chamber
  66. 40.0f,
  67. // Hall
  68. 50.0f,
  69. // Cathedral
  70. 50.0f,
  71. // Disabled
  72. 0.0f
  73. };
  74. public DelayLine[] FdnDelayLines { get; }
  75. public DecayDelay[] DecayDelays { get; }
  76. public DelayLine PreDelayLine { get; }
  77. public DelayLine FrontCenterDelayLine { get; }
  78. public uint[] EarlyDelayTime { get; }
  79. public float[] EarlyGain { get; }
  80. public uint PreDelayLineDelayTime { get; private set; }
  81. public float[] HighFrequencyDecayDirectGain { get; }
  82. public float[] HighFrequencyDecayPreviousGain { get; }
  83. public float[] PreviousFeedbackOutput { get; }
  84. public const int EarlyModeCount = 10;
  85. private const int FixedPointPrecision = 14;
  86. private ReadOnlySpan<float> GetFdnDelayTimesByLateMode(ReverbLateMode lateMode)
  87. {
  88. return FdnDelayTimes.AsSpan((int)lateMode * 4, 4);
  89. }
  90. private ReadOnlySpan<float> GetDecayDelayTimesByLateMode(ReverbLateMode lateMode)
  91. {
  92. return DecayDelayTimes.AsSpan((int)lateMode * 4, 4);
  93. }
  94. public ReverbState(ref ReverbParameter parameter, ulong workBuffer, bool isLongSizePreDelaySupported)
  95. {
  96. FdnDelayLines = new DelayLine[4];
  97. DecayDelays = new DecayDelay[4];
  98. EarlyDelayTime = new uint[EarlyModeCount];
  99. EarlyGain = new float[EarlyModeCount];
  100. HighFrequencyDecayDirectGain = new float[4];
  101. HighFrequencyDecayPreviousGain = new float[4];
  102. PreviousFeedbackOutput = new float[4];
  103. ReadOnlySpan<float> fdnDelayTimes = GetFdnDelayTimesByLateMode(ReverbLateMode.Limit);
  104. ReadOnlySpan<float> decayDelayTimes = GetDecayDelayTimesByLateMode(ReverbLateMode.Limit);
  105. uint sampleRate = (uint)FixedPointHelper.ToFloat((uint)parameter.SampleRate, FixedPointPrecision);
  106. for (int i = 0; i < 4; i++)
  107. {
  108. FdnDelayLines[i] = new DelayLine(sampleRate, fdnDelayTimes[i]);
  109. DecayDelays[i] = new DecayDelay(new DelayLine(sampleRate, decayDelayTimes[i]));
  110. }
  111. float preDelayTimeMax = 150.0f;
  112. if (isLongSizePreDelaySupported)
  113. {
  114. preDelayTimeMax = 350.0f;
  115. }
  116. PreDelayLine = new DelayLine(sampleRate, preDelayTimeMax);
  117. FrontCenterDelayLine = new DelayLine(sampleRate, 5.0f);
  118. UpdateParameter(ref parameter);
  119. }
  120. public void UpdateParameter(ref ReverbParameter parameter)
  121. {
  122. uint sampleRate = (uint)FixedPointHelper.ToFloat((uint)parameter.SampleRate, FixedPointPrecision);
  123. float preDelayTimeInMilliseconds = FixedPointHelper.ToFloat(parameter.PreDelayTime, FixedPointPrecision);
  124. float earlyGain = FixedPointHelper.ToFloat(parameter.EarlyGain, FixedPointPrecision);
  125. float coloration = FixedPointHelper.ToFloat(parameter.Coloration, FixedPointPrecision);
  126. float decayTime = FixedPointHelper.ToFloat(parameter.DecayTime, FixedPointPrecision);
  127. for (int i = 0; i < 10; i++)
  128. {
  129. EarlyDelayTime[i] = Math.Min(IDelayLine.GetSampleCount(sampleRate, EarlyDelayTimes[i] + preDelayTimeInMilliseconds), PreDelayLine.SampleCountMax) + 1;
  130. EarlyGain[i] = EarlyGainBase[i] * earlyGain;
  131. }
  132. if (parameter.ChannelCount == 2)
  133. {
  134. EarlyGain[4] = EarlyGain[4] * 0.5f;
  135. EarlyGain[5] = EarlyGain[5] * 0.5f;
  136. }
  137. PreDelayLineDelayTime = Math.Min(IDelayLine.GetSampleCount(sampleRate, PreDelayTimes[(int)parameter.EarlyMode] + preDelayTimeInMilliseconds), PreDelayLine.SampleCountMax);
  138. ReadOnlySpan<float> fdnDelayTimes = GetFdnDelayTimesByLateMode(parameter.LateMode);
  139. ReadOnlySpan<float> decayDelayTimes = GetDecayDelayTimesByLateMode(parameter.LateMode);
  140. float highFrequencyDecayRatio = FixedPointHelper.ToFloat(parameter.HighFrequencyDecayRatio, FixedPointPrecision);
  141. float highFrequencyUnknownValue = FloatingPointHelper.Cos(1280.0f / sampleRate);
  142. for (int i = 0; i < 4; i++)
  143. {
  144. FdnDelayLines[i].SetDelay(fdnDelayTimes[i]);
  145. DecayDelays[i].SetDelay(decayDelayTimes[i]);
  146. float tempA = -3 * (DecayDelays[i].CurrentSampleCount + FdnDelayLines[i].CurrentSampleCount);
  147. float tempB = tempA / (decayTime * sampleRate);
  148. float tempC;
  149. float tempD;
  150. if (highFrequencyDecayRatio < 0.995f)
  151. {
  152. float tempE = FloatingPointHelper.Pow10((((1.0f / highFrequencyDecayRatio) - 1.0f) * 2) / 100 * (tempB / 10));
  153. float tempF = 1.0f - tempE;
  154. float tempG = 2.0f - (tempE * 2 * highFrequencyUnknownValue);
  155. float tempH = MathF.Sqrt((tempG * tempG) - (tempF * tempF * 4));
  156. tempC = (tempG - tempH) / (tempF * 2);
  157. tempD = 1.0f - tempC;
  158. }
  159. else
  160. {
  161. // no high frequency decay ratio
  162. tempC = 0.0f;
  163. tempD = 1.0f;
  164. }
  165. HighFrequencyDecayDirectGain[i] = FloatingPointHelper.Pow10(tempB / 1000) * tempD * 0.7071f;
  166. HighFrequencyDecayPreviousGain[i] = tempC;
  167. PreviousFeedbackOutput[i] = 0.0f;
  168. DecayDelays[i].SetDecayRate(0.6f * (1.0f - coloration));
  169. }
  170. }
  171. }
  172. }