MixRampCommand.cs 2.8 KB

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