Skip to content

Commit

Permalink
Init view TTS with text segmentation granularity
Browse files Browse the repository at this point in the history
in order to support more TTS backends
  • Loading branch information
chrox committed Jan 8, 2025
1 parent 34b9079 commit a71f346
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
})
Expand Down
4 changes: 2 additions & 2 deletions view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit a71f346

Please sign in to comment.