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 9, 2025
1 parent fcc449c commit 3bdbd3d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion text-walker.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const textWalker = function* (x, func) {
const walker = document.createTreeWalker(root, filter, { acceptNode })
const walk = x.commonAncestorContainer ? walkRange : walkDocument
const nodes = walk(x, walker)
const strs = nodes.map(node => node.nodeValue)
const strs = nodes.map(node => node.nodeValue ?? '')
const makeRange = (startIndex, startOffset, endIndex, endOffset) => {
const range = document.createRange()
range.setStart(nodes[startIndex], startOffset)
Expand Down
10 changes: 5 additions & 5 deletions tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const getSegmenter = (lang = 'en', granularity = 'word') => {
const segmenter = new Intl.Segmenter(lang, { granularity })
const granularityIsWord = granularity === 'word'
return function* (strs, makeRange) {
const str = strs.join('')
const str = strs.join('').replace(/\r\n/g, ' ').replace(/\r/g, ' ').replace(/\n/g, ' ')
let name = 0
let strIndex = -1
let sum = 0
Expand All @@ -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)]
}
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 3bdbd3d

Please sign in to comment.