Voice: preserve emitted whitespace in the sentence splitter - #4693
Closed
rajarshidattapy wants to merge 1 commit into
Closed
Voice: preserve emitted whitespace in the sentence splitter#4693rajarshidattapy wants to merge 1 commit into
rajarshidattapy wants to merge 1 commit into
Conversation
`get_sentence_based_splitter` split a stripped buffer on a non-capturing pattern, so `re.split` discarded every inter-sentence separator and rejoined the sentences with a single space. A line break or paragraph break between two short sentences reached the TTS model as a plain space, losing the pause the engine would have rendered, and the leading whitespace of the buffer was dropped along with it. Capture the separators and split the raw buffer, then rebuild the flushed text from the alternating parts. The trailing whitespace still stays on the remainder, so the next streamed delta is not glued onto the last word.
Member
|
Please refer to #4687 (comment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4687.
Problem
get_sentence_based_splitter— the defaultTTSModelSettings.text_splitter, applied to every streamed chunk inStreamedAudioResult— split a stripped buffer on a non-capturing pattern:re.splitdiscards the matched separator, so every inter-sentence whitespace run —\n,\n\n, multiple spaces — was normalized to a single space before the text reached the TTS model, and.strip()dropped the buffer's leading whitespace.The symptom only exists in the audio: an agent that formats spoken output as list items or paragraphs loses the pause the engine renders for a line or paragraph break, so the speech runs together.
The function already guarded the trailing side of this class of bug (the
"He "+"arrived"→"Hearrived"comment); the leading and inter-sentence sides were missed.Fix
Capture the separators and split the raw buffer, then rebuild the flushed text from the alternating parts, as suggested in the issue:
partsalternates sentence, separator, …, sentence, so the last entry is the incomplete sentence and three or more entries mean a sentence has completed — which replaces the oldlen(sentences) >= 1check that was always true.The trailing whitespace handling is now implicit rather than reconstructed:
parts[-1]is the incomplete sentence with whatever whitespace followed it, so the remainder still ends with the separator that keeps the next delta from being glued onto the last word.All four cases from the issue now round-trip:
"Line one is long enough.\nLine two here.\nrest"'Line one is long enough. Line two here.''Line one is long enough.\nLine two here.'"First point.\n\nSecond point is long. tail"'First point. Second point is long.''First point.\n\nSecond point is long.'" Leading spaces here are kept? Yes indeed. tail"'Leading spaces here are kept? Yes indeed.'' Leading spaces here are kept? Yes indeed.'"Hello world this is one. Double space kept? tail"'Hello world this is one. Double space kept?''Hello world this is one. Double space kept?'Scope
The separator that falls exactly on a flush boundary is still not carried over, as before: the text on either side of it becomes a separate TTS utterance, so there is nothing for the engine to render it into. What this restores is every separator inside a flushed chunk — which is the case that matters, since short sentences (bullets, list items) accumulate until they clear
min_sentence_lengthand are spoken together.Tests
test_split_preserves_the_whitespace_the_model_emitted— the four cases above.test_streamed_paragraph_breaks_survive_any_delta_boundary— feeds short sentences through the splitter one delta at a time, parametrized over delta sizes, and asserts a\n\nreaches a spoken chunk regardless of where the model's deltas happen to break.Both fail on
main. The existing splitter tests are unchanged and still pass.