commit 87c6de73113590cf6dba08a564d3a32c7498cc96
parent 84043195b8ecc04f9e37e48c03329df19116358b
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Wed, 11 Nov 2026 18:01:30 -0500
voice: a contour's shift is set by the first file with a voiced frame -- a silent first file no longer locks it at 0
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat:
2 files changed, 64 insertions(+), 7 deletions(-)
diff --git a/scripts/voice.test.mjs b/scripts/voice.test.mjs
@@ -97,3 +97,43 @@ test("--more runs further files the same way, in the same run", { skip: !ffmpeg
assert.equal(readFileSync(outB).readUInt32LE(40), 48000);
assert.match(run(["--op", "enhance", "--engine-kind", "rnnoise", "--input", a, "--output", "/x.wav", "--more", '[{"input":1}]']).stderr, /--more is a list of \{input, output\}/);
});
+
+// scripts/voice_seedvc.py's contour shift over a batch (a take and its parts): measured once, on the first file with
+// a voiced frame. The function is stdlib-only (torch and the model are imported inside main).
+let python = true;
+try {
+ execFileSync("python3", ["-c", "import math"], { stdio: "ignore" });
+} catch {
+ python = false;
+}
+
+test("seed-vc's contour shift: set by the first file with a voiced frame, never locked at 0 by a silent one", { skip: !python && "python3 absent" }, () => {
+ const code = `
+import json, math, sys
+sys.path.insert(0, ${JSON.stringify(import.meta.dirname)})
+from voice_seedvc import pitch_move
+ref, src = math.log(200.0), math.log(100.0)
+out = {}
+# A silent first file: nothing measured, a factor of the semitones alone.
+m, hz, f = pitch_move(None, None, None, ref, "contour", 0)
+out["silent"] = [m, hz, f]
+# The next, voiced: an octave up to the reference's median, and it is kept.
+m, hz, f = pitch_move(m, hz, src, ref, "contour", 0)
+out["voiced"] = [round(m, 6), hz, round(f, 6)]
+m2, hz2, f2 = pitch_move(m, hz, math.log(150.0), ref, "contour", 0)
+out["later"] = [round(m2, 6), hz2, round(f2, 6)]
+# Source: no move, the semitones alone; contour with semitones: both.
+out["source"] = [round(x, 6) if isinstance(x, float) else x for x in pitch_move(None, None, src, ref, "source", 12)]
+out["both"] = round(pitch_move(None, None, src, ref, "contour", -12)[2], 6)
+# A silent file after the move is set does not unset it.
+out["after"] = round(pitch_move(m, hz, None, ref, "contour", 0)[0], 6)
+print(json.dumps(out))
+`;
+ const out = JSON.parse(execFileSync("python3", ["-c", code], { encoding: "utf8", env: { ...process.env, PYTHONDONTWRITEBYTECODE: "1" } }));
+ assert.deepEqual(out.silent, [null, null, 1]);
+ assert.deepEqual(out.voiced, [0.693147, 100, 2]);
+ assert.deepEqual(out.later, [0.693147, 100, 2]);
+ assert.deepEqual(out.source, [0, 100, 2]);
+ assert.equal(out.both, 1);
+ assert.equal(out.after, 0.693147);
+});
diff --git a/scripts/voice_seedvc.py b/scripts/voice_seedvc.py
@@ -31,7 +31,9 @@
# and fall is kept, the register is the stock voice's.
# --semitones then moves the followed curve up or down (2^(n/12)). In a batch
# the contour's move is measured ONCE, on the first file (the take), and used
-# for the rest (its parts): a part must sit where it sits in the whole.
+# for the rest (its parts): a part must sit where it sits in the whole. A file
+# with no voiced frame (silence, a breath) has no median to measure: it leaves
+# the move unset, and the first file that has one sets it.
#
# Several files (--batch) are converted one after another, the model and the
# target's features made once.
@@ -42,12 +44,29 @@
import argparse
import json
+import math
import os
import sys
import time
import types
+def pitch_move(move, source_hz, median, ref_median, pitch, semitones):
+ """The followed curve's factor for one file, and the batch's state after it.
+
+ `move` is the batch's shift in log-f0 (None: not measured yet), `median` this
+ file's median log-f0 (None: no voiced frame), `ref_median` the reference's.
+ The shift is measured ONCE, on the first file with a voiced frame — contour:
+ the reference's median less the file's; source: 0 — and a file before it
+ (unvoiced: nothing in it is multiplied anyway) leaves it unset rather than
+ locking it at 0. Returns (move, source_hz, factor).
+ """
+ if move is None and median is not None:
+ source_hz = round(math.exp(median), 1)
+ move = (ref_median - median) if pitch == "contour" and ref_median is not None else 0.0
+ return move, source_hz, math.exp(move or 0.0) * 2 ** (semitones / 12)
+
+
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--code", required=True, help="the seed-vc checkout")
@@ -211,18 +230,16 @@ def main() -> int:
window = max_context - mel2.size(2)
ref_median = median_log(f0_ref) if follow else None
# What was heard and done, for the result line; `move` is the contour's
- # shift in log-f0, measured on the first file and kept for the rest.
+ # shift in log-f0, measured on the first file that has a voiced frame and
+ # kept for the rest (None until then: a file with none has nothing to move).
heard = {"move": None, "sourceHz": None}
def followed_f0(src16):
f0 = f0_of(src16)
- med = median_log(f0)
- if heard["move"] is None:
- heard["sourceHz"] = round(float(np.exp(med)), 1) if med is not None else None
- heard["move"] = (ref_median - med) if a.pitch == "contour" and med is not None and ref_median is not None else 0.0
+ heard["move"], heard["sourceHz"], factor = pitch_move(heard["move"], heard["sourceHz"], median_log(f0), ref_median, a.pitch, a.semitones)
voiced = f0 > 1
out = f0.copy()
- out[voiced] = f0[voiced] * float(np.exp(heard["move"])) * 2 ** (a.semitones / 12)
+ out[voiced] = f0[voiced] * factor
return torch.from_numpy(out.astype(np.float32))[None]
def convert(src):