ReverbState.cs 8.7 KB

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