ReverbCommand.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.State;
  18. using Ryujinx.Audio.Renderer.Parameter.Effect;
  19. using System;
  20. using System.Diagnostics;
  21. using System.Runtime.CompilerServices;
  22. namespace Ryujinx.Audio.Renderer.Dsp.Command
  23. {
  24. public class ReverbCommand : ICommand
  25. {
  26. private static readonly int[] OutputEarlyIndicesTableMono = new int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  27. private static readonly int[] TargetEarlyDelayLineIndicesTableMono = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  28. private static readonly int[] OutputIndicesTableMono = new int[4] { 0, 0, 0, 0 };
  29. private static readonly int[] TargetOutputFeedbackIndicesTableMono = new int[4] { 0, 1, 2, 3 };
  30. private static readonly int[] OutputEarlyIndicesTableStereo = new int[10] { 0, 0, 1, 1, 0, 1, 0, 0, 1, 1 };
  31. private static readonly int[] TargetEarlyDelayLineIndicesTableStereo = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  32. private static readonly int[] OutputIndicesTableStereo = new int[4] { 0, 0, 1, 1 };
  33. private static readonly int[] TargetOutputFeedbackIndicesTableStereo = new int[4] { 2, 0, 3, 1 };
  34. private static readonly int[] OutputEarlyIndicesTableQuadraphonic = new int[10] { 0, 0, 1, 1, 0, 1, 2, 2, 3, 3 };
  35. private static readonly int[] TargetEarlyDelayLineIndicesTableQuadraphonic = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  36. private static readonly int[] OutputIndicesTableQuadraphonic = new int[4] { 0, 1, 2, 3 };
  37. private static readonly int[] TargetOutputFeedbackIndicesTableQuadraphonic = new int[4] { 0, 1, 2, 3 };
  38. private static readonly int[] OutputEarlyIndicesTableSurround = new int[20] { 0, 5, 0, 5, 1, 5, 1, 5, 4, 5, 4, 5, 2, 5, 2, 5, 3, 5, 3, 5 };
  39. private static readonly int[] TargetEarlyDelayLineIndicesTableSurround = new int[20] { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9 };
  40. private static readonly int[] OutputIndicesTableSurround = new int[Constants.ChannelCountMax] { 0, 1, 2, 3, 4, 5 };
  41. private static readonly int[] TargetOutputFeedbackIndicesTableSurround = new int[Constants.ChannelCountMax] { 0, 1, 2, 3, -1, 3 };
  42. public bool Enabled { get; set; }
  43. public int NodeId { get; }
  44. public CommandType CommandType => CommandType.Reverb;
  45. public ulong EstimatedProcessingTime { get; set; }
  46. public ReverbParameter Parameter => _parameter;
  47. public Memory<ReverbState> State { get; }
  48. public ulong WorkBuffer { get; }
  49. public ushort[] OutputBufferIndices { get; }
  50. public ushort[] InputBufferIndices { get; }
  51. public bool IsLongSizePreDelaySupported { get; }
  52. public bool IsEffectEnabled { get; }
  53. private ReverbParameter _parameter;
  54. private const int FixedPointPrecision = 14;
  55. public ReverbCommand(uint bufferOffset, ReverbParameter parameter, Memory<ReverbState> state, bool isEnabled, ulong workBuffer, int nodeId, bool isLongSizePreDelaySupported)
  56. {
  57. Enabled = true;
  58. IsEffectEnabled = isEnabled;
  59. NodeId = nodeId;
  60. _parameter = parameter;
  61. State = state;
  62. WorkBuffer = workBuffer;
  63. InputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  64. OutputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  65. for (int i = 0; i < Parameter.ChannelCount; i++)
  66. {
  67. InputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Input[i]);
  68. OutputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Output[i]);
  69. }
  70. IsLongSizePreDelaySupported = isLongSizePreDelaySupported;
  71. }
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. private void ProcessReverbMono(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  74. {
  75. ProcessReverbGeneric(ref state,
  76. outputBuffers,
  77. inputBuffers,
  78. sampleCount,
  79. OutputEarlyIndicesTableMono,
  80. TargetEarlyDelayLineIndicesTableMono,
  81. TargetOutputFeedbackIndicesTableMono,
  82. OutputIndicesTableMono);
  83. }
  84. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  85. private void ProcessReverbStereo(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  86. {
  87. ProcessReverbGeneric(ref state,
  88. outputBuffers,
  89. inputBuffers,
  90. sampleCount,
  91. OutputEarlyIndicesTableStereo,
  92. TargetEarlyDelayLineIndicesTableStereo,
  93. TargetOutputFeedbackIndicesTableStereo,
  94. OutputIndicesTableStereo);
  95. }
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. private void ProcessReverbQuadraphonic(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  98. {
  99. ProcessReverbGeneric(ref state,
  100. outputBuffers,
  101. inputBuffers,
  102. sampleCount,
  103. OutputEarlyIndicesTableQuadraphonic,
  104. TargetEarlyDelayLineIndicesTableQuadraphonic,
  105. TargetOutputFeedbackIndicesTableQuadraphonic,
  106. OutputIndicesTableQuadraphonic);
  107. }
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. private void ProcessReverbSurround(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  110. {
  111. ProcessReverbGeneric(ref state,
  112. outputBuffers,
  113. inputBuffers,
  114. sampleCount,
  115. OutputEarlyIndicesTableSurround,
  116. TargetEarlyDelayLineIndicesTableSurround,
  117. TargetOutputFeedbackIndicesTableSurround,
  118. OutputIndicesTableSurround);
  119. }
  120. private unsafe void ProcessReverbGeneric(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount, ReadOnlySpan<int> outputEarlyIndicesTable, ReadOnlySpan<int> targetEarlyDelayLineIndicesTable, ReadOnlySpan<int> targetOutputFeedbackIndicesTable, ReadOnlySpan<int> outputIndicesTable)
  121. {
  122. bool isSurround = Parameter.ChannelCount == 6;
  123. float reverbGain = FixedPointHelper.ToFloat(Parameter.ReverbGain, FixedPointPrecision);
  124. float lateGain = FixedPointHelper.ToFloat(Parameter.LateGain, FixedPointPrecision);
  125. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  126. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  127. Span<float> outputValues = stackalloc float[Constants.ChannelCountMax];
  128. Span<float> feedbackValues = stackalloc float[4];
  129. Span<float> feedbackOutputValues = stackalloc float[4];
  130. Span<float> channelInput = stackalloc float[Parameter.ChannelCount];
  131. for (int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
  132. {
  133. outputValues.Fill(0);
  134. for (int i = 0; i < targetEarlyDelayLineIndicesTable.Length; i++)
  135. {
  136. int earlyDelayIndex = targetEarlyDelayLineIndicesTable[i];
  137. int outputIndex = outputEarlyIndicesTable[i];
  138. float tapOutput = state.PreDelayLine.TapUnsafe(state.EarlyDelayTime[earlyDelayIndex], 0);
  139. outputValues[outputIndex] += tapOutput * state.EarlyGain[earlyDelayIndex];
  140. }
  141. if (isSurround)
  142. {
  143. outputValues[5] *= 0.2f;
  144. }
  145. float targetPreDelayValue = 0;
  146. for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
  147. {
  148. channelInput[channelIndex] = *((float*)inputBuffers[channelIndex] + sampleIndex) * 64;
  149. targetPreDelayValue += channelInput[channelIndex] * reverbGain;
  150. }
  151. state.PreDelayLine.Update(targetPreDelayValue);
  152. float lateValue = state.PreDelayLine.Tap(state.PreDelayLineDelayTime) * lateGain;
  153. for (int i = 0; i < state.FdnDelayLines.Length; i++)
  154. {
  155. feedbackOutputValues[i] = state.FdnDelayLines[i].Read() * state.HighFrequencyDecayDirectGain[i] + state.PreviousFeedbackOutput[i] * state.HighFrequencyDecayPreviousGain[i];
  156. state.PreviousFeedbackOutput[i] = feedbackOutputValues[i];
  157. }
  158. feedbackValues[0] = feedbackOutputValues[2] + feedbackOutputValues[1];
  159. feedbackValues[1] = -feedbackOutputValues[0] - feedbackOutputValues[3];
  160. feedbackValues[2] = feedbackOutputValues[0] - feedbackOutputValues[3];
  161. feedbackValues[3] = feedbackOutputValues[1] - feedbackOutputValues[2];
  162. for (int i = 0; i < state.FdnDelayLines.Length; i++)
  163. {
  164. feedbackOutputValues[i] = state.DecayDelays[i].Update(feedbackValues[i] + lateValue);
  165. state.FdnDelayLines[i].Update(feedbackOutputValues[i]);
  166. }
  167. for (int i = 0; i < targetOutputFeedbackIndicesTable.Length; i++)
  168. {
  169. int targetOutputFeedbackIndex = targetOutputFeedbackIndicesTable[i];
  170. int outputIndex = outputIndicesTable[i];
  171. if (targetOutputFeedbackIndex >= 0)
  172. {
  173. outputValues[outputIndex] += feedbackOutputValues[targetOutputFeedbackIndex];
  174. }
  175. }
  176. if (isSurround)
  177. {
  178. outputValues[4] += state.BackLeftDelayLine.Update((feedbackOutputValues[2] - feedbackOutputValues[3]) * 0.5f);
  179. }
  180. for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
  181. {
  182. *((float*)outputBuffers[channelIndex] + sampleIndex) = (outputValues[channelIndex] * outGain + channelInput[channelIndex] * dryGain) / 64;
  183. }
  184. }
  185. }
  186. private void ProcessReverb(CommandList context, ref ReverbState state)
  187. {
  188. Debug.Assert(Parameter.IsChannelCountValid());
  189. if (IsEffectEnabled && Parameter.IsChannelCountValid())
  190. {
  191. Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  192. Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  193. for (int i = 0; i < Parameter.ChannelCount; i++)
  194. {
  195. inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
  196. outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
  197. }
  198. switch (Parameter.ChannelCount)
  199. {
  200. case 1:
  201. ProcessReverbMono(ref state, outputBuffers, inputBuffers, context.SampleCount);
  202. break;
  203. case 2:
  204. ProcessReverbStereo(ref state, outputBuffers, inputBuffers, context.SampleCount);
  205. break;
  206. case 4:
  207. ProcessReverbQuadraphonic(ref state, outputBuffers, inputBuffers, context.SampleCount);
  208. break;
  209. case 6:
  210. ProcessReverbSurround(ref state, outputBuffers, inputBuffers, context.SampleCount);
  211. break;
  212. default:
  213. throw new NotImplementedException(Parameter.ChannelCount.ToString());
  214. }
  215. }
  216. else
  217. {
  218. for (int i = 0; i < Parameter.ChannelCount; i++)
  219. {
  220. if (InputBufferIndices[i] != OutputBufferIndices[i])
  221. {
  222. context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
  223. }
  224. }
  225. }
  226. }
  227. public void Process(CommandList context)
  228. {
  229. ref ReverbState state = ref State.Span[0];
  230. if (IsEffectEnabled)
  231. {
  232. if (Parameter.Status == Server.Effect.UsageState.Invalid)
  233. {
  234. state = new ReverbState(ref _parameter, WorkBuffer, IsLongSizePreDelaySupported);
  235. }
  236. else if (Parameter.Status == Server.Effect.UsageState.New)
  237. {
  238. state.UpdateParameter(ref _parameter);
  239. }
  240. }
  241. ProcessReverb(context, ref state);
  242. }
  243. }
  244. }