UpsamplerManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 System;
  18. using System.Diagnostics;
  19. namespace Ryujinx.Audio.Renderer.Server.Upsampler
  20. {
  21. /// <summary>
  22. /// Upsampler manager.
  23. /// </summary>
  24. public class UpsamplerManager
  25. {
  26. /// <summary>
  27. /// Work buffer for upsampler.
  28. /// </summary>
  29. private Memory<float> _upSamplerWorkBuffer;
  30. /// <summary>
  31. /// Global lock of the object.
  32. /// </summary>
  33. private object Lock = new object();
  34. /// <summary>
  35. /// The upsamplers instances.
  36. /// </summary>
  37. private UpsamplerState[] _upsamplers;
  38. /// <summary>
  39. /// The count of upsamplers.
  40. /// </summary>
  41. private uint _count;
  42. /// <summary>
  43. /// Create a new <see cref="UpsamplerManager"/>.
  44. /// </summary>
  45. /// <param name="upSamplerWorkBuffer">Work buffer for upsampler.</param>
  46. /// <param name="count">The count of upsamplers.</param>
  47. public UpsamplerManager(Memory<float> upSamplerWorkBuffer, uint count)
  48. {
  49. _upSamplerWorkBuffer = upSamplerWorkBuffer;
  50. _count = count;
  51. _upsamplers = new UpsamplerState[_count];
  52. }
  53. /// <summary>
  54. /// Allocate a new <see cref="UpsamplerState"/>.
  55. /// </summary>
  56. /// <returns>A new <see cref="UpsamplerState"/> or null if out of memory.</returns>
  57. public UpsamplerState Allocate()
  58. {
  59. int workBufferOffset = 0;
  60. lock (Lock)
  61. {
  62. for (int i = 0; i < _count; i++)
  63. {
  64. if (_upsamplers[i] == null)
  65. {
  66. _upsamplers[i] = new UpsamplerState(this, i, _upSamplerWorkBuffer.Slice(workBufferOffset, RendererConstants.UpSampleEntrySize), RendererConstants.TargetSampleCount);
  67. return _upsamplers[i];
  68. }
  69. workBufferOffset += RendererConstants.UpSampleEntrySize;
  70. }
  71. }
  72. return null;
  73. }
  74. /// <summary>
  75. /// Free a <see cref="UpsamplerState"/> at the given index.
  76. /// </summary>
  77. /// <param name="index">The index of the <see cref="UpsamplerState"/> to free.</param>
  78. public void Free(int index)
  79. {
  80. lock (Lock)
  81. {
  82. Debug.Assert(_upsamplers[index] != null);
  83. _upsamplers[index] = null;
  84. }
  85. }
  86. }
  87. }