Pdc.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. // Plugin delay compensation (PDC), in the "per-edge" model FL uses
  3. // (docs/analise/REA_rodada2.md §1): in routing order,
  4. // inAlign[T] = max(latency of the instruments feeding T, outLat[S] of each S -> T)
  5. // outLat[T] = inAlign[T] + sum of the latencies of T's enabled effects
  6. // and each edge (instrument -> track, track -> track) delays by what's missing to reach
  7. // the destination's inAlign. The longest path is never delayed.
  8. #include <memory>
  9. #include <vector>
  10. #include "core/Song.h"
  11. namespace lynx
  12. {
  13. struct PdcPlan
  14. {
  15. std::vector<int> channelDelay; // per channel, in samples
  16. std::vector<std::vector<int>> sendDelay; // [track][k]: k = 0 Master, k = 1 + i for sends[i], then one per
  17. // Fruity Send slot (fruitySendsOf order): k = 1 + sends.size() + j
  18. std::vector<int> inAlign, outLat; // per track
  19. int total = 0; // latency at the Master output
  20. bool any() const noexcept;
  21. };
  22. // `channelLatency[c]` = latency of the channel's instrument; `slotLatency[t][s]` = of the slot's
  23. // effect (0 if disabled). Vectors smaller than the Song count as 0.
  24. PdcPlan computePdc (const Song& song, const std::vector<int>& channelLatency, const std::vector<std::vector<int>>& slotLatency);
  25. // Stereo delay line with an integer delay, allocated on creation. process() does not allocate.
  26. class DelayLine
  27. {
  28. public:
  29. explicit DelayLine (int delaySamples);
  30. int length() const noexcept { return delay; }
  31. void process (float* left, float* right, int n) noexcept; // in place
  32. // Delays `source * level` and adds it into `dest`, in a single pass. With a null source (silent
  33. // source) pushes silence; when the line is already all silence, does nothing.
  34. void mixInto (const float* sourceLeft, const float* sourceRight, float level, float* destLeft, float* destRight, int n) noexcept;
  35. private:
  36. int delay;
  37. std::vector<float> left, right; // planar ring
  38. int position = 0;
  39. int quiet = 0; // consecutive silent samples already written (up to delay)
  40. };
  41. } // namespace lynx