the ultimate vocal synthesizer - sing, speak, beatbox, and more
Rust
compare csd review fixes: missing values can't lower the score
Jameson Hodge7f8d69a3cb7f
diff --git a/crates/chimera-cli/src/compare.rs b/crates/chimera-cli/src/compare.rsindex 504f156..0d205ae 100644--- a/crates/chimera-cli/src/compare.rs+++ b/crates/chimera-cli/src/compare.rs@@ -796,6 +796,69 @@ fn r2(x: f64) -> Value {}}+/// One voice's song-level values for the score (in `score::TERMS` order), from its measures on+/// these notes. Each is NaN when no note gives it.+fn song_values(notes: &[&Note], meas: &[&NoteMeas]) -> [f64; score::N_TERMS] {+ let col = |f: &dyn Fn(&NoteMeas) -> f64| meas.iter().map(|m| f(m)).collect::<Vec<_>>();+ let eligible: Vec<&&NoteMeas> = meas.iter().filter(|m| m.vib_eligible).collect();+ let vib_share = if eligible.is_empty() {+ f64::NAN+ } else {+ eligible.iter().filter(|m| m.vib_depth.is_finite()).count() as f64 / eligible.len() as f64+ };+ let onset = |start: Start| {+ median(+ ¬es+ .iter()+ .zip(meas)+ .filter(|(n, _)| n.phrase_start && n.starts_with() == start)+ .map(|(_, m)| m.onset)+ .collect::<Vec<_>>(),+ )+ };+ [+ mean(&col(&|m| m.bands[0])),+ mean(&col(&|m| m.bands[1])),+ mean(&col(&|m| m.bands[2])),+ mean(&col(&|m| m.bands[3])),+ mean(&col(&|m| m.bands[4])),+ median(&col(&|m| m.h1h2)),+ vib_share,+ median(&col(&|m| m.vib_depth)),+ median(&col(&|m| m.vib_rate)),+ mean(&col(&|m| m.voiced)),+ onset(Start::Vowel),+ onset(Start::Voiceless),+ median(&col(&|m| m.aperiodicity)),+ ]+}++/// On how many of these notes the synth gives each guarded measure (`score::COVERAGE` order):+/// a value that goes missing must not take its note out of the score unnoticed.+fn coverage(notes: &[&Note], meas: &[&NoteMeas]) -> [u64; score::N_COVERAGE] {+ let count = |f: &dyn Fn(&Note, &NoteMeas) -> bool| {+ notes.iter().zip(meas).filter(|(n, m)| f(n, m)).count() as u64+ };+ let onset = |start: Start| {+ count(&|n, m| n.phrase_start && n.starts_with() == start && m.onset.is_finite())+ };+ [+ notes.len() as u64,+ count(&|_, m| m.bands[0].is_finite()),+ count(&|_, m| m.bands[1].is_finite()),+ count(&|_, m| m.bands[2].is_finite()),+ count(&|_, m| m.bands[3].is_finite()),+ count(&|_, m| m.bands[4].is_finite()),+ count(&|_, m| m.h1h2.is_finite()),+ count(&|_, m| m.vib_eligible),+ count(&|_, m| m.voiced.is_finite()),+ onset(Start::Vowel),+ onset(Start::Voiceless),+ count(&|_, m| m.aperiodicity.is_finite()),+ count(&|_, m| m.cents.is_finite()),+ ]+}+fn r3(x: f64) -> Value {if x.is_finite() {json!((x * 1000.0).round() / 1000.0)@@ -840,8 +903,11 @@ fn compare_song(// notes measured on both sides; those the planner could not place on time are reported// but left out of every statistic (their windows would hold the wrong sound)let mut kept: Vec<&Note> = Vec::new();+ // the singer on every note, aligned or not: the score's units come from these+ let mut human_all: Vec<NoteMeas> = Vec::with_capacity(notes.len());for (n, &vt0) in notes.iter().zip(&vowel_t0) {let h = ha.note(&human, n);+ human_all.push(h.clone());let s = sa.note(&synth, n);let d = mcd(&h.cep, &s.cep);let bd: [f64; 5] = std::array::from_fn(|k| s.bands[k] - h.bands[k]);@@ -924,42 +990,15 @@ fn compare_song("envelopeLagSec": r2(lag), "envelopeCorr": r2(lag_r),"bandDiffDb": BAND_NAMES.iter().zip(&band_mean).map(|(n, v)| (n.to_string(), r2(*v))).collect::<serde_json::Map<_, _>>(),});- // the score's song-level values (singer, synth), unrounded, in score::TERMS order- fn side(p: &(NoteMeas, NoteMeas, f64), s: usize) -> &NoteMeas {- if s == 0 { &p.0 } else { &p.1 }- }- let with_bands: Vec<&(NoteMeas, NoteMeas, f64)> = per- .iter()- .filter(|p| p.0.bands[0].is_finite() && p.1.bands[0].is_finite())- .collect();- let level = |s: usize, k: usize| {- mean(- &with_bands- .iter()- .map(|p| side(p, s).bands[k])- .collect::<Vec<_>>(),- )- };- let med_of = |f: &dyn Fn(&NoteMeas) -> f64, s: usize| median(&col(f, s));- let pair = |f: &dyn Fn(usize) -> f64| (f(0), f(1));- let pairs: [(f64, f64); score::N_TERMS] = [- pair(&|s| level(s, 0)),- pair(&|s| level(s, 1)),- pair(&|s| level(s, 2)),- pair(&|s| level(s, 3)),- pair(&|s| level(s, 4)),- pair(&|s| med_of(&|m| m.h1h2, s)),- pair(&vib_share),- pair(&|s| med_of(&|m| m.vib_depth, s)),- pair(&|s| med_of(&|m| m.vib_rate, s)),- pair(&|s| mean(&col(&|m| m.voiced, s))),- pair(&|s| median(&onset_of(s, Start::Vowel))),- pair(&|s| median(&onset_of(s, Start::Voiceless))),- pair(&|s| med_of(&|m| m.aperiodicity, s)),- ];+ let all_notes: Vec<&Note> = notes.iter().collect();+ let singer: Vec<&NoteMeas> = per.iter().map(|p| &p.0).collect();+ let synth_m: Vec<&NoteMeas> = per.iter().map(|p| &p.1).collect();+ let (h_vals, s_vals) = (song_values(&kept, &singer), song_values(&kept, &synth_m));let song_score = score::SongScore {id: id.to_string(),- pairs,+ singer_all: song_values(&all_notes, &human_all.iter().collect::<Vec<_>>()),+ pairs: std::array::from_fn(|k| (h_vals[k], s_vals[k])),+ coverage: coverage(&kept, &synth_m),notes: notes.len(),misaligned,dropped,diff --git a/crates/chimera-cli/src/compare/score.rs b/crates/chimera-cli/src/compare/score.rsindex d1d24c1..9dbb365 100644--- a/crates/chimera-cli/src/compare/score.rs+++ b/crates/chimera-cli/src/compare/score.rs@@ -3,7 +3,8 @@//! Each term is a song-level measure both voices have: her median H1−H2 in a song against the//! synth's in the same song, her share of voiced frames against the synth's, and so on. A term's//! gap is the mean over songs of |synth − singer|, divided by the spread of her own values over-//! the training songs (median absolute deviation, scaled to a standard deviation). A gap within+//! the training songs (median absolute deviation, scaled to a standard deviation; taken from all+//! her notes, so nothing the synth does can move it). A gap within//! one spread is about as close as two of her own songs are to each other and costs nothing;//! beyond that, the excess is the term's cost. The score is the weighted mean of the costs://! lower is better, and 0 means every term is within her own variation.@@ -14,7 +15,9 @@//!//! `--against <score.json>` compares the training score with an earlier run's. A change is//! better when the score falls by at least `MARGIN`, no term's cost rises by more than-//! `TERM_SLACK`, and no guard breaks.+//! `TERM_SLACK`, and no guard breaks. A guard also breaks when a value goes missing: a measure the+//! synth gave on a song's note before and doesn't now would otherwise leave the score quietly,+//! and could take a bad note with it.use super::{mean, median};use serde_json::{Value, json};@@ -53,6 +56,28 @@ const MCD_SLACK: f64 = 0.1;const PITCH_SLACK: f64 = 2.0;const CORR_SLACK: f64 = 0.01;+/// What `coverage` counts per song: the notes compared, and the notes on which the synth gives each+/// guarded measure. Vibrato depth and rate are not counted: they exist only on notes with+/// vibrato, and the vibrato share term already scores how many have it.+pub const N_COVERAGE: usize = 13;+pub const COVERAGE: [&str; N_COVERAGE] = [+ "notes compared",+ "level 0-.5k",+ "level .5-1k",+ "level 1-2k",+ "level 2-4k",+ "level 4-8k",+ "H1-H2",+ "notes long enough for vibrato",+ "voiced share",+ "onset, vowel-initial",+ "onset, voiceless-initial",+ "aperiodicity",+ "pitch",+];+/// at most this many coverage losses are listed in a verdict+const MAX_LISTED: usize = 6;+/// Held out of tuning: every fifth song number, in both keys.pub fn held_out(id: &str) -> bool {let digits: String = id.chars().filter(|c| c.is_ascii_digit()).collect();@@ -62,8 +87,12 @@ pub fn held_out(id: &str) -> bool {/// One song's inputs to the score.pub struct SongScore {pub id: String,- /// per term (singer, synth); NaN where that side has no value in this song+ /// per term, the singer's value over all her notes: the spreads are taken from these+ pub singer_all: [f64; N_TERMS],+ /// per term (singer, synth) over the notes compared; NaN where that side has no valuepub pairs: [(f64, f64); N_TERMS],+ /// per `COVERAGE` entry, how many notes+ pub coverage: [u64; N_COVERAGE],pub notes: usize,pub misaligned: usize,pub dropped: u64,@@ -84,7 +113,7 @@ fn spreads(train: &[&SongScore]) -> [f64; N_TERMS] {std::array::from_fn(|k| {let h: Vec<f64> = train.iter()- .map(|s| s.pairs[k].0)+ .map(|s| s.singer_all[k]).filter(|v| v.is_finite()).collect();let sd = mad_sd(&h);@@ -149,8 +178,10 @@ fn score_set(songs: &[&SongScore], spread: &[f64; N_TERMS]) -> Value {let avg = |f: &dyn Fn(&SongScore) -> f64| mean(&songs.iter().map(|s| f(s)).collect::<Vec<_>>());json!({"songs": songs.iter().map(|s| s.id.clone()).collect::<Vec<_>>(),+ "coverageNames": COVERAGE,"score": r3(if weight > 0.0 { cost / weight } else { f64::NAN }),"terms": terms,+ "coverage": songs.iter().map(|s| (s.id.clone(), json!(s.coverage))).collect::<serde_json::Map<_, _>>(),"guards": {"notes": total(&|s| s.notes as u64),"notesMisaligned": total(&|s| s.misaligned as u64),@@ -185,26 +216,59 @@ pub fn verdict(old: &Value, new: &Value) -> Result<(Verdict, Vec<String>), Strinreturn Err("one of the two runs has no training score".into());}let mut why = Vec::new();- let g = |k: &str| (num(&o["guards"][k]), num(&n["guards"][k]));- let (a, b) = g("notesMisaligned");- if b > a {- why.push(format!("more notes off time: {a} → {b}"));- }- let (a, b) = g("droppedGestures");- if b > a {- why.push(format!("more dropped gestures: {a} → {b}"));- }- let (a, b) = g("noteMcdDb");- if b > a + MCD_SLACK {- why.push(format!("note MCD up: {a} → {b} dB"));+ // (old, new) of a guard; a guard that had a value and lost it breaks+ let mut guard = |k: &str, broken: &dyn Fn(f64, f64) -> bool, what: &str| {+ let (a, b) = (num(&o["guards"][k]), num(&n["guards"][k]));+ if a.is_finite() && !b.is_finite() {+ why.push(format!("{what}: no value any more"));+ } else if a.is_finite() && broken(a, b) {+ why.push(format!("{what}: {a} → {b}"));+ }+ };+ guard("notesMisaligned", &|a, b| b > a, "more notes off time");+ guard("droppedGestures", &|a, b| b > a, "more dropped gestures");+ guard("noteMcdDb", &|a, b| b > a + MCD_SLACK, "note MCD up (dB)");+ guard(+ "envelopeCorr",+ &|a, b| b < a - CORR_SLACK,+ "envelope correlation down",+ );+ guard(+ "synthPitchAbsCents",+ &|a, b| b > a + PITCH_SLACK,+ "pitch error up (cents)",+ );+ // no song may lose a note from any guarded measure+ if o["coverageNames"] != n["coverageNames"] {+ return Err(+ "the earlier score counts other measures; rescore the baseline with this build".into(),+ );}- let (a, b) = g("envelopeCorr");- if b < a - CORR_SLACK {- why.push(format!("envelope correlation down: {a} → {b}"));+ let mut lost = Vec::new();+ if let (Some(co), Some(cn)) = (o["coverage"].as_object(), n["coverage"].as_object()) {+ for (song, a) in co {+ let (a, b) = (a.as_array(), cn.get(song).and_then(|b| b.as_array()));+ let (Some(a), Some(b)) = (a, b) else { continue };+ for (k, (x, y)) in a.iter().zip(b).enumerate() {+ let (x, y) = (x.as_u64().unwrap_or(0), y.as_u64().unwrap_or(0));+ if y < x {+ lost.push(format!("{song} {}: {x} → {y} notes", COVERAGE[k]));+ }+ }+ }}- let (a, b) = g("synthPitchAbsCents");- if b > a + PITCH_SLACK {- why.push(format!("pitch error up: {a} → {b} cents"));+ if !lost.is_empty() {+ let more = lost.len().saturating_sub(MAX_LISTED);+ lost.truncate(MAX_LISTED);+ why.push(format!(+ "measured on fewer notes: {}{}",+ lost.join(", "),+ if more > 0 {+ format!(" and {more} more")+ } else {+ String::new()+ }+ ));}let empty = vec![];let (to, tn) = (@@ -225,12 +289,14 @@ pub fn verdict(old: &Value, new: &Value) -> Result<(Verdict, Vec<String>), Strinwhy.push(format!("{name} cost up: {ca} → {cb}"));}}- let d = sn - so;+ // scores are written to thousandths; compare those exactly+ let milli = |x: f64| (x * 1000.0).round() as i64;+ let (d, margin) = (milli(sn) - milli(so), milli(MARGIN));Ok(if !why.is_empty() {(Verdict::Worse, why)- } else if d <= -MARGIN {+ } else if d <= -margin {(Verdict::Better, why)- } else if d >= MARGIN {+ } else if d >= margin {(Verdict::Worse, vec![format!("score up: {so} → {sn}")])} else {(Verdict::Same, why)@@ -334,7 +400,9 @@ mod tests {fn song(id: &str, singer: f64, synth: f64) -> SongScore {SongScore {id: id.into(),+ singer_all: [singer; N_TERMS],pairs: [(singer, synth); N_TERMS],+ coverage: [10; N_COVERAGE],notes: 10,misaligned: 0,dropped: 0,@@ -344,6 +412,59 @@ mod tests {}}+ #[test]+ fn a_bad_song_that_stops_being_measured_does_not_improve_the_score() {+ // nine songs one spread (3.7065) apart from her, one far off: a cost the outlier carries+ let mk = |worst_missing: bool| {+ let mut songs: Vec<SongScore> = (1..=9)+ .map(|i| song(&format!("en{i:03}a"), i as f64, i as f64 + 3.7))+ .collect();+ songs[0].pairs[5].1 = 1.0 + 60.0;+ if worst_missing {+ // its H1−H2 can no longer be measured on any note+ songs[0].pairs[5].1 = f64::NAN;+ songs[0].coverage[6] = 0;+ }+ let refs: Vec<&SongScore> = songs.iter().collect();+ report("female", &refs, None).unwrap().0+ };+ let (base, gone) = (mk(false), mk(true));+ assert!(num(&gone["train"]["score"]) < num(&base["train"]["score"]));+ let (v, why) = verdict(&base, &gone).unwrap();+ assert_eq!(v, Verdict::Worse);+ assert!(why[0].contains("en001a H1-H2: 10 → 0 notes"), "{why:?}");+ // losing pitch on a few notes of one song is caught the same way+ let mut fewer = base.clone();+ fewer["train"]["coverage"]["en002a"][12] = json!(7);+ fewer["train"]["score"] = json!(0.1);+ let (v, why) = verdict(&base, &fewer).unwrap();+ assert_eq!(v, Verdict::Worse);+ assert!(why[0].contains("en002a pitch: 10 → 7 notes"), "{why:?}");+ }++ #[test]+ fn the_margin_is_exact_in_thousandths() {+ let base = scored(8.0, |_| {});+ let with = |x: f64| {+ let mut v = base.clone();+ v["train"]["score"] = json!(x);+ v+ };+ // 1.001 − 0.991 is 0.0099999… in floating point, and still a fall of 0.010+ assert_eq!(+ verdict(&with(1.001), &with(0.991)).unwrap().0,+ Verdict::Better+ );+ assert_eq!(+ verdict(&with(0.991), &with(1.001)).unwrap().0,+ Verdict::Worse+ );+ assert_eq!(+ verdict(&with(1.001), &with(0.992)).unwrap().0,+ Verdict::Same+ );+ }+#[test]fn every_fifth_song_is_held_out_in_both_keys() {assert!(held_out("en005a") && held_out("en005b") && held_out("en050a"));@@ -417,6 +538,13 @@ mod tests {let (v, why) = verdict(&base, &one_worse).unwrap();assert_eq!(v, Verdict::Worse);assert!(why[0].contains("H1-H2"), "{why:?}");+ // a guard that loses its value breaks+ let (v, why) = verdict(&base, &scored(6.0, |s| s.synth_pitch = f64::NAN)).unwrap();+ assert_eq!(v, Verdict::Worse);+ assert!(+ why[0].contains("pitch error up (cents): no value"),+ "{why:?}"+ );// different songs are not comparablelet mut other = scored(6.0, |_| {});other["train"]["songs"] = json!(["en001a"]);diff --git a/docs/csd-comparison.md b/docs/csd-comparison.mdindex 2204810..feccb1f 100644--- a/docs/csd-comparison.md+++ b/docs/csd-comparison.md@@ -95,8 +95,8 @@ be hard to improve without sounding closer to her.- **Gap.** A term's gap is the mean over songs of |synth − singer|. It is a mean rather than amedian so that every song's change counts, and one song gone badly wrong shows.- **Unit.** The gap is divided by her spread on that term over the training songs (median absolute- deviation, scaled to a standard deviation). The spread is a property of the singer, so a change- to the synth can't move it.+ deviation, scaled to a standard deviation). The spread comes from all her notes, including any+ the planner couldn't place on time, so a change to the synth can't move it.- **Cost.** A gap within one spread is about as close as two of her songs are to each other, so itcosts nothing. Beyond that, the excess is the term's cost. Nothing is gained by matching hermedians more closely than her own variation, which keeps a tuning loop from overfitting one@@ -118,14 +118,21 @@ deviations from it can't be predicted from the score.**The verdict.** `--against <earlier score.json>` compares the training scores of two runs overthe same songs and body:-- **Better:** the score falls by at least 0.01, no term's cost rises by more than 0.25, and every- guard holds.+- **Better:** the score falls by at least 0.01 (compared in the thousandths `score.json` gives),+ no term's cost rises by more than 0.25, and every guard holds.- **Guards:**- no more notes off time (so a change can't drop hard notes from the statistics)- no more dropped gestures- note MCD up by no more than 0.1 dB- envelope correlation down by no more than 0.01- the synth's pitch error up by no more than 2 cents+ - no song measured on fewer notes. For each song, `score.json` counts the notes compared and+ the notes on which the synth gives each measure: band levels, H1−H2, voicing, onsets,+ aperiodicity and pitch, and the notes long enough to look for vibrato. A measure that stops+ being measurable on a note would otherwise drop that note, maybe a bad one, from the score.+ Vibrato depth and rate are exempt, since they exist only on notes with vibrato, and the share+ term scores how many have it.+ - a guard that had a value and has none now breaks too.- **Worse:** any guard or term breaks, or the score rises by 0.01 or more.- **No clear change:** anything else.