the ultimate vocal synthesizer - sing, speak, beatbox, and more
Rust
Korean's three stops stay apart word-initially: the front end says how long each aspirates (Segment.vot)
Joshua Phamd7bf1c97c6d5
diff --git a/crates/chimera/src/phonetics/korean.rs b/crates/chimera/src/phonetics/korean.rsindex b92977a..8117265 100644--- a/crates/chimera/src/phonetics/korean.rs+++ b/crates/chimera/src/phonetics/korean.rs@@ -6,7 +6,8 @@//!//! Stops say their own aspiration (Segment::aspirated): aspirated ㅋ ㅌ ㅍ ㅊ are aspirated, and//! tense ㄲ ㄸ ㅃ ㅉ are voiceless and unaspirated (the engine has no stiff "fortis" phonation yet).-//! Lax stops keep the planner's default: aspirated word-initially, voiced between voiced sounds.+//! Lax stops aspirate briefly word-initially and aspirated ones long (Segment::vot); lax stops are+//! voiced between voiced sounds.//! ㅈ ㅊ ㅉ are alveolo-palatal [tɕ], and ㅅ ㅆ before [i] or [j] are [ɕ].use super::syllables::FrontEnd;@@ -157,32 +158,44 @@ fn join(a: &mut Block, b: &mut Block) {}/// An initial's sound, and its aspiration when the letter decides it (None: the planner's default).-fn onset_ipa(o: char, voiced_before: bool, front: bool, after_l: bool) -> (&'static str, Option<bool>) {+/// Korean's three stops, word-initially all voiceless, told apart by how long they aspirate:+/// aspirated ㅋ ㅌ ㅍ ㅊ long, lax ㄱ ㄷ ㅂ ㅈ briefly, tense ㄲ ㄸ ㅃ ㅉ not at all (word-initial voice+/// onset times of about 90, 45 and 10 ms). Between voiced sounds a lax stop is voiced.+const ASPIRATED_VOT: f64 = 0.09;+const LAX_VOT: f64 = 0.045;++/// An initial → its IPA, whether it aspirates, and for how long (s).+fn onset_ipa(o: char, voiced_before: bool, front: bool, after_l: bool) -> (&'static str, Option<bool>, Option<f64>) {+ let lax = |voiced: &'static str, voiceless: &'static str| {+ if voiced_before {+ (voiced, None, None)+ } else {+ (voiceless, Some(true), Some(LAX_VOT))+ }+ };+ let aspirated = |ipa: &'static str| (ipa, Some(true), Some(ASPIRATED_VOT));+ let tense = |ipa: &'static str| (ipa, Some(false), None);match o {- 'ㄱ' if voiced_before => ("g", None),- 'ㄱ' => ("k", None),- 'ㅋ' => ("k", Some(true)),- 'ㄲ' => ("k", Some(false)),- 'ㄷ' if voiced_before => ("d", None),- 'ㄷ' => ("t", None),- 'ㅌ' => ("t", Some(true)),- 'ㄸ' => ("t", Some(false)),- 'ㅂ' if voiced_before => ("b", None),- 'ㅂ' => ("p", None),- 'ㅍ' => ("p", Some(true)),- 'ㅃ' => ("p", Some(false)),- 'ㅈ' if voiced_before => ("dʑ", None),- 'ㅈ' => ("tɕ", None),- 'ㅊ' => ("tɕ", Some(true)),- 'ㅉ' => ("tɕ", Some(false)),- 'ㅅ' | 'ㅆ' if front => ("ɕ", None),- 'ㅅ' | 'ㅆ' => ("s", None),- 'ㅎ' if voiced_before => ("ɦ", None),- 'ㅎ' => ("h", None),- 'ㄴ' => ("n", None),- 'ㅁ' => ("m", None),- 'ㄹ' if after_l => ("l", None),- 'ㄹ' => ("ɾ", None),+ 'ㄱ' => lax("g", "k"),+ 'ㅋ' => aspirated("k"),+ 'ㄲ' => tense("k"),+ 'ㄷ' => lax("d", "t"),+ 'ㅌ' => aspirated("t"),+ 'ㄸ' => tense("t"),+ 'ㅂ' => lax("b", "p"),+ 'ㅍ' => aspirated("p"),+ 'ㅃ' => tense("p"),+ 'ㅈ' => lax("dʑ", "tɕ"),+ 'ㅊ' => aspirated("tɕ"),+ 'ㅉ' => tense("tɕ"),+ 'ㅅ' | 'ㅆ' if front => ("ɕ", None, None),+ 'ㅅ' | 'ㅆ' => ("s", None, None),+ 'ㅎ' if voiced_before => ("ɦ", None, None),+ 'ㅎ' => ("h", None, None),+ 'ㄴ' => ("n", None, None),+ 'ㅁ' => ("m", None, None),+ 'ㄹ' if after_l => ("l", None, None),+ 'ㄹ' => ("ɾ", None, None),_ => unreachable!("not an initial: {o}"),}}@@ -218,8 +231,8 @@ pub fn word_syllables(word: &str) -> Vec<Syllable> {let mut onset: Vec<Segment> = b.onset.map(|o| {- let (ipa, aspirated) = onset_ipa(o, voiced_before, front, prev_coda == Some('ㄹ'));- Segment { aspirated, ..Segment::new(ipa) }+ let (ipa, aspirated, vot) = onset_ipa(o, voiced_before, front, prev_coda == Some('ㄹ'));+ Segment { aspirated, vot, ..Segment::new(ipa) }}).into_iter().collect();diff --git a/crates/chimera/src/phonetics/mod.rs b/crates/chimera/src/phonetics/mod.rsindex 6d2d01e..ed597d0 100644--- a/crates/chimera/src/phonetics/mod.rs+++ b/crates/chimera/src/phonetics/mod.rs@@ -39,6 +39,10 @@ pub struct Segment {/// before a vowel, not after /s/)#[serde(default, skip_serializing_if = "Option::is_none")]pub aspirated: Option<bool>,+ /// how long an aspirated stop or affricate aspirates (voice onset time, s), as its front end+ /// decides it (Korean's lax stops aspirate briefly, its aspirated ones long); None: 50 ms+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub vot: Option<f64>,/// 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>,diff --git a/crates/chimera/src/phonetics/planner.rs b/crates/chimera/src/phonetics/planner.rsindex 64810e7..a9b0dc7 100644--- a/crates/chimera/src/phonetics/planner.rs+++ b/crates/chimera/src/phonetics/planner.rs@@ -171,6 +171,7 @@ struct Seg<'a> {vowel: Option<ArticPatch>,glide_from: bool,aspirated: Option<bool>,+ vot: Option<f64>,nasalized: Option<bool>,palatalized: Option<bool>,breathy: Option<bool>,@@ -210,10 +211,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, palatalized: None, breathy: s.breathy });+ segs.push(Seg { ipa: p, dur: Some(dur), stress: s.stress, f0: s.f0, def: None, vowel: None, glide_from: i > 0, aspirated: None, vot: None, nasalized: s.nasalized, palatalized: None, breathy: s.breathy });}} 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, palatalized: s.palatalized, breathy: s.breathy });+ 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, vot: s.vot, nasalized: s.nasalized, palatalized: s.palatalized, breathy: s.breathy });}}for s in segs.iter_mut() {@@ -338,8 +339,15 @@ pub fn plan_utterance(input: &[Segment], o: &PlanOptions) -> Plan {// as the front end says; by default English's rule: aspirated when syllable-initial// 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 };- 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");+ // the folds stay apart past the release by 40% of the default 50 ms (the ramps+ // make up the rest of the aspiration); a front end's longer or shorter `vot` moves+ // that by the whole difference, so the voice onset time it asks for is what it gets+ let past = match (aspirated, s.vot) {+ (true, None) => DEFAULT_VOT / rate * 0.4, // as before, to the bit+ (true, Some(v)) => js_max(0.0, 0.4 * DEFAULT_VOT + v - DEFAULT_VOT) / rate,+ (false, _) => 0.012 * 0.4,+ };+ let g = Gesture::new(t0 + 0.25 * dur, t1 + past, 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 {@@ -480,6 +488,8 @@ fn place_rank(place: &str) -> Option<u8> {/// How much later a pre-placed closure behind another stop's closure is aimed (s): the time its/// shorter travel saves, so it seals as the front closure opens.const PLACED_CLOSURE_DELAY: f64 = 0.025;+/// An aspirated stop's voice onset time when its front end doesn't say (English's), in seconds.+const DEFAULT_VOT: f64 = 0.05;/// Whether stop `i` closes behind the closure of the stop before it, with its own articulators/// already set by the consonant before that one (/t/ in /ɹpt/: the tip is up for the /ɹ/ anddiff --git a/crates/chimera/src/phonetics/sing.rs b/crates/chimera/src/phonetics/sing.rsindex 7124d16..37151db 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, palatalized: c.palatalized, breathy: c.breathy, ..Default::default() }).collect()+ segs.iter().map(|c| Segment { ipa: c.ipa.clone(), dur: Some(cdur(&c.ipa, body, k)), aspirated: c.aspirated, vot: c.vot, palatalized: c.palatalized, breathy: c.breathy, ..Default::default() }).collect()}/// A sung vowel (or one half of a diphthong) at the note's pitch, with the sound marks of thediff --git a/crates/chimera/tests/syllables.rs b/crates/chimera/tests/syllables.rsindex d6e9184..0bf4574 100644--- a/crates/chimera/tests/syllables.rs+++ b/crates/chimera/tests/syllables.rs@@ -59,7 +59,20 @@ fn lyrics_words_melisma_phrase_ends_unreduced_function_words() {/// Each syllable as onset+nucleus+coda IPA, joined with "."; a stop's aspiration, when its front end/// decides it, as ʰ (aspirated) or ˭ (unaspirated)fn sung(text: &str) -> String {- let marked = |segs: &[Segment]| segs.iter().map(|s| format!("{}{}", s.ipa, match s.aspirated { Some(true) => "ʰ", Some(false) => "˭", None => "" })).collect::<String>();+ // aspiration with its length when the front end gives one: ʰ45 (45 ms), ʰ90+ let marked = |segs: &[Segment]| {+ segs.iter()+ .map(|s| {+ let asp = match (s.aspirated, s.vot) {+ (Some(true), Some(v)) => format!("ʰ{}", (v * 1000.0).round()),+ (Some(true), None) => "ʰ".into(),+ (Some(false), _) => "˭".into(),+ (None, _) => String::new(),+ };+ format!("{}{asp}", s.ipa)+ })+ .collect::<String>()+ };lyrics_to_syllables(text, &word).iter().map(|s| format!("{}{}{}", marked(&s.onset), s.nucleus.ipa, marked(&s.coda))).collect::<Vec<_>>().join(".")}@@ -67,17 +80,43 @@ fn sung(text: &str) -> String {fn hangul_sings_one_syllable_per_block_with_korean_sound_rules() {assert_eq!(sung("빵야"), "p˭aŋ.ja"); // ㅇ coda stays; tense ㅃ is voiceless and unaspiratedassert_eq!(sung("먹어"), "mʌ.gʌ"); // a coda moves onto a silent ㅇ, voiced between vowels- assert_eq!(sung("좋아"), "tɕo.a"); // ㅎ drops before a vowel; ㅈ is alveolo-palatal- assert_eq!(sung("좋다"), "tɕo.tʰa"); // ㅎ + ㄷ aspirates- assert_eq!(sung("국물"), "kuŋ.mul"); // nasalisation+ assert_eq!(sung("좋아"), "tɕʰ45o.a"); // ㅎ drops before a vowel; ㅈ is alveolo-palatal+ assert_eq!(sung("좋다"), "tɕʰ45o.tʰ90a"); // ㅎ + ㄷ aspirates+ assert_eq!(sung("국물"), "kʰ45uŋ.mul"); // nasalisationassert_eq!(sung("신라"), "ɕil.la"); // ㄴ + ㄹ is [ll]; ㅅ before i is [ɕ]assert_eq!(sung("없는"), "ʌm.nɯn"); // a two-letter coda simplifies, then nasalisesassert_eq!(sung("학교"), "hak.k˭jo"); // tensing after an obstruent codaassert_eq!(sung("사랑해"), "sa.ɾaŋ.ɦɛ");- assert_eq!(sung("닭"), "tak");+ assert_eq!(sung("닭"), "tʰ45ak");assert_eq!(sung("의사"), "ɰi.sa");}+#[test]+fn korean_stops_stay_three_ways_apart_word_initially() {+ // word-initially all three are voiceless: aspirated long, lax briefly, tense not at all+ // (불 fire, 풀 grass, 뿔 horn); they used to sing as pul, pul and bul+ assert_eq!(sung("불 풀 뿔"), "pʰ45ul.pʰ90ul.p˭ul");+ assert_eq!(sung("달 탈 딸"), "tʰ45al.tʰ90al.t˭al");+ assert_eq!(sung("가 카 까"), "kʰ45a.kʰ90a.k˭a");+ // inside a word: lax voiced between vowels, the other two as word-initially+ assert_eq!(sung("아기 아키 아끼"), "a.gi.a.kʰ90i.a.k˭i");+}++#[test]+fn lax_and_aspirated_stops_plan_different_aspiration() {+ use chimera::phonetics::planner::{PlanOptions, plan_utterance};+ // how long the folds stay apart after the release: the aspiration gesture's end+ let aspiration_end = |text: &str| {+ let s = &lyrics_to_syllables(text, &word)[0];+ let segs: Vec<Segment> = s.onset.iter().cloned().chain([Segment::new(&s.nucleus.ipa)]).chain(s.coda.iter().cloned()).collect();+ let plan = plan_utterance(&segs, &PlanOptions::default());+ plan.gestures.iter().find(|g| g.label.as_ref().map(|l| l.as_str()) == Some("aspiration")).map(|g| g.t1).unwrap()+ };+ let (lax, aspirated) = (aspiration_end("불"), aspiration_end("풀"));+ // the folds stay apart 45 ms longer for 풀: the difference its front end asked for+ assert!((aspirated - lax - 0.045).abs() < 1e-9, "lax {lax}, aspirated {aspirated}");+}+#[test]fn hangul_keeps_its_spelling_and_mixes_with_english() {let syls = lyrics_to_syllables("twinkle 빵야 빵야, star", &word);diff --git a/src/wasm/types.ts b/src/wasm/types.tsindex f2ead13..db04757 100644--- a/src/wasm/types.ts+++ b/src/wasm/types.ts@@ -157,6 +157,8 @@ export interface Segment {wordEnd?: boolean;/** a voiceless stop or affricate's aspiration as its front end decides it (unset: English's rule) */aspirated?: boolean;+ /** how long an aspirated stop aspirates (s); unset: 50 ms */+ vot?: number;/** 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 ʲ) */