the ultimate vocal synthesizer - sing, speak, beatbox, and more
Rust
Batch 3 review fixes: a squeezed affricate's folds close inside its span, /ð/ after an explicit pause, a word's last /ɹ/ keeps its nucleus
Jameson Hodge3b2c838ab851
diff --git a/crates/chimera/src/phonetics/durations.rs b/crates/chimera/src/phonetics/durations.rsindex 8373154..f8cc0e4 100644--- a/crates/chimera/src/phonetics/durations.rs+++ b/crates/chimera/src/phonetics/durations.rs@@ -199,7 +199,8 @@ pub fn klatt_durations(segs: &[Segment], info: impl Fn(&Segment) -> SegInfo) ->// Klatt transcribes a vowel and its /ɹ/ as one nucleus (/AR/ "bar", /OR/ "boar"...), where the// G2P gives two segments: without this the pair gets the vowel's whole span and the /ɹ/'s on topfor i in 0..n.saturating_sub(1) {- let coda = i + 2 >= n || !inf[i + 2].vowel;+ // (a vowel in the next word doesn't make it an onset: "car is" has "car"'s nucleus)+ let coda = i + 2 >= n || word[i + 2] != word[i + 1] || !inf[i + 2].vowel;if inf[i].vowel && segs[i + 1].ipa == "ɹ" && word[i + 1] == word[i] && coda && segs[i].dur.is_none() && segs[i + 1].dur.is_none() {durs[i] = (durs[i] - durs[i + 1]).max(NUCLEUS_VOWEL_MIN);}diff --git a/crates/chimera/src/phonetics/planner.rs b/crates/chimera/src/phonetics/planner.rsindex 0f89f55..1605b67 100644--- a/crates/chimera/src/phonetics/planner.rs+++ b/crates/chimera/src/phonetics/planner.rs@@ -222,8 +222,9 @@ pub fn plan_utterance(input: &[Segment], o: &PlanOptions) -> Plan {}}for i in 0..segs.len() {- let after_obstruent = i == 0 || segs[i - 1].def.is_some_and(|d| matches!(d.manner, Manner::Stop | Manner::Affricate | Manner::Fricative));- if segs[i].ipa == "ð" && after_obstruent {+ // (a pause: the utterance's start, or an explicit one, the `_` of IPA's `|` or a comma)+ let after_pause_or_obstruent = i == 0 || segs[i - 1].ipa == "_" || segs[i - 1].def.is_some_and(|d| matches!(d.manner, Manner::Stop | Manner::Affricate | Manner::Fricative));+ if segs[i].ipa == "ð" && after_pause_or_obstruent {// /ð/ after a pause, a stop, an affricate or a fricative is a silent dental closure and a// weak burst (Zhao 2010; the TSP speakers, hill-climb log, experiment 24). Its span stays Klatt's.let d = segs[i].def.as_mut().unwrap();@@ -309,9 +310,10 @@ pub fn plan_utterance(input: &[Segment], o: &PlanOptions) -> Plan {add_oral(&def.targets, t0, split, 2.0, def.ipa);// the release is the fricative's own gesture, not a contact release's: it takes// the fricative's articulators (the jaw too) and holds to the span's end, where- // the folds have closed (below). Its 8 ms lead hands over from the closure.+ // the folds have closed (below). Its 8 ms lead hands over from the closure. A span+ // squeezed under 33 ms (a fast sung note) starts it with the span, not before.if let Some(rel) = def.release.and_then(|r| phoneme(r, body)) {- oral_gestures_into(&rel, &rel.targets, split - 0.015, t1, 1.4, def.ipa, &mut gestures);+ oral_gestures_into(&rel, &rel.targets, js_max(t0, split - 0.015), t1, 1.4, def.ipa, &mut gestures);}}Manner::Fricative | Manner::Approximant | Manner::Lateral => add_oral(&def.targets, t0, t1, 1.3, def.ipa),@@ -452,9 +454,12 @@ pub fn plan_utterance(input: &[Segment], o: &PlanOptions) -> Plan {/// /tʃ/: about 55 ms of closure, 60-65 ms of frication and a fading tail; their /dʒ/: 45-55, about 45).const AFFRICATE_CLOSURE: f64 = 0.45;-/// An affricate's folds: apart by `open` for the frication, and together again by `t1`, as it ends.+/// An affricate's folds: apart by `open` for the frication, and together again by `t1`, as it ends. A+/// span too short for the 30 ms closing (a fast sung note) closes faster, so the breath doesn't spill+/// into the vowel.fn affricate_folds(open: f64, t1: f64) -> Gesture {- Gesture::new(open, js_max(open, t1 - 0.03), 0.035, 0.03, 6.0, one(ControlKey::ABDUCTION, 0.36))+ let (hold, closing) = if t1 - open > 0.03 { (t1 - 0.03, 0.03) } else { (open, t1 - open) };+ Gesture::new(open, hold, 0.035, closing, 6.0, one(ControlKey::ABDUCTION, 0.36))}/// How much later a nasal's closure is aimed than its span's start (s). `oral_gestures_into` bringsdiff --git a/crates/chimera/tests/affricates.rs b/crates/chimera/tests/affricates.rsindex 1d58e20..a33a990 100644--- a/crates/chimera/tests/affricates.rs+++ b/crates/chimera/tests/affricates.rs@@ -51,9 +51,23 @@ fn frication(fr: &[(f64, f64)], t0: f64, t1: f64, name: &str) -> (usize, usize)let vowel = fr[at(t1 + 0.1)..at(t1 + 0.14)].iter().map(|f| f.0).fold(f64::MIN, f64::max);let fric: Vec<usize> = (at(t0)..at(t1 + 0.1)).filter(|&i| fr[i].1 < -10.0 && fr[i].0 > vowel - 30.0).collect();let (first, last) = (*fric.first().unwrap_or_else(|| panic!("{name}: no frication")), *fric.last().unwrap());- let voiced = (last..fr.len()).find(|&i| fr[i].0 > vowel - 8.0).unwrap();- // the frication's last frame can itself be within 8 dB of the vowel: no gap at all- ((last - first + 1) * 10, (voiced - last).saturating_sub(1) * 10)+ // the voice: as loud as the vowel, and with its energy below 1 kHz. Level alone would take a+ // loud frication for the vowel, and report no gap whatever follows it.+ let voiced = (last + 1..fr.len()).find(|&i| fr[i].0 > vowel - 8.0 && fr[i].1 >= -10.0).unwrap();+ ((last - first + 1) * 10, (voiced - last - 1) * 10)+}++/// The detector itself: a frication as loud as the vowel, then a quiet [h], then the vowel.+#[test]+fn the_gap_measure_sees_an_h_after_a_loud_frication() {+ let at = |t: f64| (t * FS / FRAME as f64) as usize;+ let (t0, t1) = (0.1, 0.2);+ let mut fr = vec![(-60.0, 0.0); at(0.6)];+ fr[at(t0)..at(t1)].fill((-16.0, -20.0)); // frication, with little below 1 kHz+ fr[at(t1)..at(t1 + 0.12)].fill((-35.0, -3.0)); // 120 ms of a quiet [h]+ fr[at(t1 + 0.12)..].fill((-15.0, -1.0)); // the vowel+ let (length, gap) = frication(&fr, t0, t1, "made up");+ assert_eq!((length, gap), (100, 120), "frication {length} ms, then {gap} ms to the voice");}const VOICES: [(HumanBody, &str, f64); 2] = [(BODY_MALE, "male", 112.0), (BODY_FEMALE, "female", 205.0)];diff --git a/crates/chimera/tests/nasal_spans.rs b/crates/chimera/tests/nasal_spans.rsindex d04c6c3..b34e18a 100644--- a/crates/chimera/tests/nasal_spans.rs+++ b/crates/chimera/tests/nasal_spans.rs@@ -70,6 +70,12 @@ fn a_vowel_and_its_coda_r_share_the_vowels_span() {let with_l = klatt_durations(&[seg("p"), seg("ɑ"), seg("l"), seg("k")], info);let with_r = klatt_durations(&[seg("p"), seg("ɑ"), seg("ɹ"), seg("k")], info);assert!((with_r[1] + with_r[2] - with_l[1]).abs() < 1e-9, "ɑ {} + ɹ {} against ɑ {}", with_r[1], with_r[2], with_l[1]);+ // a word's last /ɹ/ is its coda even before a vowel: "car is" keeps "car"'s nucleus+ let end = Segment { word_end: Some(true), ..seg("ɹ") };+ let linked = klatt_durations(&[seg("p"), seg("ɑ"), end.clone(), seg("ɑ")], info);+ let closed = klatt_durations(&[seg("p"), seg("ɑ"), end, seg("k"), seg("ɑ")], info);+ assert!(linked[1] + linked[2] < 0.75 * (klatt_durations(&[seg("p"), seg("ɑ"), seg("ɹ"), seg("ɑ")], info)[1..3].iter().sum::<f64>()), "\"car is\": ɑ {} + ɹ {}", linked[1], linked[2]);+ assert!((linked[1] - closed[1]).abs() < 0.03, "ɑ {} before a vowel in the next word, {} before a consonant", linked[1], closed[1]);// an /ɹ/ that opens the next syllable is not a codalet open = klatt_durations(&[seg("p"), seg("ɑ"), seg("ɹ"), seg("ɑ")], info);let open_l = klatt_durations(&[seg("p"), seg("ɑ"), seg("l"), seg("ɑ")], info);diff --git a/crates/chimera/tests/sing_fast.rs b/crates/chimera/tests/sing_fast.rsindex 8ead686..96b4ab5 100644--- a/crates/chimera/tests/sing_fast.rs+++ b/crates/chimera/tests/sing_fast.rs@@ -8,7 +8,7 @@ use chimera::phonetics::{Plan, PlannedSegment};fn g2p() -> EnglishG2P {let mut g = EnglishG2P::new();- g.load_dict("bah\tB AA1\nbee\tB IY1\nbop\tB AA1 P\ndee\tD IY1\ndoo\tD UW1\nscoo\tS K UW1\nskee\tS K IY1\nstrut\tS T R AH1 T\n");+ g.load_dict("ah\tAA1\nbah\tB AA1\nbee\tB IY1\nbop\tB AA1 P\ncha\tCH AA1\ndee\tD IY1\ndoo\tD UW1\nja\tJH AA1\nscoo\tS K UW1\nskee\tS K IY1\nstrut\tS T R AH1 T\n");g}@@ -94,3 +94,36 @@ fn a_delayed_cluster_closure_stays_inside_a_short_sung_stop() {}}}++/// A fast sung note squeezes an affricate to a few ms (12.5 ms for a 0.125-beat vowel before /tʃɑ/ at+/// 120 bpm). Its folds still close inside its span, or the breath spills into the vowel (it ran 19 ms+/// into it), and its release doesn't start ahead of the span.+#[test]+fn a_squeezed_affricate_keeps_its_folds_and_its_release_inside_its_span() {+ for lyrics in ["ah cha ah cha", "ah ja ah ja"] {+ for (bpm, beats) in [(120.0, 0.125), (184.0, 0.25), (240.0, 0.25), (120.0, 0.25), (90.0, 0.5)] {+ let (_, _, plan) = sing(lyrics, beats, bpm);+ let spans: Vec<&PlannedSegment> = plan.segments.iter().filter(|s| s.ipa == "tʃ" || s.ipa == "dʒ").collect();+ assert!(!spans.is_empty(), "{lyrics:?}");+ for span in spans {+ let folds: Vec<_> = plan+ .gestures+ .iter()+ .filter(|g| g.t0 >= span.t0 - 1e-9 && g.t0 < span.t1 && g.label.as_ref().is_some_and(|l| matches!(l.as_str(), "aspiration" | "voiced affricate")))+ .collect();+ assert!(!folds.is_empty(), "{lyrics:?} at {bpm} bpm: no folds in a {:.1} ms span", (span.t1 - span.t0) * 1000.0);+ for g in folds {+ let end = g.t1 + g.ramp_out;+ assert!(end <= span.t1 + 1e-9, "{lyrics:?}, {beats} beats at {bpm} bpm: the folds close {:.1} ms into the vowel", (end - span.t1) * 1000.0);+ }+ // the release is the fricative's own gesture (its speed picks it out from the+ // closure's), with its 8 ms lead+ let releases: Vec<_> = plan.gestures.iter().filter(|g| g.label.as_ref().is_some_and(|l| l.as_str() == span.ipa) && g.speed == Some(1.4)).filter(|g| (g.t1 - span.t1).abs() < 0.03).collect();+ assert!(!releases.is_empty(), "{lyrics:?} at {bpm} bpm: no release");+ for g in releases {+ assert!(g.t0 >= span.t0 - 0.008 - 1e-9, "{lyrics:?}, {beats} beats at {bpm} bpm: the release starts {:.1} ms before the span", (span.t0 - g.t0) * 1000.0);+ }+ }+ }+ }+}diff --git a/crates/chimera/tests/stop_spans.rs b/crates/chimera/tests/stop_spans.rsindex cc21169..5ff8423 100644--- a/crates/chimera/tests/stop_spans.rs+++ b/crates/chimera/tests/stop_spans.rs@@ -146,3 +146,15 @@ fn a_lip_release_starts_slowly() {let b = release_burst(&render("ˈɑpɑ", "male"));assert!(b < -5.0, "/p/ release {b:.1} dB re the vowel");}++/// /ð/ is a closure after any pause, not only the utterance's start: the `_` of IPA's `|` is a pause.+#[test]+fn dh_after_an_explicit_pause_is_a_closure_too() {+ let kind = |ipa: &str| {+ let plan = plan_utterance(&tokenize_ipa(ipa), &PlanOptions { body: "male".into(), t0: 0.05, f0: Some(112.0), ..Default::default() });+ plan.segments.iter().find(|s| s.ipa == "ð").unwrap().kind.clone()+ };+ assert_eq!(kind("ðə"), "stop", "at the utterance's start");+ assert_eq!(kind("ɑ | ðə"), "stop", "after an explicit pause");+ assert_eq!(kind("ɑðə"), "fricative", "between vowels");+}