Make length limitation a bit more robust when creating utterances for browser speech engines

This commit is contained in:
Kovid Goyal 2021-08-04 22:49:11 +05:30
parent fe9f96b89a
commit d12f41a4a9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -127,19 +127,35 @@ class Client:
self.onevent = onevent self.onevent = onevent
buf = v'[]' buf = v'[]'
size = 0 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: for x in text_segments:
if jstype(x) is 'number': if jstype(x) is 'number':
# Currently the sad sack brosers dont support SSML # Currently the sad sack brosers dont support SSML
# https://github.com/WICG/speech-api/issues/37 # https://github.com/WICG/speech-api/issues/37
# buf.push('<mark name="' + x + '"/>') # buf.push()
buf.push('') # markup = '<mark name="' + x + '"/>'
continue
else: else:
buf.push(self.escape_for_xml(x)) markup = self.escape_for_xml(x)
size += buf[-1].length if markup.length > limit:
if size > 24000: commit()
buf = v'[]' while x.length:
size = 0 self.create_utterance(self.escape_for_xml(x[:4096]), True)
self.create_utterance(buf.join(''), True) x = x[4096:]
continue
if size + markup.length > limit:
commit()
buf.push(markup)
size += markup.length
text = buf.join('') text = buf.join('')
if text.length: if text.length:
self.create_utterance(text) self.create_utterance(text)