Skip to content

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

Rust

Korean: Hangul typed as conjoining jamo (NFD) sang nothing; join modern jamo into blocks (#133)

jamesondh-agents[bot]c6147d019889
diff --git a/crates/chimera/src/phonetics/korean.rs b/crates/chimera/src/phonetics/korean.rs
index bb6fc62..1e4da18 100644
--- a/crates/chimera/src/phonetics/korean.rs
+++ b/crates/chimera/src/phonetics/korean.rs
@@ -11,6 +11,10 @@
//! 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 [ɕ].
+//!
+//! A block may also be typed as its letters, conjoining jamo (decomposed text, NFD): modern ones
+//! are joined into the block first. The letters modern Korean dropped (ㆍ ㅿ ㆁ ㆆ) have no block,
+//! so a block with one isn't read, like any other script no front end reads.
use super::syllables::FrontEnd;
use super::{Segment, Syllable};
@@ -21,6 +25,41 @@ pub fn is_hangul(c: char) -> bool {
('\u{AC00}'..='\u{D7A3}').contains(&c)
}
+/// Modern conjoining jamo: an initial (L), a vowel (V) and a final (T), the letters a block is
+/// made of in decomposed text.
+const L: std::ops::RangeInclusive<u32> = 0x1100..=0x1112;
+const V: std::ops::RangeInclusive<u32> = 0x1161..=0x1175;
+const T: std::ops::RangeInclusive<u32> = 0x11A8..=0x11C2;
+
+fn is_modern_jamo(c: char) -> bool {
+ let c = c as u32;
+ L.contains(&c) || V.contains(&c) || T.contains(&c)
+}
+
+/// Modern conjoining jamo joined into blocks (Unicode's Hangul composition, as NFC does): L V
+/// is a block, and a block without a final takes a following T. Anything else is left as it was.
+fn compose(word: &str) -> String {
+ let mut out = String::with_capacity(word.len());
+ let mut last: Option<char> = None;
+ for c in word.chars() {
+ let (l, n) = (last.map(|c| c as u32), c as u32);
+ let joined = match l {
+ Some(l) if L.contains(&l) && V.contains(&n) => Some(BASE + ((l - 0x1100) * 21 + n - 0x1161) * 28),
+ Some(l) if is_hangul(char::from_u32(l).unwrap()) && (l - BASE).is_multiple_of(28) && T.contains(&n) => Some(l + n - 0x11A7),
+ _ => None,
+ };
+ match joined {
+ Some(b) => last = char::from_u32(b),
+ None => {
+ out.extend(last);
+ last = Some(c);
+ }
+ }
+ }
+ out.extend(last);
+ out
+}
+
/// Initial consonants in Unicode order. 'ㅇ' as an initial is silent.
const INITIALS: [char; 19] = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'];
@@ -236,7 +275,7 @@ fn coda_ipa(c: char) -> &'static str {
/// One word of Hangul (a run of blocks) → its sung syllables, the last marked as the word's end.
pub fn word_syllables(word: &str) -> Vec<Syllable> {
- let mut blocks: Vec<Block> = word.chars().filter(|&c| is_hangul(c)).map(decompose).collect();
+ let mut blocks: Vec<Block> = compose(word).chars().filter(|&c| is_hangul(c)).map(decompose).collect();
for i in 1..blocks.len() {
let (l, r) = blocks.split_at_mut(i);
join(l.last_mut().unwrap(), &mut r[0]);
@@ -280,7 +319,10 @@ pub struct Korean;
impl FrontEnd for Korean {
fn starts(&self, c: char) -> bool {
- is_hangul(c)
+ is_hangul(c) || L.contains(&(c as u32))
+ }
+ fn continues(&self, c: char) -> bool {
+ is_hangul(c) || is_modern_jamo(c)
}
fn word(&self, word: &str) -> Vec<Syllable> {
word_syllables(word)
diff --git a/crates/chimera/src/phonetics/syllables.rs b/crates/chimera/src/phonetics/syllables.rs
index 4c90db0..7a887e1 100644
--- a/crates/chimera/src/phonetics/syllables.rs
+++ b/crates/chimera/src/phonetics/syllables.rs
@@ -230,7 +230,7 @@ enum Token {
}
/// Words (with the index of the front end that claims them), melisma marks and phrase punctuation,
-/// in order. For English and Hangul this is
+/// in order. For English and precomposed Hangul this is
/// `text.match(/[A-Za-z'][A-Za-z'\-]*|[가-힣]+|(?<=\s|^)[~-](?=\s|$)|[.,;:!?\n]/g)`.
fn lyric_tokens(text: &str, front_ends: &[&dyn FrontEnd]) -> Vec<Token> {
let s: Vec<char> = text.chars().collect();
diff --git a/crates/chimera/tests/syllables.rs b/crates/chimera/tests/syllables.rs
index c8b9ca3..f5996b6 100644
--- a/crates/chimera/tests/syllables.rs
+++ b/crates/chimera/tests/syllables.rs
@@ -118,6 +118,21 @@ fn hangul_sings_one_syllable_per_block_with_korean_sound_rules() {
assert_eq!(sung("굳이"), "kʰ45u.dʑi"); // [구지]
}
+#[test]
+fn hangul_typed_as_jamo_sings_like_its_blocks() {
+ // decomposed text (NFD: each block as its letters, conjoining jamo) used to be skipped whole,
+ // so "한국 노래" sang nothing (#132)
+ let nfd = "\u{1112}\u{1161}\u{11AB}\u{1100}\u{116E}\u{11A8} \u{1102}\u{1169}\u{1105}\u{1162}";
+ assert_eq!(sung(nfd), sung("한국 노래"));
+ let texts: Vec<String> = lyrics_to_syllables(nfd, &word).into_iter().map(|s| s.text).collect();
+ assert_eq!(texts, ["한", "국", "노", "래"]);
+ // a block with its final typed as a letter, and the sound rules across jamo blocks
+ assert_eq!(sung("머\u{11A8}어"), sung("먹어"));
+ // a block with a letter modern Korean dropped (here ㆍ) has no block and isn't read; the
+ // blocks around it still are
+ assert_eq!(sung("사\u{1105}\u{119E}\u{11B7} 노래"), sung("사 노래"));
+}
+
#[test]
fn korean_stops_stay_three_ways_apart_word_initially() {
// word-initially all three are voiceless: aspirated long, lax briefly, tense not at all
diff --git a/docs/language-front-ends.md b/docs/language-front-ends.md
index ca1843a..ff1faa7 100644
--- a/docs/language-front-ends.md
+++ b/docs/language-front-ends.md
@@ -51,6 +51,10 @@ the front end applies standard Seoul sound rules: a coda moves onto a following
머거), codas neutralise, obstruents nasalise before nasals (국물 → 궁물), ㄴ and ㄹ meet as [ll],
ㅎ aspirates its neighbour (좋다 → 조타), and obstruents tense after an obstruent coda.
+A block typed as its letters (conjoining jamo, as decomposed text has them) is joined into the
+block first. The letters modern Korean dropped (ㆍ ㅿ ㆁ ㆆ) make no block, so a block with one is
+skipped like any script no front end reads (#132).
+
Korean's three stops are all voiceless at the start of a word. The front end tells them apart by
how long they aspirate (`vot`, voice onset time):