DeviceSinkCommand.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.Integration;
  18. using Ryujinx.Audio.Renderer.Server.Sink;
  19. using System;
  20. using System.Runtime.CompilerServices;
  21. using System.Text;
  22. namespace Ryujinx.Audio.Renderer.Dsp.Command
  23. {
  24. public class DeviceSinkCommand : ICommand
  25. {
  26. public bool Enabled { get; set; }
  27. public int NodeId { get; }
  28. public CommandType CommandType => CommandType.DeviceSink;
  29. public ulong EstimatedProcessingTime { get; set; }
  30. public string DeviceName { get; }
  31. public int SessionId { get; }
  32. public uint InputCount { get; }
  33. public ushort[] InputBufferIndices { get; }
  34. public Memory<float> Buffers { get; }
  35. public DeviceSinkCommand(uint bufferOffset, DeviceSink sink, int sessionId, Memory<float> buffers, int nodeId)
  36. {
  37. Enabled = true;
  38. NodeId = nodeId;
  39. DeviceName = Encoding.ASCII.GetString(sink.Parameter.DeviceName).TrimEnd('\0');
  40. SessionId = sessionId;
  41. InputCount = sink.Parameter.InputCount;
  42. InputBufferIndices = new ushort[InputCount];
  43. for (int i = 0; i < InputCount; i++)
  44. {
  45. InputBufferIndices[i] = (ushort)(bufferOffset + sink.Parameter.Input[i]);
  46. }
  47. if (sink.UpsamplerState != null)
  48. {
  49. Buffers = sink.UpsamplerState.OutputBuffer;
  50. }
  51. else
  52. {
  53. Buffers = buffers;
  54. }
  55. }
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. private Span<float> GetBuffer(int index, int sampleCount)
  58. {
  59. return Buffers.Span.Slice(index * sampleCount, sampleCount);
  60. }
  61. public void Process(CommandList context)
  62. {
  63. HardwareDevice device = context.OutputDevice;
  64. if (device.GetSampleRate() == RendererConstants.TargetSampleRate)
  65. {
  66. int channelCount = (int)device.GetChannelCount();
  67. uint bufferCount = Math.Min(device.GetChannelCount(), InputCount);
  68. const int sampleCount = RendererConstants.TargetSampleCount;
  69. short[] outputBuffer = new short[bufferCount * sampleCount];
  70. for (int i = 0; i < bufferCount; i++)
  71. {
  72. ReadOnlySpan<float> inputBuffer = GetBuffer(InputBufferIndices[i], sampleCount);
  73. for (int j = 0; j < sampleCount; j++)
  74. {
  75. outputBuffer[i + j * channelCount] = PcmHelper.Saturate(inputBuffer[j]);
  76. }
  77. }
  78. device.AppendBuffer(outputBuffer, InputCount);
  79. }
  80. else
  81. {
  82. // TODO: support resampling for device only supporting something different
  83. throw new NotImplementedException();
  84. }
  85. }
  86. }
  87. }