DelayCommand.cs 15 KB

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