Add background TTS for EPUB and TXT reader

This commit is contained in:
BookOasis Reader
2026-09-26 21:13:45 +09:00
parent 74cb92278f
commit e8ce988974
14 changed files with 958 additions and 17 deletions
+6
View File
@@ -32,6 +32,7 @@
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<application
android:name=".App"
@@ -209,6 +210,11 @@
android:exported="false"
android:foregroundServiceType="shortService" />
<service
android:name=".ui.reader.tts.ReaderTtsService"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="false"
@@ -73,6 +73,9 @@ fun ReaderAppBars(
onClickOrientation: () -> Unit,
cropEnabled: Boolean,
onClickCropBorder: () -> Unit,
ttsAvailable: Boolean,
ttsPlaying: Boolean,
onClickTts: () -> Unit,
onClickSettings: () -> Unit,
) {
val backgroundColor = MaterialTheme.colorScheme
@@ -172,6 +175,9 @@ fun ReaderAppBars(
onClickOrientation = onClickOrientation,
cropEnabled = cropEnabled,
onClickCropBorder = onClickCropBorder,
ttsAvailable = ttsAvailable,
ttsPlaying = ttsPlaying,
onClickTts = onClickTts,
onClickSettings = onClickSettings,
)
}
@@ -25,6 +25,9 @@ fun ReaderBottomBar(
onClickOrientation: () -> Unit,
cropEnabled: Boolean,
onClickCropBorder: () -> Unit,
ttsAvailable: Boolean,
ttsPlaying: Boolean,
onClickTts: () -> Unit,
onClickSettings: () -> Unit,
modifier: Modifier = Modifier,
) {
@@ -55,6 +58,17 @@ fun ReaderBottomBar(
)
}
if (ttsAvailable) {
IconButton(onClick = onClickTts) {
Icon(
painter = painterResource(if (ttsPlaying) R.drawable.ic_pause_24dp else R.drawable.ic_play_arrow_24dp),
contentDescription = stringResource(
if (ttsPlaying) MR.strings.reader_tts_pause else MR.strings.reader_tts_start,
),
)
}
}
IconButton(onClick = onClickSettings) {
Icon(
imageVector = MaterialSymbols.Rounded.Settings,
@@ -1,16 +1,23 @@
package eu.kanade.presentation.reader.settings
import android.content.Intent
import android.speech.tts.TextToSpeech
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.FilterChip
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences
import eu.kanade.tachiyomi.ui.reader.setting.ReaderSettingsViewModel
import eu.kanade.tachiyomi.ui.reader.tts.ReaderTtsController
import eu.kanade.tachiyomi.ui.reader.tts.ReaderTtsService
import tachiyomi.i18n.MR
import tachiyomi.presentation.core.components.SettingsChipRow
import tachiyomi.presentation.core.components.SliderItem
@@ -26,6 +33,8 @@ private val fontFamilies = listOf(
@Composable
internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) {
val context = LocalContext.current
val fontSizePref = viewModel.preferences.textFontSize
val fontSize by fontSizePref.collectAsState()
SliderItem(
@@ -86,6 +95,69 @@ internal fun ColumnScope.TextSettingsPage(viewModel: ReaderSettingsViewModel) {
}
}
val ttsSpeedPref = viewModel.preferences.textTtsSpeedStep
val ttsSpeedStep by ttsSpeedPref.collectAsState()
SliderItem(
label = stringResource(MR.strings.pref_text_reader_tts_speed),
value = ttsSpeedStep,
valueRange = 6..15,
steps = 8,
valueString = String.format("%.1fx", ttsSpeedStep / 5f),
onChange = { value ->
ttsSpeedPref.set(value)
ReaderTtsController.setSpeed(context, value / 5f)
},
pillColor = MaterialTheme.colorScheme.surfaceContainerHighest,
)
val enginePref = viewModel.preferences.textTtsEnginePackage
val selectedEngine by enginePref.collectAsState()
val installedEngines = remember {
@Suppress("DEPRECATION")
context.packageManager.queryIntentServices(
Intent(TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE),
0,
)
.mapNotNull { info ->
val serviceInfo = info.serviceInfo ?: return@mapNotNull null
val packageName = serviceInfo.packageName ?: return@mapNotNull null
packageName to info.loadLabel(context.packageManager).toString()
}
.distinctBy { it.first }
.sortedBy { it.second.lowercase() }
}
SettingsChipRow(MR.strings.pref_text_reader_tts_engine) {
FilterChip(
selected = selectedEngine == ReaderTtsService.AUTO_ENGINE,
onClick = { enginePref.set(ReaderTtsService.AUTO_ENGINE) },
label = { Text(stringResource(MR.strings.pref_text_reader_tts_engine_auto)) },
)
FilterChip(
selected = selectedEngine == ReaderTtsService.SYSTEM_DEFAULT_ENGINE,
onClick = { enginePref.set(ReaderTtsService.SYSTEM_DEFAULT_ENGINE) },
label = { Text(stringResource(MR.strings.pref_text_reader_tts_engine_system)) },
)
installedEngines.forEach { (packageName, label) ->
FilterChip(
selected = selectedEngine == packageName,
onClick = { enginePref.set(packageName) },
label = { Text(label) },
)
}
}
TextButton(
onClick = {
runCatching {
context.startActivity(Intent("com.android.settings.TTS_SETTINGS"))
}
},
modifier = Modifier.padding(horizontal = 16.dp),
) {
Text(stringResource(MR.strings.pref_text_reader_tts_voice_settings))
}
Text(
text = stringResource(MR.strings.pref_text_reader_pdf_note),
style = MaterialTheme.typography.bodySmall,
@@ -21,6 +21,10 @@ object Notifications {
const val CHANNEL_COMMON = "common_channel"
const val ID_DOWNLOAD_IMAGE = 2
/** Foreground notification used by EPUB/TXT text-to-speech playback. */
const val CHANNEL_READER_TTS = "reader_tts_channel"
const val ID_READER_TTS = -801
/**
* Notification channel and ids used by the library updater.
*/
@@ -119,6 +123,10 @@ object Notifications {
buildNotificationChannel(CHANNEL_COMMON, IMPORTANCE_LOW) {
setName(context.stringResource(MR.strings.channel_common))
},
buildNotificationChannel(CHANNEL_READER_TTS, IMPORTANCE_LOW) {
setName("Text-to-speech")
setShowBadge(false)
},
buildNotificationChannel(CHANNEL_LIBRARY_PROGRESS, IMPORTANCE_LOW) {
setName(context.stringResource(MR.strings.channel_progress))
setGroup(GROUP_LIBRARY)
@@ -42,6 +42,8 @@ import androidx.core.transition.doOnEnd
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.google.android.material.transition.platform.MaterialContainerTransform
import dev.zacsweers.metro.Inject
@@ -65,6 +67,7 @@ import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.reader.ReaderViewModel.SetAsCoverResult.AddToLibraryFirst
import eu.kanade.tachiyomi.ui.reader.ReaderViewModel.SetAsCoverResult.Error
import eu.kanade.tachiyomi.ui.reader.ReaderViewModel.SetAsCoverResult.Success
import eu.kanade.tachiyomi.ui.reader.loader.TextReaderPage
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters
@@ -72,6 +75,8 @@ import eu.kanade.tachiyomi.ui.reader.setting.ReaderOrientation
import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences
import eu.kanade.tachiyomi.ui.reader.setting.ReaderSettingsViewModel
import eu.kanade.tachiyomi.ui.reader.setting.ReadingMode
import eu.kanade.tachiyomi.ui.reader.tts.ReaderTtsController
import eu.kanade.tachiyomi.ui.reader.tts.ReaderTtsSessionStore
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressIndicator
import eu.kanade.tachiyomi.ui.reader.viewer.pager.R2LPagerViewer
import eu.kanade.tachiyomi.ui.reader.viewer.webgpu.WebGpuViewer
@@ -137,6 +142,7 @@ class ReaderActivity : BaseActivity() {
private val windowInsetsController by lazy { WindowInsetsControllerCompat(window, window.decorView) }
private var loadingIndicator: ReaderProgressIndicator? = null
private var ttsHighlightedPage: TextReaderPage? = null
var isScrollingThroughPages = false
private set
@@ -217,6 +223,12 @@ class ReaderActivity : BaseActivity() {
.onEach(::setChapters)
.launchIn(lifecycleScope)
ReaderTtsController.state
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.sample(0.2.seconds)
.onEach(::applyTtsState)
.launchIn(lifecycleScope)
viewModel.eventFlow
.onEach { event ->
when (event) {
@@ -473,6 +485,10 @@ class ReaderActivity : BaseActivity() {
)
val verticalNavigatorOnLeft by readerPreferences.verticalNavigatorOnLeft.collectAsState()
val verticalNavigatorHeight by readerPreferences.verticalNavigatorHeight.collectAsState()
val ttsState by ReaderTtsController.state.collectAsState()
val ttsAvailable = state.currentChapter?.pages?.any { it is TextReaderPage } == true
val ttsForCurrentChapter = ttsState.active &&
ttsState.readerChapterId == state.currentChapter?.chapter?.id
ReaderAppBars(
visible = state.menuVisible,
@@ -529,6 +545,9 @@ class ReaderActivity : BaseActivity() {
menuToggleToast?.cancel()
menuToggleToast = toast(if (enabled) MR.strings.on else MR.strings.off)
},
ttsAvailable = ttsAvailable,
ttsPlaying = ttsForCurrentChapter && ttsState.playing,
onClickTts = ::toggleTts,
onClickSettings = viewModel::openSettingsDialog,
)
}
@@ -675,6 +694,98 @@ class ReaderActivity : BaseActivity() {
viewer.moveToPage(page)
}
private fun toggleTts() {
val state = viewModel.state.value
val chapter = state.currentChapter ?: return
val chapterId = chapter.chapter.id ?: return
val ttsState = ReaderTtsController.state.value
if (ttsState.active && ttsState.readerChapterId == chapterId) {
ReaderTtsController.toggle(this)
return
}
val textPages = chapter.pages?.filterIsInstance<TextReaderPage>().orEmpty()
if (textPages.isEmpty()) return
val startPageIndex = (state.currentPage - 1).coerceAtLeast(0)
val title = state.manga?.title ?: chapter.chapter.name
val subtitle = chapter.chapter.name
val speed = readerPreferences.textTtsSpeedStep.get().coerceIn(6, 15) / 5f
val enginePackage = readerPreferences.textTtsEnginePackage.get()
lifecycleScope.launchIO {
val session = runCatching {
ReaderTtsSessionStore.create(this@ReaderActivity, textPages)
}.getOrElse { error ->
logcat(LogPriority.ERROR, error) { "Failed to create TTS text session" }
return@launchIO
}
ReaderTtsController.start(
context = this@ReaderActivity,
sessionId = session.id,
startReaderPageIndex = startPageIndex,
readerChapterId = chapterId,
title = title,
subtitle = subtitle,
speed = speed,
enginePackage = enginePackage,
)
}
}
private fun applyTtsState(ttsState: ReaderTtsController.State) {
val state = viewModel.state.value
val chapter = state.currentChapter
val chapterId = chapter?.chapter?.id
if (!ttsState.active || chapter == null || chapterId != ttsState.readerChapterId) {
if (ttsHighlightedPage != null) {
ttsHighlightedPage?.clearTtsHighlight()
ttsHighlightedPage = null
state.viewer?.refreshCurrentPage()
}
return
}
val textPages = chapter.pages?.filterIsInstance<TextReaderPage>().orEmpty()
if (textPages.isEmpty()) return
val targetPage = if (ttsState.sourceHighlightStart >= 0L) {
textPages.firstOrNull { page ->
page.anchor.documentIndex == ttsState.documentIndex &&
ttsState.sourceHighlightStart >= page.anchor.startOffset &&
ttsState.sourceHighlightStart < page.anchor.endOffsetExclusive
}
} else {
chapter.pages?.getOrNull(ttsState.readerPageIndex) as? TextReaderPage
} ?: return
if (ttsHighlightedPage !== targetPage) {
ttsHighlightedPage?.clearTtsHighlight()
}
if (
ttsState.sourceHighlightStart >= 0L &&
ttsState.sourceHighlightEndExclusive > ttsState.sourceHighlightStart
) {
val start = maxOf(ttsState.sourceHighlightStart, targetPage.anchor.startOffset)
val end = minOf(ttsState.sourceHighlightEndExclusive, targetPage.anchor.endOffsetExclusive)
targetPage.updateTtsHighlight(
start = (start - targetPage.anchor.startOffset).toInt(),
endExclusive = (end - targetPage.anchor.startOffset).toInt(),
)
} else {
targetPage.clearTtsHighlight()
}
ttsHighlightedPage = targetPage
if (state.currentPage != targetPage.index + 1) {
moveToPageIndex(targetPage.index)
}
state.viewer?.refreshCurrentPage()
}
/**
* Tells the presenter to load the next chapter and mark it as active. The progress dialog
* should be automatically shown.
@@ -10,6 +10,7 @@ import android.text.Spannable
import android.text.SpannableString
import android.text.StaticLayout
import android.text.TextPaint
import android.text.style.BackgroundColorSpan
import android.text.style.LineHeightSpan
import eu.kanade.tachiyomi.ui.reader.setting.TextReaderStyle
import java.io.ByteArrayInputStream
@@ -115,14 +116,15 @@ internal object TextPageRenderer {
slice: PageSlice,
baseOffset: Long,
): PaginatedTextPage? {
val pageText = text.substring(slice.start, slice.endExclusive)
.trim()
.takeIf { it.isNotEmpty() }
?: return null
val rawPageText = text.substring(slice.start, slice.endExclusive)
val leading = rawPageText.indexOfFirst { !it.isWhitespace() }
if (leading < 0) return null
val trailing = rawPageText.indexOfLast { !it.isWhitespace() }
val pageText = rawPageText.substring(leading, trailing + 1)
return PaginatedTextPage(
text = pageText,
startOffset = baseOffset + slice.start,
endOffsetExclusive = baseOffset + slice.endExclusive,
startOffset = baseOffset + slice.start + leading,
endOffsetExclusive = baseOffset + slice.start + trailing + 1,
)
}
@@ -153,7 +155,11 @@ internal object TextPageRenderer {
return pages.ifEmpty { listOf(PageSlice(0, text.length)) }
}
fun render(text: String, style: TextReaderStyle): InputStream {
fun render(
text: String,
style: TextReaderStyle,
highlightRange: IntRange? = null,
): InputStream {
val metrics = resolveLayoutMetrics(style)
val bitmap = Bitmap.createBitmap(metrics.pageWidth, metrics.pageHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
@@ -161,7 +167,7 @@ internal object TextPageRenderer {
canvas.drawColor(backgroundColor)
canvas.save()
canvas.translate(metrics.horizontalMargin.toFloat(), metrics.verticalMargin.toFloat())
createLayout(text, style, metrics.contentWidth).draw(canvas)
createLayout(text, style, metrics.contentWidth, highlightRange).draw(canvas)
canvas.restore()
val output = ByteArrayOutputStream()
@@ -174,6 +180,7 @@ internal object TextPageRenderer {
text: String,
style: TextReaderStyle,
contentWidth: Int,
highlightRange: IntRange? = null,
): StaticLayout {
val (_, textColor) = resolveColors(style)
val paint = TextPaint().apply {
@@ -184,7 +191,7 @@ internal object TextPageRenderer {
?.let { Typeface.create(it, Typeface.NORMAL) }
?: Typeface.DEFAULT
}
val styledText = addParagraphSpacing(text, style.paragraphSpacingPx)
val styledText = addTextSpans(text, style, highlightRange)
return StaticLayout.Builder
.obtain(styledText, 0, styledText.length, paint, contentWidth)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
@@ -229,15 +236,41 @@ internal object TextPageRenderer {
return background to foreground
}
private fun addParagraphSpacing(text: String, spacing: Int): CharSequence {
if (spacing <= 0 || '\n' !in text) return text
private fun addTextSpans(
text: String,
style: TextReaderStyle,
highlightRange: IntRange?,
): CharSequence {
val needsParagraphSpacing = style.paragraphSpacingPx > 0 && '\n' in text
val needsHighlight = highlightRange != null && !highlightRange.isEmpty()
if (!needsParagraphSpacing && !needsHighlight) return text
val spannable = SpannableString(text)
text.forEachIndexed { index, char ->
if (char == '\n') {
if (needsParagraphSpacing) {
text.forEachIndexed { index, char ->
if (char == '\n') {
spannable.setSpan(
ParagraphSpacingSpan(style.paragraphSpacingPx),
index,
(index + 1).coerceAtMost(text.length),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
)
}
}
}
if (needsHighlight) {
val start = highlightRange!!.first.coerceIn(0, text.length)
val endExclusive = (highlightRange.last + 1).coerceIn(start, text.length)
if (start < endExclusive) {
val highlightColor = if (resolveColors(style).first == Color.BLACK) {
Color.rgb(92, 82, 24)
} else {
Color.rgb(255, 245, 157)
}
spannable.setSpan(
ParagraphSpacingSpan(spacing),
index,
(index + 1).coerceAtMost(text.length),
BackgroundColorSpan(highlightColor),
start,
endExclusive,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
)
}
@@ -30,8 +30,11 @@ internal class TextReaderPage(
@Volatile
private var style: TextReaderStyle = initialStyle
@Volatile
private var highlightRange: IntRange? = null
init {
stream = { TextPageRenderer.render(pageTextProvider(), style) }
stream = { TextPageRenderer.render(pageTextProvider(), style, highlightRange) }
status = Page.State.Ready
}
@@ -45,4 +48,14 @@ internal class TextReaderPage(
fun updateStyle(newStyle: TextReaderStyle) {
style = newStyle
}
fun readText(): String = pageTextProvider()
fun updateTtsHighlight(start: Int, endExclusive: Int) {
highlightRange = if (start >= 0 && endExclusive > start) start until endExclusive else null
}
fun clearTtsHighlight() {
highlightRange = null
}
}
@@ -125,6 +125,12 @@ class ReaderPreferences(
TextFontFamily.SYSTEM,
)
// TTS speed is stored as fifths: 6 = 1.2x, 15 = 3.0x.
val textTtsSpeedStep: Preference<Int> = preferenceStore.getInt("reader_text_tts_speed_step", 6)
// auto: prefer Google TTS when installed, then fall back to the Android system default.
val textTtsEnginePackage: Preference<String> = preferenceStore.getString("reader_text_tts_engine", "auto")
private val textUnitsMigrated = preferenceStore.getBoolean("reader_text_units_v2_migrated", false)
init {
@@ -0,0 +1,73 @@
package eu.kanade.tachiyomi.ui.reader.tts
import android.content.Context
import android.content.Intent
import androidx.core.content.ContextCompat
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
object ReaderTtsController {
data class State(
val active: Boolean = false,
val playing: Boolean = false,
val readerChapterId: Long = -1L,
val readerPageIndex: Int = -1,
val documentIndex: Int = -1,
val sourceHighlightStart: Long = -1L,
val sourceHighlightEndExclusive: Long = -1L,
)
private val mutableState = MutableStateFlow(State())
val state = mutableState.asStateFlow()
internal fun updateState(state: State) {
mutableState.value = state
}
fun start(
context: Context,
sessionId: String,
startReaderPageIndex: Int,
readerChapterId: Long,
title: String,
subtitle: String?,
speed: Float,
enginePackage: String,
) {
val intent = Intent(context, ReaderTtsService::class.java).apply {
action = ReaderTtsService.ACTION_START
putExtra(ReaderTtsService.EXTRA_SESSION_ID, sessionId)
putExtra(ReaderTtsService.EXTRA_START_PAGE_INDEX, startReaderPageIndex)
putExtra(ReaderTtsService.EXTRA_CHAPTER_ID, readerChapterId)
putExtra(ReaderTtsService.EXTRA_TITLE, title)
putExtra(ReaderTtsService.EXTRA_SUBTITLE, subtitle)
putExtra(ReaderTtsService.EXTRA_SPEED, speed)
putExtra(ReaderTtsService.EXTRA_ENGINE_PACKAGE, enginePackage)
}
ContextCompat.startForegroundService(context, intent)
}
fun toggle(context: Context) {
if (!state.value.active) return
context.startService(
Intent(context, ReaderTtsService::class.java).setAction(ReaderTtsService.ACTION_TOGGLE),
)
}
fun stop(context: Context) {
if (!state.value.active) return
context.startService(
Intent(context, ReaderTtsService::class.java).setAction(ReaderTtsService.ACTION_STOP),
)
}
fun setSpeed(context: Context, speed: Float) {
if (!state.value.active) return
context.startService(
Intent(context, ReaderTtsService::class.java)
.setAction(ReaderTtsService.ACTION_SET_SPEED)
.putExtra(ReaderTtsService.EXTRA_SPEED, speed),
)
}
}
@@ -0,0 +1,449 @@
package eu.kanade.tachiyomi.ui.reader.tts
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.media.AudioAttributes
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener
import androidx.core.app.NotificationCompat
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.util.system.notificationBuilder
import java.text.BreakIterator
import java.util.Locale
import kotlin.math.min
class ReaderTtsService : Service() {
private val mainHandler = Handler(Looper.getMainLooper())
private var tts: TextToSpeech? = null
private var sessionId: String? = null
private var session: ReaderTtsSessionStore.SessionReader? = null
private var pagePosition = 0
private var currentPageText: String? = null
private var currentSegments: List<Segment> = emptyList()
private var segmentPosition = 0
private var playbackSpeed = DEFAULT_SPEED
private var readerChapterId = -1L
private var title = "Text reader"
private var subtitle: String? = null
private var playing = false
private var initialized = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
ACTION_START -> startSession(intent)
ACTION_TOGGLE -> if (playing) pausePlayback() else resumePlayback()
ACTION_STOP -> stopPlayback()
ACTION_SET_SPEED -> {
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, playbackSpeed).coerceIn(MIN_SPEED, MAX_SPEED)
tts?.setSpeechRate(playbackSpeed)
refreshNotification()
}
}
return START_NOT_STICKY
}
private fun startSession(intent: Intent) {
val newSessionId = intent.getStringExtra(EXTRA_SESSION_ID) ?: run {
stopSelf()
return
}
val newSession = ReaderTtsSessionStore.open(this, newSessionId) ?: run {
ReaderTtsSessionStore.delete(this, newSessionId)
stopSelf()
return
}
cleanupTts()
session?.close()
ReaderTtsSessionStore.delete(this, sessionId)
sessionId = newSessionId
session = newSession
readerChapterId = intent.getLongExtra(EXTRA_CHAPTER_ID, -1L)
title = intent.getStringExtra(EXTRA_TITLE).orEmpty().ifBlank { "Text reader" }
subtitle = intent.getStringExtra(EXTRA_SUBTITLE)
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, DEFAULT_SPEED).coerceIn(MIN_SPEED, MAX_SPEED)
val requestedPageIndex = intent.getIntExtra(EXTRA_START_PAGE_INDEX, 0)
pagePosition = newSession.pages.indexOfFirst { it.readerPageIndex >= requestedPageIndex }
.takeIf { it >= 0 }
?: 0
currentPageText = null
currentSegments = emptyList()
segmentPosition = 0
playing = false
initialized = false
startForeground(Notifications.ID_READER_TTS, buildNotification(preparing = true))
ReaderTtsController.updateState(
ReaderTtsController.State(
active = true,
playing = false,
readerChapterId = readerChapterId,
readerPageIndex = newSession.pages[pagePosition].readerPageIndex,
documentIndex = newSession.pages[pagePosition].documentIndex,
),
)
initializeTts(intent.getStringExtra(EXTRA_ENGINE_PACKAGE).orEmpty())
}
private fun initializeTts(enginePreference: String) {
val engine = resolveEngine(enginePreference)
val listener = TextToSpeech.OnInitListener { status ->
mainHandler.post {
if (status != TextToSpeech.SUCCESS) {
if (engine != null) {
cleanupTts()
initializeTts(SYSTEM_DEFAULT_ENGINE)
} else {
stopPlayback()
}
return@post
}
initialized = true
tts?.apply {
setSpeechRate(playbackSpeed)
setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build(),
)
setOnUtteranceProgressListener(progressListener)
}
playing = true
updatePlaybackState()
speakCurrentSegment()
}
}
tts = if (engine == null) {
TextToSpeech(this, listener)
} else {
TextToSpeech(this, listener, engine)
}
}
private fun resolveEngine(preference: String): String? {
if (preference == SYSTEM_DEFAULT_ENGINE) return null
if (preference.isNotBlank() && preference != AUTO_ENGINE) return preference
@Suppress("DEPRECATION")
val installed = packageManager.queryIntentServices(
Intent(TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE),
0,
)
return installed
.asSequence()
.mapNotNull { it.serviceInfo?.packageName }
.firstOrNull { it == GOOGLE_TTS_PACKAGE }
}
private fun pausePlayback() {
if (!initialized || !playing) return
tts?.stop()
playing = false
updatePlaybackState(clearHighlight = false)
}
private fun resumePlayback() {
if (!initialized || session == null) return
playing = true
updatePlaybackState(clearHighlight = false)
speakCurrentSegment()
}
private fun speakCurrentSegment() {
if (!playing || !initialized) return
val session = session ?: return stopPlayback()
if (currentPageText == null) {
currentPageText = session.readPage(pagePosition)
currentSegments = segmentText(currentPageText.orEmpty())
segmentPosition = 0
}
if (currentSegments.isEmpty() || segmentPosition !in currentSegments.indices) {
advancePage()
return
}
val segment = currentSegments[segmentPosition]
val utteranceId = pagePosition.toString() + ":" + segmentPosition
val result = tts?.speak(
segment.text,
TextToSpeech.QUEUE_FLUSH,
null,
utteranceId,
)
if (result == TextToSpeech.ERROR) advanceSegment()
}
private fun advanceSegment() {
segmentPosition++
if (segmentPosition >= currentSegments.size) advancePage() else speakCurrentSegment()
}
private fun advancePage() {
val session = session ?: return stopPlayback()
if (pagePosition + 1 >= session.pages.size) {
stopPlayback()
return
}
pagePosition++
currentPageText = null
currentSegments = emptyList()
segmentPosition = 0
speakCurrentSegment()
}
private fun stopPlayback() {
playing = false
cleanupTts()
session?.close()
session = null
currentPageText = null
currentSegments = emptyList()
val oldSessionId = sessionId
sessionId = null
ReaderTtsSessionStore.delete(this, oldSessionId)
ReaderTtsController.updateState(ReaderTtsController.State())
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
}
private fun cleanupTts() {
initialized = false
tts?.stop()
tts?.shutdown()
tts = null
}
private fun updatePlaybackState(clearHighlight: Boolean = true) {
val session = session ?: return
val page = session.pages.getOrNull(pagePosition) ?: return
val previous = ReaderTtsController.state.value
ReaderTtsController.updateState(
ReaderTtsController.State(
active = true,
playing = playing,
readerChapterId = readerChapterId,
readerPageIndex = page.readerPageIndex,
documentIndex = page.documentIndex,
sourceHighlightStart = if (clearHighlight) -1L else previous.sourceHighlightStart,
sourceHighlightEndExclusive = if (clearHighlight) -1L else previous.sourceHighlightEndExclusive,
),
)
refreshNotification()
}
private fun publishHighlight(
segment: Segment,
relativeStart: Int = 0,
relativeEndExclusive: Int = segment.text.length,
) {
val session = session ?: return
val page = session.pages.getOrNull(pagePosition) ?: return
val safeStart = relativeStart.coerceIn(0, segment.text.length)
val safeEnd = relativeEndExclusive.coerceIn(safeStart, segment.text.length)
ReaderTtsController.updateState(
ReaderTtsController.State(
active = true,
playing = playing,
readerChapterId = readerChapterId,
readerPageIndex = page.readerPageIndex,
documentIndex = page.documentIndex,
sourceHighlightStart = page.sourceStartOffset + segment.start + safeStart,
sourceHighlightEndExclusive = page.sourceStartOffset + segment.start + safeEnd,
),
)
}
private fun refreshNotification() {
if (session == null) return
startForeground(Notifications.ID_READER_TTS, buildNotification(preparing = false))
}
private fun buildNotification(preparing: Boolean): android.app.Notification {
val toggleIntent = PendingIntent.getService(
this,
REQUEST_TOGGLE,
Intent(this, ReaderTtsService::class.java).setAction(ACTION_TOGGLE),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
val stopIntent = PendingIntent.getService(
this,
REQUEST_STOP,
Intent(this, ReaderTtsService::class.java).setAction(ACTION_STOP),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
return notificationBuilder(Notifications.CHANNEL_READER_TTS) {
setSmallIcon(R.drawable.ic_mihon)
setOngoing(true)
setOnlyAlertOnce(true)
setShowWhen(false)
setCategory(NotificationCompat.CATEGORY_TRANSPORT)
setContentTitle(title)
setContentText(
when {
preparing -> "TTS preparing"
subtitle.isNullOrBlank() -> String.format(Locale.US, "TTS %.1fx", playbackSpeed)
else -> subtitle + " · " + String.format(Locale.US, "%.1fx", playbackSpeed)
},
)
addAction(
if (playing) R.drawable.ic_pause_24dp else R.drawable.ic_play_arrow_24dp,
if (playing) "Pause" else "Play",
toggleIntent,
)
addAction(R.drawable.ic_close_24dp, "Stop", stopIntent)
}.build()
}
private val progressListener = object : UtteranceProgressListener() {
override fun onStart(utteranceId: String?) {
mainHandler.post {
if (!playing) return@post
currentSegments.getOrNull(segmentPosition)?.let(::publishHighlight)
}
}
override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) {
mainHandler.post {
if (!playing) return@post
currentSegments.getOrNull(segmentPosition)?.let { segment ->
publishHighlight(segment, start, end)
}
}
}
override fun onDone(utteranceId: String?) {
mainHandler.post {
if (playing) advanceSegment()
}
}
@Deprecated("Deprecated in Java")
override fun onError(utteranceId: String?) {
mainHandler.post {
if (playing) advanceSegment()
}
}
override fun onError(utteranceId: String?, errorCode: Int) {
onError(utteranceId)
}
}
override fun onDestroy() {
cleanupTts()
session?.close()
session = null
ReaderTtsSessionStore.delete(this, sessionId)
sessionId = null
ReaderTtsController.updateState(ReaderTtsController.State())
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? = null
private fun segmentText(text: String): List<Segment> {
if (text.isBlank()) return emptyList()
val segments = ArrayList<Segment>()
val iterator = BreakIterator.getSentenceInstance(Locale.getDefault())
iterator.setText(text)
var start = iterator.first()
var end = iterator.next()
while (end != BreakIterator.DONE) {
addBoundedSegments(text, start, end, segments)
start = end
end = iterator.next()
}
if (segments.isEmpty()) addBoundedSegments(text, 0, text.length, segments)
return segments
}
private fun addBoundedSegments(
text: String,
rawStart: Int,
rawEnd: Int,
out: MutableList<Segment>,
) {
var start = rawStart
var end = rawEnd
while (start < end && text[start].isWhitespace()) start++
while (end > start && text[end - 1].isWhitespace()) end--
if (start >= end) return
val maxLength = min(TextToSpeech.getMaxSpeechInputLength() - 64, MAX_SEGMENT_CHARS)
var cursor = start
while (cursor < end) {
var chunkEnd = min(cursor + maxLength, end)
if (chunkEnd < end) {
val preferredBreak = text.lastIndexOfAny(
charArrayOf(' ', '\n', ',', '.', '!', '?', '。', '!', '?'),
startIndex = chunkEnd,
)
if (preferredBreak > cursor + maxLength / 2) chunkEnd = preferredBreak + 1
}
val chunk = text.substring(cursor, chunkEnd)
val leading = chunk.indexOfFirst { !it.isWhitespace() }.coerceAtLeast(0)
val last = chunk.indexOfLast { !it.isWhitespace() }
if (last >= 0) {
out += Segment(
start = cursor + leading,
endExclusive = cursor + last + 1,
text = chunk.substring(leading, last + 1),
)
}
cursor = chunkEnd
}
}
private data class Segment(
val start: Int,
val endExclusive: Int,
val text: String,
)
companion object {
const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START"
const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE"
const val ACTION_STOP = "eu.kanade.tachiyomi.reader.tts.STOP"
const val ACTION_SET_SPEED = "eu.kanade.tachiyomi.reader.tts.SET_SPEED"
const val EXTRA_SESSION_ID = "session_id"
const val EXTRA_START_PAGE_INDEX = "start_page_index"
const val EXTRA_CHAPTER_ID = "chapter_id"
const val EXTRA_TITLE = "title"
const val EXTRA_SUBTITLE = "subtitle"
const val EXTRA_SPEED = "speed"
const val EXTRA_ENGINE_PACKAGE = "engine_package"
const val AUTO_ENGINE = "auto"
const val SYSTEM_DEFAULT_ENGINE = "system"
private const val GOOGLE_TTS_PACKAGE = "com.google.android.tts"
private const val DEFAULT_SPEED = 1.2f
private const val MIN_SPEED = 1.2f
private const val MAX_SPEED = 3.0f
private const val MAX_SEGMENT_CHARS = 1200
private const val REQUEST_TOGGLE = 9101
private const val REQUEST_STOP = 9102
}
}
@@ -0,0 +1,136 @@
package eu.kanade.tachiyomi.ui.reader.tts
import android.content.Context
import eu.kanade.tachiyomi.ui.reader.loader.TextReaderPage
import java.io.Closeable
import java.io.File
import java.io.RandomAccessFile
import java.nio.charset.StandardCharsets
import java.util.UUID
internal object ReaderTtsSessionStore {
data class PageEntry(
val readerPageIndex: Int,
val documentIndex: Int,
val sourceStartOffset: Long,
val sourceEndOffsetExclusive: Long,
val fileOffset: Long,
val byteLength: Int,
)
data class SessionInfo(
val id: String,
val pages: List<PageEntry>,
)
class SessionReader(
private val contentFile: File,
val pages: List<PageEntry>,
) : Closeable {
fun readPage(position: Int): String {
val entry = pages[position]
val bytes = ByteArray(entry.byteLength)
RandomAccessFile(contentFile, "r").use { input ->
input.seek(entry.fileOffset)
input.readFully(bytes)
}
return String(bytes, StandardCharsets.UTF_8)
}
override fun close() = Unit
}
fun create(
context: Context,
pages: List<TextReaderPage>,
): SessionInfo {
val dir = sessionDir(context)
pruneStale(dir)
val id = UUID.randomUUID().toString()
val contentFile = File(dir, "$id.utf8")
val indexFile = File(dir, "$id.idx")
val entries = ArrayList<PageEntry>(pages.size)
RandomAccessFile(contentFile, "rw").use { output ->
pages.forEach { page ->
val text = page.readText()
val bytes = text.toByteArray(StandardCharsets.UTF_8)
val fileOffset = output.filePointer
output.write(bytes)
entries += PageEntry(
readerPageIndex = page.index,
documentIndex = page.anchor.documentIndex,
sourceStartOffset = page.anchor.startOffset,
sourceEndOffsetExclusive = page.anchor.endOffsetExclusive,
fileOffset = fileOffset,
byteLength = bytes.size,
)
}
output.fd.sync()
}
indexFile.bufferedWriter(StandardCharsets.UTF_8).use { writer ->
entries.forEach { entry ->
writer.append(entry.readerPageIndex.toString())
.append('\t')
.append(entry.documentIndex.toString())
.append('\t')
.append(entry.sourceStartOffset.toString())
.append('\t')
.append(entry.sourceEndOffsetExclusive.toString())
.append('\t')
.append(entry.fileOffset.toString())
.append('\t')
.append(entry.byteLength.toString())
.append('\n')
}
}
return SessionInfo(id, entries)
}
fun open(context: Context, id: String): SessionReader? {
val dir = sessionDir(context)
val contentFile = File(dir, "$id.utf8")
val indexFile = File(dir, "$id.idx")
if (!contentFile.isFile || !indexFile.isFile) return null
val pages = indexFile.useLines { lines ->
lines.mapNotNull { line ->
val parts = line.split('\t')
if (parts.size != 6) return@mapNotNull null
PageEntry(
readerPageIndex = parts[0].toIntOrNull() ?: return@mapNotNull null,
documentIndex = parts[1].toIntOrNull() ?: return@mapNotNull null,
sourceStartOffset = parts[2].toLongOrNull() ?: return@mapNotNull null,
sourceEndOffsetExclusive = parts[3].toLongOrNull() ?: return@mapNotNull null,
fileOffset = parts[4].toLongOrNull() ?: return@mapNotNull null,
byteLength = parts[5].toIntOrNull() ?: return@mapNotNull null,
)
}.toList()
}
if (pages.isEmpty()) return null
return SessionReader(contentFile, pages)
}
fun delete(context: Context, id: String?) {
if (id.isNullOrBlank()) return
val dir = sessionDir(context)
File(dir, "$id.utf8").delete()
File(dir, "$id.idx").delete()
}
private fun sessionDir(context: Context): File {
return File(context.cacheDir, "reader_tts_sessions").apply { mkdirs() }
}
private fun pruneStale(dir: File) {
val cutoff = System.currentTimeMillis() - STALE_FILE_AGE_MS
dir.listFiles()
?.filter { it.isFile && it.lastModified() < cutoff }
?.forEach { it.delete() }
}
private const val STALE_FILE_AGE_MS = 24L * 60L * 60L * 1000L
}
@@ -1103,6 +1103,13 @@
<!-- Text reader -->
<string name="pref_category_text_reader">Text</string>
<string name="pref_text_reader_font_size">Font size</string>
<string name="pref_text_reader_tts_speed">TTS speed</string>
<string name="pref_text_reader_tts_engine">TTS engine</string>
<string name="pref_text_reader_tts_engine_auto">Google TTS preferred</string>
<string name="pref_text_reader_tts_engine_system">System default</string>
<string name="pref_text_reader_tts_voice_settings">Voice data settings</string>
<string name="reader_tts_start">Start TTS</string>
<string name="reader_tts_pause">Pause TTS</string>
<string name="pref_text_reader_line_spacing">Line spacing</string>
<string name="pref_text_reader_paragraph_spacing">Paragraph spacing</string>
<string name="pref_text_reader_font_family">Font</string>
@@ -911,6 +911,13 @@
<!-- Text reader -->
<string name="pref_category_text_reader">텍스트</string>
<string name="pref_text_reader_font_size">글자 크기</string>
<string name="pref_text_reader_tts_speed">TTS 배속</string>
<string name="pref_text_reader_tts_engine">TTS 엔진</string>
<string name="pref_text_reader_tts_engine_auto">Google TTS 우선</string>
<string name="pref_text_reader_tts_engine_system">시스템 기본</string>
<string name="pref_text_reader_tts_voice_settings">음성 데이터 설정</string>
<string name="reader_tts_start">TTS 시작</string>
<string name="reader_tts_pause">TTS 일시정지</string>
<string name="pref_text_reader_line_spacing">행간</string>
<string name="pref_text_reader_paragraph_spacing">문단 간격</string>
<string name="pref_text_reader_font_family">글꼴</string>