From a71f346b7bf8727cddbdc679185297c54dbf965c Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Tue, 7 Jan 2025 21:09:04 +0100 Subject: [PATCH] Init view TTS with text segmentation granularity in order to support more TTS backends --- tts.js | 6 +++--- view.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tts.js b/tts.js index 0089ed3..320565d 100644 --- a/tts.js +++ b/tts.js @@ -34,7 +34,7 @@ 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) @@ -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] }) diff --git a/view.js b/view.js index 17566ac..55c818f 100644 --- a/view.js +++ b/view.js @@ -577,12 +577,12 @@ export class View extends HTMLElement { for (const item of list) this.deleteAnnotation(item) this.#searchResults.clear() } - async initTTS() { + async initTTS(granularity = 'word') { const doc = this.renderer.getContents()[0].doc if (this.tts && this.tts.doc === doc) return const { TTS } = await import('./tts.js') this.tts = new TTS(doc, textWalker, range => - this.renderer.scrollToAnchor(range, true)) + this.renderer.scrollToAnchor(range, true), granularity) } startMediaOverlay() { const { index } = this.renderer.getContents()[0]