DelayCommand.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using Ryujinx.Audio.Renderer.Dsp.State;
  2. using Ryujinx.Audio.Renderer.Parameter.Effect;
  3. using Ryujinx.Audio.Renderer.Server.Effect;
  4. using Ryujinx.Audio.Renderer.Utils.Math;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Numerics;
  8. using System.Runtime.CompilerServices;
  9. namespace Ryujinx.Audio.Renderer.Dsp.Command
  10. {
  11. public class DelayCommand : ICommand
  12. {
  13. public bool Enabled { get; set; }
  14. public int NodeId { get; }
  15. public CommandType CommandType => CommandType.Delay;
  16. public ulong EstimatedProcessingTime { get; set; }
  17. public DelayParameter Parameter => _parameter;
  18. public Memory<DelayState> State { get; }
  19. public ulong WorkBuffer { get; }
  20. public ushort[] OutputBufferIndices { get; }
  21. public ushort[] InputBufferIndices { get; }
  22. public bool IsEffectEnabled { get; }
  23. private DelayParameter _parameter;
  24. private const int FixedPointPrecision = 14;
  25. public DelayCommand(uint bufferOffset, DelayParameter parameter, Memory<DelayState> state, bool isEnabled, ulong workBuffer, int nodeId, bool newEffectChannelMappingSupported)
  26. {
  27. Enabled = true;
  28. NodeId = nodeId;
  29. _parameter = parameter;
  30. State = state;
  31. WorkBuffer = workBuffer;
  32. IsEffectEnabled = isEnabled;
  33. InputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  34. OutputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
  35. for (int i = 0; i < Parameter.ChannelCount; i++)
  36. {
  37. InputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Input[i]);
  38. OutputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Output[i]);
  39. }
  40. // NOTE: We do the opposite as Nintendo here for now to restore previous behaviour
  41. // TODO: Update delay processing and remove this to use RemapLegacyChannelEffectMappingToChannelResourceMapping.
  42. DataSourceHelper.RemapChannelResourceMappingToLegacy(newEffectChannelMappingSupported, InputBufferIndices);
  43. DataSourceHelper.RemapChannelResourceMappingToLegacy(newEffectChannelMappingSupported, OutputBufferIndices);
  44. }
  45. [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
  46. private unsafe void ProcessDelayMono(ref DelayState state, float* outputBuffer, float* inputBuffer, uint sampleCount)
  47. {
  48. float feedbackGain = FixedPointHelper.ToFloat(Parameter.FeedbackGain, FixedPointPrecision);
  49. float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
  50. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  51. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  52. for (int i = 0; i < sampleCount; i++)
  53. {
  54. float input = inputBuffer[i] * 64;
  55. float delayLineValue = state.DelayLines[0].Read();
  56. float temp = input * inGain + delayLineValue * feedbackGain;
  57. state.UpdateLowPassFilter(ref temp, 1);
  58. outputBuffer[i] = (input * dryGain + delayLineValue * outGain) / 64;
  59. }
  60. }
  61. [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
  62. private unsafe void ProcessDelayStereo(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  63. {
  64. const ushort channelCount = 2;
  65. float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
  66. float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
  67. float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
  68. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  69. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  70. Matrix2x2 delayFeedback = new Matrix2x2(delayFeedbackBaseGain , delayFeedbackCrossGain,
  71. delayFeedbackCrossGain, delayFeedbackBaseGain);
  72. for (int i = 0; i < sampleCount; i++)
  73. {
  74. Vector2 channelInput = new Vector2
  75. {
  76. X = *((float*)inputBuffers[0] + i) * 64,
  77. Y = *((float*)inputBuffers[1] + i) * 64,
  78. };
  79. Vector2 delayLineValues = new Vector2()
  80. {
  81. X = state.DelayLines[0].Read(),
  82. Y = state.DelayLines[1].Read(),
  83. };
  84. Vector2 temp = MatrixHelper.Transform(ref channelInput, ref delayFeedback) + channelInput * inGain;
  85. state.UpdateLowPassFilter(ref Unsafe.As<Vector2, float>(ref temp), channelCount);
  86. *((float*)outputBuffers[0] + i) = (channelInput.X * dryGain + delayLineValues.X * outGain) / 64;
  87. *((float*)outputBuffers[1] + i) = (channelInput.Y * dryGain + delayLineValues.Y * outGain) / 64;
  88. }
  89. }
  90. [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
  91. private unsafe void ProcessDelayQuadraphonic(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  92. {
  93. const ushort channelCount = 4;
  94. float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
  95. float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
  96. float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
  97. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  98. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  99. Matrix4x4 delayFeedback = new Matrix4x4(delayFeedbackBaseGain , delayFeedbackCrossGain, delayFeedbackCrossGain, 0.0f,
  100. delayFeedbackCrossGain, delayFeedbackBaseGain , 0.0f , delayFeedbackCrossGain,
  101. delayFeedbackCrossGain, 0.0f , delayFeedbackBaseGain , delayFeedbackCrossGain,
  102. 0.0f , delayFeedbackCrossGain, delayFeedbackCrossGain, delayFeedbackBaseGain);
  103. for (int i = 0; i < sampleCount; i++)
  104. {
  105. Vector4 channelInput = new Vector4
  106. {
  107. X = *((float*)inputBuffers[0] + i) * 64,
  108. Y = *((float*)inputBuffers[1] + i) * 64,
  109. Z = *((float*)inputBuffers[2] + i) * 64,
  110. W = *((float*)inputBuffers[3] + i) * 64
  111. };
  112. Vector4 delayLineValues = new Vector4()
  113. {
  114. X = state.DelayLines[0].Read(),
  115. Y = state.DelayLines[1].Read(),
  116. Z = state.DelayLines[2].Read(),
  117. W = state.DelayLines[3].Read()
  118. };
  119. Vector4 temp = MatrixHelper.Transform(ref channelInput, ref delayFeedback) + channelInput * inGain;
  120. state.UpdateLowPassFilter(ref Unsafe.As<Vector4, float>(ref temp), channelCount);
  121. *((float*)outputBuffers[0] + i) = (channelInput.X * dryGain + delayLineValues.X * outGain) / 64;
  122. *((float*)outputBuffers[1] + i) = (channelInput.Y * dryGain + delayLineValues.Y * outGain) / 64;
  123. *((float*)outputBuffers[2] + i) = (channelInput.Z * dryGain + delayLineValues.Z * outGain) / 64;
  124. *((float*)outputBuffers[3] + i) = (channelInput.W * dryGain + delayLineValues.W * outGain) / 64;
  125. }
  126. }
  127. [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
  128. private unsafe void ProcessDelaySurround(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  129. {
  130. const ushort channelCount = 6;
  131. float feedbackGain = FixedPointHelper.ToFloat(Parameter.FeedbackGain, FixedPointPrecision);
  132. float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
  133. float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
  134. float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
  135. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  136. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  137. Matrix6x6 delayFeedback = new Matrix6x6(delayFeedbackBaseGain , 0.0f , 0.0f , 0.0f , delayFeedbackCrossGain, delayFeedbackCrossGain,
  138. 0.0f , delayFeedbackBaseGain , 0.0f , delayFeedbackCrossGain, delayFeedbackCrossGain, 0.0f ,
  139. delayFeedbackCrossGain, 0.0f , delayFeedbackBaseGain , delayFeedbackCrossGain, 0.0f , 0.0f ,
  140. 0.0f , delayFeedbackCrossGain, delayFeedbackCrossGain, delayFeedbackBaseGain , 0.0f , 0.0f ,
  141. delayFeedbackCrossGain, delayFeedbackCrossGain, 0.0f , 0.0f , delayFeedbackBaseGain , 0.0f ,
  142. 0.0f , 0.0f , 0.0f , 0.0f , 0.0f , feedbackGain);
  143. for (int i = 0; i < sampleCount; i++)
  144. {
  145. Vector6 channelInput = new Vector6
  146. {
  147. X = *((float*)inputBuffers[0] + i) * 64,
  148. Y = *((float*)inputBuffers[1] + i) * 64,
  149. Z = *((float*)inputBuffers[2] + i) * 64,
  150. W = *((float*)inputBuffers[3] + i) * 64,
  151. V = *((float*)inputBuffers[4] + i) * 64,
  152. U = *((float*)inputBuffers[5] + i) * 64
  153. };
  154. Vector6 delayLineValues = new Vector6
  155. {
  156. X = state.DelayLines[0].Read(),
  157. Y = state.DelayLines[1].Read(),
  158. Z = state.DelayLines[2].Read(),
  159. W = state.DelayLines[3].Read(),
  160. V = state.DelayLines[4].Read(),
  161. U = state.DelayLines[5].Read()
  162. };
  163. Vector6 temp = MatrixHelper.Transform(ref channelInput, ref delayFeedback) + channelInput * inGain;
  164. state.UpdateLowPassFilter(ref Unsafe.As<Vector6, float>(ref temp), channelCount);
  165. *((float*)outputBuffers[0] + i) = (channelInput.X * dryGain + delayLineValues.X * outGain) / 64;
  166. *((float*)outputBuffers[1] + i) = (channelInput.Y * dryGain + delayLineValues.Y * outGain) / 64;
  167. *((float*)outputBuffers[2] + i) = (channelInput.Z * dryGain + delayLineValues.Z * outGain) / 64;
  168. *((float*)outputBuffers[3] + i) = (channelInput.W * dryGain + delayLineValues.W * outGain) / 64;
  169. *((float*)outputBuffers[4] + i) = (channelInput.V * dryGain + delayLineValues.V * outGain) / 64;
  170. *((float*)outputBuffers[5] + i) = (channelInput.U * dryGain + delayLineValues.U * outGain) / 64;
  171. }
  172. }
  173. private unsafe void ProcessDelay(CommandList context, ref DelayState state)
  174. {
  175. Debug.Assert(Parameter.IsChannelCountValid());
  176. if (IsEffectEnabled && Parameter.IsChannelCountValid())
  177. {
  178. Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  179. Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  180. for (int i = 0; i < Parameter.ChannelCount; i++)
  181. {
  182. inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
  183. outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
  184. }
  185. switch (Parameter.ChannelCount)
  186. {
  187. case 1:
  188. ProcessDelayMono(ref state, (float*)outputBuffers[0], (float*)inputBuffers[0], context.SampleCount);
  189. break;
  190. case 2:
  191. ProcessDelayStereo(ref state, outputBuffers, inputBuffers, context.SampleCount);
  192. break;
  193. case 4:
  194. ProcessDelayQuadraphonic(ref state, outputBuffers, inputBuffers, context.SampleCount);
  195. break;
  196. case 6:
  197. ProcessDelaySurround(ref state, outputBuffers, inputBuffers, context.SampleCount);
  198. break;
  199. default:
  200. throw new NotImplementedException(Parameter.ChannelCount.ToString());
  201. }
  202. }
  203. else
  204. {
  205. for (int i = 0; i < Parameter.ChannelCount; i++)
  206. {
  207. if (InputBufferIndices[i] != OutputBufferIndices[i])
  208. {
  209. context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
  210. }
  211. }
  212. }
  213. }
  214. public void Process(CommandList context)
  215. {
  216. ref DelayState state = ref State.Span[0];
  217. if (IsEffectEnabled)
  218. {
  219. if (Parameter.Status == UsageState.Invalid)
  220. {
  221. state = new DelayState(ref _parameter, WorkBuffer);
  222. }
  223. else if (Parameter.Status == UsageState.New)
  224. {
  225. state.UpdateParameter(ref _parameter);
  226. }
  227. }
  228. ProcessDelay(context, ref state);
  229. }
  230. }
  231. }