ReverbCommand.cs 14 KB

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