UpsampleCommand.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.Server.Upsampler;
  18. using System;
  19. using System.Runtime.CompilerServices;
  20. namespace Ryujinx.Audio.Renderer.Dsp.Command
  21. {
  22. public class UpsampleCommand : ICommand
  23. {
  24. public bool Enabled { get; set; }
  25. public int NodeId { get; }
  26. public CommandType CommandType => CommandType.Upsample;
  27. public ulong EstimatedProcessingTime { get; set; }
  28. public uint BufferCount { get; }
  29. public uint InputBufferIndex { get; }
  30. public uint InputSampleCount { get; }
  31. public uint InputSampleRate { get; }
  32. public UpsamplerState UpsamplerInfo { get; }
  33. public Memory<float> OutBuffer { get; }
  34. public UpsampleCommand(uint bufferOffset, UpsamplerState info, uint inputCount, Span<byte> inputBufferOffset, uint bufferCount, uint sampleCount, uint sampleRate, int nodeId)
  35. {
  36. Enabled = true;
  37. NodeId = nodeId;
  38. InputBufferIndex = 0;
  39. OutBuffer = info.OutputBuffer;
  40. BufferCount = bufferCount;
  41. InputSampleCount = sampleCount;
  42. InputSampleRate = sampleRate;
  43. info.SourceSampleCount = inputCount;
  44. info.InputBufferIndices = new ushort[inputCount];
  45. for (int i = 0; i < inputCount; i++)
  46. {
  47. info.InputBufferIndices[i] = (ushort)(bufferOffset + inputBufferOffset[i]);
  48. }
  49. UpsamplerInfo = info;
  50. }
  51. private Span<float> GetBuffer(int index, int sampleCount)
  52. {
  53. return UpsamplerInfo.OutputBuffer.Span.Slice(index * sampleCount, sampleCount);
  54. }
  55. public void Process(CommandList context)
  56. {
  57. float ratio = (float)InputSampleRate / Constants.TargetSampleRate;
  58. uint bufferCount = Math.Min(BufferCount, UpsamplerInfo.SourceSampleCount);
  59. for (int i = 0; i < bufferCount; i++)
  60. {
  61. Span<float> inputBuffer = context.GetBuffer(UpsamplerInfo.InputBufferIndices[i]);
  62. Span<float> outputBuffer = GetBuffer(UpsamplerInfo.InputBufferIndices[i], (int)UpsamplerInfo.SampleCount);
  63. float fraction = 0.0f;
  64. ResamplerHelper.ResampleForUpsampler(outputBuffer, inputBuffer, ratio, ref fraction, (int)(InputSampleCount / ratio));
  65. }
  66. }
  67. }
  68. }