Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 105 additions & 2 deletions crates/buzz-voice/src/pocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ pub fn load_text_to_speech(model_dir: &str) -> Result<PocketTts, String> {
}

impl PocketTts {
/// Split text into synthesis units that satisfy the bundle's exact
/// 50-token input limit.
/// Split text into model-safe synthesis units that satisfy the bundle's
/// exact 50-token input limit, packing sentences whenever they fit.
pub fn split_text_into_chunks(&self, text: &str) -> Result<Vec<String>, String> {
let Some(prepared) = prepare_april_prompt(text) else {
return Ok(Vec::new());
Expand All @@ -110,6 +110,23 @@ impl PocketTts {
.split_prompt(&prepared)
}

/// Split text into ordered playback units, keeping the first sentence
/// separate so it reaches synthesis before the remainder is packed.
///
/// Units are contiguous substrings of the prepared model prompt and may
/// retain boundary whitespace. Concatenating them with `chunks.concat()`
/// reconstructs that prompt exactly, and each unit's prepared token count
/// is at most 50.
pub fn split_text_for_playback(&self, text: &str) -> Result<Vec<String>, String> {
let Some(prepared) = prepare_april_prompt(text) else {
return Ok(Vec::new());
};
self.inner
.lock()
.map_err(|_| "Pocket TTS engine lock poisoned".to_string())?
.split_playback_prompt(&prepared)
}

/// Synthesize text with the supplied reference voice.
///
/// Pocket detects language from text and this model uses one synthesis
Expand Down Expand Up @@ -188,6 +205,92 @@ mod tests {
.any(|artifact| artifact.filename == "flow_lm_main.onnx"));
}

/// Which splitter each production function delegates to, across the whole
/// file rather than one hand-picked window.
///
/// A wrong delegation can reinstate either shipped defect in one token:
/// removing first-sentence priority from playback, or re-isolating sentence
/// one inside units that already fit. Asserting the whole map means a new
/// delegation must be declared here to compile green.
fn splitter_delegations(source: &str) -> Vec<(String, Vec<String>)> {
let production = source
.split_once("\n#[cfg(test)]")
.map_or(source, |(production, _)| production);
// Scan code only. Prose cannot call a splitter, but it can contain
// ` fn `, which would end a body early and hide a call after it, and it
// can name a splitter, which would report a call the code never makes.
let production: String = production
.lines()
.map(|line| line.split_once("//").map_or(line, |(code, _)| code))
.collect::<Vec<_>>()
.join("\n");
let mut out = Vec::new();
let mut rest = production.as_str();
while let Some((_, after)) = rest.split_once(" fn ") {
let (name, body) = after
.split_once('(')
.expect("a function signature has an argument list");
// End at this function's own closing brace, not at the next ` fn `:
// a body provably stops where its braces balance, so no later
// function's calls are attributed here and none of this one's are
// dropped.
let inner = body.split_once('{').map_or("", |(_, inner)| inner);
let mut depth = 1usize;
let body = inner
.char_indices()
.find(|&(_, ch)| {
depth = match ch {
'{' => depth + 1,
'}' => depth - 1,
_ => depth,
};
depth == 0
})
.map_or(inner, |(end, _)| &inner[..end]);
let mut calls = Vec::new();
// Check the isolating spelling first: ".split_prompt(" is a
// substring of neither, but a naive contains() on the shorter name
// would also match the longer one.
for _ in 0..body.matches(".split_playback_prompt(").count() {
calls.push("split_playback_prompt".to_string());
}
let plain = body.matches(".split_prompt(").count();
for _ in 0..plain {
calls.push("split_prompt".to_string());
}
if !calls.is_empty() {
out.push((name.trim().to_string(), calls));
}
rest = after;
}
out
}

#[test]
fn every_production_splitter_delegation_is_declared() {
let source = include_str!("pocket.rs");
let actual = splitter_delegations(source);
let expected: Vec<(String, Vec<String>)> = vec![
// Model units: pack sentences, never isolate.
("split_text_into_chunks".into(), vec!["split_prompt".into()]),
// Playback units: isolate sentence one for time-to-first-audio.
(
"split_text_for_playback".into(),
vec!["split_playback_prompt".into()],
),
// Synthesis receives an already-packed unit: re-isolating here
// re-adds the per-sentence seam this PR removes.
("synth_chunk".into(), vec!["split_prompt".into()]),
("synth_chunk_streaming".into(), vec!["split_prompt".into()]),
];
assert_eq!(
actual, expected,
"a production function changed which splitter it calls (or a new \
one appeared); isolating outside split_text_for_playback delays \
first audio, packing inside it removes the guarantee"
);
}

#[test]
#[ignore = "requires BUZZ_POCKET_TEST_MODEL_DIR"]
fn production_api_emits_non_silent_april_int8_pcm() {
Expand Down
Loading
Loading