103 void prepare(
int blockSize,
const T* irData,
int irLength,
106 assert(blockSize >= 2 && (blockSize & (blockSize - 1)) == 0);
107 assert(irLength > 0);
108 assert(irData !=
nullptr);
109 if (irData ==
nullptr || irLength <= 0)
return;
115 while (bs < blockSize) bs <<= 1;
119 numBins_ = fftSize_ + 2;
123 const int headLength = tailBlock_ > 0 ? 2 * tailBlock_ - bs : irLength;
126 fft_ = std::make_unique<FFTReal<T>>(fftSize_);
129 numPartitions_ = (headLength + bs - 1) / bs;
132 irPartitions_.resize(
static_cast<size_t>(numPartitions_));
133 std::vector<T> paddedBlock(
static_cast<size_t>(fftSize_), T(0));
135 for (
int p = 0; p < numPartitions_; ++p)
138 int remaining = std::min(bs, headLength - offset);
141 std::fill(paddedBlock.begin(), paddedBlock.end(), T(0));
142 std::copy_n(irData + offset, remaining, paddedBlock.begin());
145 irPartitions_[
static_cast<size_t>(p)].resize(
static_cast<size_t>(numBins_));
146 fft_->forward(paddedBlock.data(), irPartitions_[
static_cast<size_t>(p)].data());
150 inputBuffer_.assign(
static_cast<size_t>(fftSize_), T(0));
151 outputBuffer_.assign(
static_cast<size_t>(fftSize_), T(0));
152 overlapBuffer_.assign(
static_cast<size_t>(blockSize_), T(0));
153 fftAccum_.assign(
static_cast<size_t>(numBins_), T(0));
156 fdlBuffer_.resize(
static_cast<size_t>(numPartitions_));
157 for (
auto& fdl : fdlBuffer_)
158 fdl.assign(
static_cast<size_t>(numBins_), T(0));
160 prepareTail(irData + headLength, irLength - headLength);
170 std::fill(inputBuffer_.begin(), inputBuffer_.end(), T(0));
171 std::fill(outputBuffer_.begin(), outputBuffer_.end(), T(0));
172 std::fill(overlapBuffer_.begin(), overlapBuffer_.end(), T(0));
173 for (
auto& fdl : fdlBuffer_)
174 std::fill(fdl.begin(), fdl.end(), T(0));
192 void process(
const T* input, T* output,
int numSamples)
noexcept
197 std::copy_n(input, numSamples, output);
204 while (i < numSamples)
206 const int run = std::min(numSamples - i, blockSize_ - inputPos_);
211 std::copy_n(input + i, run,
212 inputBuffer_.begin() + blockSize_ + inputPos_);
214 for (
int k = 0; k < run; ++k)
215 inRing_[
static_cast<size_t>((absPos_ + k) & inMask_)] = input[i + k];
216 std::copy_n(overlapBuffer_.begin() + inputPos_, run, output + i);
218 addTail(output + i, run);
223 if (inputPos_ >= blockSize_)
225 processPartitionBlock();
228 std::copy_n(outputBuffer_.begin() + blockSize_,
static_cast<size_t>(blockSize_),
229 overlapBuffer_.begin());
242 process(data, data, numSamples);
275 if (buffer.getNumChannels() > 0 && buffer.getNumSamples() > 0)
280 [[nodiscard]]
int getLatency() const noexcept {
return blockSize_; }
287 [[nodiscard]]
int getNumPartitions() const noexcept {
return numPartitions_ + numTailParts_; }
304 static int chooseTailBlock(
int bs,
int irLength)
noexcept
306 const double uniform =
static_cast<double>(irLength) / bs;
307 double best = 0.8 * uniform;
309 for (
int t = 4 * bs; t <= kMaxTailBlock; t <<= 1)
311 const int head = 2 * t - bs;
312 if (irLength <= head + t)
break;
313 const double tailParts = std::ceil(
static_cast<double>(irLength - head) / t);
314 const double cost =
static_cast<double>(head) / bs + tailParts
315 + 1.5 * std::log2(2.0 * t);
316 if (cost < best) { best = cost; choice = t; }
322 void prepareTail(
const T* tailIr,
int tailLength)
326 if (tailBlock_ == 0 || tailLength <= 0)
330 tailIR_.clear(); tailFdl_.clear(); tailAccum_.clear();
331 tailScratch_.clear(); tailOut_.clear(); inRing_.clear();
334 const int tb = tailBlock_;
335 const std::size_t spec =
static_cast<std::size_t
>(2 * tb + 2);
336 numTailParts_ = (tailLength + tb - 1) / tb;
337 totalUnits_ = numTailParts_ + 3;
339 tailFft_ = std::make_unique<FFTReal<T>>(2 * tb);
340 tailIR_.assign(
static_cast<std::size_t
>(numTailParts_) * spec, T(0));
341 tailFdl_.assign(
static_cast<std::size_t
>(numTailParts_) * spec, T(0));
342 tailAccum_.assign(spec, T(0));
343 tailScratch_.assign(
static_cast<std::size_t
>(2 * tb), T(0));
344 tailOut_.assign(
static_cast<std::size_t
>(8 * tb), T(0));
345 inRing_.assign(
static_cast<std::size_t
>(4 * tb), T(0));
346 outMask_ = 8 * tb - 1;
347 inMask_ = 4 * tb - 1;
349 std::vector<T> padded(
static_cast<std::size_t
>(2 * tb), T(0));
350 for (
int p = 0; p < numTailParts_; ++p)
352 const int len = std::min(tb, tailLength - p * tb);
353 std::fill(padded.begin(), padded.end(), T(0));
354 std::copy_n(tailIr +
static_cast<std::size_t
>(p) * tb, len, padded.begin());
355 tailFft_->forward(padded.data(), &tailIR_[
static_cast<std::size_t
>(p) * spec]);
359 void resetTail() noexcept
361 std::fill(tailFdl_.begin(), tailFdl_.end(), T(0));
362 std::fill(tailOut_.begin(), tailOut_.end(), T(0));
363 std::fill(inRing_.begin(), inRing_.end(), T(0));
364 absPos_ =
static_cast<std::int64_t
>(inRing_.size());
367 taskPhase_ = totalUnits_;
381 void addTail(T* out,
int count)
noexcept
383 for (
int k = 0; k < count; ++k)
385 T& slot = tailOut_[
static_cast<size_t>((absPos_ + k) & outMask_)];
392 runTaskUnits(
static_cast<int>((
static_cast<std::int64_t
>(totalUnits_) * cyclePos_
393 + tailBlock_ - 1) / tailBlock_));
394 if (cyclePos_ >= tailBlock_)
397 taskStart_ = absPos_;
398 tailFdlIndex_ = (tailFdlIndex_ + 1) % numTailParts_;
409 void runTaskUnits(
int target)
noexcept
411 const int tb = tailBlock_;
412 const std::size_t spec =
static_cast<std::size_t
>(2 * tb + 2);
413 target = std::min(target, totalUnits_);
414 while (taskPhase_ < target)
416 const int unit = taskPhase_++;
419 const std::int64_t blockStart = taskStart_ - tb;
420 const int s0 =
static_cast<int>(blockStart & inMask_);
421 const int first = std::min(tb, inMask_ + 1 - s0);
422 std::copy_n(&inRing_[
static_cast<size_t>(s0)], first, tailScratch_.begin());
424 std::copy_n(inRing_.data(), tb - first, tailScratch_.begin() + first);
425 std::fill(tailScratch_.begin() + tb, tailScratch_.end(), T(0));
426 tailFft_->forward(tailScratch_.data(),
427 &tailFdl_[
static_cast<size_t>(tailFdlIndex_) * spec]);
428 std::fill(tailAccum_.begin(), tailAccum_.end(), T(0));
430 else if (unit <= numTailParts_)
432 const int p = unit - 1;
433 const int slot = (tailFdlIndex_ - p + numTailParts_) % numTailParts_;
435 &tailIR_[
static_cast<size_t>(p) * spec], tb + 1);
437 else if (unit == numTailParts_ + 1)
439 tailFft_->inverse(tailAccum_.data(), tailScratch_.data());
443 const std::int64_t dst = taskStart_ + tb;
444 for (
int m = 0; m < 2 * tb; ++m)
445 tailOut_[
static_cast<size_t>((dst + m) & outMask_)] += tailScratch_[
static_cast<size_t>(m)];
459 void processPartitionBlock() noexcept
463 fft_->forward(inputBuffer_.data(),
464 fdlBuffer_[
static_cast<size_t>(fdlIndex_)].data());
467 std::fill(fftAccum_.begin(), fftAccum_.end(), T(0));
469 for (
int p = 0; p < numPartitions_; ++p)
471 int fdlIdx = (fdlIndex_ - p + numPartitions_) % numPartitions_;
472 const auto& irPart = irPartitions_[
static_cast<size_t>(p)];
473 const auto& fdlPart = fdlBuffer_[
static_cast<size_t>(fdlIdx)];
476 int bins = numBins_ / 2;
481 fft_->inverse(fftAccum_.data(), outputBuffer_.data());
484 std::copy_n(inputBuffer_.begin() + blockSize_,
static_cast<size_t>(blockSize_),
485 inputBuffer_.begin());
486 std::fill(inputBuffer_.begin() + blockSize_, inputBuffer_.end(), T(0));
489 fdlIndex_ = (fdlIndex_ + 1) % numPartitions_;
492 static constexpr int kMaxTailBlock = 65536;
497 int numPartitions_ = 0;
501 std::unique_ptr<FFTReal<T>> fft_;
504 std::vector<std::vector<T>> irPartitions_;
507 std::vector<std::vector<T>> fdlBuffer_;
510 std::vector<T> inputBuffer_;
511 std::vector<T> outputBuffer_;
512 std::vector<T> overlapBuffer_;
513 std::vector<T> fftAccum_;
517 int numTailParts_ = 0;
519 std::unique_ptr<FFTReal<T>> tailFft_;
520 std::vector<T> tailIR_;
521 std::vector<T> tailFdl_;
522 std::vector<T> tailAccum_;
523 std::vector<T> tailScratch_;
524 std::vector<T> tailOut_;
525 std::vector<T> inRing_;
528 std::int64_t absPos_ = 0;
529 std::int64_t taskStart_ = 0;
531 int tailFdlIndex_ = 0;