CircularBufferSinkCommand.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Ryujinx.Audio.Renderer.Parameter.Sink;
  2. using Ryujinx.Audio.Renderer.Server.MemoryPool;
  3. using System;
  4. using System.Diagnostics;
  5. namespace Ryujinx.Audio.Renderer.Dsp.Command
  6. {
  7. public class CircularBufferSinkCommand : ICommand
  8. {
  9. public bool Enabled { get; set; }
  10. public int NodeId { get; }
  11. public CommandType CommandType => CommandType.CircularBufferSink;
  12. public ulong EstimatedProcessingTime { get; set; }
  13. public ushort[] Input { get; }
  14. public uint InputCount { get; }
  15. public ulong CircularBuffer { get; }
  16. public ulong CircularBufferSize { get; }
  17. public ulong CurrentOffset { get; }
  18. public CircularBufferSinkCommand(uint bufferOffset, ref CircularBufferParameter parameter, ref AddressInfo circularBufferAddressInfo, uint currentOffset, int nodeId)
  19. {
  20. Enabled = true;
  21. NodeId = nodeId;
  22. Input = new ushort[Constants.ChannelCountMax];
  23. InputCount = parameter.InputCount;
  24. for (int i = 0; i < InputCount; i++)
  25. {
  26. Input[i] = (ushort)(bufferOffset + parameter.Input[i]);
  27. }
  28. CircularBuffer = circularBufferAddressInfo.GetReference(true);
  29. CircularBufferSize = parameter.BufferSize;
  30. CurrentOffset = currentOffset;
  31. Debug.Assert(CircularBuffer != 0);
  32. }
  33. public void Process(CommandList context)
  34. {
  35. const int targetChannelCount = 2;
  36. ulong currentOffset = CurrentOffset;
  37. if (CircularBufferSize > 0)
  38. {
  39. for (int i = 0; i < InputCount; i++)
  40. {
  41. unsafe
  42. {
  43. float* inputBuffer = (float*)context.GetBufferPointer(Input[i]);
  44. ulong targetOffset = CircularBuffer + currentOffset;
  45. for (int y = 0; y < context.SampleCount; y++)
  46. {
  47. context.MemoryManager.Write(targetOffset + (ulong)y * targetChannelCount, PcmHelper.Saturate(inputBuffer[y]));
  48. }
  49. currentOffset += context.SampleCount * targetChannelCount;
  50. if (currentOffset >= CircularBufferSize)
  51. {
  52. currentOffset = 0;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }