Init view TTS with text segmentation granularity (#47)

in order to support more TTS backends

This PR also fixes a potential off-by-one error at the end boundary of the segment using `sentence` granularity.
This commit is contained in:
Huang Xin
2025-02-13 07:52:03 +00:00
committed by GitHub
parent 34b9079a1b
commit 27f8b57d05
2 changed files with 6 additions and 6 deletions
+4 -4
View File
@@ -34,10 +34,10 @@ const getSegmenter = (lang = 'en', granularity = 'word') => {
while (sum <= index) sum += strs[++strIndex].length
const startIndex = strIndex
const startOffset = index - (sum - strs[strIndex].length)
const end = index + segment.length
const end = index + segment.length - 1
if (end < str.length) while (sum <= end) sum += strs[++strIndex].length
const endIndex = strIndex
const endOffset = end - (sum - strs[strIndex].length)
const endOffset = end - (sum - strs[strIndex].length) + 1
yield [(name++).toString(),
makeRange(startIndex, startOffset, endIndex, endOffset)]
}
@@ -207,11 +207,11 @@ export class TTS {
#ranges
#lastMark
#serializer = new XMLSerializer()
constructor(doc, textWalker, highlight) {
constructor(doc, textWalker, highlight, granularity) {
this.doc = doc
this.highlight = highlight
this.#list = new ListIterator(getBlocks(doc), range => {
const { entries, ssml } = getFragmentWithMarks(range, textWalker)
const { entries, ssml } = getFragmentWithMarks(range, textWalker, granularity)
this.#ranges = new Map(entries)
return [ssml, range]
})