MixRampGroupedCommand.cs 3.6 KB

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