MixRampGroupedCommand.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.Common;
  18. using System;
  19. using System.Runtime.CompilerServices;
  20. namespace Ryujinx.Audio.Renderer.Dsp.Command
  21. {
  22. public class MixRampGroupedCommand : ICommand
  23. {
  24. public bool Enabled { get; set; }
  25. public int NodeId { get; }
  26. public CommandType CommandType => CommandType.MixRampGrouped;
  27. public ulong EstimatedProcessingTime { get; set; }
  28. public uint MixBufferCount { get; }
  29. public ushort[] InputBufferIndices { get; }
  30. public ushort[] OutputBufferIndices { get; }
  31. public float[] Volume0 { get; }
  32. public float[] Volume1 { get; }
  33. public Memory<VoiceUpdateState> State { get; }
  34. public MixRampGroupedCommand(uint mixBufferCount, uint inputBufferIndex, uint outputBufferIndex, Span<float> volume0, Span<float> volume1, Memory<VoiceUpdateState> state, int nodeId)
  35. {
  36. Enabled = true;
  37. MixBufferCount = mixBufferCount;
  38. NodeId = nodeId;
  39. InputBufferIndices = new ushort[Constants.MixBufferCountMax];
  40. OutputBufferIndices = new ushort[Constants.MixBufferCountMax];
  41. Volume0 = new float[Constants.MixBufferCountMax];
  42. Volume1 = new float[Constants.MixBufferCountMax];
  43. for (int i = 0; i < mixBufferCount; i++)
  44. {
  45. InputBufferIndices[i] = (ushort)inputBufferIndex;
  46. OutputBufferIndices[i] = (ushort)(outputBufferIndex + i);
  47. Volume0[i] = volume0[i];
  48. Volume1[i] = volume1[i];
  49. }
  50. State = state;
  51. }
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. private float ProcessMixRampGrouped(Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, float volume0, float volume1, int sampleCount)
  54. {
  55. float ramp = (volume1 - volume0) / sampleCount;
  56. float volume = volume0;
  57. float state = 0;
  58. for (int i = 0; i < sampleCount; i++)
  59. {
  60. state = FloatingPointHelper.MultiplyRoundUp(inputBuffer[i], volume);
  61. outputBuffer[i] += state;
  62. volume += ramp;
  63. }
  64. return state;
  65. }
  66. public void Process(CommandList context)
  67. {
  68. for (int i = 0; i < MixBufferCount; i++)
  69. {
  70. ReadOnlySpan<float> inputBuffer = context.GetBuffer(InputBufferIndices[i]);
  71. Span<float> outputBuffer = context.GetBuffer(OutputBufferIndices[i]);
  72. float volume0 = Volume0[i];
  73. float volume1 = Volume1[i];
  74. ref VoiceUpdateState state = ref State.Span[0];
  75. if (volume0 != 0 || volume1 != 0)
  76. {
  77. state.LastSamples[i] = ProcessMixRampGrouped(outputBuffer, inputBuffer, volume0, volume1, (int)context.SampleCount);
  78. }
  79. else
  80. {
  81. state.LastSamples[i] = 0;
  82. }
  83. }
  84. }
  85. }
  86. }