DelayCommand.cs 12 KB

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