DelayCommand.cs 13 KB

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