DelayLine.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. namespace Ryujinx.Audio.Renderer.Dsp.Effect
  19. {
  20. public class DelayLine : IDelayLine
  21. {
  22. private float[] _workBuffer;
  23. private uint _sampleRate;
  24. private uint _currentSampleIndex;
  25. private uint _lastSampleIndex;
  26. public uint CurrentSampleCount { get; private set; }
  27. public uint SampleCountMax { get; private set; }
  28. public DelayLine(uint sampleRate, float delayTimeMax)
  29. {
  30. _sampleRate = sampleRate;
  31. SampleCountMax = IDelayLine.GetSampleCount(_sampleRate, delayTimeMax);
  32. _workBuffer = new float[SampleCountMax + 1];
  33. SetDelay(delayTimeMax);
  34. }
  35. private void ConfigureDelay(uint targetSampleCount)
  36. {
  37. CurrentSampleCount = Math.Min(SampleCountMax, targetSampleCount);
  38. _currentSampleIndex = 0;
  39. if (CurrentSampleCount == 0)
  40. {
  41. _lastSampleIndex = 0;
  42. }
  43. else
  44. {
  45. _lastSampleIndex = CurrentSampleCount - 1;
  46. }
  47. }
  48. public void SetDelay(float delayTime)
  49. {
  50. ConfigureDelay(IDelayLine.GetSampleCount(_sampleRate, delayTime));
  51. }
  52. public float Read()
  53. {
  54. return _workBuffer[_currentSampleIndex];
  55. }
  56. public float Update(float value)
  57. {
  58. float output = Read();
  59. _workBuffer[_currentSampleIndex++] = value;
  60. if (_currentSampleIndex >= _lastSampleIndex)
  61. {
  62. _currentSampleIndex = 0;
  63. }
  64. return output;
  65. }
  66. public float TapUnsafe(uint sampleIndex, int offset)
  67. {
  68. return IDelayLine.Tap(_workBuffer, (int)_currentSampleIndex, (int)sampleIndex + offset, (int)CurrentSampleCount);
  69. }
  70. public float Tap(uint sampleIndex)
  71. {
  72. if (sampleIndex >= CurrentSampleCount)
  73. {
  74. sampleIndex = CurrentSampleCount - 1;
  75. }
  76. return TapUnsafe(sampleIndex, -1);
  77. }
  78. }
  79. }