MixRampCommand.cs 2.1 KB

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