Reverb3dCommand.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using Ryujinx.Audio.Renderer.Dsp.State;
  2. using Ryujinx.Audio.Renderer.Parameter.Effect;
  3. using Ryujinx.Audio.Renderer.Server.Effect;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Runtime.CompilerServices;
  7. namespace Ryujinx.Audio.Renderer.Dsp.Command
  8. {
  9. public class Reverb3dCommand : ICommand
  10. {
  11. private static readonly int[] OutputEarlyIndicesTableMono = new int[20] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  12. private static readonly int[] TargetEarlyDelayLineIndicesTableMono = new int[20] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
  13. private static readonly int[] TargetOutputFeedbackIndicesTableMono = new int[1] { 0 };
  14. private static readonly int[] OutputEarlyIndicesTableStereo = new int[20] { 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 };
  15. private static readonly int[] TargetEarlyDelayLineIndicesTableStereo = new int[20] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
  16. private static readonly int[] TargetOutputFeedbackIndicesTableStereo = new int[2] { 0, 1 };
  17. private static readonly int[] OutputEarlyIndicesTableQuadraphonic = new int[20] { 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 3, 3, 3 };
  18. private static readonly int[] TargetEarlyDelayLineIndicesTableQuadraphonic = new int[20] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
  19. private static readonly int[] TargetOutputFeedbackIndicesTableQuadraphonic = new int[4] { 0, 1, 2, 3 };
  20. private static readonly int[] OutputEarlyIndicesTableSurround = new int[40] { 4, 5, 0, 5, 0, 5, 1, 5, 1, 5, 1, 5, 1, 5, 2, 5, 2, 5, 2, 5, 1, 5, 1, 5, 1, 5, 0, 5, 0, 5, 0, 5, 0, 5, 3, 5, 3, 5, 3, 5 };
  21. private static readonly int[] TargetEarlyDelayLineIndicesTableSurround = new int[40] { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19 };
  22. private static readonly int[] TargetOutputFeedbackIndicesTableSurround = new int[6] { 0, 1, 2, 3, -1, 3 };
  23. public bool Enabled { get; set; }
  24. public int NodeId { get; }
  25. public CommandType CommandType => CommandType.Reverb3d;
  26. public uint EstimatedProcessingTime { get; set; }
  27. public ushort InputBufferIndex { get; }
  28. public ushort OutputBufferIndex { get; }
  29. public Reverb3dParameter Parameter => _parameter;
  30. public Memory<Reverb3dState> State { get; }
  31. public ulong WorkBuffer { get; }
  32. public ushort[] OutputBufferIndices { get; }
  33. public ushort[] InputBufferIndices { get; }
  34. public bool IsEffectEnabled { get; }
  35. private Reverb3dParameter _parameter;
  36. public Reverb3dCommand(uint bufferOffset, Reverb3dParameter parameter, Memory<Reverb3dState> state, bool isEnabled, ulong workBuffer, int nodeId, bool newEffectChannelMappingSupported)
  37. {
  38. Enabled = true;
  39. IsEffectEnabled = isEnabled;
  40. NodeId = nodeId;
  41. _parameter = parameter;
  42. State = state;
  43. WorkBuffer = workBuffer;
  44. InputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  45. OutputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  46. for (int i = 0; i < Parameter.ChannelCount; i++)
  47. {
  48. InputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Input[i]);
  49. OutputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Output[i]);
  50. }
  51. // NOTE: We do the opposite as Nintendo here for now to restore previous behaviour
  52. // TODO: Update reverb 3d processing and remove this to use RemapLegacyChannelEffectMappingToChannelResourceMapping.
  53. DataSourceHelper.RemapChannelResourceMappingToLegacy(newEffectChannelMappingSupported, InputBufferIndices);
  54. DataSourceHelper.RemapChannelResourceMappingToLegacy(newEffectChannelMappingSupported, OutputBufferIndices);
  55. }
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. private void ProcessReverb3dMono(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  58. {
  59. ProcessReverb3dGeneric(ref state, outputBuffers, inputBuffers, sampleCount, OutputEarlyIndicesTableMono, TargetEarlyDelayLineIndicesTableMono, TargetOutputFeedbackIndicesTableMono);
  60. }
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. private void ProcessReverb3dStereo(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  63. {
  64. ProcessReverb3dGeneric(ref state, outputBuffers, inputBuffers, sampleCount, OutputEarlyIndicesTableStereo, TargetEarlyDelayLineIndicesTableStereo, TargetOutputFeedbackIndicesTableStereo);
  65. }
  66. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  67. private void ProcessReverb3dQuadraphonic(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  68. {
  69. ProcessReverb3dGeneric(ref state, outputBuffers, inputBuffers, sampleCount, OutputEarlyIndicesTableQuadraphonic, TargetEarlyDelayLineIndicesTableQuadraphonic, TargetOutputFeedbackIndicesTableQuadraphonic);
  70. }
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. private void ProcessReverb3dSurround(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  73. {
  74. ProcessReverb3dGeneric(ref state, outputBuffers, inputBuffers, sampleCount, OutputEarlyIndicesTableSurround, TargetEarlyDelayLineIndicesTableSurround, TargetOutputFeedbackIndicesTableSurround);
  75. }
  76. private unsafe void ProcessReverb3dGeneric(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount, ReadOnlySpan<int> outputEarlyIndicesTable, ReadOnlySpan<int> targetEarlyDelayLineIndicesTable, ReadOnlySpan<int> targetOutputFeedbackIndicesTable)
  77. {
  78. const int delayLineSampleIndexOffset = 1;
  79. bool isMono = Parameter.ChannelCount == 1;
  80. bool isSurround = Parameter.ChannelCount == 6;
  81. Span<float> outputValues = stackalloc float[Constants.ChannelCountMax];
  82. Span<float> channelInput = stackalloc float[Parameter.ChannelCount];
  83. Span<float> feedbackValues = stackalloc float[4];
  84. Span<float> feedbackOutputValues = stackalloc float[4];
  85. Span<float> values = stackalloc float[4];
  86. for (int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
  87. {
  88. outputValues.Fill(0);
  89. float tapOut = state.PreDelayLine.TapUnsafe(state.ReflectionDelayTime, delayLineSampleIndexOffset);
  90. for (int i = 0; i < targetEarlyDelayLineIndicesTable.Length; i++)
  91. {
  92. int earlyDelayIndex = targetEarlyDelayLineIndicesTable[i];
  93. int outputIndex = outputEarlyIndicesTable[i];
  94. float tempTapOut = state.PreDelayLine.TapUnsafe(state.EarlyDelayTime[earlyDelayIndex], delayLineSampleIndexOffset);
  95. outputValues[outputIndex] += tempTapOut * state.EarlyGain[earlyDelayIndex];
  96. }
  97. float targetPreDelayValue = 0;
  98. for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
  99. {
  100. channelInput[channelIndex] = *((float*)inputBuffers[channelIndex] + sampleIndex);
  101. targetPreDelayValue += channelInput[channelIndex];
  102. }
  103. for (int i = 0; i < Parameter.ChannelCount; i++)
  104. {
  105. outputValues[i] *= state.EarlyReflectionsGain;
  106. }
  107. state.PreviousPreDelayValue = (targetPreDelayValue * state.TargetPreDelayGain) + (state.PreviousPreDelayValue * state.PreviousPreDelayGain);
  108. state.PreDelayLine.Update(state.PreviousPreDelayValue);
  109. for (int i = 0; i < state.FdnDelayLines.Length; i++)
  110. {
  111. float fdnValue = state.FdnDelayLines[i].Read();
  112. float feedbackOutputValue = fdnValue * state.DecayDirectFdnGain[i] + state.PreviousFeedbackOutputDecayed[i];
  113. state.PreviousFeedbackOutputDecayed[i] = (fdnValue * state.DecayCurrentFdnGain[i]) + (feedbackOutputValue * state.DecayCurrentOutputGain[i]);
  114. feedbackOutputValues[i] = feedbackOutputValue;
  115. }
  116. feedbackValues[0] = feedbackOutputValues[2] + feedbackOutputValues[1];
  117. feedbackValues[1] = -feedbackOutputValues[0] - feedbackOutputValues[3];
  118. feedbackValues[2] = feedbackOutputValues[0] - feedbackOutputValues[3];
  119. feedbackValues[3] = feedbackOutputValues[1] - feedbackOutputValues[2];
  120. for (int i = 0; i < state.DecayDelays1.Length; i++)
  121. {
  122. float temp = state.DecayDelays1[i].Update(tapOut * state.LateReverbGain + feedbackValues[i]);
  123. values[i] = state.DecayDelays2[i].Update(temp);
  124. state.FdnDelayLines[i].Update(values[i]);
  125. }
  126. for (int channelIndex = 0; channelIndex < targetOutputFeedbackIndicesTable.Length; channelIndex++)
  127. {
  128. int targetOutputFeedbackIndex = targetOutputFeedbackIndicesTable[channelIndex];
  129. if (targetOutputFeedbackIndex >= 0)
  130. {
  131. *((float*)outputBuffers[channelIndex] + sampleIndex) = (outputValues[channelIndex] + values[targetOutputFeedbackIndex] + channelInput[channelIndex] * state.DryGain);
  132. }
  133. }
  134. if (isMono)
  135. {
  136. *((float*)outputBuffers[0] + sampleIndex) += values[1];
  137. }
  138. if (isSurround)
  139. {
  140. *((float*)outputBuffers[4] + sampleIndex) += (outputValues[4] + state.FrontCenterDelayLine.Update((values[2] - values[3]) * 0.5f) + channelInput[4] * state.DryGain);
  141. }
  142. }
  143. }
  144. public void ProcessReverb3d(CommandList context, ref Reverb3dState state)
  145. {
  146. Debug.Assert(Parameter.IsChannelCountValid());
  147. if (IsEffectEnabled && Parameter.IsChannelCountValid())
  148. {
  149. Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  150. Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  151. for (int i = 0; i < Parameter.ChannelCount; i++)
  152. {
  153. inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
  154. outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
  155. }
  156. switch (Parameter.ChannelCount)
  157. {
  158. case 1:
  159. ProcessReverb3dMono(ref state, outputBuffers, inputBuffers, context.SampleCount);
  160. break;
  161. case 2:
  162. ProcessReverb3dStereo(ref state, outputBuffers, inputBuffers, context.SampleCount);
  163. break;
  164. case 4:
  165. ProcessReverb3dQuadraphonic(ref state, outputBuffers, inputBuffers, context.SampleCount);
  166. break;
  167. case 6:
  168. ProcessReverb3dSurround(ref state, outputBuffers, inputBuffers, context.SampleCount);
  169. break;
  170. default:
  171. throw new NotImplementedException(Parameter.ChannelCount.ToString());
  172. }
  173. }
  174. else
  175. {
  176. for (int i = 0; i < Parameter.ChannelCount; i++)
  177. {
  178. if (InputBufferIndices[i] != OutputBufferIndices[i])
  179. {
  180. context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
  181. }
  182. }
  183. }
  184. }
  185. public void Process(CommandList context)
  186. {
  187. ref Reverb3dState state = ref State.Span[0];
  188. if (IsEffectEnabled)
  189. {
  190. if (Parameter.ParameterStatus == UsageState.Invalid)
  191. {
  192. state = new Reverb3dState(ref _parameter, WorkBuffer);
  193. }
  194. else if (Parameter.ParameterStatus == UsageState.New)
  195. {
  196. state.UpdateParameter(ref _parameter);
  197. }
  198. }
  199. ProcessReverb3d(context, ref state);
  200. }
  201. }
  202. }