the ultimate vocal synthesizer - sing, speak, beatbox, and more
Rust
compare tsp review fixes: a missing measurement has no cost, and the verdict checks the recording set and the aligner
Jameson Hodgee939cf733f7b
diff --git a/crates/chimera-cli/src/compare/tsp.rs b/crates/chimera-cli/src/compare/tsp.rsindex 8fabd12..946c48a 100644--- a/crates/chimera-cli/src/compare/tsp.rs+++ b/crates/chimera-cli/src/compare/tsp.rs@@ -215,6 +215,16 @@ fn mfa_command(root: &str) -> Result<(std::process::Command, String), String> {Ok((c, model))}+/// MFA's version and a fingerprint of its settings (tools/tsp/mfa.yaml): the renders' boundaries+/// move with either, so the verdict compares only runs that share both.+fn aligner_id(dir: &str) -> Result<String, String> {+ let dir = std::fs::canonicalize(dir).map_err(|e| format!("{dir}: {e}"))?.to_string_lossy().into_owned();+ let (mut mfa, _) = mfa_command(&format!("{dir}/root"))?;+ let out = mfa.arg("version").output().map_err(|e| format!("mfa version: {e}"))?;+ let version = String::from_utf8_lossy(&out.stdout).trim().to_string();+ Ok(format!("mfa {version}, settings {}", fingerprint(&repo_path("tools/tsp/mfa.yaml"))))+}+/// Align the renders with MFA as the recordings were (tools/tsp/mfa.yaml: one utterance per/// "speaker", no dither, no speaker adaptation, so each file's alignment depends on that file/// alone and is the same on every run), with a dictionary of exactly what the G2P said.@@ -831,6 +841,7 @@ pub fn run(ctx: &Ctx, a: &[String]) -> Result<(), String> {let g = g2p()?;let t = std::time::Instant::now();let synth_phones = align_synth(&g, &said, &texts, &format!("{out_dir}/mfa"), jobs)?;+ let aligner = aligner_id(&format!("{out_dir}/mfa"))?;println!("aligned {} of {} renders in {:.0} s", synth_phones.len(), said.len(), t.elapsed().as_secs_f64());let t = std::time::Instant::now();let next = AtomicUsize::new(0);@@ -861,7 +872,7 @@ pub fn run(ctx: &Ctx, a: &[String]) -> Result<(), String> {eprintln!("{e}");}println!("measured {} recordings in {:.0} s", pairs.len(), t.elapsed().as_secs_f64());- let scored = score(&pairs);+ let scored = score(&pairs, &aligner);let mut out = scored.json;let mut md = report_md(&out);if let Some(path) = &against {@@ -1173,18 +1184,24 @@ fn reference(female: bool, speakers: &BTreeMap<String, Values>) -> Reference {Reference { terms, ceps }}+/// max(0, gap / unit − 1), or NaN when any of them is missing (`f64::max` would make a missing+/// measurement a perfect 0).+fn cost(gap: f64, unit: f64) -> f64 {+ if gap.is_finite() && unit.is_finite() && unit > 0.0 { (gap / unit - 1.0).max(0.0) } else { f64::NAN }+}+/// Each term's (value, cost) for one voice against a reference.fn costs(v: &Values, r: &Reference) -> BTreeMap<String, (f64, f64)> {let mut out = BTreeMap::new();for (name, &(c, sd, _)) in &r.terms {let x = v.terms.get(name).copied().unwrap_or(f64::NAN);- out.insert(name.clone(), (x, ((x - c).abs() / sd - 1.0).max(0.0)));+ out.insert(name.clone(), (x, cost((x - c).abs(), sd)));}for (c, (centre, unit)) in &r.ceps {let (d, cost) = match v.ceps.get(c) {Some(cc) => {let d = mcd(cc, centre);- (d, (d / unit - 1.0).max(0.0))+ (d, cost(d, *unit))}None => (f64::NAN, f64::NAN),};@@ -1219,9 +1236,12 @@ fn num(v: &Value) -> f64 {/// A cheap fingerprint of a file (FNV-1a), so the verdict can tell that two runs used the same/// recordings' alignment and the same aligner model.fn fingerprint(path: &str) -> String {- let b = std::fs::read(path).unwrap_or_default();+ fingerprint_bytes(&std::fs::read(path).unwrap_or_default())+}++fn fingerprint_bytes(b: &[u8]) -> String {let mut h: u64 = 0xcbf29ce484222325;- for x in b {+ for &x in b {h ^= x as u64;h = h.wrapping_mul(0x100000001b3);}@@ -1263,7 +1283,7 @@ struct Scored {}/// Score the synth (and each real speaker, left out in turn) against the training speakers.-fn score(pairs: &[Pair]) -> Scored {+fn score(pairs: &[Pair], aligner: &str) -> Scored {let defs = term_defs();let utts = |pred: &dyn Fn(&Pair) -> bool, synth: bool| -> Vec<(&UttMeas, &[Tok])> {pairs.iter().filter(|p| pred(p)).map(|p| if synth { (&p.synth.0, &p.synth.1[..]) } else { (&p.human.0, &p.human.1[..]) }).collect()@@ -1281,7 +1301,9 @@ fn score(pairs: &[Pair]) -> Scored {"judge": JUDGE_VERSION,"alignment": fingerprint(&repo_path("tools/tsp/alignment.tsv.gz")),"model": fingerprint(&format!("{}/.cache/chimera-mfa/mfa/pretrained_models/acoustic/english_us_arpa.zip", std::env::var("HOME").unwrap_or_default())),+ "aligner": aligner,"recordings": pairs.len(),+ "recording_set": fingerprint_bytes(pairs.iter().map(|p| p.id.as_str()).collect::<Vec<_>>().join(",").as_bytes()),});let mut human_json = json!({});for (part, held) in [("train", false), ("held", true)] {@@ -1362,7 +1384,7 @@ enum Verdict {/// The verdict on the training score between two runs over the same recordings, by the same/// judge. Scored terms only; diagnostics that moved a lot are noted, not counted.fn verdict(old: &Value, new: &Value) -> Result<(Verdict, Vec<String>, f64), String> {- for k in ["judge", "alignment", "model", "recordings"] {+ for k in ["judge", "alignment", "model", "aligner", "recording_set"] {if old[k] != new[k] {return Err(format!("the earlier score has another {k} ({} here, {} there); rescore it with this build over the same speakers", new[k], old[k]));}@@ -1463,6 +1485,21 @@ mod tests {Phone { ipa: ipa.into(), stress: 0, t0: 0.0, t1: 0.1 }}+ #[test]+ fn a_missing_measurement_has_no_cost() {+ assert!(cost(f64::NAN, 1.0).is_nan());+ assert!(cost(1.0, f64::NAN).is_nan());+ assert!(cost(1.0, 0.0).is_nan());+ assert_eq!(cost(0.5, 1.0), 0.0);+ assert_eq!(cost(3.0, 1.0), 2.0);+ // a scored group with no cost leaves no score, rather than a perfect one+ let mut c = BTreeMap::new();+ c.insert("level ʃ".to_string(), (f64::NAN, f64::NAN));+ c.insert("centroid ʃ".to_string(), (1.0, 0.0));+ c.insert("duration k".to_string(), (1.0, 0.0));+ assert!(fold(&c).0.is_nan());+ }+#[test]fn matching_keeps_only_the_same_phones_in_order() {// "the sheet" said ðə ʃit by the synth, ði ʃiːt (an inserted t) by a speaker@@ -1512,7 +1549,8 @@ mod tests {"judge": JUDGE_VERSION,"alignment": "a","model": "m",- "recordings": 10,+ "aligner": "mfa 3.4.3",+ "recording_set": "r","train": {"score": cost,"female": { "terms": { "level ʃ": term, "F2 u": { "scored": false, "cost": cost, "support": support } } },@@ -1542,7 +1580,7 @@ mod tests {let mut new = scored(0.9, 1000);new["train"]["female"]["terms"]["F2 u"]["cost"] = json!(5.0);assert_eq!(v(scored(1.0, 1000), new), "better");- for k in ["recordings", "judge", "alignment", "model"] {+ for k in ["recording_set", "judge", "alignment", "model", "aligner"] {let mut other = scored(1.0, 1000);other[k] = json!(99);assert!(verdict(&scored(1.0, 1000), &other).is_err(), "{k}");diff --git a/docs/speech-comparison.md b/docs/speech-comparison.mdindex 5c128fa..d235454 100644--- a/docs/speech-comparison.md+++ b/docs/speech-comparison.md@@ -171,7 +171,9 @@ each other. It is built like the singer judge's ([csd-comparison.md](csd-compariWithout the floor, a measure every speaker happens to share (no voicing at all in /s/: aspread of 0.01) would turn any difference into a large cost.- **Cost.** A term's cost is max(0, |synth − centre| / spread − 1). Within one spread of the- real speakers costs nothing.+ real speakers costs nothing. A term with no value has no cost, never a cost of 0. A scored+ group with no cost leaves the score empty: a run over a few speakers has no centres, so no+ score.- **Spectrum terms (diagnostic).** These are distances. The synth's cepstrum is the mean overthe speakers' sentence sets, compared with the centre (the mean of the training speakers'cepstra), in MCD dB. The unit is the median, over training speakers, of a speaker's distance@@ -188,7 +190,7 @@ each other. It is built like the singer judge's ([csd-comparison.md](csd-compari**The verdict.** `--against <earlier score.json>` compares the training scores of two runs. Itrefuses unless both used the same judge version, the same alignment of the recordings, the same-aligner model and the same recordings. The synth and the aligner are deterministic, so the same+aligner (MFA's version, its model and `mfa.yaml`) and the same recordings, by ID. The synth and the aligner are deterministic, so the samebuild gives the same score (checked: a second run is identical, and its verdict is NO CLEARCHANGE, +0.000). Only scored terms count:diff --git a/tools/tsp/align.py b/tools/tsp/align.pyindex bf8e44a..09bf2b6 100644--- a/tools/tsp/align.py+++ b/tools/tsp/align.py@@ -35,7 +35,8 @@ def header_text(path):def prepare(tsp, corpus):rows = []- root = os.path.join(tsp, "48k")+ # absolute, so the links resolve from each utterance's directory+ root = os.path.join(os.path.abspath(tsp), "48k")for spk in sorted(os.listdir(root)):if not re.fullmatch(r"[FM][A-L]", spk):continue # children CA, CBdiff --git a/tools/tsp/setup.sh b/tools/tsp/setup.shindex e736ea0..b0326d5 100755--- a/tools/tsp/setup.sh+++ b/tools/tsp/setup.sh@@ -12,6 +12,11 @@ if [ ! -x "$cache/env/bin/mfa" ]; thenchmod +x "$cache/micromamba""$cache/micromamba" create -y -q -p "$cache/env" -c conda-forge montreal-forced-aligner=3.4.3 python=3.11fi+have="$(mfa version 2>/dev/null || true)"+if [ "$have" != "3.4.3" ]; then+ echo "$cache/env has MFA ${have:-?}, not 3.4.3: remove $cache/env and run this again" >&2+ exit 1+fimodels="$cache/mfa/pretrained_models"check() { echo "$1 $2" | sha256sum -c --status 2>/dev/null; }acoustic=d35ce271ded357d833d2f4b8d1041dc3748b9538567ba13f2c697f4e4126711b