116 assert((fftSize & (fftSize - 1)) == 0 && fftSize >= kMinFftSize
117 &&
"FFT size must be a power of 2");
121 fftSize = std::clamp(fftSize, kMinFftSize, kMaxFftSize);
122 int pow2 = kMinFftSize;
123 while (pow2 < fftSize) pow2 <<= 1;
125 mask_ = fftSize_ - 1;
127 hopSize_ = (hopSize > 0) ? hopSize : fftSize_ / 2;
128 hopSize_ = std::clamp(hopSize_, 1, fftSize_ / 2);
129 if (fftSize_ % hopSize_ != 0) hopSize_ = fftSize_ / 2;
130 numBins_ = fftSize_ / 2 + 1;
132 fft_ = std::make_unique<FFTReal<T>>(fftSize_);
135 window_.resize(
static_cast<std::size_t
>(fftSize_));
137 for (
auto& w : window_) w = std::sqrt(w);
142 inputRing_.resize(
static_cast<std::size_t
>(nCh));
143 outputAccum_.resize(
static_cast<std::size_t
>(nCh));
144 inputPos_.assign(
static_cast<std::size_t
>(nCh), 0);
145 outputReadPos_.assign(
static_cast<std::size_t
>(nCh), 0);
146 spectra_.assign(
static_cast<std::size_t
>(nCh),
147 std::vector<T>(
static_cast<std::size_t
>(fftSize_ + 2), T(0)));
150 accumMask_ = (fftSize_ * 2) - 1;
152 for (
int ch = 0; ch < nCh; ++ch)
154 inputRing_[
static_cast<std::size_t
>(ch)]
155 .assign(
static_cast<std::size_t
>(fftSize_), T(0));
156 outputAccum_[
static_cast<std::size_t
>(ch)]
157 .assign(
static_cast<std::size_t
>(fftSize_ * 2), T(0));
162 fftIn_.resize(
static_cast<std::size_t
>(fftSize_));
163 fftOut_.resize(
static_cast<std::size_t
>(fftSize_ + 2));
164 fftResult_.resize(
static_cast<std::size_t
>(fftSize_));
183 template <
typename Func>
186 processHops(buffer, [&](
int nCh)
noexcept
188 for (
int ch = 0; ch < nCh; ++ch)
190 analyse(ch, fftOut_.data());
191 processFunc(fftOut_.data(), numBins_);
192 synthesise(ch, fftOut_.data());
211 template <
typename Observe,
typename Func>
214 processHops(buffer, [&](
int nCh)
noexcept
216 for (
int ch = 0; ch < nCh; ++ch)
218 T* spectrum = spectra_[
static_cast<std::size_t
>(ch)].data();
219 analyse(ch, spectrum);
220 observe(ch,
static_cast<const T*
>(spectrum), numBins_);
222 for (
int ch = 0; ch < nCh; ++ch)
224 T* spectrum = spectra_[
static_cast<std::size_t
>(ch)].data();
225 processFunc(ch, spectrum, numBins_);
226 synthesise(ch, spectrum);
233 template <
typename Hop>
236 if (!prepared_)
return;
239 const int nCh = std::min(buffer.getNumChannels(),
240 static_cast<int>(inputRing_.size()));
241 const int nS = buffer.getNumSamples();
253 const int chunk = std::min(nS - i, hopSize_ - hopCounter_);
257 for (
int ch = 0; ch < nCh; ++ch)
259 const T* data = buffer.getChannel(ch) + i;
260 auto& ring = inputRing_[
static_cast<std::size_t
>(ch)];
261 const int wp = inputPos_[
static_cast<std::size_t
>(ch)];
262 const int first = std::min(chunk, fftSize_ - wp);
263 std::copy_n(data, first, ring.data() + wp);
264 std::copy_n(data + first, chunk - first, ring.data());
265 inputPos_[
static_cast<std::size_t
>(ch)] = (wp + chunk) & mask_;
270 for (
int ch = 0; ch < nCh; ++ch)
272 T* data = buffer.getChannel(ch) + i;
273 T* acc = outputAccum_[
static_cast<std::size_t
>(ch)].data();
274 const int rp = outputReadPos_[
static_cast<std::size_t
>(ch)];
275 const int first = std::min(chunk, fftSize_ * 2 - rp);
276 for (
int k = 0; k < first; ++k)
278 data[k] = acc[rp + k];
281 for (
int k = first; k < chunk; ++k)
283 data[k] = acc[k - first];
284 acc[k - first] = T(0);
286 outputReadPos_[
static_cast<std::size_t
>(ch)] = (rp + chunk) & accumMask_;
290 hopCounter_ += chunk;
291 if (hopCounter_ >= hopSize_)
309 [[nodiscard]]
int getLatency() const noexcept {
return fftSize_; }
310 [[nodiscard]]
int getFFTSize() const noexcept {
return fftSize_; }
311 [[nodiscard]]
int getNumBins() const noexcept {
return numBins_; }
312 [[nodiscard]]
int getHopSize() const noexcept {
return hopSize_; }
317 for (
auto& ring : inputRing_) std::fill(ring.begin(), ring.end(), T(0));
318 for (
auto& acc : outputAccum_) std::fill(acc.begin(), acc.end(), T(0));
319 std::fill(inputPos_.begin(), inputPos_.end(), 0);
320 std::fill(outputReadPos_.begin(), outputReadPos_.end(), 0);
333 void analyse(
int ch, T* spectrum)
noexcept
337 const T* ring = inputRing_[
static_cast<std::size_t
>(ch)].data();
338 const T* win = window_.data();
339 const int readPos = inputPos_[
static_cast<std::size_t
>(ch)];
340 const int head = fftSize_ - readPos;
341 for (
int k = 0; k < head; ++k)
342 fftIn_[
static_cast<std::size_t
>(k)] = ring[readPos + k] * win[k];
343 for (
int k = head; k < fftSize_; ++k)
344 fftIn_[
static_cast<std::size_t
>(k)] = ring[k - head] * win[k];
347 fft_->forward(fftIn_.data(), spectrum);
351 void synthesise(
int ch, T* spectrum)
noexcept
353 const T* win = window_.data();
357 fft_->inverse(spectrum, fftResult_.data());
360 T* acc = outputAccum_[
static_cast<std::size_t
>(ch)].data();
361 const T* res = fftResult_.data();
362 const int writePos = outputReadPos_[
static_cast<std::size_t
>(ch)];
363 const int headOut = std::min(fftSize_, fftSize_ * 2 - writePos);
364 for (
int k = 0; k < headOut; ++k)
365 acc[writePos + k] += res[k] * win[k] * wolaNorm_;
366 for (
int k = headOut; k < fftSize_; ++k)
367 acc[k - headOut] += res[k] * win[k] * wolaNorm_;
378 void computeWolaNorm() noexcept
380 const int numOverlaps = fftSize_ / hopSize_;
382 for (
int pos = 0; pos < hopSize_; ++pos)
385 for (
int hop = 0; hop < numOverlaps; ++hop)
387 const int idx = pos + hop * hopSize_;
390 const T w = window_[
static_cast<std::size_t
>(idx)];
394 if (sumSq > maxSum) maxSum = sumSq;
396 wolaNorm_ = (maxSum > T(1e-10)) ? (T(1) / maxSum) : T(1);
400 static constexpr int kMinFftSize = 4;
401 static constexpr int kMaxFftSize = 1 << 20;
404 bool prepared_ =
false;
410 int accumMask_ = 4095;
412 std::unique_ptr<FFTReal<T>> fft_;
413 std::vector<T> window_;
416 std::vector<std::vector<T>> inputRing_;
417 std::vector<std::vector<T>> outputAccum_;
418 std::vector<int> inputPos_;
419 std::vector<int> outputReadPos_;
420 std::vector<std::vector<T>> spectra_;
424 std::vector<T> fftIn_, fftOut_, fftResult_;