UpsampleCommand.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. private Span<float> GetBuffer(int index, int sampleCount)
  53. {
  54. return UpsamplerInfo.OutputBuffer.Span.Slice(index * sampleCount, sampleCount);
  55. }
  56. public void Process(CommandList context)
  57. {
  58. float ratio = (float)InputSampleRate / RendererConstants.TargetSampleRate;
  59. uint bufferCount = Math.Min(BufferCount, UpsamplerInfo.SourceSampleCount);
  60. for (int i = 0; i < bufferCount; i++)
  61. {
  62. Span<float> inputBuffer = context.GetBuffer(UpsamplerInfo.InputBufferIndices[i]);
  63. Span<float> outputBuffer = GetBuffer(UpsamplerInfo.InputBufferIndices[i], (int)UpsamplerInfo.SampleCount);
  64. float fraction = 0.0f;
  65. ResamplerHelper.ResampleForUpsampler(outputBuffer, inputBuffer, ratio, ref fraction, (int)(InputSampleCount / ratio));
  66. }
  67. }
  68. }
  69. }