library:ml: tag supertonic utterances with their language
- Commit
367869922c721ab16728ef2daa2a21e05e11f680- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-31 16:28 UTC (3 months ago)
- Parents
3684172
Diff
library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.kt
diff --git a/library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.kt b/library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.ktindex adc61300e5..e96f887ca2 100644--- a/library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.kt+++ b/library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.kt@@ -88,7 +88,7 @@ internal object MlNative { * No shape arguments: every Supertonic plan is utterance-shaped, so each net is re-recorded per * sentence rather than compiled once at a padded width. There is also no phoneme dictionary - * phoneme dictionary - the front end is [indexer], a flat 65,536-entry codepoint table, which is- * why [synthesizeSupertonic] insists on NFD.+ * why [synthesizeSupertonic] insists on NFKD. * * The four plans arrive as **file descriptors** rather than byte arrays. A `ByteArray` would * allocate the model three times over - the Java array, the `Vec<u8>` JNI hands Rust, and the@@ -127,17 +127,23 @@ internal object MlNative { external fun setSupertonicVoice(handle: Long, style: ByteArray): Boolean /**- * Synthesise [text] and return mono samples in `-1..1` at 44,100 Hz, or null on failure.+ * Synthesise [text] in [language] and return mono samples in `-1..1` at 44,100 Hz, or null on+ * failure. *- * [text] **must already be NFD**-decomposed - use `java.text.Normalizer.normalize(text,- * Form.NFD)`. The model has no precomposed accents: `é` is unmapped while `e` and the+ * [text] **must already be NFKD**-decomposed - use `java.text.Normalizer.normalize(text,+ * Form.NFKD)`. The model has no precomposed accents: `é` is unmapped while `e` and the * combining acute are both first-class tokens, so precomposed text silently loses * characters. Doing the decomposition natively would mean carrying Unicode tables in the * APK when the platform already has them. *+ * [language] is the ISO-639-1 code, or `na` for one the model does not list. It is not a+ * hint. Supertonic 3 is the multilingual model and was trained with every utterance wrapped+ * in a `<en>...</en>` tag, so the wrong code reads the text in the wrong language and no+ * code at all produces fluent-sounding non-words rather than an error.+ * * Two calls with the same text differ, as flow matching starts from a sampled latent. */- external fun synthesizeSupertonic(handle: Long, text: String): FloatArray?+ external fun synthesizeSupertonic(handle: Long, text: String, language: String): FloatArray? /** * Bring up SMaLL-100 from its one `.maml` and its tokenizer table. Returns 0 on failure.
library/ml/src/main/java/com/vayunmathur/library/ml/SupertonicSynthesizer.kt
diff --git a/library/ml/src/main/java/com/vayunmathur/library/ml/SupertonicSynthesizer.kt b/library/ml/src/main/java/com/vayunmathur/library/ml/SupertonicSynthesizer.ktindex c5426118f5..0dfad576e3 100644--- a/library/ml/src/main/java/com/vayunmathur/library/ml/SupertonicSynthesizer.kt+++ b/library/ml/src/main/java/com/vayunmathur/library/ml/SupertonicSynthesizer.kt@@ -99,7 +99,11 @@ class SupertonicSynthesizer private constructor( } /**- * Synthesise [text] and return mono samples in `-1..1` at [sampleRate].+ * Synthesise [text] in [language] and return mono samples in `-1..1` at [sampleRate].+ *+ * [language] is the ISO-639-1 code the model should read in, or `na` for one it does not+ * list. Supertonic 3 is the multilingual model and was trained with the language tag always+ * present; getting it wrong is silent, so this has no default. * * Returns an empty array when there is nothing in the model's vocabulary, when the engine is * unavailable, or when the pass failed — all three are "no audio" to a caller, and the reason@@ -110,10 +114,10 @@ class SupertonicSynthesizer private constructor( * * Two calls with the same text differ, as flow matching starts from a sampled latent. */- fun synthesize(text: String): FloatArray {+ fun synthesize(text: String, language: String): FloatArray { if (handle == 0L || text.isBlank()) return FloatArray(0)- val decomposed = Normalizer.normalize(text, Normalizer.Form.NFD)- return MlNative.synthesizeSupertonic(handle, decomposed) ?: FloatArray(0)+ val decomposed = Normalizer.normalize(text, Normalizer.Form.NFKD)+ return MlNative.synthesizeSupertonic(handle, decomposed, language) ?: FloatArray(0) } /** Free all four networks. Idempotent. */
library/ml/src/main/rust/src/bridge.rs
diff --git a/library/ml/src/main/rust/src/bridge.rs b/library/ml/src/main/rust/src/bridge.rsindex 1bf05271b2..467509e4cf 100644--- a/library/ml/src/main/rust/src/bridge.rs+++ b/library/ml/src/main/rust/src/bridge.rs@@ -949,13 +949,17 @@ pub unsafe extern "system" fn Java_com_vayunmathur_library_ml_MlNative_destroySu drop(unsafe { Box::from_raw(handle as *mut SupertonicHandle) }); } -/// Synthesise `text` and return the waveform, or null on failure.+/// Synthesise `text` in `language` and return the waveform, or null on failure. ///-/// `text` must already be **NFD**-decomposed. That is the Kotlin side's job, through+/// `text` must already be **NFKD**-decomposed. That is the Kotlin side's job, through /// `java.text.Normalizer`, because the model has no precomposed accents and doing the /// decomposition here would mean carrying Unicode tables in the APK — see /// [`supertonic::to_ids`]. ///+/// `language` is the ISO-639-1 code the model should read in, or `na` for one it does not list.+/// It is not a hint: Supertonic 3 is the multilingual model and was trained with the tag always+/// present, so a wrong or missing one produces fluent-sounding non-words rather than an error.+/// /// The samples are mono `-1..1` at 44,100 Hz. Two calls with the same text differ: flow matching /// starts from a sampled latent, which it is meant to. ///@@ -969,6 +973,7 @@ pub unsafe extern "system" fn Java_com_vayunmathur_library_ml_MlNative_synthesiz _class: JClass<'l>, handle: jlong, text: JString<'l>,+ language: JString<'l>, ) -> jfloatArray { let null = std::ptr::null_mut(); if handle == 0 {@@ -984,7 +989,14 @@ pub unsafe extern "system" fn Java_com_vayunmathur_library_ml_MlNative_synthesiz return null; } };- match speak_supertonic(state, &words) {+ let code: String = match env.get_string(&language) {+ Ok(found) => found.into(),+ Err(e) => {+ log(&format!("cannot read the language: {e}"));+ return null;+ }+ };+ match speak_supertonic(state, &words, &code) { Ok(samples) => match new_float_array(&mut env, &samples) { Ok(array) => array, Err(e) => {@@ -1000,7 +1012,11 @@ pub unsafe extern "system" fn Java_com_vayunmathur_library_ml_MlNative_synthesiz } /// The whole pipeline for one utterance.-fn speak_supertonic(state: &mut SupertonicHandle, text: &str) -> Result<Vec<f32>, String> {+fn speak_supertonic(+ state: &mut SupertonicHandle,+ text: &str,+ language: &str,+) -> Result<Vec<f32>, String> { let SupertonicHandle { duration, text: encoder,@@ -1015,7 +1031,7 @@ fn speak_supertonic(state: &mut SupertonicHandle, text: &str) -> Result<Vec<f32> // frame count, so the generator has to be reachable from a `Fn` rather than pre-drawn. let noise = std::cell::RefCell::new(rng); let mut nets = SupertonicNets { duration, text: encoder, sampler, vocoder };- supertonic::synthesise(&mut nets, conditioning, indexer, voice, text, &|count| {+ supertonic::synthesise(&mut nets, conditioning, indexer, voice, text, language, &|count| { noise.borrow_mut().normal(count) }) }
library/ml/src/main/rust/src/nets/reference.rs
diff --git a/library/ml/src/main/rust/src/nets/reference.rs b/library/ml/src/main/rust/src/nets/reference.rsindex b32c806db9..0578f88cd4 100644--- a/library/ml/src/main/rust/src/nets/reference.rs+++ b/library/ml/src/main/rust/src/nets/reference.rs@@ -3501,11 +3501,14 @@ mod tests { .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])) .collect(); let mut nets = Interpreted { dp: &dp, ttl: &ttl, ve: &ve, voc: &voc };- let samples = post::synthesise(&mut nets, &conditioning, &indexer, &voice, &text, &|count| {+ let language = std::env::var("SUPERTONIC_LANG").unwrap_or_else(|_| "en".into());+ let draw = |count: usize| { assert_eq!(count, drawn.len(), "the driver drew a latent of a different size"); drawn.clone()- })- .expect("the utterance synthesises");+ };+ let samples =+ post::synthesise(&mut nets, &conditioning, &indexer, &voice, &text, &language, &draw)+ .expect("the utterance synthesises"); println!("supertonic utterance: {} samples", samples.len()); write(&dir.join("ours.f32"), &samples); }
library/ml/src/main/rust/src/nets/supertonic_duration.rs
diff --git a/library/ml/src/main/rust/src/nets/supertonic_duration.rs b/library/ml/src/main/rust/src/nets/supertonic_duration.rsindex b52c089604..8306b9d91c 100644--- a/library/ml/src/main/rust/src/nets/supertonic_duration.rs+++ b/library/ml/src/main/rust/src/nets/supertonic_duration.rs@@ -129,10 +129,14 @@ pub const INT8_CONVS: usize = BLOCKS * 2 + ATTN_LAYERS * 6; /// /// One frame is [`super::supertonic_vocoder::SAMPLES_PER_FRAME`] samples at /// [`super::supertonic_vocoder::SAMPLE_RATE`]. Verified against onnxruntime at 54, 55 and 108.+///+/// `ceil`, not `round`, which is what `supertonic`'s Python SDK does: a partial frame still has+/// speech in it, and rounding down truncates the tail. Rounding agrees with the SDK on most+/// lengths and is one frame short on the rest, so a fixture will not catch it. pub fn latent_frames(seconds: f32) -> u32 { let per_frame = super::supertonic_vocoder::SAMPLES_PER_FRAME as f32; let frames = seconds * super::supertonic_vocoder::SAMPLE_RATE as f32 / per_frame;- frames.round().max(1.0) as u32+ frames.ceil().max(1.0) as u32 } /// The export's trailing `Exp`, on the plan's one output value.
library/ml/src/main/rust/src/post/supertonic.rs
diff --git a/library/ml/src/main/rust/src/post/supertonic.rs b/library/ml/src/main/rust/src/post/supertonic.rsindex cc0be1fa1f..dacc7936c2 100644--- a/library/ml/src/main/rust/src/post/supertonic.rs+++ b/library/ml/src/main/rust/src/post/supertonic.rs@@ -158,26 +158,41 @@ pub fn style_keys(weights: Reader, conditional: bool) -> Result<Vec<f32>, String /// Entries in the codepoint table: every code unit of the Basic Multilingual Plane. pub const INDEXER_ENTRIES: usize = 65_536; -/// Character ids for text that is **already NFD-decomposed**, dropping what the model has no-/// token for.+/// Character ids for text that is **already NFKD-decomposed**, wrapped in its language tag and+/// dropping what the model has no token for. /// /// `indexer` is the voice bundle's `unicode_indexer.bin`: [`INDEXER_ENTRIES`] little-endian /// `int16`, one per BMP codepoint, `-1` where there is no token. 8,321 of them are mapped. ///-/// # NFD is the caller's job, and it is not optional+/// # The language tag is not optional, and getting it wrong is silent+///+/// Supertonic 3 is the 31-language model, and it was trained with every utterance wrapped as+/// `<en>text</en>`. `language` is the ISO-639-1 code, or `na` for one the model does not list.+///+/// The tag is not a special token with a row of its own — it is literally the characters `<`,+/// `e`, `n` and `>` through this same table, which is why it needs no re-export. That is also+/// what makes omitting it so expensive to find: the ids stay in range, every net still matches+/// onnxruntime to five decimal places, and the model reads the sentence in confident,+/// correctly-timed gibberish. The symptom is fluent-sounding speech that is not words. Untagged,+/// two noise draws of one sentence agreed spectrally at 0.36; tagged, 0.70 — a conditioned flow+/// says the same thing whatever the noise, and that ratio is the cheapest check that this+/// argument is still being threaded through.+///+/// # NFKD is the caller's job, and it is not optional either /// /// The model has no precomposed accents: `U+00E9` is unmapped while `e` and `U+0301` are both /// first-class tokens, and Hangul syllables map only through the Jamo block. So `café`, `über`,-/// `niño`, `안녕` and `привет` index completely under NFD and partially or not at all otherwise.-/// The Kotlin side calls `java.text.Normalizer.normalize(text, Form.NFD)`, which is a platform+/// `niño`, `안녕` and `привет` index completely under NFKD and partially or not at all otherwise.+/// The Kotlin side calls `java.text.Normalizer.normalize(text, Form.NFKD)`, which is a platform /// API and free; doing it here would mean carrying Unicode decomposition tables in the APK. /// /// # Unmapped codepoints are dropped, not substituted /// /// There is no unknown token to substitute. Dropping loses a character; mapping to something /// else would mispronounce it, and mapping past the table would read the sentence token — see-/// [`crate::nets::supertonic_duration`].-pub fn to_ids(indexer: &[u8], text: &str) -> Result<Vec<u32>, String> {+/// [`crate::nets::supertonic_duration`]. Text of which *nothing* maps is refused rather than+/// synthesised as an empty tag pair.+pub fn to_ids(indexer: &[u8], text: &str, language: &str) -> Result<Vec<u32>, String> { if indexer.len() != INDEXER_ENTRIES * 2 { return Err(format!( "a codepoint table of {} bytes, not {}",@@ -185,29 +200,56 @@ pub fn to_ids(indexer: &[u8], text: &str) -> Result<Vec<u32>, String> { INDEXER_ENTRIES * 2 )); }- let mut ids = Vec::new();- for codepoint in text.chars() {- let index = codepoint as usize;- // Astral-plane characters — emoji, most CJK extensions — are outside the table- // entirely rather than mapped to -1.- if index >= INDEXER_ENTRIES {- continue;- }- let at = index * 2;- let token = i16::from_le_bytes([indexer[at], indexer[at + 1]]);- if token >= 0 {- ids.push(token as u32);- }+ // Every code the model knows is two ASCII lowercase letters, `na` included. Checked because+ // a malformed tag indexes cleanly and then mispronounces the whole utterance.+ if language.len() != 2 || !language.bytes().all(|b| b.is_ascii_lowercase()) {+ return Err(format!("a language code of {language:?}, not two lowercase letters")); }+ let index = |text: &str| {+ text.chars()+ .filter_map(|codepoint| {+ let entry = codepoint as usize;+ // Astral-plane characters — emoji, most CJK extensions — are outside the table+ // entirely rather than mapped to -1.+ if entry >= INDEXER_ENTRIES {+ return None;+ }+ let at = entry * 2;+ let token = i16::from_le_bytes([indexer[at], indexer[at + 1]]);+ (token >= 0).then_some(token as u32)+ })+ .collect::<Vec<u32>>()+ };++ // The tag is indexed apart from the text so that text which maps to nothing is still refused.+ // Tagged, the ids would never be empty, and the model would read out an empty utterance.+ let body = index(text);+ if body.is_empty() {+ return Err("nothing in this text is in the model's vocabulary".into());+ }+ let mut ids = index(&format!("<{language}>"));+ ids.extend(body);+ ids.extend(index(&format!("</{language}>"))); Ok(ids) } +/// The speed the SDK reads at by default, which the predicted duration is divided by.+///+/// `supertonic`'s Python SDK defaults `synthesize(speed=1.05)` and calls values near it "more+/// natural speech"; the duration predictor is trained against un-sped reference audio, so+/// reading its answer literally is a 5% drawl. It divides the seconds, so a *larger* speed is a+/// *shorter* utterance.+pub const SPEED: f32 = 1.05;+ /// Sampler steps per utterance. /// /// Measured, not chosen: at four or fewer the waveform's peak goes above 1.0, sixteen is the floor /// for a stable level, and audio at 16 correlates with audio at 32 at only 0.883. There is no /// cheap-steps escape hatch here, which with two guidance branches per step means 32 passes of /// [`crate::nets::supertonic_sampler`] for one sentence.+///+/// The 0.883 is not a quality argument either way. It was once read as one, and 32 was tried+/// against speech that turned out to be garbled for an unrelated reason — see [`to_ids`]. pub const STEPS: u32 = 16; /// The GPU stages, as a trait, so the sequencing below is host-testable against stubs.@@ -313,16 +355,16 @@ impl Voice { /// Synthesise one utterance. ///-/// `text` must already be NFD; see [`to_ids`]. `noise` supplies the flow's starting latent, one-/// standard normal per value — flow matching is meant to vary between calls, so the caller seeds-/// it from the clock.+/// `text` must already be NFKD; `language` is its ISO-639-1 code. See [`to_ids`] for why both+/// matter. `noise` supplies the flow's starting latent, one standard normal per value — flow+/// matching is meant to vary between calls, so the caller seeds it from the clock. /// /// ```text-/// text -> ids to_ids, over the bundle's codepoint table-/// ids + style_dp -> seconds the duration predictor, then exp-/// seconds -> frames round(seconds * 44100 / 3072)+/// text + language -> ids to_ids, over the bundle's codepoint table+/// ids + style_dp -> seconds the duration predictor, then exp, then / SPEED+/// seconds -> frames ceil(seconds * 44100 / 3072) /// ids + style_ttl -> text_emb the text encoder-/// noise + text_emb -> latent the sampler, 16 steps of two guidance branches+/// noise + text_emb -> latent the sampler, [`STEPS`] steps of two guidance branches /// latent -> waveform the vocoder /// ``` pub fn synthesise(@@ -331,12 +373,10 @@ pub fn synthesise( indexer: &[u8], voice: &Voice, text: &str,+ language: &str, noise: &dyn Fn(usize) -> Vec<f32>, ) -> Result<Vec<f32>, String> {- let ids = to_ids(indexer, text)?;- if ids.is_empty() {- return Err("nothing in this text is in the model's vocabulary".into());- }+ let ids = to_ids(indexer, text, language)?; let chars = ids.len() as u32; // The duration predictor's sequence leads with the sentence token; the text encoder's does@@ -345,7 +385,7 @@ pub fn synthesise( with_token.push(duration_net::SENTENCE_TOKEN); with_token.extend_from_slice(&ids); let log_seconds = stages.duration(&embed_lanes(&with_token), &voice.duration)?;- let frames = duration_net::latent_frames(duration_net::seconds(log_seconds));+ let frames = duration_net::latent_frames(duration_net::seconds(log_seconds) / SPEED); let conditioning_text = stages.text(&embed_lanes(&ids), &voice.text)?; @@ -420,26 +460,55 @@ pub fn step( mod tests { use super::*; - #[test]- fn the_indexer_drops_what_it_cannot_map() {- // A table where only 'a' (0x61) and the combining acute (0x301) are mapped. 'z' and an- // emoji outside the BMP both disappear rather than becoming some other character.+ /// A table where only the characters named are mapped; everything else is `-1`.+ fn table_of(mapped: &[(char, i16)]) -> Vec<u8> { let mut table = vec![0u8; INDEXER_ENTRIES * 2];- for (codepoint, token) in [(0x61usize, 60i16), (0x301, 146)] {- table[codepoint * 2..codepoint * 2 + 2].copy_from_slice(&token.to_le_bytes());- } for entry in 0..INDEXER_ENTRIES {- if entry != 0x61 && entry != 0x301 {- table[entry * 2..entry * 2 + 2].copy_from_slice(&(-1i16).to_le_bytes());- }+ table[entry * 2..entry * 2 + 2].copy_from_slice(&(-1i16).to_le_bytes()); }- let got = to_ids(&table, "a\u{301}z\u{1F600}a").expect("indexes");+ for &(codepoint, token) in mapped {+ let at = codepoint as usize * 2;+ table[at..at + 2].copy_from_slice(&token.to_le_bytes());+ }+ table+ }++ #[test]+ fn the_indexer_drops_what_it_cannot_map() {+ // Only 'a' and the combining acute are mapped. 'z' and an emoji outside the BMP both+ // disappear rather than becoming some other character. The tag's characters are unmapped+ // here too, so this says nothing about the tag - `the_indexer_wraps_the_text_in_its_+ // language_tag` does that.+ let table = table_of(&[('a', 60), ('\u{301}', 146)]);+ let got = to_ids(&table, "a\u{301}z\u{1F600}a", "en").expect("indexes"); assert_eq!(got, vec![60, 146, 60]); } + #[test]+ fn the_indexer_wraps_the_text_in_its_language_tag() {+ // The tag is ordinary characters through the ordinary table, so it has to come out as+ // ids on both sides of the text. Omitting it costs nothing detectable downstream, which+ // is why it is asserted here.+ let table = table_of(&[('<', 1), ('>', 2), ('/', 3), ('e', 4), ('n', 5), ('a', 6)]);+ assert_eq!(+ to_ids(&table, "a", "en").expect("indexes"),+ vec![1, 4, 5, 2, 6, 1, 3, 4, 5, 2],+ "<en>a</en>"+ );+ }++ #[test]+ fn the_indexer_refuses_a_language_that_is_not_a_two_letter_code() {+ let table = table_of(&[('a', 6)]);+ for bad in ["", "e", "eng", "EN", "e1"] {+ let error = to_ids(&table, "a", bad).expect_err("a bad code");+ assert!(error.contains("language code"), "{bad:?}: {error}");+ }+ }+ #[test] fn the_indexer_refuses_a_table_of_the_wrong_size() {- let error = to_ids(&[0u8; 16], "a").expect_err("a short table");+ let error = to_ids(&[0u8; 16], "a", "en").expect_err("a short table"); assert!(error.contains("codepoint table"), "{error}"); } @@ -532,12 +601,15 @@ mod tests { } } - /// A codepoint table mapping the ASCII letters to themselves and nothing else.+ /// A codepoint table mapping the ASCII letters to themselves, plus the three punctuation+ /// marks the language tag is spelled with, and nothing else. fn letters() -> Vec<u8> { let mut table = vec![0u8; INDEXER_ENTRIES * 2]; for entry in 0..INDEXER_ENTRIES { let token = if (b'a' as usize..=b'z' as usize).contains(&entry) { (entry - b'a' as usize + 1) as i16+ } else if entry < 128 && [b'<', b'>', b'/'].contains(&(entry as u8)) {+ 27 } else { -1 };@@ -559,10 +631,11 @@ mod tests { #[test] fn the_pipeline_runs_the_four_nets_in_order_at_the_predicted_length() {- // "hello" is five mapped letters. At one second the duration predictor's answer becomes- // round(44100 / 3072) = 14 frames, and the vocoder emits 3072 samples a frame.+ // "hello" is five mapped letters, and `<en>` and `</en>` are nine more. At one second the+ // duration predictor's answer becomes ceil(44100 / 1.05 / 3072) = 14 frames, and the+ // vocoder emits 3072 samples a frame. let mut stages =- Recording { seconds: 1.0, chars: 5, frames: 14, sampler_calls: 0, latents: Vec::new(), vocoded_frames: Vec::new() };+ Recording { seconds: 1.0, chars: 14, frames: 14, sampler_calls: 0, latents: Vec::new(), vocoded_frames: Vec::new() }; let voice = Voice { duration: vec![0.1; 128], text: vec![0.2; 256 * 50] }; let samples = synthesise( &mut stages,@@ -570,6 +643,7 @@ mod tests { &letters(), &voice, "hello",+ "en", &|count| vec![0.0; count], ) .expect("synthesises");@@ -585,7 +659,7 @@ mod tests { // the same latent. A `step` that scaled or reordered would show here rather than as // quiet noise on a device. let mut stages =- Recording { seconds: 1.0, chars: 5, frames: 14, sampler_calls: 0, latents: Vec::new(), vocoded_frames: Vec::new() };+ Recording { seconds: 1.0, chars: 14, frames: 14, sampler_calls: 0, latents: Vec::new(), vocoded_frames: Vec::new() }; let voice = Voice { duration: vec![0.1; 128], text: vec![0.2; 256 * 50] }; synthesise( &mut stages,@@ -593,6 +667,7 @@ mod tests { &letters(), &voice, "hello",+ "en", &|count| (0..count).map(|i| i as f32 * 0.001).collect(), ) .expect("synthesises");@@ -605,7 +680,8 @@ mod tests { #[test] fn text_with_nothing_in_the_vocabulary_is_refused() { // Rather than synthesising silence, or a plan over zero characters that the nets refuse- // with a message about frames.+ // with a message about frames. The language tag maps in `letters()`, so this also covers+ // the tag alone not being mistaken for content. let mut stages = Recording { seconds: 1.0, chars: 0, frames: 1, sampler_calls: 0, latents: Vec::new(), vocoded_frames: Vec::new() }; let voice = Voice { duration: vec![0.1; 128], text: vec![0.2; 256 * 50] };@@ -615,6 +691,7 @@ mod tests { &letters(), &voice, "12345",+ "en", &|count| vec![0.0; count], ) .expect_err("no vocabulary");
speech/src/main/java/com/vayunmathur/speech/platform/SupertonicEngine.kt
diff --git a/speech/src/main/java/com/vayunmathur/speech/platform/SupertonicEngine.kt b/speech/src/main/java/com/vayunmathur/speech/platform/SupertonicEngine.ktindex 8612d6a85d..25d783cb5a 100644--- a/speech/src/main/java/com/vayunmathur/speech/platform/SupertonicEngine.kt+++ b/speech/src/main/java/com/vayunmathur/speech/platform/SupertonicEngine.kt@@ -70,6 +70,8 @@ class SupertonicEngine(private val context: Context) { * audio track and blocks while it plays, so holding the handle across it would make [close] — * which arrives on the main thread from `onDestroy` — wait out a whole utterance. *+ * [language] is the ISO-639-1 code the model reads in; see [SupertonicSynthesizer.synthesize].+ * * There is no `speed`: [SupertonicSynthesizer.synthesize] takes none, and the only ways to fake * one on the host are to resample the PCM, which shifts the pitch, or to time-stretch it, which * is a signal-processing feature rather than part of a port. Rate control therefore does not@@ -77,11 +79,11 @@ class SupertonicEngine(private val context: Context) { * scaling the duration predictor's frame count natively, which is where Piper's `length_scale` * did it too. */- fun synthesize(text: String, onChunk: (FloatArray) -> Boolean): Boolean {+ fun synthesize(text: String, language: String, onChunk: (FloatArray) -> Boolean): Boolean { val samples = synchronized(lock) { val engine = ensure() ?: return false try {- engine.synthesize(text)+ engine.synthesize(text, language) } catch (e: Throwable) { Log.e(TAG, "synthesis failed", e) return false
speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt
diff --git a/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt b/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.ktindex bb62446cbe..677a4b47a7 100644--- a/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt+++ b/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt@@ -189,7 +189,7 @@ class SupertonicTtsService : TextToSpeechService() { } val maxBytes = callback.maxBufferSize- val ok = engine.synthesize(text) { samples ->+ val ok = engine.synthesize(text, language.code) { samples -> if (stopped) return@synthesize false val pcm = floatsToPcm16(samples) var offset = 0