FixedBlock.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. // FL's wrapper option "Use fixed size buffers" (REA_rodada118 Part B): the plugin is always called with the same
  3. // block length, whatever the host's chunk sizes are (LYNX splits its blocks at every event, so without this a plugin
  4. // sees variable, sometimes tiny, blocks). Input is collected until a whole block is there; the processed block is
  5. // played back while the next one fills, so the output is the input delayed by exactly `block` samples — reported to
  6. // PDC as extra latency. JUCE-free; real-time safe after construction (no allocation in `run`).
  7. #include <algorithm>
  8. #include <cstddef>
  9. #include <vector>
  10. namespace lynx
  11. {
  12. // The block FL's wrapper uses (Fruity Wrapper FUN_00870670, REA round 134): "Use fixed size buffers" alone = 2 ms
  13. // of audio rounded up to a multiple of 4 samples (88 at 44.1 kHz, 96 at 48 kHz); with "Process maximum size buffers"
  14. // the host's maximum block (LYNX: the audio device's buffer) - more latency.
  15. inline int flFixedBlockSize (double sampleRate, int deviceBlock, bool maximumSize) noexcept
  16. {
  17. if (maximumSize)
  18. return std::max (16, deviceBlock);
  19. const auto twoMs = (int) (sampleRate * 2.0 / 1000.0 + 0.5);
  20. return std::max (16, (twoMs + 3) & ~3);
  21. }
  22. class FixedBlock
  23. {
  24. public:
  25. FixedBlock (int blockSize, int channels)
  26. : block (std::max (1, blockSize)), numChannels (std::max (1, channels)),
  27. input ((std::size_t) (block * numChannels), 0.0f), output ((std::size_t) (block * numChannels), 0.0f)
  28. {
  29. for (int c = 0; c < numChannels; ++c)
  30. pointers.push_back (input.data() + (std::size_t) (c * block));
  31. }
  32. int blockSize() const noexcept { return block; }
  33. int latency() const noexcept { return block; }
  34. // where the next input sample goes inside the block being collected (MIDI offsets of events arriving now)
  35. int position() const noexcept { return pos; }
  36. // `io[c]` (c < channels given at construction, `n` samples each) is replaced with the delayed output. `hasInput`
  37. // false: blocks start silent (instruments). `process (float* const* channels, int block)` works in place on a
  38. // whole block.
  39. template <typename Process>
  40. void run (float* const* io, int n, bool hasInput, Process&& process) noexcept
  41. {
  42. for (int done = 0; done < n;)
  43. {
  44. const auto count = std::min (n - done, block - pos);
  45. for (int c = 0; c < numChannels; ++c)
  46. {
  47. auto* in = input.data() + (std::size_t) (c * block + pos);
  48. auto* out = output.data() + (std::size_t) (c * block + pos);
  49. auto* x = io[c] + done;
  50. for (int i = 0; i < count; ++i)
  51. {
  52. const auto v = x[i];
  53. x[i] = out[i];
  54. in[i] = hasInput ? v : 0.0f;
  55. }
  56. }
  57. pos += count;
  58. done += count;
  59. if (pos == block)
  60. {
  61. process (pointers.data(), block);
  62. std::copy (input.begin(), input.end(), output.begin());
  63. pos = 0;
  64. }
  65. }
  66. }
  67. void clear() noexcept
  68. {
  69. std::fill (input.begin(), input.end(), 0.0f);
  70. std::fill (output.begin(), output.end(), 0.0f);
  71. pos = 0;
  72. }
  73. private:
  74. int block, numChannels, pos = 0;
  75. std::vector<float> input, output;
  76. std::vector<float*> pointers;
  77. };
  78. } // namespace lynx