Reverb3dCommand.cs 13 KB

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