the ultimate vocal synthesizer - sing, speak, beatbox, and more
Rust
Router: a front end may decline a word it scanned, and the others read it; full-width phrase punctuation
Joshua Pham520b398b6727
diff --git a/crates/chimera/src/phonetics/syllables.rs b/crates/chimera/src/phonetics/syllables.rsindex cc160bc..ca99a47 100644--- a/crates/chimera/src/phonetics/syllables.rs+++ b/crates/chimera/src/phonetics/syllables.rs@@ -163,7 +163,8 @@ impl<F: Fn(&str, bool) -> Vec<Segment>> WordSource for F {}fn is_lyric_punct(c: char) -> bool {- matches!(c, '.' | ',' | ';' | ':' | '!' | '?' | '\n')+ // ASCII, and the full-width forms Chinese and Japanese lyrics use+ matches!(c, '.' | ',' | ';' | ':' | '!' | '?' | '\n' | ',' | '。' | '!' | '?' | ';' | ':' | '、')}/// A language front end: claims the words written in its script and turns each into sung syllables@@ -175,6 +176,12 @@ pub trait FrontEnd {fn continues(&self, c: char) -> bool {self.starts(c)}+ /// Whether this front end reads the whole `word` it scanned (default: yes). A front end that+ /// shares a script with another says no to what isn't its own, and the next one gets the word+ /// (pinyin and English share the Latin alphabet).+ fn claims(&self, _word: &str) -> bool {+ true+ }/// One word → its syllables (the router marks the last as the word's end).fn word(&self, word: &str) -> Vec<Syllable>;/// The same, knowing the word of this front end just before it in the phrase, for a language@@ -228,15 +235,33 @@ fn lyric_tokens(text: &str, front_ends: &[&dyn FrontEnd]) -> Vec<Token> {let s: Vec<char> = text.chars().collect();let mut out = Vec::new();let mut i = 0;+ // where each front end may claim again after declining a word (the end of that word)+ let mut barred = vec![0usize; front_ends.len()];while i < s.len() {let c = s[i];- if let Some(k) = front_ends.iter().position(|f| f.starts(c)) {- let start = i;- i += 1;- while i < s.len() && front_ends[k].continues(s[i]) {- i += 1;+ // the first front end that starts here and claims the word it scans. A front end that+ // declines a word (pinyin and English share the Latin alphabet) can't claim a piece of it+ // either: the others read that span as they would without it ("café" stays English's "caf",+ // not "caf" and a pinyin "é"), and nothing in it is lost ("Zürich" is "Z" "rich", as on main)+ let mut word = None;+ for (k, f) in front_ends.iter().enumerate() {+ if !f.starts(c) || barred[k] > i {+ continue;}- out.push(Token::Word(k, s[start..i].iter().collect()));+ let mut j = i + 1;+ while j < s.len() && f.continues(s[j]) {+ j += 1;+ }+ let w: String = s[i..j].iter().collect();+ if f.claims(&w) {+ word = Some((k, j, w));+ break;+ }+ barred[k] = j;+ }+ if let Some((k, j, w)) = word {+ out.push(Token::Word(k, w));+ i = j;continue;}let melisma = (c == '~' || c == '-') && (i == 0 || js_is_space(s[i - 1])) && (i + 1 == s.len() || js_is_space(s[i + 1]));diff --git a/crates/chimera/tests/syllables.rs b/crates/chimera/tests/syllables.rsindex 36bb268..ce92bef 100644--- a/crates/chimera/tests/syllables.rs+++ b/crates/chimera/tests/syllables.rs@@ -151,3 +151,30 @@ fn hand_written_syllables_may_leave_out_word_and_phrase_ends() {assert!(!s.word_end && !s.phrase_end);assert_eq!(s.onset[0].vot, Some(0.09));}++/// A front end that shares English's letters but reads only words in capitals (as pinyin reads+/// only words with tones).+struct Shouting;+impl chimera::phonetics::syllables::FrontEnd for Shouting {+ fn starts(&self, c: char) -> bool {+ c.is_ascii_alphabetic()+ }+ fn claims(&self, word: &str) -> bool {+ word.chars().all(|c| c.is_ascii_uppercase())+ }+ fn word(&self, word: &str) -> Vec<chimera::phonetics::Syllable> {+ vec![chimera::phonetics::Syllable { text: format!("!{word}"), nucleus: Segment::new("a"), ..Default::default() }]+ }+}++#[test]+fn a_declined_word_goes_to_the_next_front_end_and_nothing_is_lost() {+ use chimera::phonetics::syllables::{English, read_lyrics};+ let g2p = chimera::phonetics::english::EnglishG2P::new();+ let texts: Vec<String> = read_lyrics("HEY there LOUD", &[&Shouting, &English(&g2p)]).into_iter().map(|s| s.text).collect();+ assert_eq!(texts, ["!HEY", "there", "!LOUD"]);+ // the front end that declined a word can't claim a piece of it: "MIXed" is English's, not a+ // shouted "MIX" and "ed"+ let texts: Vec<String> = read_lyrics("MIXed", &[&Shouting, &English(&g2p)]).into_iter().map(|s| s.text).collect();+ assert!(texts.iter().all(|t| !t.starts_with('!')), "{texts:?}");+}