CircularBufferSinkCommand.cs 2.4 KB

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