VolumeRampCommand.cs 2.4 KB

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