DelayLineReverb3d.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Copyright (c) 2019-2020 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.Diagnostics;
  18. namespace Ryujinx.Audio.Renderer.Dsp.Effect
  19. {
  20. public class DelayLine3d : 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 DelayLine3d(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. if (SampleCountMax >= targetSampleCount)
  38. {
  39. CurrentSampleCount = targetSampleCount;
  40. _lastSampleIndex = (_currentSampleIndex + targetSampleCount) % (SampleCountMax + 1);
  41. }
  42. }
  43. public void SetDelay(float delayTime)
  44. {
  45. ConfigureDelay(IDelayLine.GetSampleCount(_sampleRate, delayTime));
  46. }
  47. public float Read()
  48. {
  49. return _workBuffer[_currentSampleIndex];
  50. }
  51. public float Update(float value)
  52. {
  53. Debug.Assert(!float.IsNaN(value) && !float.IsInfinity(value));
  54. _workBuffer[_lastSampleIndex++] = value;
  55. float output = Read();
  56. _currentSampleIndex++;
  57. if (_currentSampleIndex >= SampleCountMax)
  58. {
  59. _currentSampleIndex = 0;
  60. }
  61. if (_lastSampleIndex >= SampleCountMax)
  62. {
  63. _lastSampleIndex = 0;
  64. }
  65. return output;
  66. }
  67. public float TapUnsafe(uint sampleIndex, int offset)
  68. {
  69. return IDelayLine.Tap(_workBuffer, (int)_lastSampleIndex, (int)sampleIndex + offset, (int)SampleCountMax + 1);
  70. }
  71. public float Tap(uint sampleIndex)
  72. {
  73. return TapUnsafe(sampleIndex, -1);
  74. }
  75. }
  76. }