Reverb3dCommand.cs 13 KB

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