Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dfd57544a9 | |||
| 3a5a4c0fb2 |
@@ -2,6 +2,8 @@ package eu.kanade.presentation.reader.settings
|
|||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.speech.tts.TextToSpeech
|
import android.speech.tts.TextToSpeech
|
||||||
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.foundation.layout.ColumnScope
|
import androidx.compose.foundation.layout.ColumnScope
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material3.FilterChip
|
import androidx.compose.material3.FilterChip
|
||||||
@@ -34,6 +36,14 @@ private val fontFamilies = listOf(
|
|||||||
@Composable
|
@Composable
|
||||||
internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) {
|
internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
val ttsSettingsLauncher = rememberLauncherForActivityResult(
|
||||||
|
ActivityResultContracts.StartActivityForResult(),
|
||||||
|
) {
|
||||||
|
ReaderTtsController.refreshVoiceSettings(
|
||||||
|
context,
|
||||||
|
viewModel.preferences.textTtsEnginePackage.get(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
val fontSizePref = viewModel.preferences.textFontSize
|
val fontSizePref = viewModel.preferences.textFontSize
|
||||||
val fontSize by fontSizePref.collectAsState()
|
val fontSize by fontSizePref.collectAsState()
|
||||||
@@ -130,18 +140,27 @@ internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) {
|
|||||||
SettingsChipRow(MR.strings.pref_text_reader_tts_engine) {
|
SettingsChipRow(MR.strings.pref_text_reader_tts_engine) {
|
||||||
FilterChip(
|
FilterChip(
|
||||||
selected = selectedEngine == ReaderTtsService.AUTO_ENGINE,
|
selected = selectedEngine == ReaderTtsService.AUTO_ENGINE,
|
||||||
onClick = { enginePref.set(ReaderTtsService.AUTO_ENGINE) },
|
onClick = {
|
||||||
|
enginePref.set(ReaderTtsService.AUTO_ENGINE)
|
||||||
|
ReaderTtsController.setEngine(context, ReaderTtsService.AUTO_ENGINE)
|
||||||
|
},
|
||||||
label = { Text(stringResource(MR.strings.pref_text_reader_tts_engine_auto)) },
|
label = { Text(stringResource(MR.strings.pref_text_reader_tts_engine_auto)) },
|
||||||
)
|
)
|
||||||
FilterChip(
|
FilterChip(
|
||||||
selected = selectedEngine == ReaderTtsService.SYSTEM_DEFAULT_ENGINE,
|
selected = selectedEngine == ReaderTtsService.SYSTEM_DEFAULT_ENGINE,
|
||||||
onClick = { enginePref.set(ReaderTtsService.SYSTEM_DEFAULT_ENGINE) },
|
onClick = {
|
||||||
|
enginePref.set(ReaderTtsService.SYSTEM_DEFAULT_ENGINE)
|
||||||
|
ReaderTtsController.setEngine(context, ReaderTtsService.SYSTEM_DEFAULT_ENGINE)
|
||||||
|
},
|
||||||
label = { Text(stringResource(MR.strings.pref_text_reader_tts_engine_system)) },
|
label = { Text(stringResource(MR.strings.pref_text_reader_tts_engine_system)) },
|
||||||
)
|
)
|
||||||
installedEngines.forEach { (packageName, label) ->
|
installedEngines.forEach { (packageName, label) ->
|
||||||
FilterChip(
|
FilterChip(
|
||||||
selected = selectedEngine == packageName,
|
selected = selectedEngine == packageName,
|
||||||
onClick = { enginePref.set(packageName) },
|
onClick = {
|
||||||
|
enginePref.set(packageName)
|
||||||
|
ReaderTtsController.setEngine(context, packageName)
|
||||||
|
},
|
||||||
label = { Text(label) },
|
label = { Text(label) },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -150,7 +169,7 @@ internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) {
|
|||||||
TextButton(
|
TextButton(
|
||||||
onClick = {
|
onClick = {
|
||||||
runCatching {
|
runCatching {
|
||||||
context.startActivity(Intent("com.android.settings.TTS_SETTINGS"))
|
ttsSettingsLauncher.launch(Intent("com.android.settings.TTS_SETTINGS"))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modifier = Modifier.padding(horizontal = 16.dp),
|
modifier = Modifier.padding(horizontal = 16.dp),
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ class DownloadCache(
|
|||||||
.stateIn(scope, SharingStarted.WhileSubscribed(), false)
|
.stateIn(scope, SharingStarted.WhileSubscribed(), false)
|
||||||
|
|
||||||
private val diskCacheFile: File
|
private val diskCacheFile: File
|
||||||
get() = File(context.cacheDir, "dl_index_cache_v3")
|
get() = File(context.cacheDir, "dl_index_cache_v4")
|
||||||
|
|
||||||
private val rootDownloadsDirMutex = Mutex()
|
private val rootDownloadsDirMutex = Mutex()
|
||||||
private var rootDownloadsDir = RootDirectory(storageManager.getDownloadsDirectory())
|
private var rootDownloadsDir = RootDirectory(storageManager.getDownloadsDirectory())
|
||||||
|
|||||||
@@ -214,7 +214,8 @@ class DownloadManager(
|
|||||||
mangaTitle: String,
|
mangaTitle: String,
|
||||||
source: Source,
|
source: Source,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
return provider.findChapterDir(chapterName, chapterScanlator, chapterUrl, mangaTitle, source) != null
|
return provider.findChapterDir(chapterName, chapterScanlator, chapterUrl, mangaTitle, source) != null ||
|
||||||
|
provider.findChapterDocument(chapterName, chapterScanlator, chapterUrl, mangaTitle, source) != null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -253,8 +254,8 @@ class DownloadManager(
|
|||||||
|
|
||||||
removeFromDownloadQueue(filteredChapters)
|
removeFromDownloadQueue(filteredChapters)
|
||||||
|
|
||||||
val (mangaDir, chapterDirs) = provider.findChapterDirs(filteredChapters, manga, source)
|
val (mangaDir, chapterDownloads) = provider.findChapterDirs(filteredChapters, manga, source)
|
||||||
chapterDirs.forEach { it.delete() }
|
chapterDownloads.forEach { it.delete() }
|
||||||
cache.removeChapters(filteredChapters, manga)
|
cache.removeChapters(filteredChapters, manga)
|
||||||
|
|
||||||
// Delete manga directory if empty
|
// Delete manga directory if empty
|
||||||
|
|||||||
@@ -149,11 +149,8 @@ class DownloadProvider(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of downloaded directories for the chapters that exist.
|
* Returns downloaded chapter entries that exist, including image directories,
|
||||||
*
|
* CBZ files and standalone EPUB/TXT document files.
|
||||||
* @param chapters the chapters to query.
|
|
||||||
* @param manga the manga of the chapter.
|
|
||||||
* @param source the source of the chapter.
|
|
||||||
*/
|
*/
|
||||||
fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): Pair<UniFile?, List<UniFile>> {
|
fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): Pair<UniFile?, List<UniFile>> {
|
||||||
val mangaDir = findMangaDir(manga.title, source) ?: return null to emptyList()
|
val mangaDir = findMangaDir(manga.title, source) ?: return null to emptyList()
|
||||||
@@ -161,6 +158,13 @@ class DownloadProvider(
|
|||||||
getValidChapterDirNames(chapter.name, chapter.scanlator, chapter.url).asSequence()
|
getValidChapterDirNames(chapter.name, chapter.scanlator, chapter.url).asSequence()
|
||||||
.mapNotNull { mangaDir.findFile(it) }
|
.mapNotNull { mangaDir.findFile(it) }
|
||||||
.firstOrNull()
|
.firstOrNull()
|
||||||
|
?: findChapterDocument(
|
||||||
|
chapter.name,
|
||||||
|
chapter.scanlator,
|
||||||
|
chapter.url,
|
||||||
|
manga.title,
|
||||||
|
source,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -751,11 +751,16 @@ class ReaderActivity : BaseActivity() {
|
|||||||
val textPages = chapter.pages?.filterIsInstance<TextReaderPage>().orEmpty()
|
val textPages = chapter.pages?.filterIsInstance<TextReaderPage>().orEmpty()
|
||||||
if (textPages.isEmpty()) return
|
if (textPages.isEmpty()) return
|
||||||
|
|
||||||
val targetPage = if (ttsState.sourceHighlightStart >= 0L) {
|
val targetSourceOffset = when {
|
||||||
|
ttsState.sourceHighlightStart >= 0L -> ttsState.sourceHighlightStart
|
||||||
|
ttsState.sourceSentenceStart >= 0L -> ttsState.sourceSentenceStart
|
||||||
|
else -> -1L
|
||||||
|
}
|
||||||
|
val targetPage = if (targetSourceOffset >= 0L) {
|
||||||
textPages.firstOrNull { page ->
|
textPages.firstOrNull { page ->
|
||||||
page.anchor.documentIndex == ttsState.documentIndex &&
|
page.anchor.documentIndex == ttsState.documentIndex &&
|
||||||
ttsState.sourceHighlightStart >= page.anchor.startOffset &&
|
targetSourceOffset >= page.anchor.startOffset &&
|
||||||
ttsState.sourceHighlightStart < page.anchor.endOffsetExclusive
|
targetSourceOffset < page.anchor.endOffsetExclusive
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
chapter.pages?.getOrNull(ttsState.readerPageIndex) as? TextReaderPage
|
chapter.pages?.getOrNull(ttsState.readerPageIndex) as? TextReaderPage
|
||||||
@@ -765,15 +770,29 @@ class ReaderActivity : BaseActivity() {
|
|||||||
ttsHighlightedPage?.clearTtsHighlight()
|
ttsHighlightedPage?.clearTtsHighlight()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val sentenceStart = maxOf(ttsState.sourceSentenceStart, targetPage.anchor.startOffset)
|
||||||
|
val sentenceEnd = minOf(ttsState.sourceSentenceEndExclusive, targetPage.anchor.endOffsetExclusive)
|
||||||
|
val activeStart = maxOf(ttsState.sourceHighlightStart, targetPage.anchor.startOffset)
|
||||||
|
val activeEnd = minOf(ttsState.sourceHighlightEndExclusive, targetPage.anchor.endOffsetExclusive)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
ttsState.sourceHighlightStart >= 0L &&
|
ttsState.sourceSentenceStart >= 0L &&
|
||||||
ttsState.sourceHighlightEndExclusive > ttsState.sourceHighlightStart
|
ttsState.sourceSentenceEndExclusive > ttsState.sourceSentenceStart &&
|
||||||
|
sentenceEnd > sentenceStart
|
||||||
) {
|
) {
|
||||||
val start = maxOf(ttsState.sourceHighlightStart, targetPage.anchor.startOffset)
|
|
||||||
val end = minOf(ttsState.sourceHighlightEndExclusive, targetPage.anchor.endOffsetExclusive)
|
|
||||||
targetPage.updateTtsHighlight(
|
targetPage.updateTtsHighlight(
|
||||||
start = (start - targetPage.anchor.startOffset).toInt(),
|
sentenceStart = (sentenceStart - targetPage.anchor.startOffset).toInt(),
|
||||||
endExclusive = (end - targetPage.anchor.startOffset).toInt(),
|
sentenceEndExclusive = (sentenceEnd - targetPage.anchor.startOffset).toInt(),
|
||||||
|
activeStart = if (activeEnd > activeStart) {
|
||||||
|
(activeStart - targetPage.anchor.startOffset).toInt()
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
},
|
||||||
|
activeEndExclusive = if (activeEnd > activeStart) {
|
||||||
|
(activeEnd - targetPage.anchor.startOffset).toInt()
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
},
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
targetPage.clearTtsHighlight()
|
targetPage.clearTtsHighlight()
|
||||||
|
|||||||
@@ -158,7 +158,8 @@ internal object TextPageRenderer {
|
|||||||
fun render(
|
fun render(
|
||||||
text: String,
|
text: String,
|
||||||
style: TextReaderStyle,
|
style: TextReaderStyle,
|
||||||
highlightRange: IntRange? = null,
|
sentenceHighlightRange: IntRange? = null,
|
||||||
|
activeHighlightRange: IntRange? = null,
|
||||||
): InputStream {
|
): InputStream {
|
||||||
val metrics = resolveLayoutMetrics(style)
|
val metrics = resolveLayoutMetrics(style)
|
||||||
val bitmap = Bitmap.createBitmap(metrics.pageWidth, metrics.pageHeight, Bitmap.Config.ARGB_8888)
|
val bitmap = Bitmap.createBitmap(metrics.pageWidth, metrics.pageHeight, Bitmap.Config.ARGB_8888)
|
||||||
@@ -167,7 +168,13 @@ internal object TextPageRenderer {
|
|||||||
canvas.drawColor(backgroundColor)
|
canvas.drawColor(backgroundColor)
|
||||||
canvas.save()
|
canvas.save()
|
||||||
canvas.translate(metrics.horizontalMargin.toFloat(), metrics.verticalMargin.toFloat())
|
canvas.translate(metrics.horizontalMargin.toFloat(), metrics.verticalMargin.toFloat())
|
||||||
createLayout(text, style, metrics.contentWidth, highlightRange).draw(canvas)
|
createLayout(
|
||||||
|
text = text,
|
||||||
|
style = style,
|
||||||
|
contentWidth = metrics.contentWidth,
|
||||||
|
sentenceHighlightRange = sentenceHighlightRange,
|
||||||
|
activeHighlightRange = activeHighlightRange,
|
||||||
|
).draw(canvas)
|
||||||
canvas.restore()
|
canvas.restore()
|
||||||
|
|
||||||
val output = ByteArrayOutputStream()
|
val output = ByteArrayOutputStream()
|
||||||
@@ -180,7 +187,8 @@ internal object TextPageRenderer {
|
|||||||
text: String,
|
text: String,
|
||||||
style: TextReaderStyle,
|
style: TextReaderStyle,
|
||||||
contentWidth: Int,
|
contentWidth: Int,
|
||||||
highlightRange: IntRange? = null,
|
sentenceHighlightRange: IntRange? = null,
|
||||||
|
activeHighlightRange: IntRange? = null,
|
||||||
): StaticLayout {
|
): StaticLayout {
|
||||||
val (_, textColor) = resolveColors(style)
|
val (_, textColor) = resolveColors(style)
|
||||||
val paint = TextPaint().apply {
|
val paint = TextPaint().apply {
|
||||||
@@ -191,7 +199,12 @@ internal object TextPageRenderer {
|
|||||||
?.let { Typeface.create(it, Typeface.NORMAL) }
|
?.let { Typeface.create(it, Typeface.NORMAL) }
|
||||||
?: Typeface.DEFAULT
|
?: Typeface.DEFAULT
|
||||||
}
|
}
|
||||||
val styledText = addTextSpans(text, style, highlightRange)
|
val styledText = addTextSpans(
|
||||||
|
text = text,
|
||||||
|
style = style,
|
||||||
|
sentenceHighlightRange = sentenceHighlightRange,
|
||||||
|
activeHighlightRange = activeHighlightRange,
|
||||||
|
)
|
||||||
return StaticLayout.Builder
|
return StaticLayout.Builder
|
||||||
.obtain(styledText, 0, styledText.length, paint, contentWidth)
|
.obtain(styledText, 0, styledText.length, paint, contentWidth)
|
||||||
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
|
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
|
||||||
@@ -239,11 +252,13 @@ internal object TextPageRenderer {
|
|||||||
private fun addTextSpans(
|
private fun addTextSpans(
|
||||||
text: String,
|
text: String,
|
||||||
style: TextReaderStyle,
|
style: TextReaderStyle,
|
||||||
highlightRange: IntRange?,
|
sentenceHighlightRange: IntRange?,
|
||||||
|
activeHighlightRange: IntRange?,
|
||||||
): CharSequence {
|
): CharSequence {
|
||||||
val needsParagraphSpacing = style.paragraphSpacingPx > 0 && '\n' in text
|
val needsParagraphSpacing = style.paragraphSpacingPx > 0 && '\n' in text
|
||||||
val needsHighlight = highlightRange != null && !highlightRange.isEmpty()
|
val needsSentenceHighlight = sentenceHighlightRange != null && !sentenceHighlightRange.isEmpty()
|
||||||
if (!needsParagraphSpacing && !needsHighlight) return text
|
val needsActiveHighlight = activeHighlightRange != null && !activeHighlightRange.isEmpty()
|
||||||
|
if (!needsParagraphSpacing && !needsSentenceHighlight && !needsActiveHighlight) return text
|
||||||
|
|
||||||
val spannable = SpannableString(text)
|
val spannable = SpannableString(text)
|
||||||
if (needsParagraphSpacing) {
|
if (needsParagraphSpacing) {
|
||||||
@@ -258,26 +273,45 @@ internal object TextPageRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (needsHighlight) {
|
|
||||||
val start = highlightRange!!.first.coerceIn(0, text.length)
|
val darkBackground = resolveColors(style).first == Color.BLACK
|
||||||
val endExclusive = (highlightRange.last + 1).coerceIn(start, text.length)
|
sentenceHighlightRange?.let { range ->
|
||||||
if (start < endExclusive) {
|
applyHighlightSpan(
|
||||||
val highlightColor = if (resolveColors(style).first == Color.BLACK) {
|
spannable = spannable,
|
||||||
Color.rgb(92, 82, 24)
|
textLength = text.length,
|
||||||
} else {
|
range = range,
|
||||||
Color.rgb(255, 245, 157)
|
color = if (darkBackground) Color.rgb(72, 68, 40) else Color.rgb(255, 249, 196),
|
||||||
}
|
)
|
||||||
spannable.setSpan(
|
}
|
||||||
BackgroundColorSpan(highlightColor),
|
activeHighlightRange?.let { range ->
|
||||||
start,
|
applyHighlightSpan(
|
||||||
endExclusive,
|
spannable = spannable,
|
||||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
textLength = text.length,
|
||||||
)
|
range = range,
|
||||||
}
|
color = if (darkBackground) Color.rgb(150, 117, 0) else Color.rgb(255, 193, 7),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return spannable
|
return spannable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun applyHighlightSpan(
|
||||||
|
spannable: SpannableString,
|
||||||
|
textLength: Int,
|
||||||
|
range: IntRange,
|
||||||
|
color: Int,
|
||||||
|
) {
|
||||||
|
if (range.isEmpty()) return
|
||||||
|
val start = range.first.coerceIn(0, textLength)
|
||||||
|
val endExclusive = (range.last + 1).coerceIn(start, textLength)
|
||||||
|
if (start >= endExclusive) return
|
||||||
|
spannable.setSpan(
|
||||||
|
BackgroundColorSpan(color),
|
||||||
|
start,
|
||||||
|
endExclusive,
|
||||||
|
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private class ParagraphSpacingSpan(private val spacing: Int) : LineHeightSpan {
|
private class ParagraphSpacingSpan(private val spacing: Int) : LineHeightSpan {
|
||||||
override fun chooseHeight(
|
override fun chooseHeight(
|
||||||
text: CharSequence?,
|
text: CharSequence?,
|
||||||
|
|||||||
@@ -31,10 +31,20 @@ internal class TextReaderPage(
|
|||||||
private var style: TextReaderStyle = initialStyle
|
private var style: TextReaderStyle = initialStyle
|
||||||
|
|
||||||
@Volatile
|
@Volatile
|
||||||
private var highlightRange: IntRange? = null
|
private var sentenceHighlightRange: IntRange? = null
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var activeHighlightRange: IntRange? = null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
stream = { TextPageRenderer.render(pageTextProvider(), style, highlightRange) }
|
stream = {
|
||||||
|
TextPageRenderer.render(
|
||||||
|
text = pageTextProvider(),
|
||||||
|
style = style,
|
||||||
|
sentenceHighlightRange = sentenceHighlightRange,
|
||||||
|
activeHighlightRange = activeHighlightRange,
|
||||||
|
)
|
||||||
|
}
|
||||||
status = Page.State.Ready
|
status = Page.State.Ready
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,11 +61,26 @@ internal class TextReaderPage(
|
|||||||
|
|
||||||
fun readText(): String = pageTextProvider()
|
fun readText(): String = pageTextProvider()
|
||||||
|
|
||||||
fun updateTtsHighlight(start: Int, endExclusive: Int) {
|
fun updateTtsHighlight(
|
||||||
highlightRange = if (start >= 0 && endExclusive > start) start until endExclusive else null
|
sentenceStart: Int,
|
||||||
|
sentenceEndExclusive: Int,
|
||||||
|
activeStart: Int,
|
||||||
|
activeEndExclusive: Int,
|
||||||
|
) {
|
||||||
|
sentenceHighlightRange = if (sentenceStart >= 0 && sentenceEndExclusive > sentenceStart) {
|
||||||
|
sentenceStart until sentenceEndExclusive
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
activeHighlightRange = if (activeStart >= 0 && activeEndExclusive > activeStart) {
|
||||||
|
activeStart until activeEndExclusive
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearTtsHighlight() {
|
fun clearTtsHighlight() {
|
||||||
highlightRange = null
|
sentenceHighlightRange = null
|
||||||
|
activeHighlightRange = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ object ReaderTtsController {
|
|||||||
val readerChapterId: Long = -1L,
|
val readerChapterId: Long = -1L,
|
||||||
val readerPageIndex: Int = -1,
|
val readerPageIndex: Int = -1,
|
||||||
val documentIndex: Int = -1,
|
val documentIndex: Int = -1,
|
||||||
|
val sourceSentenceStart: Long = -1L,
|
||||||
|
val sourceSentenceEndExclusive: Long = -1L,
|
||||||
val sourceHighlightStart: Long = -1L,
|
val sourceHighlightStart: Long = -1L,
|
||||||
val sourceHighlightEndExclusive: Long = -1L,
|
val sourceHighlightEndExclusive: Long = -1L,
|
||||||
)
|
)
|
||||||
@@ -70,4 +72,22 @@ object ReaderTtsController {
|
|||||||
.putExtra(ReaderTtsService.EXTRA_SPEED, speed),
|
.putExtra(ReaderTtsService.EXTRA_SPEED, speed),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setEngine(context: Context, enginePackage: String) {
|
||||||
|
if (!state.value.active) return
|
||||||
|
context.startService(
|
||||||
|
Intent(context, ReaderTtsService::class.java)
|
||||||
|
.setAction(ReaderTtsService.ACTION_SET_ENGINE)
|
||||||
|
.putExtra(ReaderTtsService.EXTRA_ENGINE_PACKAGE, enginePackage),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun refreshVoiceSettings(context: Context, enginePackage: String) {
|
||||||
|
if (!state.value.active) return
|
||||||
|
context.startService(
|
||||||
|
Intent(context, ReaderTtsService::class.java)
|
||||||
|
.setAction(ReaderTtsService.ACTION_REFRESH_VOICE)
|
||||||
|
.putExtra(ReaderTtsService.EXTRA_ENGINE_PACKAGE, enginePackage),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,10 +25,17 @@ class ReaderTtsService : Service() {
|
|||||||
private var sessionId: String? = null
|
private var sessionId: String? = null
|
||||||
private var session: ReaderTtsSessionStore.SessionReader? = null
|
private var session: ReaderTtsSessionStore.SessionReader? = null
|
||||||
private var pagePosition = 0
|
private var pagePosition = 0
|
||||||
private var currentPageText: String? = null
|
private var pageSpeechStartOffset = 0
|
||||||
|
private var currentWindow: SpeechWindow? = null
|
||||||
private var currentSegments: List<Segment> = emptyList()
|
private var currentSegments: List<Segment> = emptyList()
|
||||||
|
private var lookaheadConsumedChars: Map<Int, Int> = emptyMap()
|
||||||
private var segmentPosition = 0
|
private var segmentPosition = 0
|
||||||
|
private var segmentSpeechOffset = 0
|
||||||
|
private var currentUtteranceBaseOffset = 0
|
||||||
|
private var currentUtteranceId: String? = null
|
||||||
|
private var ttsGeneration = 0
|
||||||
private var playbackSpeed = DEFAULT_SPEED
|
private var playbackSpeed = DEFAULT_SPEED
|
||||||
|
private var enginePreference = AUTO_ENGINE
|
||||||
private var readerChapterId = -1L
|
private var readerChapterId = -1L
|
||||||
private var title = "Text reader"
|
private var title = "Text reader"
|
||||||
private var subtitle: String? = null
|
private var subtitle: String? = null
|
||||||
@@ -43,8 +50,13 @@ class ReaderTtsService : Service() {
|
|||||||
ACTION_SET_SPEED -> {
|
ACTION_SET_SPEED -> {
|
||||||
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, playbackSpeed).coerceIn(MIN_SPEED, MAX_SPEED)
|
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, playbackSpeed).coerceIn(MIN_SPEED, MAX_SPEED)
|
||||||
tts?.setSpeechRate(playbackSpeed)
|
tts?.setSpeechRate(playbackSpeed)
|
||||||
|
if (playing && initialized) restartCurrentUtterance()
|
||||||
refreshNotification()
|
refreshNotification()
|
||||||
}
|
}
|
||||||
|
ACTION_SET_ENGINE, ACTION_REFRESH_VOICE -> {
|
||||||
|
val requestedEngine = intent.getStringExtra(EXTRA_ENGINE_PACKAGE).orEmpty()
|
||||||
|
reinitializeTts(requestedEngine)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return START_NOT_STICKY
|
return START_NOT_STICKY
|
||||||
}
|
}
|
||||||
@@ -70,14 +82,20 @@ class ReaderTtsService : Service() {
|
|||||||
title = intent.getStringExtra(EXTRA_TITLE).orEmpty().ifBlank { "Text reader" }
|
title = intent.getStringExtra(EXTRA_TITLE).orEmpty().ifBlank { "Text reader" }
|
||||||
subtitle = intent.getStringExtra(EXTRA_SUBTITLE)
|
subtitle = intent.getStringExtra(EXTRA_SUBTITLE)
|
||||||
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, DEFAULT_SPEED).coerceIn(MIN_SPEED, MAX_SPEED)
|
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, DEFAULT_SPEED).coerceIn(MIN_SPEED, MAX_SPEED)
|
||||||
|
enginePreference = intent.getStringExtra(EXTRA_ENGINE_PACKAGE).orEmpty().ifBlank { AUTO_ENGINE }
|
||||||
|
|
||||||
val requestedPageIndex = intent.getIntExtra(EXTRA_START_PAGE_INDEX, 0)
|
val requestedPageIndex = intent.getIntExtra(EXTRA_START_PAGE_INDEX, 0)
|
||||||
pagePosition = newSession.pages.indexOfFirst { it.readerPageIndex >= requestedPageIndex }
|
pagePosition = newSession.pages.indexOfFirst { it.readerPageIndex >= requestedPageIndex }
|
||||||
.takeIf { it >= 0 }
|
.takeIf { it >= 0 }
|
||||||
?: 0
|
?: 0
|
||||||
currentPageText = null
|
pageSpeechStartOffset = 0
|
||||||
|
currentWindow = null
|
||||||
currentSegments = emptyList()
|
currentSegments = emptyList()
|
||||||
|
lookaheadConsumedChars = emptyMap()
|
||||||
segmentPosition = 0
|
segmentPosition = 0
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
currentUtteranceBaseOffset = 0
|
||||||
|
currentUtteranceId = null
|
||||||
playing = false
|
playing = false
|
||||||
initialized = false
|
initialized = false
|
||||||
|
|
||||||
@@ -92,17 +110,23 @@ class ReaderTtsService : Service() {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
initializeTts(intent.getStringExtra(EXTRA_ENGINE_PACKAGE).orEmpty())
|
initializeTts(enginePreference, resumeAfterInit = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initializeTts(enginePreference: String) {
|
private fun initializeTts(
|
||||||
|
requestedEnginePreference: String,
|
||||||
|
resumeAfterInit: Boolean,
|
||||||
|
) {
|
||||||
|
enginePreference = requestedEnginePreference.ifBlank { AUTO_ENGINE }
|
||||||
val engine = resolveEngine(enginePreference)
|
val engine = resolveEngine(enginePreference)
|
||||||
|
val generation = ++ttsGeneration
|
||||||
val listener = TextToSpeech.OnInitListener { status ->
|
val listener = TextToSpeech.OnInitListener { status ->
|
||||||
mainHandler.post {
|
mainHandler.post {
|
||||||
|
if (generation != ttsGeneration) return@post
|
||||||
if (status != TextToSpeech.SUCCESS) {
|
if (status != TextToSpeech.SUCCESS) {
|
||||||
if (engine != null) {
|
if (engine != null) {
|
||||||
cleanupTts()
|
cleanupTts()
|
||||||
initializeTts(SYSTEM_DEFAULT_ENGINE)
|
initializeTts(SYSTEM_DEFAULT_ENGINE, resumeAfterInit)
|
||||||
} else {
|
} else {
|
||||||
stopPlayback()
|
stopPlayback()
|
||||||
}
|
}
|
||||||
@@ -120,9 +144,9 @@ class ReaderTtsService : Service() {
|
|||||||
)
|
)
|
||||||
setOnUtteranceProgressListener(progressListener)
|
setOnUtteranceProgressListener(progressListener)
|
||||||
}
|
}
|
||||||
playing = true
|
playing = resumeAfterInit
|
||||||
updatePlaybackState()
|
updatePlaybackState(clearHighlight = false)
|
||||||
speakCurrentSegment()
|
if (playing) speakCurrentSegment()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,6 +157,24 @@ class ReaderTtsService : Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun reinitializeTts(requestedEnginePreference: String) {
|
||||||
|
if (session == null) return
|
||||||
|
val resumeAfterInit = playing
|
||||||
|
currentUtteranceId = null
|
||||||
|
cleanupTts()
|
||||||
|
initializeTts(
|
||||||
|
requestedEnginePreference.ifBlank { enginePreference },
|
||||||
|
resumeAfterInit = resumeAfterInit,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun restartCurrentUtterance() {
|
||||||
|
if (!playing || !initialized) return
|
||||||
|
currentUtteranceId = null
|
||||||
|
tts?.stop()
|
||||||
|
speakCurrentSegment()
|
||||||
|
}
|
||||||
|
|
||||||
private fun resolveEngine(preference: String): String? {
|
private fun resolveEngine(preference: String): String? {
|
||||||
if (preference == SYSTEM_DEFAULT_ENGINE) return null
|
if (preference == SYSTEM_DEFAULT_ENGINE) return null
|
||||||
if (preference.isNotBlank() && preference != AUTO_ENGINE) return preference
|
if (preference.isNotBlank() && preference != AUTO_ENGINE) return preference
|
||||||
@@ -164,31 +206,163 @@ class ReaderTtsService : Service() {
|
|||||||
|
|
||||||
private fun speakCurrentSegment() {
|
private fun speakCurrentSegment() {
|
||||||
if (!playing || !initialized) return
|
if (!playing || !initialized) return
|
||||||
val session = session ?: return stopPlayback()
|
|
||||||
|
|
||||||
if (currentPageText == null) {
|
if (currentSegments.isEmpty()) {
|
||||||
currentPageText = session.readPage(pagePosition)
|
if (!prepareCurrentSegments()) return
|
||||||
currentSegments = segmentText(currentPageText.orEmpty())
|
|
||||||
segmentPosition = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentSegments.isEmpty() || segmentPosition !in currentSegments.indices) {
|
if (segmentPosition !in currentSegments.indices) {
|
||||||
advancePage()
|
advancePage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val segment = currentSegments[segmentPosition]
|
val segment = currentSegments[segmentPosition]
|
||||||
val utteranceId = pagePosition.toString() + ":" + segmentPosition
|
val speechOffset = segmentSpeechOffset.coerceIn(0, segment.text.length)
|
||||||
|
if (speechOffset >= segment.text.length) {
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
advanceSegment()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
currentUtteranceBaseOffset = speechOffset
|
||||||
|
val utteranceId = "$ttsGeneration:$pagePosition:$segmentPosition:$speechOffset"
|
||||||
|
currentUtteranceId = utteranceId
|
||||||
val result = tts?.speak(
|
val result = tts?.speak(
|
||||||
segment.text,
|
segment.text.substring(speechOffset),
|
||||||
TextToSpeech.QUEUE_FLUSH,
|
TextToSpeech.QUEUE_FLUSH,
|
||||||
null,
|
null,
|
||||||
utteranceId,
|
utteranceId,
|
||||||
)
|
)
|
||||||
if (result == TextToSpeech.ERROR) advanceSegment()
|
if (result == TextToSpeech.ERROR) {
|
||||||
|
currentUtteranceId = null
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
advanceSegment()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun prepareCurrentSegments(): Boolean {
|
||||||
|
val session = session ?: run {
|
||||||
|
stopPlayback()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val currentEntry = session.pages.getOrNull(pagePosition) ?: run {
|
||||||
|
stopPlayback()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val currentText = session.readPage(pagePosition)
|
||||||
|
val safeStart = pageSpeechStartOffset.coerceIn(0, currentText.length)
|
||||||
|
|
||||||
|
if (safeStart >= currentText.length) {
|
||||||
|
advancePage()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
val window = buildSpeechWindow(session, currentEntry.documentIndex, safeStart)
|
||||||
|
val currentPart = window.parts.firstOrNull { it.pagePosition == pagePosition } ?: run {
|
||||||
|
advancePage()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val segments = segmentText(window.text)
|
||||||
|
.filter { it.start < currentPart.windowEndExclusive }
|
||||||
|
|
||||||
|
if (segments.isEmpty()) {
|
||||||
|
advancePage()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
currentWindow = window
|
||||||
|
currentSegments = segments
|
||||||
|
segmentPosition = 0
|
||||||
|
lookaheadConsumedChars = buildMap {
|
||||||
|
val last = segments.last()
|
||||||
|
window.parts
|
||||||
|
.asSequence()
|
||||||
|
.filter { it.pagePosition > pagePosition }
|
||||||
|
.forEach { part ->
|
||||||
|
val consumedEnd = min(last.endExclusive, part.windowEndExclusive)
|
||||||
|
if (consumedEnd > part.windowStart) {
|
||||||
|
put(part.pagePosition, consumedEnd - part.windowStart)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildSpeechWindow(
|
||||||
|
session: ReaderTtsSessionStore.SessionReader,
|
||||||
|
documentIndex: Int,
|
||||||
|
currentPageStart: Int,
|
||||||
|
): SpeechWindow {
|
||||||
|
val text = StringBuilder()
|
||||||
|
val parts = mutableListOf<WindowPart>()
|
||||||
|
var candidatePage = pagePosition
|
||||||
|
var pagesAdded = 0
|
||||||
|
var firstPageEnd = -1
|
||||||
|
|
||||||
|
while (
|
||||||
|
candidatePage < session.pages.size &&
|
||||||
|
pagesAdded < MAX_LOOKAHEAD_PAGES &&
|
||||||
|
text.length < MAX_SPEECH_WINDOW_CHARS
|
||||||
|
) {
|
||||||
|
val entry = session.pages[candidatePage]
|
||||||
|
if (entry.documentIndex != documentIndex) break
|
||||||
|
|
||||||
|
val pageText = session.readPage(candidatePage)
|
||||||
|
val pageStart = if (candidatePage == pagePosition) currentPageStart else 0
|
||||||
|
if (pageStart >= pageText.length) {
|
||||||
|
candidatePage++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (text.isNotEmpty()) text.append(' ')
|
||||||
|
val windowStart = text.length
|
||||||
|
val available = (MAX_SPEECH_WINDOW_CHARS - text.length).coerceAtLeast(0)
|
||||||
|
if (available == 0) break
|
||||||
|
|
||||||
|
val takeCount = min(pageText.length - pageStart, available)
|
||||||
|
text.append(pageText, pageStart, pageStart + takeCount)
|
||||||
|
val windowEnd = text.length
|
||||||
|
parts += WindowPart(
|
||||||
|
windowStart = windowStart,
|
||||||
|
windowEndExclusive = windowEnd,
|
||||||
|
pagePosition = candidatePage,
|
||||||
|
documentIndex = entry.documentIndex,
|
||||||
|
sourceStartOffset = entry.sourceStartOffset + pageStart,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (candidatePage == pagePosition) {
|
||||||
|
firstPageEnd = windowEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
pagesAdded++
|
||||||
|
if (
|
||||||
|
candidatePage > pagePosition &&
|
||||||
|
firstPageEnd >= 0 &&
|
||||||
|
hasSentenceTerminatorAfter(text, firstPageEnd)
|
||||||
|
) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (takeCount < pageText.length - pageStart) break
|
||||||
|
candidatePage++
|
||||||
|
}
|
||||||
|
|
||||||
|
return SpeechWindow(
|
||||||
|
text = text.toString(),
|
||||||
|
parts = parts,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hasSentenceTerminatorAfter(text: CharSequence, start: Int): Boolean {
|
||||||
|
for (index in start.coerceAtLeast(0) until text.length) {
|
||||||
|
if (text[index] in SENTENCE_TERMINATORS) return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun advanceSegment() {
|
private fun advanceSegment() {
|
||||||
|
currentUtteranceId = null
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
currentUtteranceBaseOffset = 0
|
||||||
segmentPosition++
|
segmentPosition++
|
||||||
if (segmentPosition >= currentSegments.size) advancePage() else speakCurrentSegment()
|
if (segmentPosition >= currentSegments.size) advancePage() else speakCurrentSegment()
|
||||||
}
|
}
|
||||||
@@ -201,9 +375,13 @@ class ReaderTtsService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pagePosition++
|
pagePosition++
|
||||||
currentPageText = null
|
pageSpeechStartOffset = lookaheadConsumedChars[pagePosition] ?: 0
|
||||||
|
currentWindow = null
|
||||||
currentSegments = emptyList()
|
currentSegments = emptyList()
|
||||||
segmentPosition = 0
|
segmentPosition = 0
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
currentUtteranceBaseOffset = 0
|
||||||
|
currentUtteranceId = null
|
||||||
speakCurrentSegment()
|
speakCurrentSegment()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,8 +390,9 @@ class ReaderTtsService : Service() {
|
|||||||
cleanupTts()
|
cleanupTts()
|
||||||
session?.close()
|
session?.close()
|
||||||
session = null
|
session = null
|
||||||
currentPageText = null
|
currentWindow = null
|
||||||
currentSegments = emptyList()
|
currentSegments = emptyList()
|
||||||
|
lookaheadConsumedChars = emptyMap()
|
||||||
val oldSessionId = sessionId
|
val oldSessionId = sessionId
|
||||||
sessionId = null
|
sessionId = null
|
||||||
ReaderTtsSessionStore.delete(this, oldSessionId)
|
ReaderTtsSessionStore.delete(this, oldSessionId)
|
||||||
@@ -223,6 +402,8 @@ class ReaderTtsService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun cleanupTts() {
|
private fun cleanupTts() {
|
||||||
|
ttsGeneration++
|
||||||
|
currentUtteranceId = null
|
||||||
initialized = false
|
initialized = false
|
||||||
tts?.stop()
|
tts?.stop()
|
||||||
tts?.shutdown()
|
tts?.shutdown()
|
||||||
@@ -240,6 +421,8 @@ class ReaderTtsService : Service() {
|
|||||||
readerChapterId = readerChapterId,
|
readerChapterId = readerChapterId,
|
||||||
readerPageIndex = page.readerPageIndex,
|
readerPageIndex = page.readerPageIndex,
|
||||||
documentIndex = page.documentIndex,
|
documentIndex = page.documentIndex,
|
||||||
|
sourceSentenceStart = if (clearHighlight) -1L else previous.sourceSentenceStart,
|
||||||
|
sourceSentenceEndExclusive = if (clearHighlight) -1L else previous.sourceSentenceEndExclusive,
|
||||||
sourceHighlightStart = if (clearHighlight) -1L else previous.sourceHighlightStart,
|
sourceHighlightStart = if (clearHighlight) -1L else previous.sourceHighlightStart,
|
||||||
sourceHighlightEndExclusive = if (clearHighlight) -1L else previous.sourceHighlightEndExclusive,
|
sourceHighlightEndExclusive = if (clearHighlight) -1L else previous.sourceHighlightEndExclusive,
|
||||||
),
|
),
|
||||||
@@ -253,22 +436,70 @@ class ReaderTtsService : Service() {
|
|||||||
relativeEndExclusive: Int = segment.text.length,
|
relativeEndExclusive: Int = segment.text.length,
|
||||||
) {
|
) {
|
||||||
val session = session ?: return
|
val session = session ?: return
|
||||||
val page = session.pages.getOrNull(pagePosition) ?: return
|
val window = currentWindow ?: return
|
||||||
|
val sentenceStart = window.mapStart(segment.start) ?: return
|
||||||
|
val sentenceEnd = window.mapEnd(segment.endExclusive) ?: return
|
||||||
|
|
||||||
val safeStart = relativeStart.coerceIn(0, segment.text.length)
|
val safeStart = relativeStart.coerceIn(0, segment.text.length)
|
||||||
val safeEnd = relativeEndExclusive.coerceIn(safeStart, segment.text.length)
|
val safeEnd = relativeEndExclusive.coerceIn(safeStart, segment.text.length)
|
||||||
|
val activeStart = if (safeEnd > safeStart) {
|
||||||
|
window.mapStart(segment.start + safeStart)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
val activeEnd = if (safeEnd > safeStart) {
|
||||||
|
window.mapEnd(segment.start + safeEnd)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
val activePosition = activeStart ?: sentenceStart
|
||||||
|
val page = session.pages.getOrNull(activePosition.pagePosition) ?: return
|
||||||
|
|
||||||
ReaderTtsController.updateState(
|
ReaderTtsController.updateState(
|
||||||
ReaderTtsController.State(
|
ReaderTtsController.State(
|
||||||
active = true,
|
active = true,
|
||||||
playing = playing,
|
playing = playing,
|
||||||
readerChapterId = readerChapterId,
|
readerChapterId = readerChapterId,
|
||||||
readerPageIndex = page.readerPageIndex,
|
readerPageIndex = page.readerPageIndex,
|
||||||
documentIndex = page.documentIndex,
|
documentIndex = activePosition.documentIndex,
|
||||||
sourceHighlightStart = page.sourceStartOffset + segment.start + safeStart,
|
sourceSentenceStart = sentenceStart.sourceOffset,
|
||||||
sourceHighlightEndExclusive = page.sourceStartOffset + segment.start + safeEnd,
|
sourceSentenceEndExclusive = sentenceEnd.sourceOffset,
|
||||||
|
sourceHighlightStart = activeStart?.sourceOffset ?: -1L,
|
||||||
|
sourceHighlightEndExclusive = activeEnd?.sourceOffset ?: -1L,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun SpeechWindow.mapStart(windowOffset: Int): MappedPosition? {
|
||||||
|
if (text.isEmpty() || parts.isEmpty()) return null
|
||||||
|
val clamped = windowOffset.coerceIn(0, text.lastIndex)
|
||||||
|
val part = parts.firstOrNull { clamped in it.windowStart until it.windowEndExclusive }
|
||||||
|
?: parts.firstOrNull { it.windowStart > clamped }
|
||||||
|
?: parts.lastOrNull()
|
||||||
|
?: return null
|
||||||
|
val charIndex = clamped.coerceIn(part.windowStart, part.windowEndExclusive - 1)
|
||||||
|
return MappedPosition(
|
||||||
|
pagePosition = part.pagePosition,
|
||||||
|
documentIndex = part.documentIndex,
|
||||||
|
sourceOffset = part.sourceStartOffset + (charIndex - part.windowStart),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun SpeechWindow.mapEnd(windowEndExclusive: Int): MappedPosition? {
|
||||||
|
if (text.isEmpty() || parts.isEmpty()) return null
|
||||||
|
val clamped = (windowEndExclusive - 1).coerceIn(0, text.lastIndex)
|
||||||
|
val part = parts.firstOrNull { clamped in it.windowStart until it.windowEndExclusive }
|
||||||
|
?: parts.lastOrNull { it.windowEndExclusive <= clamped + 1 }
|
||||||
|
?: parts.firstOrNull()
|
||||||
|
?: return null
|
||||||
|
val charIndex = clamped.coerceIn(part.windowStart, part.windowEndExclusive - 1)
|
||||||
|
return MappedPosition(
|
||||||
|
pagePosition = part.pagePosition,
|
||||||
|
documentIndex = part.documentIndex,
|
||||||
|
sourceOffset = part.sourceStartOffset + (charIndex - part.windowStart) + 1L,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private fun refreshNotification() {
|
private fun refreshNotification() {
|
||||||
if (session == null) return
|
if (session == null) return
|
||||||
startForeground(Notifications.ID_READER_TTS, buildNotification(preparing = false))
|
startForeground(Notifications.ID_READER_TTS, buildNotification(preparing = false))
|
||||||
@@ -314,30 +545,45 @@ class ReaderTtsService : Service() {
|
|||||||
private val progressListener = object : UtteranceProgressListener() {
|
private val progressListener = object : UtteranceProgressListener() {
|
||||||
override fun onStart(utteranceId: String?) {
|
override fun onStart(utteranceId: String?) {
|
||||||
mainHandler.post {
|
mainHandler.post {
|
||||||
if (!playing) return@post
|
if (!playing || utteranceId != currentUtteranceId) return@post
|
||||||
currentSegments.getOrNull(segmentPosition)?.let(::publishHighlight)
|
currentSegments.getOrNull(segmentPosition)?.let { segment ->
|
||||||
|
publishHighlight(
|
||||||
|
segment = segment,
|
||||||
|
relativeStart = currentUtteranceBaseOffset,
|
||||||
|
relativeEndExclusive = (currentUtteranceBaseOffset + 1).coerceAtMost(segment.text.length),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) {
|
override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) {
|
||||||
mainHandler.post {
|
mainHandler.post {
|
||||||
if (!playing) return@post
|
if (!playing || utteranceId != currentUtteranceId) return@post
|
||||||
currentSegments.getOrNull(segmentPosition)?.let { segment ->
|
currentSegments.getOrNull(segmentPosition)?.let { segment ->
|
||||||
publishHighlight(segment, start, end)
|
val absoluteStart = (currentUtteranceBaseOffset + start).coerceIn(0, segment.text.length)
|
||||||
|
val absoluteEnd = (currentUtteranceBaseOffset + end).coerceIn(absoluteStart, segment.text.length)
|
||||||
|
segmentSpeechOffset = absoluteStart
|
||||||
|
publishHighlight(segment, absoluteStart, absoluteEnd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDone(utteranceId: String?) {
|
override fun onDone(utteranceId: String?) {
|
||||||
mainHandler.post {
|
mainHandler.post {
|
||||||
if (playing) advanceSegment()
|
if (!playing || utteranceId != currentUtteranceId) return@post
|
||||||
|
currentUtteranceId = null
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
advanceSegment()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Deprecated in Java")
|
@Deprecated("Deprecated in Java")
|
||||||
override fun onError(utteranceId: String?) {
|
override fun onError(utteranceId: String?) {
|
||||||
mainHandler.post {
|
mainHandler.post {
|
||||||
if (playing) advanceSegment()
|
if (!playing || utteranceId != currentUtteranceId) return@post
|
||||||
|
currentUtteranceId = null
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
advanceSegment()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,11 +666,32 @@ class ReaderTtsService : Service() {
|
|||||||
val text: String,
|
val text: String,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private data class SpeechWindow(
|
||||||
|
val text: String,
|
||||||
|
val parts: List<WindowPart>,
|
||||||
|
)
|
||||||
|
|
||||||
|
private data class WindowPart(
|
||||||
|
val windowStart: Int,
|
||||||
|
val windowEndExclusive: Int,
|
||||||
|
val pagePosition: Int,
|
||||||
|
val documentIndex: Int,
|
||||||
|
val sourceStartOffset: Long,
|
||||||
|
)
|
||||||
|
|
||||||
|
private data class MappedPosition(
|
||||||
|
val pagePosition: Int,
|
||||||
|
val documentIndex: Int,
|
||||||
|
val sourceOffset: Long,
|
||||||
|
)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START"
|
const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START"
|
||||||
const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE"
|
const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE"
|
||||||
const val ACTION_STOP = "eu.kanade.tachiyomi.reader.tts.STOP"
|
const val ACTION_STOP = "eu.kanade.tachiyomi.reader.tts.STOP"
|
||||||
const val ACTION_SET_SPEED = "eu.kanade.tachiyomi.reader.tts.SET_SPEED"
|
const val ACTION_SET_SPEED = "eu.kanade.tachiyomi.reader.tts.SET_SPEED"
|
||||||
|
const val ACTION_SET_ENGINE = "eu.kanade.tachiyomi.reader.tts.SET_ENGINE"
|
||||||
|
const val ACTION_REFRESH_VOICE = "eu.kanade.tachiyomi.reader.tts.REFRESH_VOICE"
|
||||||
|
|
||||||
const val EXTRA_SESSION_ID = "session_id"
|
const val EXTRA_SESSION_ID = "session_id"
|
||||||
const val EXTRA_START_PAGE_INDEX = "start_page_index"
|
const val EXTRA_START_PAGE_INDEX = "start_page_index"
|
||||||
@@ -442,6 +709,9 @@ class ReaderTtsService : Service() {
|
|||||||
private const val MIN_SPEED = 1.2f
|
private const val MIN_SPEED = 1.2f
|
||||||
private const val MAX_SPEED = 3.0f
|
private const val MAX_SPEED = 3.0f
|
||||||
private const val MAX_SEGMENT_CHARS = 1200
|
private const val MAX_SEGMENT_CHARS = 1200
|
||||||
|
private const val MAX_LOOKAHEAD_PAGES = 3
|
||||||
|
private const val MAX_SPEECH_WINDOW_CHARS = 6000
|
||||||
|
private val SENTENCE_TERMINATORS = setOf('.', '!', '?', '。', '!', '?')
|
||||||
|
|
||||||
private const val REQUEST_TOGGLE = 9101
|
private const val REQUEST_TOGGLE = 9101
|
||||||
private const val REQUEST_STOP = 9102
|
private const val REQUEST_STOP = 9102
|
||||||
|
|||||||
Reference in New Issue
Block a user