132 if (fftSize != 0 && ((fftSize & (fftSize - 1)) != 0
133 || fftSize < 256 || fftSize > (1 << 20)))
136 prepared_.store(
false, std::memory_order_relaxed);
141 if (fftSize == 0) fftSize = defaultFrame(spec.
sampleRate);
142 const int fftStudio = fftSize;
149 const int synthHop = fftSize / 4;
150 readOffset_ = fftSize + synthHop;
151 latencyLegacy_ = readOffset_ + fftSize - synthHop;
162 studio_.prepare(spec.
sampleRate, numChannels_, fftStudio,
true);
163 studio_.setAnchorLeadLimit(
static_cast<double>(fftStudio / 4));
164 latencyStudio_ = fftStudio + fftStudio / 2 + studio_.lookahead() + fftStudio / 4 + 64;
166 while (drySize_ < std::max(latencyLegacy_, latencyStudio_) + 1) drySize_ <<= 1;
167 dryMask_ = drySize_ - 1;
187 engine_.prepare(spec.
sampleRate, numChannels_, fftSize,
true,
false,
false,
false);
188 mixMaxStep_ =
static_cast<T
>(1.0 / std::max(1.0, spec.
sampleRate * 0.02));
189 accumMask_ = engine_.olaMask();
191 dryRing_.assign(
static_cast<size_t>(numChannels_), {});
192 for (
int ch = 0; ch < numChannels_; ++ch)
193 dryRing_[
static_cast<size_t>(ch)].assign(
static_cast<size_t>(drySize_), T(0));
195 publishEngineParams();
196 prepared_.store(
true, std::memory_order_relaxed);
203 if (!prepared_.load(std::memory_order_relaxed))
return;
204 studioActive_ = quality_.load(std::memory_order_relaxed) ==
Quality::Studio;
205 latency_.store(studioActive_ ? latencyStudio_ : latencyLegacy_,
206 std::memory_order_relaxed);
209 for (
auto& r : dryRing_) std::fill(r.begin(), r.end(), T(0));
214 const double start = studio_.streamPositionOf(-
static_cast<double>(latencyStudio_));
215 studioReadInt_ =
static_cast<int64_t
>(std::floor(start));
216 studioReadFrac_ = start -
static_cast<double>(studioReadInt_);
220 readPosInt_ = engine_.writeHead() - readOffset_;
222 currentMix_ = mix_.load(std::memory_order_relaxed);
246 quality =
static_cast<Quality>(std::clamp(
static_cast<int>(quality), 0,
248 quality_.store(quality, std::memory_order_relaxed);
254 return quality_.load(std::memory_order_relaxed);
266 if (!std::isfinite(st))
return;
267 semitones_.store(std::clamp(st, T(-12), T(12)), std::memory_order_relaxed);
268 publishEngineParams();
275 if (!std::isfinite(ratio))
return;
276 ratio = std::clamp(ratio, T(0.5), T(2));
277 semitones_.store(
static_cast<T
>(12.0 * std::log2(
static_cast<double>(ratio))),
278 std::memory_order_relaxed);
279 publishEngineParams();
288 if (!std::isfinite(mix))
return;
289 mix_.store(std::clamp(mix, T(0), T(1)), std::memory_order_relaxed);
295 transientPreserve_.store(enabled, std::memory_order_relaxed);
296 publishEngineParams();
310 formantPreserve_.store(enabled, std::memory_order_relaxed);
311 publishEngineParams();
317 return semitones_.load(std::memory_order_relaxed);
321 [[nodiscard]] T
getMix() const noexcept {
return mix_.load(std::memory_order_relaxed); }
326 return transientPreserve_.load(std::memory_order_relaxed);
332 return formantPreserve_.load(std::memory_order_relaxed);
342 return prepared_.load(std::memory_order_relaxed)
343 ? latency_.load(std::memory_order_relaxed)
348 [[nodiscard]] std::vector<uint8_t>
getState()
const
351 w.
write(
"semitones",
static_cast<float>(semitones_.load(std::memory_order_relaxed)));
352 w.
write(
"mix",
static_cast<float>(mix_.load(std::memory_order_relaxed)));
353 w.
write(
"transient", transientPreserve_.load(std::memory_order_relaxed));
354 w.
write(
"formant", formantPreserve_.load(std::memory_order_relaxed));
355 w.
write(
"quality",
static_cast<int32_t
>(quality_.load(std::memory_order_relaxed)));
384 if (!prepared_.load(std::memory_order_relaxed))
return;
387 const int nCh = std::min(buffer.getNumChannels(), numChannels_);
388 const int nS = buffer.getNumSamples();
391 if ((quality_.load(std::memory_order_relaxed) ==
Quality::Studio) != studioActive_)
395 processStudio(buffer, nCh, nS);
399 const int latency = latency_.load(std::memory_order_relaxed);
404 const T mixTarget = mix_.load(std::memory_order_relaxed);
405 const T mixStart = currentMix_;
409 const bool highQuality = quality_.load(std::memory_order_relaxed) ==
Quality::High;
410 if (highQuality != highReader_)
412 highReader_ = highQuality;
413 readerFadeLeft_ = kReaderFade;
419 const int chunk = std::min(nS - i, engine_.samplesToNextHop());
422 for (
int ch = 0; ch < nCh; ++ch)
424 const T* in = buffer.getChannel(ch) + i;
425 engine_.pushInput(ch, in, chunk);
426 auto& dry = dryRing_[
static_cast<size_t>(ch)];
428 for (
int k = 0; k < chunk; ++k)
430 dry[
static_cast<size_t>(dp)] = std::isfinite(in[k]) ? in[k] : T(0);
431 dp = (dp + 1) & dryMask_;
436 const double ratio = engine_.activeRatio();
437 int64_t rpEnd = readPosInt_;
438 double rfEnd = readPosFrac_;
439 for (
int ch = 0; ch < nCh; ++ch)
441 T* out = buffer.getChannel(ch) + i;
442 const T* acc = engine_.olaData(ch);
443 const auto& dry = dryRing_[
static_cast<size_t>(ch)];
445 int64_t rp = readPosInt_;
446 double rf = readPosFrac_;
448 int fadeLeft = readerFadeLeft_;
450 for (
int k = 0; k < chunk; ++k)
452 T wet = readWet(highReader_, acc, rp, rf);
455 const T w =
static_cast<T
>(fadeLeft) * (T(1) / T(kReaderFade));
456 const T old = readWet(!highReader_, acc, rp, rf);
457 wet += (old - wet) * w;
460 const int dryIdx = (dp - latency) & dryMask_;
461 const T drySample = dry[
static_cast<size_t>(dryIdx)];
462 const T mixVal =
moveTowards(mixStart, mixTarget, mixMaxStep_ *
static_cast<T
>(i + k + 1));
465 out[k] = drySample * (T(1) - mixVal) + wet * mixVal;
468 const auto adv =
static_cast<int64_t
>(rf);
470 rf -=
static_cast<double>(adv);
471 dp = (dp + 1) & dryMask_;
473 if (ch == 0) { rpEnd = rp; rfEnd = rf; }
487 for (
int k = 0; k < chunk; ++k)
490 const auto adv =
static_cast<int64_t
>(rfEnd);
492 rfEnd -=
static_cast<double>(adv);
496 readPosFrac_ = rfEnd;
497 dryPos_ = (dryPos_ + chunk) & dryMask_;
498 readerFadeLeft_ = std::max(0, readerFadeLeft_ - chunk);
502 engine_.commitInput(chunk, nCh);
507 currentMix_ =
moveTowards(mixStart, mixTarget, mixMaxStep_ *
static_cast<T
>(nS));
523 const int latency = latency_.load(std::memory_order_relaxed);
524 const T mixTarget = mix_.load(std::memory_order_relaxed);
525 const T mixStart = currentMix_;
526 const int64_t mask = studio_.olaMask();
530 const int need = studio_.samplesToNextHop();
533 studio_.commitInput(0, nCh);
536 const int chunk = std::min(nS - i, need);
537 for (
int ch = 0; ch < nCh; ++ch)
539 const T* in = buffer.getChannel(ch) + i;
540 studio_.pushInput(ch, in, chunk);
541 auto& dry = dryRing_[
static_cast<size_t>(ch)];
543 for (
int k = 0; k < chunk; ++k)
545 dry[
static_cast<size_t>(dp)] = std::isfinite(in[k]) ? in[k] : T(0);
546 dp = (dp + 1) & dryMask_;
550 const double ratio = studio_.activeRatio();
551 int64_t rpEnd = studioReadInt_;
552 double rfEnd = studioReadFrac_;
553 for (
int ch = 0; ch < nCh; ++ch)
555 T* out = buffer.getChannel(ch) + i;
556 const T* acc = studio_.olaData(ch);
557 const auto& dry = dryRing_[
static_cast<size_t>(ch)];
558 int64_t rp = studioReadInt_;
559 double rf = studioReadFrac_;
561 for (
int k = 0; k < chunk; ++k)
563 const T wet = reader_.readRing(acc, mask, rp, rf);
564 const T drySample = dry[
static_cast<size_t>((dp -
latency) & dryMask_)];
565 const T mixVal =
moveTowards(mixStart, mixTarget, mixMaxStep_ *
static_cast<T
>(i + k + 1));
566 out[k] = drySample * (T(1) - mixVal) + wet * mixVal;
568 const auto adv =
static_cast<int64_t
>(rf);
570 rf -=
static_cast<double>(adv);
571 dp = (dp + 1) & dryMask_;
573 if (ch == 0) { rpEnd = rp; rfEnd = rf; }
577 for (
int k = 0; k < chunk; ++k)
580 const auto adv =
static_cast<int64_t
>(rfEnd);
582 rfEnd -=
static_cast<double>(adv);
585 studioReadInt_ = rpEnd;
586 studioReadFrac_ = rfEnd;
587 dryPos_ = (dryPos_ + chunk) & dryMask_;
592 const double c = studio_.nextStreamCentre() +
static_cast<double>(studio_.synthHop());
593 const double here =
static_cast<double>(studioReadInt_) + studioReadFrac_;
594 studio_.steerTimeline(
static_cast<double>(studioOut_) + (c - here) / ratio
595 -
static_cast<double>(latencyStudio_));
596 studio_.commitInput(chunk, nCh);
599 currentMix_ =
moveTowards(mixStart, mixTarget, mixMaxStep_ *
static_cast<T
>(nS));
603 [[nodiscard]]
static int defaultFrame(
double sampleRate)
noexcept
606 while (n < (1 << 16) &&
static_cast<double>(n) * 1.5 < 0.0427 * sampleRate) n <<= 1;
611 [[nodiscard]] T readWet(
bool high,
const T* acc, int64_t ip,
double frac)
const noexcept
613 return high ? reader_.readRing(acc, accumMask_, ip, frac)
614 : readCatmullRom(acc, ip, frac);
618 [[nodiscard]] T readCatmullRom(
const T* acc, int64_t ip,
double frac)
const noexcept
620 const int64_t m = accumMask_;
621 const T x0 = acc[
static_cast<size_t>((ip - 1) & m)];
622 const T x1 = acc[
static_cast<size_t>(ip & m)];
623 const T x2 = acc[
static_cast<size_t>((ip + 1) & m)];
624 const T x3 = acc[
static_cast<size_t>((ip + 2) & m)];
625 const T f =
static_cast<T
>(frac);
626 return x1 + T(0.5) * f * (x2 - x0
627 + f * (T(2) * x0 - T(5) * x1 + T(4) * x2 - x3
628 + f * (T(3) * (x1 - x2) + x3 - x0)));
633 void publishEngineParams() noexcept
635 typename detail::PhaseVocoderEngine<T>::Params p;
636 p.targetSemitones =
static_cast<double>(semitones_.load(std::memory_order_relaxed));
637 p.transientPreserve = transientPreserve_.load(std::memory_order_relaxed);
638 p.formantPreserve = formantPreserve_.load(std::memory_order_relaxed);
639 engine_.publishParams(p);
640 typename detail::StudioVocoder<T>::Params q;
641 q.targetSemitones = p.targetSemitones;
642 q.transientPreserve = p.transientPreserve;
643 q.formantPreserve = p.formantPreserve;
644 studio_.publishParams(q);
648 int numChannels_ = 0;
649 std::atomic<bool> prepared_ {
false };
651 int readOffset_ = 2560;
652 std::atomic<int> latency_ { 4096 };
656 int64_t accumMask_ = 8191;
658 detail::PhaseVocoderEngine<T> engine_;
659 detail::StudioVocoder<T> studio_;
660 bool studioActive_ =
true;
661 int latencyStudio_ = 5184;
662 int latencyLegacy_ = 4096;
663 int64_t studioReadInt_ = 0;
664 double studioReadFrac_ = 0.0;
665 int64_t studioOut_ = 0;
666 SincInterpolator<T> reader_;
669 static constexpr int kReaderFade = 64;
670 bool highReader_ =
false;
671 int readerFadeLeft_ = 0;
673 std::vector<std::vector<T>> dryRing_;
676 int64_t readPosInt_ = 0;
677 double readPosFrac_ = 0.0;
678 T currentMix_ = T(1);
679 T mixMaxStep_ = T(1.0 / 960.0);
681 std::atomic<T> semitones_ { T(0) };
682 std::atomic<T> mix_ { T(1) };
683 std::atomic<bool> transientPreserve_ {
true };
684 std::atomic<bool> formantPreserve_ {
false };