Skip to content

Commit d10683b

Browse files
committed
fix(pwa): reduce TTS follow overhead on mobile
1 parent 3605302 commit d10683b

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

pwa/src/components/Reader.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const TTS_HIGHLIGHT_LENGTH = 4
113113
const TTS_GEOMETRY_LINE_BUFFER = 0.9
114114
const TTS_NEW_PAGE_AUTO_FOLLOW_GUARD = 36
115115
const TTS_PAGE_END_FIXED_LEAD = 8
116-
const DEBUG_TTS_FOLLOW = true
116+
const DEBUG_TTS_FOLLOW = false
117117

118118
type TextIndex = { nodes: Text[]; starts: number[]; total: number; text: string }
119119
type TTSRangeViewportState = {
@@ -2014,7 +2014,9 @@ const Reader = ({ bookPath, bookId, bookRecord, getCoverDataUrl, onBack, darkMod
20142014
return
20152015
}
20162016

2017-
const geometry = getTTSRangeViewportState(doc, range)
2017+
const geometry = (DEBUG_TTS_FOLLOW || pageEndOffset === null)
2018+
? getTTSRangeViewportState(doc, range)
2019+
: null
20182020
const pageSpan = pageStartOffset !== null && pageEndOffset !== null
20192021
? Math.max(1, pageEndOffset - pageStartOffset)
20202022
: null
@@ -2031,7 +2033,7 @@ const Reader = ({ bookPath, bookId, bookRecord, getCoverDataUrl, onBack, darkMod
20312033
const progressShouldAdvance =
20322034
!beforeCurrentPageGuard &&
20332035
!insideNewPageGuard &&
2034-
!geometry.hasUsableRect && pageEndOffset === null && (
2036+
!(geometry?.hasUsableRect ?? true) && pageEndOffset === null && (
20352037
(source === 'boundary' && continuousPage >= currentPage + 1.05) ||
20362038
(continuousPage >= currentPage + 1.12)
20372039
)
@@ -2064,21 +2066,21 @@ const Reader = ({ bookPath, bookId, bookRecord, getCoverDataUrl, onBack, darkMod
20642066
continuousPage: Number(continuousPage.toFixed(2)),
20652067
currentPage,
20662068
totalPages,
2067-
rect: geometry.rect ? {
2069+
rect: geometry?.rect ? {
20682070
left: Math.round(geometry.rect.left),
20692071
right: Math.round(geometry.rect.right),
20702072
top: Math.round(geometry.rect.top),
20712073
bottom: Math.round(geometry.rect.bottom),
20722074
width: Math.round(geometry.rect.width),
20732075
height: Math.round(geometry.rect.height),
20742076
} : null,
2075-
viewport: geometry.viewport,
2076-
lineHeight: Math.round(geometry.lineHeight),
2077-
bottomGap: geometry.bottomGap === null ? null : Math.round(geometry.bottomGap),
2078-
leftGap: geometry.leftGap === null ? null : Math.round(geometry.leftGap),
2079-
rightGap: geometry.rightGap === null ? null : Math.round(geometry.rightGap),
2080-
hasUsableRect: geometry.hasUsableRect,
2081-
geometryReason: geometry.reason,
2077+
viewport: geometry?.viewport ?? null,
2078+
lineHeight: geometry ? Math.round(geometry.lineHeight) : null,
2079+
bottomGap: geometry?.bottomGap === null || geometry?.bottomGap === undefined ? null : Math.round(geometry.bottomGap),
2080+
leftGap: geometry?.leftGap === null || geometry?.leftGap === undefined ? null : Math.round(geometry.leftGap),
2081+
rightGap: geometry?.rightGap === null || geometry?.rightGap === undefined ? null : Math.round(geometry.rightGap),
2082+
hasUsableRect: geometry?.hasUsableRect ?? null,
2083+
geometryReason: geometry?.reason ?? null,
20822084
pageBoundaryShouldAdvance,
20832085
pageEntryGuard,
20842086
beforeCurrentPageGuard,
@@ -2102,13 +2104,13 @@ const Reader = ({ bookPath, bookId, bookRecord, getCoverDataUrl, onBack, darkMod
21022104
pageEndLead: TTS_PAGE_END_FIXED_LEAD,
21032105
distanceToPageEnd,
21042106
trigger: pageBoundaryShouldAdvance ? 'page-end-cfi' : 'progress-fallback',
2105-
geometryReason: geometry.reason,
2107+
geometryReason: geometry?.reason ?? null,
21062108
pageEntryGuard,
21072109
beforeCurrentPageGuard,
2108-
bottomGap: geometry.bottomGap === null ? null : Math.round(geometry.bottomGap),
2109-
leftGap: geometry.leftGap === null ? null : Math.round(geometry.leftGap),
2110-
rightGap: geometry.rightGap === null ? null : Math.round(geometry.rightGap),
2111-
lineHeight: Math.round(geometry.lineHeight),
2110+
bottomGap: geometry?.bottomGap === null || geometry?.bottomGap === undefined ? null : Math.round(geometry.bottomGap),
2111+
leftGap: geometry?.leftGap === null || geometry?.leftGap === undefined ? null : Math.round(geometry.leftGap),
2112+
rightGap: geometry?.rightGap === null || geometry?.rightGap === undefined ? null : Math.round(geometry.rightGap),
2113+
lineHeight: geometry ? Math.round(geometry.lineHeight) : null,
21122114
})
21132115
requestTTSAutoNextPage(displayed, absoluteOffset, pageStartOffset, pageEndOffset)
21142116
}

pwa/src/hooks/useTTS.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const MAX_UTTERANCE_LENGTH = 3000
1313
const PROGRESS_TICK_INTERVAL = 250
1414
const DEFAULT_CHARS_PER_SECOND = 6.2
1515
const MAX_ESTIMATED_BOUNDARY_LEAD = 90
16-
const DEBUG_TTS_PROGRESS = true
16+
const DEBUG_TTS_PROGRESS = false
1717

1818
export type TTSProgressSource = 'boundary' | 'estimate'
1919

@@ -57,7 +57,9 @@ const useTTS = () => {
5757
if (keepaliveTimerRef.current !== null) return
5858
keepaliveTimerRef.current = setInterval(() => {
5959
if (!playingRef.current) return
60-
console.log('[TTS] keepalive ping', { speaking: window.speechSynthesis.speaking, paused: window.speechSynthesis.paused })
60+
if (DEBUG_TTS_PROGRESS) {
61+
console.log('[TTS] keepalive ping', { speaking: window.speechSynthesis.speaking, paused: window.speechSynthesis.paused })
62+
}
6163
window.speechSynthesis.pause()
6264
window.speechSynthesis.resume()
6365
}, IOS_KEEPALIVE_INTERVAL)
@@ -200,11 +202,13 @@ const useTTS = () => {
200202
}
201203
utterance.onpause = () => {
202204
if (generationRef.current !== generation) return
203-
console.warn('[TTS] onpause(系統暫停)', { generation, charIndex: charIndexRef.current })
205+
if (DEBUG_TTS_PROGRESS) {
206+
console.warn('[TTS] onpause(系統暫停)', { generation, charIndex: charIndexRef.current })
207+
}
204208
}
205209
utterance.onresume = () => {
206210
if (generationRef.current !== generation) return
207-
console.log('[TTS] onresume', { generation })
211+
if (DEBUG_TTS_PROGRESS) console.log('[TTS] onresume', { generation })
208212
}
209213
utterance.onboundary = (e) => {
210214
if (generationRef.current !== generation) return

0 commit comments

Comments
 (0)