UpsamplerManager.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Diagnostics;
  3. namespace Ryujinx.Audio.Renderer.Server.Upsampler
  4. {
  5. /// <summary>
  6. /// Upsampler manager.
  7. /// </summary>
  8. public class UpsamplerManager
  9. {
  10. /// <summary>
  11. /// Work buffer for upsampler.
  12. /// </summary>
  13. private Memory<float> _upSamplerWorkBuffer;
  14. /// <summary>
  15. /// Global lock of the object.
  16. /// </summary>
  17. private object Lock = new object();
  18. /// <summary>
  19. /// The upsamplers instances.
  20. /// </summary>
  21. private UpsamplerState[] _upsamplers;
  22. /// <summary>
  23. /// The count of upsamplers.
  24. /// </summary>
  25. private uint _count;
  26. /// <summary>
  27. /// Create a new <see cref="UpsamplerManager"/>.
  28. /// </summary>
  29. /// <param name="upSamplerWorkBuffer">Work buffer for upsampler.</param>
  30. /// <param name="count">The count of upsamplers.</param>
  31. public UpsamplerManager(Memory<float> upSamplerWorkBuffer, uint count)
  32. {
  33. _upSamplerWorkBuffer = upSamplerWorkBuffer;
  34. _count = count;
  35. _upsamplers = new UpsamplerState[_count];
  36. }
  37. /// <summary>
  38. /// Allocate a new <see cref="UpsamplerState"/>.
  39. /// </summary>
  40. /// <returns>A new <see cref="UpsamplerState"/> or null if out of memory.</returns>
  41. public UpsamplerState Allocate()
  42. {
  43. int workBufferOffset = 0;
  44. lock (Lock)
  45. {
  46. for (int i = 0; i < _count; i++)
  47. {
  48. if (_upsamplers[i] == null)
  49. {
  50. _upsamplers[i] = new UpsamplerState(this, i, _upSamplerWorkBuffer.Slice(workBufferOffset, Constants.UpSampleEntrySize), Constants.TargetSampleCount);
  51. return _upsamplers[i];
  52. }
  53. workBufferOffset += Constants.UpSampleEntrySize;
  54. }
  55. }
  56. return null;
  57. }
  58. /// <summary>
  59. /// Free a <see cref="UpsamplerState"/> at the given index.
  60. /// </summary>
  61. /// <param name="index">The index of the <see cref="UpsamplerState"/> to free.</param>
  62. public void Free(int index)
  63. {
  64. lock (Lock)
  65. {
  66. Debug.Assert(_upsamplers[index] != null);
  67. _upsamplers[index] = null;
  68. }
  69. }
  70. }
  71. }