DelayCommand.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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)
  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. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. private unsafe void ProcessDelayMono(ref DelayState state, float* outputBuffer, float* inputBuffer, uint sampleCount)
  57. {
  58. float feedbackGain = FixedPointHelper.ToFloat(Parameter.FeedbackGain, FixedPointPrecision);
  59. float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
  60. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  61. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  62. for (int i = 0; i < sampleCount; i++)
  63. {
  64. float input = inputBuffer[i] * 64;
  65. float delayLineValue = state.DelayLines[0].Read();
  66. float lowPassResult = (input * inGain + delayLineValue * feedbackGain) * state.LowPassBaseGain + state.LowPassZ[0] * state.LowPassFeedbackGain;
  67. state.LowPassZ[0] = lowPassResult;
  68. state.DelayLines[0].Update(lowPassResult);
  69. outputBuffer[i] = (input * dryGain + delayLineValue * outGain) / 64;
  70. }
  71. }
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. private unsafe void ProcessDelayStereo(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  74. {
  75. const ushort channelCount = 2;
  76. Span<float> channelInput = stackalloc float[channelCount];
  77. Span<float> delayLineValues = stackalloc float[channelCount];
  78. Span<float> temp = stackalloc float[channelCount];
  79. float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
  80. float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
  81. float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
  82. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  83. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  84. for (int i = 0; i < sampleCount; i++)
  85. {
  86. for (int j = 0; j < channelCount; j++)
  87. {
  88. channelInput[j] = *((float*)inputBuffers[j] + i) * 64;
  89. delayLineValues[j] = state.DelayLines[j].Read();
  90. }
  91. temp[0] = channelInput[0] * inGain + delayLineValues[1] * delayFeedbackCrossGain + delayLineValues[0] * delayFeedbackBaseGain;
  92. temp[1] = channelInput[1] * inGain + delayLineValues[0] * delayFeedbackCrossGain + delayLineValues[1] * delayFeedbackBaseGain;
  93. for (int j = 0; j < channelCount; j++)
  94. {
  95. float lowPassResult = state.LowPassFeedbackGain * state.LowPassZ[j] + temp[j] * state.LowPassBaseGain;
  96. state.LowPassZ[j] = lowPassResult;
  97. state.DelayLines[j].Update(lowPassResult);
  98. *((float*)outputBuffers[j] + i) = (channelInput[j] * dryGain + delayLineValues[j] * outGain) / 64;
  99. }
  100. }
  101. }
  102. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  103. private unsafe void ProcessDelayQuadraphonic(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  104. {
  105. const ushort channelCount = 4;
  106. Span<float> channelInput = stackalloc float[channelCount];
  107. Span<float> delayLineValues = stackalloc float[channelCount];
  108. Span<float> temp = stackalloc float[channelCount];
  109. float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
  110. float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
  111. float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
  112. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  113. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  114. for (int i = 0; i < sampleCount; i++)
  115. {
  116. for (int j = 0; j < channelCount; j++)
  117. {
  118. channelInput[j] = *((float*)inputBuffers[j] + i) * 64;
  119. delayLineValues[j] = state.DelayLines[j].Read();
  120. }
  121. temp[0] = channelInput[0] * inGain + (delayLineValues[2] + delayLineValues[1]) * delayFeedbackCrossGain + delayLineValues[0] * delayFeedbackBaseGain;
  122. temp[1] = channelInput[1] * inGain + (delayLineValues[0] + delayLineValues[3]) * delayFeedbackCrossGain + delayLineValues[1] * delayFeedbackBaseGain;
  123. temp[2] = channelInput[2] * inGain + (delayLineValues[3] + delayLineValues[0]) * delayFeedbackCrossGain + delayLineValues[2] * delayFeedbackBaseGain;
  124. temp[3] = channelInput[3] * inGain + (delayLineValues[1] + delayLineValues[2]) * delayFeedbackCrossGain + delayLineValues[3] * delayFeedbackBaseGain;
  125. for (int j = 0; j < channelCount; j++)
  126. {
  127. float lowPassResult = state.LowPassFeedbackGain * state.LowPassZ[j] + temp[j] * state.LowPassBaseGain;
  128. state.LowPassZ[j] = lowPassResult;
  129. state.DelayLines[j].Update(lowPassResult);
  130. *((float*)outputBuffers[j] + i) = (channelInput[j] * dryGain + delayLineValues[j] * outGain) / 64;
  131. }
  132. }
  133. }
  134. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  135. private unsafe void ProcessDelaySurround(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
  136. {
  137. const ushort channelCount = 6;
  138. Span<float> channelInput = stackalloc float[channelCount];
  139. Span<float> delayLineValues = stackalloc float[channelCount];
  140. Span<float> temp = stackalloc float[channelCount];
  141. float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
  142. float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
  143. float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
  144. float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
  145. float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
  146. for (int i = 0; i < sampleCount; i++)
  147. {
  148. for (int j = 0; j < channelCount; j++)
  149. {
  150. channelInput[j] = *((float*)inputBuffers[j] + i) * 64;
  151. delayLineValues[j] = state.DelayLines[j].Read();
  152. }
  153. temp[0] = channelInput[0] * inGain + (delayLineValues[2] + delayLineValues[4]) * delayFeedbackCrossGain + delayLineValues[0] * delayFeedbackBaseGain;
  154. temp[1] = channelInput[1] * inGain + (delayLineValues[4] + delayLineValues[3]) * delayFeedbackCrossGain + delayLineValues[1] * delayFeedbackBaseGain;
  155. temp[2] = channelInput[2] * inGain + (delayLineValues[3] + delayLineValues[0]) * delayFeedbackCrossGain + delayLineValues[2] * delayFeedbackBaseGain;
  156. temp[3] = channelInput[3] * inGain + (delayLineValues[1] + delayLineValues[2]) * delayFeedbackCrossGain + delayLineValues[3] * delayFeedbackBaseGain;
  157. temp[4] = channelInput[4] * inGain + (delayLineValues[0] + delayLineValues[1]) * delayFeedbackCrossGain + delayLineValues[4] * delayFeedbackBaseGain;
  158. temp[5] = channelInput[5] * inGain + delayLineValues[5] * delayFeedbackBaseGain;
  159. for (int j = 0; j < channelCount; j++)
  160. {
  161. float lowPassResult = state.LowPassFeedbackGain * state.LowPassZ[j] + temp[j] * state.LowPassBaseGain;
  162. state.LowPassZ[j] = lowPassResult;
  163. state.DelayLines[j].Update(lowPassResult);
  164. *((float*)outputBuffers[j] + i) = (channelInput[j] * dryGain + delayLineValues[j] * outGain) / 64;
  165. }
  166. }
  167. }
  168. private unsafe void ProcessDelay(CommandList context, ref DelayState state)
  169. {
  170. Debug.Assert(Parameter.IsChannelCountValid());
  171. if (IsEffectEnabled && Parameter.IsChannelCountValid())
  172. {
  173. Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  174. Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
  175. for (int i = 0; i < Parameter.ChannelCount; i++)
  176. {
  177. inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
  178. outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
  179. }
  180. switch (Parameter.ChannelCount)
  181. {
  182. case 1:
  183. ProcessDelayMono(ref state, (float*)outputBuffers[0], (float*)inputBuffers[0], context.SampleCount);
  184. break;
  185. case 2:
  186. ProcessDelayStereo(ref state, outputBuffers, inputBuffers, context.SampleCount);
  187. break;
  188. case 4:
  189. ProcessDelayQuadraphonic(ref state, outputBuffers, inputBuffers, context.SampleCount);
  190. break;
  191. case 6:
  192. ProcessDelaySurround(ref state, outputBuffers, inputBuffers, context.SampleCount);
  193. break;
  194. default:
  195. throw new NotImplementedException(Parameter.ChannelCount.ToString());
  196. }
  197. }
  198. else
  199. {
  200. for (int i = 0; i < Parameter.ChannelCount; i++)
  201. {
  202. if (InputBufferIndices[i] != OutputBufferIndices[i])
  203. {
  204. context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
  205. }
  206. }
  207. }
  208. }
  209. public void Process(CommandList context)
  210. {
  211. ref DelayState state = ref State.Span[0];
  212. if (IsEffectEnabled)
  213. {
  214. if (Parameter.Status == UsageState.Invalid)
  215. {
  216. state = new DelayState(ref _parameter, WorkBuffer);
  217. }
  218. else if (Parameter.Status == UsageState.New)
  219. {
  220. state.UpdateParameter(ref _parameter);
  221. }
  222. }
  223. ProcessDelay(context, ref state);
  224. }
  225. }
  226. }