DelayLineReverb3d.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Diagnostics;
  2. namespace Ryujinx.Audio.Renderer.Dsp.Effect
  3. {
  4. public class DelayLine3d : IDelayLine
  5. {
  6. private float[] _workBuffer;
  7. private uint _sampleRate;
  8. private uint _currentSampleIndex;
  9. private uint _lastSampleIndex;
  10. public uint CurrentSampleCount { get; private set; }
  11. public uint SampleCountMax { get; private set; }
  12. public DelayLine3d(uint sampleRate, float delayTimeMax)
  13. {
  14. _sampleRate = sampleRate;
  15. SampleCountMax = IDelayLine.GetSampleCount(_sampleRate, delayTimeMax);
  16. _workBuffer = new float[SampleCountMax + 1];
  17. SetDelay(delayTimeMax);
  18. }
  19. private void ConfigureDelay(uint targetSampleCount)
  20. {
  21. if (SampleCountMax >= targetSampleCount)
  22. {
  23. CurrentSampleCount = targetSampleCount;
  24. _lastSampleIndex = (_currentSampleIndex + targetSampleCount) % (SampleCountMax + 1);
  25. }
  26. }
  27. public void SetDelay(float delayTime)
  28. {
  29. ConfigureDelay(IDelayLine.GetSampleCount(_sampleRate, delayTime));
  30. }
  31. public float Read()
  32. {
  33. return _workBuffer[_currentSampleIndex];
  34. }
  35. public float Update(float value)
  36. {
  37. Debug.Assert(!float.IsNaN(value) && !float.IsInfinity(value));
  38. _workBuffer[_lastSampleIndex++] = value;
  39. float output = Read();
  40. _currentSampleIndex++;
  41. if (_currentSampleIndex >= SampleCountMax)
  42. {
  43. _currentSampleIndex = 0;
  44. }
  45. if (_lastSampleIndex >= SampleCountMax)
  46. {
  47. _lastSampleIndex = 0;
  48. }
  49. return output;
  50. }
  51. public float TapUnsafe(uint sampleIndex, int offset)
  52. {
  53. return IDelayLine.Tap(_workBuffer, (int)_lastSampleIndex, (int)sampleIndex + offset, (int)SampleCountMax + 1);
  54. }
  55. public float Tap(uint sampleIndex)
  56. {
  57. return TapUnsafe(sampleIndex, -1);
  58. }
  59. }
  60. }