فهرست منبع

LYNX Tune / Voice Shift: PSOLA marks by period cross-correlation with sub-sample peak, continuous drift (round 148: held notes steady in noise)

Ka1-S 1 ماه پیش
والد
کامیت
2eb7edfbca
2فایلهای تغییر یافته به همراه56 افزوده شده و 14 حذف شده
  1. 53 13
      src/core/VocalPitch.cpp
  2. 3 1
      src/core/VocalPitch.h

+ 53 - 13
src/core/VocalPitch.cpp

@@ -391,6 +391,7 @@ void Psola::reset() noexcept
     period = 0.0;
     period = 0.0;
     voiced = false;
     voiced = false;
     lastVoiced = false;
     lastVoiced = false;
+    lastMark = 0.0;
     ratio = formant = 1.0;
     ratio = formant = 1.0;
 }
 }
 
 
@@ -460,21 +461,55 @@ void Psola::placeMarks() noexcept
         if (nextMark + reach <= last)
         if (nextMark + reach <= last)
         {
         {
             constexpr auto m = ringSize - 1;
             constexpr auto m = ringSize - 1;
-            const auto from = (std::int64_t) std::ceil (nextMark - reach), to = (std::int64_t) std::floor (nextMark + reach);
-            std::int64_t best = (std::int64_t) std::lround (nextMark);
-            float top = -std::numeric_limits<float>::max();
-            for (auto i = std::max<std::int64_t> (from, now - ringSize + 1); i <= to; ++i)
+            const auto from = std::max<std::int64_t> ((std::int64_t) std::ceil (nextMark - reach), now - ringSize + 1),
+                       to = (std::int64_t) std::floor (nextMark + reach);
+            // Round 148: the mark goes where the waveform repeats the one at the previous mark best (the cross-correlation
+            // of the low-passed signal over one period, peak refined to a fraction of a sample), not to the highest
+            // single peak. [EXEC] (REA_rodada148 §1) the peak in noise moved by a sample or two from period to period:
+            // the held note wobbled 4–11 c on low notes with white noise at 20 / 10 dB SNR or breath noise (Auto-Tune
+            // Pro: 0.3–4 c); a whole period integrates the noise away, and the fraction of a sample matters on high
+            // notes (the integer marks alone left 2 c of jitter). The first mark of a phrase still takes the peak.
+            const auto prev = lastMark;
+            const auto gap = (double) from - prev;
+            const bool corr = prev > 0.0 && gap > 0.5 * period && gap < 1.5 * period && prev - 2.0 * period > (double) (now - ringSize + 2);
+            // the period just before each (it is in even with the shortest latency)
+            const auto span = (int) period;
+            const auto pm = (std::int64_t) std::lround (prev);
+            // long periods: every other sample (or fewer) of the window — the low-passed signal is smooth there
+            const auto stride = std::max (1, span / 256);
+            auto score = [&] (std::int64_t i) {
+                if (! corr)
+                    return (double) env[(std::size_t) (i & m)];
+                double sxy = 0.0;
+                for (int j = -span; j <= 0; j += stride)
+                    sxy += (double) env[(std::size_t) ((i + j) & m)] * (double) env[(std::size_t) ((pm + j) & m)];
+                return sxy;
+            };
+            // prefer the predicted position a little: ties and flat tops do not make the marks wander
+            const auto pen = corr ? 0.0 : 1e-4;
+            std::int64_t best = std::clamp<std::int64_t> ((std::int64_t) std::lround (nextMark), from, std::max (from, to));
+            auto top = -std::numeric_limits<double>::max();
+            for (auto i = from; i <= to; ++i)
             {
             {
-                // prefer the predicted position a little: ties and flat tops do not make the marks wander
-                const auto v = env[(std::size_t) (i & m)] - 1e-4f * (float) std::abs ((double) i - nextMark) / (float) period;
+                const auto v = score (i) - pen * std::abs ((double) i - nextMark) / period;
                 if (v > top)
                 if (v > top)
                 {
                 {
                     top = v;
                     top = v;
                     best = i;
                     best = i;
                 }
                 }
             }
             }
-            push ((double) best, (float) period, true);
-            nextMark = (double) best + period;
+            auto pos = (double) best;
+            if (best > from && best < to)
+            {
+                const auto y0 = score (best - 1), y1 = score (best), y2 = score (best + 1), den = y0 - 2.0 * y1 + y2;
+                if (den < 0.0)
+                    pos += std::clamp (0.5 * (y0 - y2) / den, -0.5, 0.5);
+            }
+            if (corr)
+                pos += prev - (double) pm; // the lag is measured from the previous mark's own (fractional) position
+            push (pos, (float) period, true);
+            lastMark = pos;
+            nextMark = pos + period;
         }
         }
     }
     }
     else if (nextMark <= last)
     else if (nextMark <= last)
@@ -570,12 +605,17 @@ void Psola::process (const float* x, float* y, int channels) noexcept
             for (int c = 0; c < channels; ++c)
             for (int c = 0; c < channels; ++c)
                 acc[c][(std::size_t) (o & m)] += w * readIn (c, src);
                 acc[c][(std::size_t) (o & m)] += w * readIn (c, src);
         }
         }
-        // unshifted voiced grains (within 1.7 cents) drift toward their marks, so a voice already on its note comes
-        // out as the input itself (delayed), not displaced by up to half a period
-        // (gently: at most 0.15 % of a period per grain, ~2.6 cents)
+        // unshifted voiced grains drift toward their marks, so a voice already on its note comes out as the input
+        // itself (delayed), not displaced by up to half a period. Round 148: faded in over the last 3.4 cents of
+        // shift and at most 0.05 % of a period per grain (~0.9 c) — the on / off switch at 1.7 cents (0.15 %) turned
+        // into a 0.5 c wobble on held notes whose correction hovers around zero
         auto hop = isVoiced ? ha / r : ha;
         auto hop = isVoiced ? ha / r : ha;
-        if (isVoiced && std::abs (r - 1.0) < 1e-3 && std::abs (f - 1.0) < 1e-3)
-            hop += std::clamp (0.5 * (ta - ts), -0.0015 * ha, 0.0015 * ha);
+        if (isVoiced && std::abs (f - 1.0) < 1e-3)
+        {
+            const auto w = std::max (0.0, 1.0 - std::abs (r - 1.0) / 2e-3);
+            if (w > 0.0)
+                hop += w * std::clamp (0.5 * (ta - ts), -0.0005 * ha, 0.0005 * ha);
+        }
         nextSynth = ts + hop;
         nextSynth = ts + hop;
         lastVoiced = isVoiced;
         lastVoiced = isVoiced;
     }
     }

+ 3 - 1
src/core/VocalPitch.h

@@ -145,7 +145,8 @@ public:
     // One frame: `in` / `out` hold `channels` samples (1 or 2). The output is the input delayed by latency(), shifted.
     // One frame: `in` / `out` hold `channels` samples (1 or 2). The output is the input delayed by latency(), shifted.
     void process (const float* in, float* out, int channels) noexcept;
     void process (const float* in, float* out, int channels) noexcept;
 
 
-    // Marks align to the waveform's strongest peak near the predicted position (pitch-synchronous). Default on.
+    // Marks align to the waveform near the predicted position (pitch-synchronous): where it best repeats the period at
+    // the previous mark (round 148), the strongest peak for a phrase's first mark. Default on.
     void setPeakAlign (bool on) noexcept { peakAlign = on; }
     void setPeakAlign (bool on) noexcept { peakAlign = on; }
     // Marks are placed this many samples behind the newest input (the centre of the pitch detector's window, so the
     // Marks are placed this many samples behind the newest input (the centre of the pitch detector's window, so the
     // period and shift given with setPitch / setShift describe the signal where the mark lies). Clamped so that the
     // period and shift given with setPitch / setShift describe the signal where the mark lies). Clamped so that the
@@ -169,6 +170,7 @@ private:
     std::int64_t now = 0;        // samples written so far
     std::int64_t now = 0;        // samples written so far
     double nextMark = 0.0;       // next analysis mark (absolute)
     double nextMark = 0.0;       // next analysis mark (absolute)
     double nextSynth = 0.0;      // next synthesis mark (absolute, output timeline = input timeline)
     double nextSynth = 0.0;      // next synthesis mark (absolute, output timeline = input timeline)
+    double lastMark = 0.0;       // the last voiced mark placed (absolute; 0 = none): the next one correlates with it
     double period = 0.0, ratio = 1.0, formant = 1.0;
     double period = 0.0, ratio = 1.0, formant = 1.0;
     bool voiced = false, peakAlign = true, lastVoiced = false;
     bool voiced = false, peakAlign = true, lastVoiced = false;
     std::array<Mark, maxMarks> marks {};
     std::array<Mark, maxMarks> marks {};