Skip to content

the ultimate vocal synthesizer - sing, speak, beatbox, and more

Rust

Palatalization: IPA ʲ raises and fronts the tongue body under a consonant, held until the voice returns (#32 step 1)

Sayob3c12d48ae02
diff --git a/crates/chimera/src/phonetics/ipa.rs b/crates/chimera/src/phonetics/ipa.rs
index dce731c..1d6809f 100644
--- a/crates/chimera/src/phonetics/ipa.rs
+++ b/crates/chimera/src/phonetics/ipa.rs
@@ -1,6 +1,7 @@
//! IPA string → segments (stress ˈ ˌ, length ː, tie bars, affricates, diphthongs, word gaps).
use super::Segment;
+use super::inventory::Manner;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::sync::OnceLock;
@@ -77,6 +78,14 @@ pub fn tokenize_ipa(input: &str) -> Vec<Segment> {
i += 1;
continue;
}
+ // palatalization, on a consonant made with a mouth gesture (not a glottal one)
+ if ch == 'ʲ' {
+ if let Some(last) = out.last_mut().filter(|l| super::inventory::consonants().iter().any(|c| c.ipa == l.ipa && c.manner != Manner::Glottal)) {
+ last.palatalized = Some(true);
+ }
+ i += 1;
+ continue;
+ }
// a tilde after the length mark instead of the vowel (ɛː̃)
if ch == NASALIZED && i > 0 && (s[i - 1] == 'ː' || s[i - 1] == ':') {
if let Some(last) = out.last_mut().filter(|l| is_vowel_token(&l.ipa)) {
diff --git a/crates/chimera/src/phonetics/mod.rs b/crates/chimera/src/phonetics/mod.rs
index 274b248..105531e 100644
--- a/crates/chimera/src/phonetics/mod.rs
+++ b/crates/chimera/src/phonetics/mod.rs
@@ -41,6 +41,10 @@ pub struct Segment {
/// a vowel said with the velum lowered (IPA combining tilde); None or false: oral
#[serde(default, skip_serializing_if = "Option::is_none")]
pub nasalized: Option<bool>,
+ /// a consonant made with a second, palatal gesture: the tongue body raised and fronted toward
+ /// the palate (IPA ʲ); None or false: plain
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub palatalized: Option<bool>,
}
impl Segment {
diff --git a/crates/chimera/src/phonetics/planner.rs b/crates/chimera/src/phonetics/planner.rs
index 6817d08..ca79954 100644
--- a/crates/chimera/src/phonetics/planner.rs
+++ b/crates/chimera/src/phonetics/planner.rs
@@ -172,6 +172,7 @@ struct Seg<'a> {
glide_from: bool,
aspirated: Option<bool>,
nasalized: Option<bool>,
+ palatalized: Option<bool>,
}
/// velum port area (cm²) of a nasalized vowel, and the ramps of its lowering and raising (s)
@@ -202,10 +203,10 @@ pub fn plan_utterance(input: &[Segment], o: &PlanOptions) -> Plan {
let base = durs[si];
for (i, p) in parts.iter().enumerate() {
let dur = if parts.len() > 1 { base * (if i == 0 { 0.55 } else { 0.45 }) } else { base };
- segs.push(Seg { ipa: p, dur: Some(dur), stress: s.stress, f0: s.f0, def: None, vowel: None, glide_from: i > 0, aspirated: None, nasalized: s.nasalized });
+ segs.push(Seg { ipa: p, dur: Some(dur), stress: s.stress, f0: s.f0, def: None, vowel: None, glide_from: i > 0, aspirated: None, nasalized: s.nasalized, palatalized: None });
}
} else {
- segs.push(Seg { ipa: &s.ipa, dur: Some(durs[si]), stress: s.stress, f0: s.f0, def: None, vowel: None, glide_from: false, aspirated: s.aspirated, nasalized: s.nasalized });
+ segs.push(Seg { ipa: &s.ipa, dur: Some(durs[si]), stress: s.stress, f0: s.f0, def: None, vowel: None, glide_from: false, aspirated: s.aspirated, nasalized: s.nasalized, palatalized: s.palatalized });
}
}
for s in segs.iter_mut() {
@@ -311,6 +312,7 @@ pub fn plan_utterance(input: &[Segment], o: &PlanOptions) -> Plan {
}
// phonation
let k2 = 1.0; // abduction is body-scaled in the voice
+ let mut voiced_again = t1; // when the folds are back, after an aspirated release
if def.glottal == Some(Glottal::Open) {
gestures.push(Gesture::new(t0 - 0.01, t1, 0.03, 0.035, 5.0, one(ControlKey::ABDUCTION, 0.33 * k2)).label("h"));
} else if def.glottal == Some(Glottal::Breathy) {
@@ -323,13 +325,22 @@ pub fn plan_utterance(input: &[Segment], o: &PlanOptions) -> Plan {
// before a vowel and not after /s/
let aspirated = s.aspirated.unwrap_or_else(|| next_vowel && !prev_seg.is_some_and(|p| p.ipa == "s"));
let vot = if aspirated { 0.05 / rate } else { 0.012 };
- gestures.push(Gesture::new(t0 + 0.25 * dur, t1 + vot * 0.4, 0.035, if aspirated { 0.045 } else { 0.02 }, 6.0, one(ControlKey::ABDUCTION, if aspirated { 0.36 } else { 0.22 })).label("aspiration"));
+ let g = Gesture::new(t0 + 0.25 * dur, t1 + vot * 0.4, 0.035, if aspirated { 0.045 } else { 0.02 }, 6.0, one(ControlKey::ABDUCTION, if aspirated { 0.36 } else { 0.22 })).label("aspiration");
+ voiced_again = g.end();
+ gestures.push(g);
} else {
gestures.push(Gesture::new(t0 - 0.015, t1 + 0.005, 0.03, 0.03, 6.0, one(ControlKey::ABDUCTION, 0.3)).label("voiceless"));
}
} else if def.manner == Manner::Fricative {
gestures.push(Gesture::new(t0, t1, 0.03, 0.03, 3.0, one(ControlKey::ABDUCTION, 0.035)).label("voiced frication"));
}
+ // palatalization: the tongue body goes toward /j/ under the consonant, stays there until the
+ // voice returns, and lets go into the vowel, so the release carries the [j]-like transition
+ if s.palatalized == Some(true) {
+ if let Some(tg) = palatal_body(def, body) {
+ gestures.push(Gesture::new(t0, voiced_again, PALATAL_RAMP, PALATAL_RAMP, PALATAL_STRENGTH, artic_targets(&tg)).label("palatalized"));
+ }
+ }
}
let open_end = o.open_end == Some(true);
let sung = o.sung == Some(true);
@@ -410,6 +421,26 @@ pub fn plan_utterance(input: &[Segment], o: &PlanOptions) -> Plan {
Plan { gestures, segments: planned, end }
}
+/// How firmly a palatalized consonant's tongue body is held (the overlapping vowel has 1.0, the
+/// consonant's own articulators 40), and how long it takes to take and let go of the posture (s)
+const PALATAL_STRENGTH: f64 = 3.0;
+const PALATAL_RAMP: f64 = 0.04;
+
+/// The tongue body's posture for a palatalized consonant: /j/'s, on the coordinates the consonant
+/// itself does not use. A velar closes with the body's height, so only its front-back
+/// place moves (fronted); a consonant with no free body coordinate (palatals, uvulars, /w/, /ɫ/,
+/// glottals) has nothing to add.
+fn palatal_body(def: &PhonemeDef, body: BodyMix) -> Option<ArticPatch> {
+ let j = phoneme("j", body)?;
+ let mut out = ArticPatch::default();
+ for k in [ArticKey::TbX, ArticKey::TbY] {
+ if let (false, Some(x)) = (def.dominant.contains(&k), j.targets.get(k)) {
+ out.set(k, x);
+ }
+ }
+ (!out.is_empty() && def.place != "palatal" && def.manner != Manner::Glottal).then_some(out)
+}
+
/// Front-to-back order of the oral places of articulation.
fn place_rank(place: &str) -> Option<u8> {
Some(match place {
diff --git a/crates/chimera/src/phonetics/sing.rs b/crates/chimera/src/phonetics/sing.rs
index eac4de5..56ee989 100644
--- a/crates/chimera/src/phonetics/sing.rs
+++ b/crates/chimera/src/phonetics/sing.rs
@@ -28,7 +28,7 @@ fn cdur(ipa: &str, body: BodyMix, k: f64) -> f64 {
}
fn consonants(segs: &[Segment], body: BodyMix, k: f64) -> Vec<Segment> {
- segs.iter().map(|c| Segment { ipa: c.ipa.clone(), dur: Some(cdur(&c.ipa, body, k)), aspirated: c.aspirated, ..Default::default() }).collect()
+ segs.iter().map(|c| Segment { ipa: c.ipa.clone(), dur: Some(cdur(&c.ipa, body, k)), aspirated: c.aspirated, palatalized: c.palatalized, ..Default::default() }).collect()
}
/// A sung vowel (or one half of a diphthong) at the note's pitch, with the sound marks of the
diff --git a/crates/chimera/tests/sound_marks.rs b/crates/chimera/tests/sound_marks.rs
index 596f176..08b9abb 100644
--- a/crates/chimera/tests/sound_marks.rs
+++ b/crates/chimera/tests/sound_marks.rs
@@ -109,3 +109,89 @@ fn a_sung_vowel_keeps_its_nasalization() {
let p = plan_song(&[SungNote { midi: 57.0, beats: 2.0, syl: syl(vec![], diph) }], &SongOptions { body: "male".into(), bpm: 90.0, t0: 0.5, vibrato: 0.0 });
assert_eq!(labelled(&p.gestures, "velum").len(), 2);
}
+
+fn palatalized(s: &Segment) -> Option<bool> {
+ s.palatalized
+}
+
+#[test]
+fn the_modifier_j_palatalizes_the_consonant_before_it() {
+ assert_eq!(flags("tʲa", palatalized), pairs(&[("t", true), ("a", false)]));
+ assert_eq!(flags("pʲ kʲ dʒʲ ɡʲ", palatalized), pairs(&[("p", true), ("k", true), ("dʒ", true), ("g", true)]));
+ // with the other marks, in either order
+ let s = tokenize_ipa("pʰʲ");
+ assert_eq!((s[0].aspirated, s[0].palatalized), (Some(true), Some(true)));
+}
+
+#[test]
+fn a_palatal_mark_on_the_wrong_sound_is_ignored() {
+ // a vowel, a glottal (no mouth gesture), a pause, and no sound at all
+ for ipa in ["aʲ", "ɛʲ", "hʲ", "ʔʲ", "ɦʲ", "ʲ", "ʲt", "|ʲ"] {
+ assert!(tokenize_ipa(ipa).iter().all(|s| s.palatalized.is_none()), "{ipa}");
+ }
+ assert_eq!(tokenize_ipa("aʲ"), tokenize_ipa("a"));
+}
+
+#[test]
+fn the_owners_russian_pair_now_differs() {
+ // брат / брать were both "b r ɑ t"
+ let (brat, brat_soft) = (tokenize_ipa("brɑt"), tokenize_ipa("brɑtʲ"));
+ assert_ne!(brat, brat_soft);
+ assert_eq!(brat.iter().map(|s| &s.ipa).collect::<Vec<_>>(), brat_soft.iter().map(|s| &s.ipa).collect::<Vec<_>>());
+ assert_eq!(brat_soft.iter().map(|s| s.palatalized).collect::<Vec<_>>(), [None, None, None, Some(true)]);
+}
+
+#[test]
+fn a_palatalized_consonant_gets_a_tongue_body_gesture_that_leaves_its_own_alone() {
+ let own = |ipa: &str| chimera::phonetics::inventory::phoneme(ipa, BodyMix::MALE).unwrap().dominant;
+ for c in ["p", "t", "d", "k", "g", "s", "ʃ", "n", "m", "l", "f", "ɹ", "tʃ"] {
+ let plain = plan(&tokenize_ipa(&format!("{c}a")));
+ let soft = plan(&tokenize_ipa(&format!("{c}ʲa")));
+ assert!(labelled(&plain.gestures, "palatalized").is_empty(), "{c}");
+ let g = labelled(&soft.gestures, "palatalized");
+ assert_eq!(g.len(), 1, "{c}");
+ // only the tongue body, and never a coordinate the consonant's closure or constriction owns
+ for k in ArticKey::ALL {
+ if g[0].targets.get(k).is_some() {
+ assert!(matches!(k, ArticKey::TbX | ArticKey::TbY), "{c}: {k:?}");
+ assert!(!own(c).contains(&k), "{c} owns {k:?}");
+ }
+ }
+ // the consonant's own gestures and everything else are unchanged
+ let rest = |p: &Plan| serde_json::to_string(&p.gestures.iter().filter(|g| g.label.as_ref().is_none_or(|l| l.as_str() != "palatalized")).collect::<Vec<_>>()).unwrap();
+ assert_eq!(rest(&plain), rest(&soft), "{c}");
+ }
+ // a velar is fronted, not raised: its closure owns the body's height
+ let k = plan(&tokenize_ipa("kʲa"));
+ let g = labelled(&k.gestures, "palatalized")[0];
+ assert!(g.targets.get(ArticKey::TbX).is_some() && g.targets.get(ArticKey::TbY).is_none());
+ // a consonant with nothing free on the body, or already palatal, adds no gesture
+ for c in ["j", "ɲ", "w", "ɫ", "q"] {
+ assert!(labelled(&plan(&tokenize_ipa(&format!("{c}ʲa"))).gestures, "palatalized").is_empty(), "{c}");
+ }
+}
+
+#[test]
+fn the_palatal_posture_holds_until_the_voice_returns() {
+ // after an aspirated stop that is later than the release: the [j]-like transition is voiced
+ let seg = |aspirated| vec![Segment { aspirated, palatalized: Some(true), ..Segment::new("t") }, Segment::new("a")];
+ let end = |aspirated| labelled(&plan(&seg(aspirated)).gestures, "palatalized")[0].t1;
+ let t1 = plan(&seg(None)).segments[0].t1;
+ assert!(end(Some(true)) > t1 + 0.05, "aspirated: {}", end(Some(true)) - t1);
+ assert!(end(Some(false)) < t1 + 0.03, "unaspirated: {}", end(Some(false)) - t1);
+}
+
+#[test]
+fn a_sung_consonant_keeps_its_palatalization() {
+ let onset = |soft| vec![Segment { palatalized: soft, ..Segment::new("t") }];
+ let sung = |s| plan_song(&[SungNote { midi: 57.0, beats: 1.0, syl: syl(onset(s), Segment::new("a")) }], &SongOptions { body: "male".into(), bpm: 90.0, t0: 0.5, vibrato: 0.0 });
+ assert_eq!(labelled(&sung(Some(true)).gestures, "palatalized").len(), 1);
+ assert!(labelled(&sung(None).gestures, "palatalized").is_empty());
+ let live = |s| plan_note_on(&syl(onset(s), Segment::new("a")), 57.0, &NoteOnOptions { body: "male".into(), vibrato: 0.0, tag: 1.0, legato_from: None });
+ assert_eq!(labelled(&live(Some(true)), "palatalized").len(), 1);
+ assert!(labelled(&live(None), "palatalized").is_empty());
+ // a coda too
+ let coda = |s| Syllable { coda: onset(s), ..syl(vec![], Segment::new("a")) };
+ let p = plan_song(&[SungNote { midi: 57.0, beats: 1.0, syl: coda(Some(true)) }], &SongOptions { body: "male".into(), bpm: 90.0, t0: 0.5, vibrato: 0.0 });
+ assert_eq!(labelled(&p.gestures, "palatalized").len(), 1);
+}
diff --git a/crates/chimera/tests/sound_marks_acoustic.rs b/crates/chimera/tests/sound_marks_acoustic.rs
index ee09d7b..dde0d48 100644
--- a/crates/chimera/tests/sound_marks_acoustic.rs
+++ b/crates/chimera/tests/sound_marks_acoustic.rs
@@ -1,6 +1,7 @@
//! The sound marks, heard: each is rendered through the real voice (male and female bodies) and
//! measured against the same sound without the mark.
use chimera::analysis::fft::{Fft, hann, power_spectrum_db};
+use chimera::analysis::lpc::{FormantOpts, estimate_formants};
use chimera::anatomy::human::{BODY_FEMALE, BODY_MALE, HumanBody};
use chimera::phonetics::planner::{PlanOptions, plan_utterance};
use chimera::phonetics::{Plan, Segment};
@@ -16,6 +17,10 @@ struct Rendered {
out: Vec<f32>,
/// velum port area (cm²) at the end of each 128-sample block
port: Vec<f64>,
+ /// glottal voicing (0..1) at the end of each 128-sample block
+ voicing: Vec<f64>,
+ /// the narrowest area (cm²) of the oral tract and lips at the end of each 128-sample block
+ narrowest: Vec<f64>,
}
fn render(segs: &[Segment], body: HumanBody, name: &str, f0: f64) -> Rendered {
@@ -24,12 +29,14 @@ fn render(segs: &[Segment], body: HumanBody, name: &str, f0: f64) -> Rendered {
let plan = plan_utterance(segs, &PlanOptions { body: name.into(), t0: 0.05, f0: Some(f0), ..Default::default() });
v.schedule(&plan.gestures);
let mut out = vec![0.0f32; (FS * (plan.end + 0.3)).round() as usize];
- let mut port = vec![];
+ let (mut port, mut voicing, mut narrowest) = (vec![], vec![], vec![]);
for c in out.chunks_mut(128) {
v.render(c);
port.push(v.airway.frame.as_ref().map_or(0.0, |f| f.velum_port));
+ voicing.push(v.glottis.voicing);
+ narrowest.push(v.airway.frame.as_ref().map_or(f64::MAX, |f| f.grid.iter().filter(|g| g.region >= 2).map(|g| g.area).fold(f64::MAX, f64::min)));
}
- Rendered { plan, out, port }
+ Rendered { plan, out, port, voicing, narrowest }
}
impl Rendered {
@@ -44,6 +51,25 @@ impl Rendered {
}
}
+impl Rendered {
+ /// when the voice sets in after `t` (the first block with voicing above one half)
+ fn voicing_onset(&self, t: f64) -> f64 {
+ let first = (t * FS / 128.0) as usize;
+ (first..self.voicing.len()).find(|&i| self.voicing[i] > 0.5).expect("the voice sets in") as f64 * 128.0 / FS
+ }
+ /// the widest "narrowest oral area" (cm²) over [a, b]: 0 while the tract stays sealed
+ fn min_area_between(&self, a: f64, b: f64) -> f64 {
+ let blocks = (a * FS / 128.0) as usize..(b * FS / 128.0) as usize;
+ self.narrowest[blocks].iter().fold(0.0, |m, x| m.max(*x))
+ }
+ /// second formant (Hz) of the 30 ms centred on t
+ fn f2(&self, t: f64) -> f64 {
+ let (mid, w) = ((t * FS) as usize, (0.03 * FS) as usize);
+ let frame: Vec<f64> = self.out[mid - w / 2..mid + w / 2].iter().map(|x| *x as f64).collect();
+ estimate_formants(&frame, FS, FormantOpts::default())[1].freq
+ }
+}
+
/// Level (dB) of the power in [lo, hi) Hz of a spectrum from [`Rendered::spectrum`].
fn band(db: &[f64], lo: f64, hi: f64) -> f64 {
let df = FS / N as f64;
@@ -100,3 +126,57 @@ fn a_nasalized_vowel_leaves_the_stop_before_it_closed() {
assert!(nasal.out.iter().all(|x| x.is_finite()));
}
}
+
+/// A consonant and a vowel, the vowel held and stressed.
+fn cv(consonant: Segment, vowel: &str) -> Vec<Segment> {
+ vec![consonant, Segment { dur: Some(0.35), stress: Some(1), ..Segment::new(vowel) }]
+}
+
+fn palatalized(ipa: &str) -> Segment {
+ Segment { palatalized: Some(true), ..Segment::new(ipa) }
+}
+
+#[test]
+fn a_palatalized_consonant_starts_its_vowel_with_a_high_f2() {
+ // The vowel's F2 (LPC, 30 ms window) as the voice sets in after the release, and once the
+ // vowel has settled, /a/ after a plain and a palatalized consonant (Hz):
+ // male /ta/ 1485 → /tʲa/ 2092 (+607); /ka/ 1464 → /kʲa/ 1724 (+260)
+ // female /ta/ 1790 → /tʲa/ 2269 (+479); /ka/ 1779 → /kʲa/ 2117 (+338)
+ // and 200 ms later both are back at the vowel's own F2, within 27 Hz (the largest gap, /kʲa/ male).
+ for (body, name, f0) in VOICES {
+ for (c, rise) in [("t", 350.0), ("p", 350.0), ("k", 150.0)] {
+ let plain = render(&cv(Segment::new(c), "a"), body, name, f0);
+ let pal = render(&cv(palatalized(c), "a"), body, name, f0);
+ let release = plain.plan.segments[0].t1;
+ let (on_plain, on_pal) = (plain.voicing_onset(release), pal.voicing_onset(release));
+ let (a, b) = (plain.f2(on_plain + 0.015), pal.f2(on_pal + 0.015));
+ println!("{name} /{c}a/ F2 {a:.0} Hz, /{c}ʲa/ F2 {b:.0} Hz at the voice onset");
+ assert!(b > a + rise, "{name} /{c}ʲa/: F2 {b:.0} Hz against {a:.0} Hz plain");
+ let (a, b) = (plain.f2(on_plain + 0.2), pal.f2(on_pal + 0.2));
+ assert!((a - b).abs() < 50.0, "{name} /{c}ʲa/: F2 {b:.0} Hz against {a:.0} Hz plain, 200 ms after the voice sets in");
+ }
+ }
+}
+
+#[test]
+fn a_palatalized_stop_still_closes() {
+ // The tongue body's posture is a secondary gesture: the primary articulator still seals. The
+ // oral tract's narrowest area is 0 through the closure's middle, and a voiceless closure is as
+ // silent as the plain one (-47 to -72 dB re the vowel in every case measured; a voiced
+ // closure's voice bar varies from -19 to -62 dB with or without the mark, so it is not compared).
+ for (body, name, f0) in VOICES {
+ for c in ["p", "t", "k", "b", "d", "g"] {
+ let r = render(&cv(palatalized(c), "a"), body, name, f0);
+ let (s, vowel) = (&r.plan.segments[0], &r.plan.segments[1]);
+ let (a, b) = (s.t0 + 0.2 * (s.t1 - s.t0), s.t1 - 0.03);
+ let widest = r.min_area_between(a, b);
+ assert!(widest < 1e-6, "/{c}ʲ/ ({name}) leaks: the narrowest oral area is {widest} cm² in the closure");
+ if "ptk".contains(c) {
+ let peak = |a: f64, b: f64| r.out[(a * FS) as usize..(b * FS) as usize].iter().fold(0.0f64, |m, x| m.max(x.abs() as f64));
+ let db = 20.0 * (peak(a, b) / peak(vowel.t0 + 0.1, vowel.t0 + 0.3)).log10();
+ println!("{name} /{c}ʲ/: closure {db:.1} dB re the vowel");
+ assert!(db < -40.0, "/{c}ʲ/ ({name}): closure at {db:.1} dB re the vowel");
+ }
+ }
+ }
+}
diff --git a/src/wasm/types.ts b/src/wasm/types.ts
index 93017a8..8619905 100644
--- a/src/wasm/types.ts
+++ b/src/wasm/types.ts
@@ -159,6 +159,8 @@ export interface Segment {
aspirated?: boolean;
/** a vowel said with the velum lowered (IPA combining tilde) */
nasalized?: boolean;
+ /** a consonant with a secondary palatal gesture, the tongue body raised and fronted (IPA ʲ) */
+ palatalized?: boolean;
}
export interface PlannedSegment {