ReverbCommand.cs 13 KB

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