VolumeRampCommand.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Ryujinx.Audio.Renderer.Dsp.Command
  4. {
  5. public class VolumeRampCommand : ICommand
  6. {
  7. public bool Enabled { get; set; }
  8. public int NodeId { get; }
  9. public CommandType CommandType => CommandType.VolumeRamp;
  10. public uint EstimatedProcessingTime { get; set; }
  11. public ushort InputBufferIndex { get; }
  12. public ushort OutputBufferIndex { get; }
  13. public float Volume0 { get; }
  14. public float Volume1 { get; }
  15. public VolumeRampCommand(float volume0, float volume1, uint bufferIndex, int nodeId)
  16. {
  17. Enabled = true;
  18. NodeId = nodeId;
  19. InputBufferIndex = (ushort)bufferIndex;
  20. OutputBufferIndex = (ushort)bufferIndex;
  21. Volume0 = volume0;
  22. Volume1 = volume1;
  23. }
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. private void ProcessVolumeRamp(Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, int sampleCount)
  26. {
  27. float ramp = (Volume1 - Volume0) / sampleCount;
  28. float volume = Volume0;
  29. for (int i = 0; i < sampleCount; i++)
  30. {
  31. outputBuffer[i] = FloatingPointHelper.MultiplyRoundUp(inputBuffer[i], volume);
  32. volume += ramp;
  33. }
  34. }
  35. public void Process(CommandList context)
  36. {
  37. ReadOnlySpan<float> inputBuffer = context.GetBuffer(InputBufferIndex);
  38. Span<float> outputBuffer = context.GetBuffer(OutputBufferIndex);
  39. ProcessVolumeRamp(outputBuffer, inputBuffer, (int)context.SampleCount);
  40. }
  41. }
  42. }