Reverb3dCommand.cs 12 KB

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