MixRampCommand.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 MixRampCommand : ICommand
  22. {
  23. public bool Enabled { get; set; }
  24. public int NodeId { get; }
  25. public CommandType CommandType => CommandType.MixRamp;
  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 Memory<VoiceUpdateState> State { get; }
  32. public int LastSampleIndex { get; }
  33. public MixRampCommand(float volume0, float volume1, uint inputBufferIndex, uint outputBufferIndex, int lastSampleIndex, Memory<VoiceUpdateState> state, int nodeId)
  34. {
  35. Enabled = true;
  36. NodeId = nodeId;
  37. InputBufferIndex = (ushort)inputBufferIndex;
  38. OutputBufferIndex = (ushort)outputBufferIndex;
  39. Volume0 = volume0;
  40. Volume1 = volume1;
  41. State = state;
  42. LastSampleIndex = lastSampleIndex;
  43. }
  44. private float ProcessMixRamp(Span<float> outputBuffer, ReadOnlySpan<float> inputBuffer, int sampleCount)
  45. {
  46. float ramp = (Volume1 - Volume0) / sampleCount;
  47. float volume = Volume0;
  48. float state = 0;
  49. for (int i = 0; i < sampleCount; i++)
  50. {
  51. state = FloatingPointHelper.MultiplyRoundUp(inputBuffer[i], volume);
  52. outputBuffer[i] += state;
  53. volume += ramp;
  54. }
  55. return state;
  56. }
  57. public void Process(CommandList context)
  58. {
  59. ReadOnlySpan<float> inputBuffer = context.GetBuffer(InputBufferIndex);
  60. Span<float> outputBuffer = context.GetBuffer(OutputBufferIndex);
  61. State.Span[0].LastSamples[LastSampleIndex] = ProcessMixRamp(outputBuffer, inputBuffer, (int)context.SampleCount);
  62. }
  63. }
  64. }