VolumeRampCommand.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 System;
  18. namespace Ryujinx.Audio.Renderer.Dsp.Command
  19. {
  20. public class VolumeRampCommand : ICommand
  21. {
  22. public bool Enabled { get; set; }
  23. public int NodeId { get; }
  24. public CommandType CommandType => CommandType.VolumeRamp;
  25. public ulong EstimatedProcessingTime { get; set; }
  26. public ushort InputBufferIndex { get; }
  27. public ushort OutputBufferIndex { get; }
  28. public float Volume0 { get; }
  29. public float Volume1 { get; }
  30. public VolumeRampCommand(float volume0, float volume1, uint bufferIndex, int nodeId)
  31. {
  32. Enabled = true;
  33. NodeId = nodeId;
  34. InputBufferIndex = (ushort)bufferIndex;
  35. OutputBufferIndex = (ushort)bufferIndex;
  36. Volume0 = volume0;
  37. Volume1 = volume1;
  38. }
  39. private void ProcessVolumeRamp(Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, int sampleCount)
  40. {
  41. float ramp = (Volume1 - Volume0) / sampleCount;
  42. float volume = Volume0;
  43. for (int i = 0; i < sampleCount; i++)
  44. {
  45. outputBuffer[i] = FloatingPointHelper.MultiplyRoundUp(inputBuffer[i], volume);
  46. volume += ramp;
  47. }
  48. }
  49. public void Process(CommandList context)
  50. {
  51. ReadOnlySpan<float> inputBuffer = context.GetBuffer(InputBufferIndex);
  52. Span<float> outputBuffer = context.GetBuffer(OutputBufferIndex);
  53. ProcessVolumeRamp(outputBuffer, inputBuffer, (int)context.SampleCount);
  54. }
  55. }
  56. }