From 665013fdff5105d54018bb2a268c369a12779f99 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 2 Sep 2020 11:40:08 -0500 Subject: [PATCH] Split text sent to Tolk backend to account for some sort of length limit. Tolk seems to fail on strings larger than 325 characters in length. Here we: * Send any strings with 300 or fewer characters through directly. * For larger strings, split on whitespace boundaries, then create and send buffers of 300 or fewer characters. This may not handle internationalized text, and may not handle someone bombarding TTS with a giant word. PRs for either welcome. --- src/backends/tolk.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/backends/tolk.rs b/src/backends/tolk.rs index 370da65..c71cdfa 100644 --- a/src/backends/tolk.rs +++ b/src/backends/tolk.rs @@ -28,7 +28,25 @@ impl Backend for Tolk { fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> { trace!("speak({}, {})", text, interrupt); - self.0.speak(text, interrupt); + const BUFFER_LENGTH: usize = 300; + if text.len() <= BUFFER_LENGTH { + self.0.speak(text, interrupt); + } else { + if interrupt { + self.stop()?; + } + let tokens = text.split_whitespace(); + let mut buffer = String::new(); + for token in tokens { + if buffer.len() + token.len() > BUFFER_LENGTH { + self.0.speak(buffer, false); + buffer = String::new(); + } else { + buffer.push_str(token); + buffer.push(' '); + } + } + } Ok(()) }