From d12f41a4a9dcb61f942288d4643bd77015732607 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 4 Aug 2021 22:49:11 +0530 Subject: [PATCH] Make length limitation a bit more robust when creating utterances for browser speech engines --- src/pyj/read_book/tts.pyj | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/pyj/read_book/tts.pyj b/src/pyj/read_book/tts.pyj index 07f7636e36..457d86e6f5 100644 --- a/src/pyj/read_book/tts.pyj +++ b/src/pyj/read_book/tts.pyj @@ -127,19 +127,35 @@ class Client: self.onevent = onevent buf = v'[]' size = 0 + limit = 24000 + + def commit(): + nonlocal buf, size + text = buf.join('') + if text.length: + self.create_utterance(text, True) + buf = v'[]' + size = 0 + for x in text_segments: if jstype(x) is 'number': # Currently the sad sack brosers dont support SSML # https://github.com/WICG/speech-api/issues/37 - # buf.push('') - buf.push('') + # buf.push() + # markup = '' + continue else: - buf.push(self.escape_for_xml(x)) - size += buf[-1].length - if size > 24000: - buf = v'[]' - size = 0 - self.create_utterance(buf.join(''), True) + markup = self.escape_for_xml(x) + if markup.length > limit: + commit() + while x.length: + self.create_utterance(self.escape_for_xml(x[:4096]), True) + x = x[4096:] + continue + if size + markup.length > limit: + commit() + buf.push(markup) + size += markup.length text = buf.join('') if text.length: self.create_utterance(text)