Reverb3dCommand.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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)
  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. }
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. private void ProcessReverb3dMono(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  70. {
  71. ProcessReverb3dGeneric(ref state, outputBuffers, inputBuffers, sampleCount, OutputEarlyIndicesTableMono, TargetEarlyDelayLineIndicesTableMono, TargetOutputFeedbackIndicesTableMono);
  72. }
  73. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  74. private void ProcessReverb3dStereo(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  75. {
  76. ProcessReverb3dGeneric(ref state, outputBuffers, inputBuffers, sampleCount, OutputEarlyIndicesTableStereo, TargetEarlyDelayLineIndicesTableStereo, TargetOutputFeedbackIndicesTableStereo);
  77. }
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. private void ProcessReverb3dQuadraphonic(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  80. {
  81. ProcessReverb3dGeneric(ref state, outputBuffers, inputBuffers, sampleCount, OutputEarlyIndicesTableQuadraphonic, TargetEarlyDelayLineIndicesTableQuadraphonic, TargetOutputFeedbackIndicesTableQuadraphonic);
  82. }
  83. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  84. private void ProcessReverb3dSurround(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  85. {
  86. ProcessReverb3dGeneric(ref state, outputBuffers, inputBuffers, sampleCount, OutputEarlyIndicesTableSurround, TargetEarlyDelayLineIndicesTableSurround, TargetOutputFeedbackIndicesTableSurround);
  87. }
  88. private unsafe void ProcessReverb3dGeneric(ref Reverb3dState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount, ReadOnlySpan<int> outputEarlyIndicesTable, ReadOnlySpan<int> targetEarlyDelayLineIndicesTable, ReadOnlySpan<int> targetOutputFeedbackIndicesTable)
  89. {
  90. const int delayLineSampleIndexOffset = 1;
  91. bool isMono = Parameter.ChannelCount == 1;
  92. bool isSurround = Parameter.ChannelCount == 6;
  93. Span<float> outputValues = stackalloc float[Constants.ChannelCountMax];
  94. Span<float> channelInput = stackalloc float[Parameter.ChannelCount];
  95. Span<float> feedbackValues = stackalloc float[4];
  96. Span<float> feedbackOutputValues = stackalloc float[4];
  97. Span<float> values = stackalloc float[4];
  98. for (int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
  99. {
  100. outputValues.Fill(0);
  101. float tapOut = state.PreDelayLine.TapUnsafe(state.ReflectionDelayTime, delayLineSampleIndexOffset);
  102. for (int i = 0; i < targetEarlyDelayLineIndicesTable.Length; i++)
  103. {
  104. int earlyDelayIndex = targetEarlyDelayLineIndicesTable[i];
  105. int outputIndex = outputEarlyIndicesTable[i];
  106. float tempTapOut = state.PreDelayLine.TapUnsafe(state.EarlyDelayTime[earlyDelayIndex], delayLineSampleIndexOffset);
  107. outputValues[outputIndex] += tempTapOut * state.EarlyGain[earlyDelayIndex];
  108. }
  109. float targetPreDelayValue = 0;
  110. for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
  111. {
  112. channelInput[channelIndex] = *((float*)inputBuffers[channelIndex] + sampleIndex);
  113. targetPreDelayValue += channelInput[channelIndex];
  114. }
  115. for (int i = 0; i < Parameter.ChannelCount; i++)
  116. {
  117. outputValues[i] *= state.EarlyReflectionsGain;
  118. }
  119. state.PreviousPreDelayValue = (targetPreDelayValue * state.TargetPreDelayGain) + (state.PreviousPreDelayValue * state.PreviousPreDelayGain);
  120. state.PreDelayLine.Update(state.PreviousPreDelayValue);
  121. for (int i = 0; i < state.FdnDelayLines.Length; i++)
  122. {
  123. float fdnValue = state.FdnDelayLines[i].Read();
  124. float feedbackOutputValue = fdnValue * state.DecayDirectFdnGain[i] + state.PreviousFeedbackOutputDecayed[i];
  125. state.PreviousFeedbackOutputDecayed[i] = (fdnValue * state.DecayCurrentFdnGain[i]) + (feedbackOutputValue * state.DecayCurrentOutputGain[i]);
  126. feedbackOutputValues[i] = feedbackOutputValue;
  127. }
  128. feedbackValues[0] = feedbackOutputValues[2] + feedbackOutputValues[1];
  129. feedbackValues[1] = -feedbackOutputValues[0] - feedbackOutputValues[3];
  130. feedbackValues[2] = feedbackOutputValues[0] - feedbackOutputValues[3];
  131. feedbackValues[3] = feedbackOutputValues[1] - feedbackOutputValues[2];
  132. for (int i = 0; i < state.DecayDelays1.Length; i++)
  133. {
  134. float temp = state.DecayDelays1[i].Update(tapOut * state.LateReverbGain + feedbackValues[i]);
  135. values[i] = state.DecayDelays2[i].Update(temp);
  136. state.FdnDelayLines[i].Update(values[i]);
  137. }
  138. for (int channelIndex = 0; channelIndex < targetOutputFeedbackIndicesTable.Length; channelIndex++)
  139. {
  140. int targetOutputFeedbackIndex = targetOutputFeedbackIndicesTable[channelIndex];
  141. if (targetOutputFeedbackIndex >= 0)
  142. {
  143. *((float*)outputBuffers[channelIndex] + sampleIndex) = (outputValues[channelIndex] + values[targetOutputFeedbackIndex] + channelInput[channelIndex] * state.DryGain);
  144. }
  145. }
  146. if (isMono)
  147. {
  148. *((float*)outputBuffers[0] + sampleIndex) += values[1];
  149. }
  150. if (isSurround)
  151. {
  152. *((float*)outputBuffers[4] + sampleIndex) += (outputValues[4] + state.BackLeftDelayLine.Update((values[2] - values[3]) * 0.5f) + channelInput[4] * state.DryGain);
  153. }
  154. }
  155. }
  156. public void ProcessReverb3d(CommandList context, ref Reverb3dState state)
  157. {
  158. Debug.Assert(Parameter.IsChannelCountValid());
  159. if (IsEffectEnabled && Parameter.IsChannelCountValid())
  160. {
  161. Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  162. Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  163. for (int i = 0; i < Parameter.ChannelCount; i++)
  164. {
  165. inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
  166. outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
  167. }
  168. switch (Parameter.ChannelCount)
  169. {
  170. case 1:
  171. ProcessReverb3dMono(ref state, outputBuffers, inputBuffers, context.SampleCount);
  172. break;
  173. case 2:
  174. ProcessReverb3dStereo(ref state, outputBuffers, inputBuffers, context.SampleCount);
  175. break;
  176. case 4:
  177. ProcessReverb3dQuadraphonic(ref state, outputBuffers, inputBuffers, context.SampleCount);
  178. break;
  179. case 6:
  180. ProcessReverb3dSurround(ref state, outputBuffers, inputBuffers, context.SampleCount);
  181. break;
  182. default:
  183. throw new NotImplementedException(Parameter.ChannelCount.ToString());
  184. }
  185. }
  186. else
  187. {
  188. for (int i = 0; i < Parameter.ChannelCount; i++)
  189. {
  190. if (InputBufferIndices[i] != OutputBufferIndices[i])
  191. {
  192. context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
  193. }
  194. }
  195. }
  196. }
  197. public void Process(CommandList context)
  198. {
  199. ref Reverb3dState state = ref State.Span[0];
  200. if (IsEffectEnabled)
  201. {
  202. if (Parameter.ParameterStatus == UsageState.Invalid)
  203. {
  204. state = new Reverb3dState(ref _parameter, WorkBuffer);
  205. }
  206. else if (Parameter.ParameterStatus == UsageState.New)
  207. {
  208. state.UpdateParameter(ref _parameter);
  209. }
  210. }
  211. ProcessReverb3d(context, ref state);
  212. }
  213. }
  214. }