ReverbCommand.cs 13 KB

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