Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2f417d147 | ||
|
|
9dbe30b20c | ||
|
|
9cd8ebc422 | ||
|
|
85d97d5a90 | ||
|
|
f344befbdd | ||
|
|
dbf63c351c | ||
|
|
e8b18ec472 | ||
|
|
37abbf884c | ||
|
|
0084c478a3 | ||
|
|
6990e3c233 | ||
|
|
9f54c0aca0 | ||
|
|
7681590992 | ||
|
|
cde4d89eed | ||
|
|
2e9a52f4af | ||
|
|
2fddacb7e7 | ||
|
|
3e83185e4f | ||
|
|
65925f2a07 | ||
|
|
17c885b3f5 | ||
|
|
89b1115169 | ||
|
|
8e7dcc0307 | ||
|
|
50f201d3c6 | ||
|
|
fe4d569afa | ||
|
|
c7b7ca68ab | ||
|
|
de04b9cbed |
No files matched your search
@@ -18,3 +18,6 @@ captures
|
||||
**/xcshareddata/WorkspaceSettings.xcsettings
|
||||
node_modules/
|
||||
/composeApp/release/
|
||||
/test_books/
|
||||
/.junie/
|
||||
/.local/
|
||||
@@ -31,6 +31,8 @@ in your IDE’s toolbar or build it directly from the terminal:
|
||||
|
||||
### Build and Run Desktop (JVM) Application
|
||||
|
||||
Desktop offline English/Russian speech: [CPU TTS setup and audition](docs/desktop-tts.md).
|
||||
|
||||
To build and run the development version of the desktop app, use the run configuration from the run widget
|
||||
in your IDE’s toolbar or run it directly from the terminal:
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
val appVersionName = "1.0"
|
||||
val appVersionCode = 2
|
||||
val appVersionName = "1.1"
|
||||
val appVersionCode = 10
|
||||
val appVersionDisplay = "$appVersionName.$appVersionCode"
|
||||
|
||||
plugins {
|
||||
@@ -96,10 +96,29 @@ kotlin {
|
||||
jvmMain.dependencies {
|
||||
implementation(compose.desktop.currentOs)
|
||||
implementation(libs.kotlinx.coroutinesSwing)
|
||||
implementation(libs.onnxruntime)
|
||||
implementation(libs.jackson.databind)
|
||||
}
|
||||
jvmTest.dependencies {
|
||||
implementation(libs.kotlinx.coroutinesTest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.register<JavaExec>("ttsSample") {
|
||||
group = "verification"
|
||||
description = "Generate plain English/Russian WAV samples using the local Supertonic CPU model."
|
||||
dependsOn("jvmMainClasses")
|
||||
mainClass.set("net.sergeych.toread.tts.TtsSampleKt")
|
||||
classpath = configurations["jvmRuntimeClasspath"] + kotlin.targets["jvm"].compilations["main"].output.allOutputs
|
||||
workingDir = rootProject.projectDir
|
||||
args(layout.buildDirectory.dir("tts-samples").get().asFile.absolutePath)
|
||||
if (providers.gradleProperty("ttsPlay").orNull == "true") args("--play")
|
||||
if (providers.gradleProperty("ttsStress").orNull == "true") args("--stress")
|
||||
if (providers.gradleProperty("ttsRuAccent").orNull == "true") args("--ruaccent")
|
||||
if (providers.gradleProperty("ttsSilero").orNull == "true") args("--silero")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "net.sergeych.toread"
|
||||
compileSdk = libs.versions.android.compileSdk.get().toInt()
|
||||
@@ -140,6 +159,8 @@ compose.desktop {
|
||||
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
|
||||
packageName = "To Read"
|
||||
packageVersion = appVersionDisplay
|
||||
fileAssociation("application/x-fictionbook+xml", "fb2", "FictionBook document")
|
||||
fileAssociation("application/zip", "fb2.zip", "Zipped FictionBook document")
|
||||
|
||||
macOS {
|
||||
iconFile.set(project.file("src/jvmMain/resources/icons/icon.icns"))
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
android:theme="@style/AppTheme">
|
||||
<activity
|
||||
android:exported="true"
|
||||
android:launchMode="singleTop"
|
||||
android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
@@ -39,6 +40,8 @@
|
||||
<data android:scheme="content"/>
|
||||
<data android:scheme="file"/>
|
||||
<data android:mimeType="application/x-fictionbook+xml"/>
|
||||
<data android:mimeType="application/xml"/>
|
||||
<data android:mimeType="text/xml"/>
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.VIEW"/>
|
||||
@@ -48,8 +51,12 @@
|
||||
|
||||
<data android:scheme="content" android:mimeType="*/*" android:pathPattern=".*\\.fb2"/>
|
||||
<data android:scheme="content" android:mimeType="*/*" android:pathPattern=".*\\.fb2\\.zip"/>
|
||||
<data android:scheme="content" android:mimeType="application/octet-stream" android:pathPattern=".*\\.fb2"/>
|
||||
<data android:scheme="content" android:mimeType="application/octet-stream" android:pathPattern=".*\\.fb2\\.zip"/>
|
||||
<data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*\\.fb2"/>
|
||||
<data android:scheme="file" android:mimeType="*/*" android:pathPattern=".*\\.fb2\\.zip"/>
|
||||
<data android:scheme="file" android:mimeType="application/octet-stream" android:pathPattern=".*\\.fb2"/>
|
||||
<data android:scheme="file" android:mimeType="application/octet-stream" android:pathPattern=".*\\.fb2\\.zip"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<provider
|
||||
|
||||
@@ -102,6 +102,14 @@ actual fun defaultLibraryScanPath(): String? =
|
||||
?: appContext.filesDir.absolutePath
|
||||
|
||||
fun rememberPlatformOpenBookIntent(intent: Intent?) {
|
||||
val uri = intent
|
||||
?.takeIf { it.action == Intent.ACTION_VIEW }
|
||||
?.data
|
||||
if (uri != null && intent.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION != 0) {
|
||||
runCatching {
|
||||
appContext.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
}
|
||||
}
|
||||
pendingOpenBookUri = intent
|
||||
?.takeIf { it.action == Intent.ACTION_VIEW }
|
||||
?.data
|
||||
@@ -111,9 +119,21 @@ actual suspend fun loadPlatformOpenBookRequest(): PlatformOpenBookRequest? = wit
|
||||
val uri = pendingOpenBookUri ?: return@withContext null
|
||||
pendingOpenBookUri = null
|
||||
val bytes = readStorageUriBytes(uri.toString()) ?: return@withContext null
|
||||
val displayName = displayNameFor(uri)
|
||||
val sizeBytes = sizeBytesFor(uri) ?: bytes.size.toLong()
|
||||
val importResult = openLibraryDatabase().useLibrary { db ->
|
||||
LibraryScanner(db, ::appendLibraryLog).importExternalFileAndGetResult(
|
||||
bytes = bytes,
|
||||
displayName = displayName,
|
||||
storageUri = uri.toString(),
|
||||
sizeBytes = sizeBytes,
|
||||
lastModifiedMillis = null,
|
||||
)
|
||||
}
|
||||
appendLibraryLog("platform open imported=${importResult.imported} fileId=${importResult.fileId} uri=$uri")
|
||||
PlatformOpenBookRequest(
|
||||
id = "platform:${uri}",
|
||||
displayName = displayNameFor(uri),
|
||||
id = importResult.fileId,
|
||||
displayName = displayName,
|
||||
bytes = bytes,
|
||||
)
|
||||
}
|
||||
@@ -372,14 +392,29 @@ actual suspend fun shareLibraryBookFile(fileId: String): Boolean = withContext(D
|
||||
}.getOrDefault(false)
|
||||
}
|
||||
|
||||
actual suspend fun viewLibraryBookFile(fileId: String): Boolean = false
|
||||
actual suspend fun viewLibraryBookFile(folder: String, fileName: String): Boolean = false
|
||||
|
||||
actual fun openPlatformUrl(url: String): Boolean =
|
||||
runCatching {
|
||||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
appContext.startActivity(intent)
|
||||
true
|
||||
}.getOrDefault(false)
|
||||
|
||||
actual suspend fun loadBookInfoExtras(fileId: String): BookInfoExtras = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
val file = db.files.get(fileId) ?: return@useLibrary BookInfoExtras()
|
||||
val clusterId = file.bodyClusterId ?: return@useLibrary BookInfoExtras()
|
||||
val sourceFileName = file.originalFilename ?: file.storageUri?.substringAfterLast('/')
|
||||
val clusterId = file.bodyClusterId ?: return@useLibrary BookInfoExtras(
|
||||
sourceFileName = sourceFileName,
|
||||
sourceFilePath = file.storageUri,
|
||||
)
|
||||
val readingPosition = db.readingStates.getForBodyCluster(clusterId)?.anchor?.formatHintsJson?.toReadingPosition()
|
||||
BookInfoExtras(
|
||||
sourceFileName = sourceFileName,
|
||||
sourceFilePath = file.storageUri,
|
||||
bookmarks = db.bookmarks.listForBodyCluster(clusterId).map {
|
||||
BookmarkInfo(
|
||||
title = it.title,
|
||||
@@ -426,6 +461,42 @@ actual suspend fun saveThemeMode(mode: ThemeMode) = withContext(Dispatchers.IO)
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadReaderFontSettings(): ReaderFontSettings = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
readerFontSettingsFromStorageValue(db.getAppFlag(ReaderFontSettingsFlag))
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun saveReaderFontSettings(settings: ReaderFontSettings) = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.setAppFlag(ReaderFontSettingsFlag, settings.toStorageValue())
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadReaderFullScreen(): Boolean = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(ReaderFullScreenFlag)?.toBooleanStrictOrNull() ?: false
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun saveReaderFullScreen(fullScreen: Boolean) = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.setAppFlag(ReaderFullScreenFlag, fullScreen.toString())
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadReaderPagingOnly(): Boolean = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(ReaderPagingOnlyFlag)?.toBooleanStrictOrNull() ?: false
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun saveReaderPagingOnly(enabled: Boolean) = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.setAppFlag(ReaderPagingOnlyFlag, enabled.toString())
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadScanDownloadsAutomatically(): Boolean = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
|
||||
@@ -438,6 +509,29 @@ actual suspend fun saveScanDownloadsAutomatically(enabled: Boolean) = withContex
|
||||
}
|
||||
}
|
||||
|
||||
internal actual suspend fun loadLibraryFilter(): LibraryFilter = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
val saved = db.getAppFlag(LibraryFilterFlag)
|
||||
when {
|
||||
saved == null -> {
|
||||
appendLibraryLog("warning library filter was not saved; using ${LibraryFilter.MyLibrary.name}")
|
||||
LibraryFilter.MyLibrary
|
||||
}
|
||||
else -> runCatching { LibraryFilter.valueOf(saved) }
|
||||
.getOrElse {
|
||||
appendLibraryLog("warning invalid saved library filter '$saved'; using ${LibraryFilter.MyLibrary.name}")
|
||||
LibraryFilter.MyLibrary
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal actual suspend fun saveLibraryFilter(filter: LibraryFilter) = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.setAppFlag(LibraryFilterFlag, filter.name)
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadAppLocaleTag(): String? = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(AppLocaleTagFlag)?.takeIf { it.isNotBlank() }
|
||||
@@ -671,6 +765,11 @@ private fun displayNameFor(uri: Uri): String =
|
||||
if (cursor.moveToFirst()) cursor.getString(0) else null
|
||||
} ?: uri.lastPathSegment ?: uri.toString()
|
||||
|
||||
private fun sizeBytesFor(uri: Uri): Long? =
|
||||
appContext.contentResolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null)?.use { cursor ->
|
||||
if (cursor.moveToFirst() && !cursor.isNull(0)) cursor.getLong(0) else null
|
||||
}
|
||||
|
||||
private fun LibraryScanSummary.toLibraryScanProgress(): LibraryScanProgress =
|
||||
LibraryScanProgress(
|
||||
scannedFiles = scannedFiles,
|
||||
@@ -780,16 +879,34 @@ private fun ReadingPosition.toFormatHintsJson(): String =
|
||||
buildString {
|
||||
append("""{"firstVisibleItemIndex":$itemIndex,"firstVisibleItemScrollOffset":$scrollOffset""")
|
||||
readAloudSentenceIndex?.let { append(""","readAloudSentenceIndex":$it""") }
|
||||
if (backStack.isNotEmpty()) {
|
||||
append(""","backStack":[""")
|
||||
append(backStack.take(MaxStoredReadingBackStack).joinToString(",") { it.toFormatHintsJson() })
|
||||
append("]")
|
||||
}
|
||||
append("}")
|
||||
}
|
||||
|
||||
private fun String.toReadingPosition(): ReadingPosition? {
|
||||
val position = toReadingPositionObject() ?: return null
|
||||
val backStackJson = Regex(""""backStack"\s*:\s*\[(.*)]""").find(this)?.groupValues?.getOrNull(1)
|
||||
val backStack = backStackJson
|
||||
?.let { ReadingPositionObjectRegex.findAll(it).mapNotNull { match -> match.value.toReadingPositionObject() }.toList() }
|
||||
.orEmpty()
|
||||
.take(MaxStoredReadingBackStack)
|
||||
return position.copy(backStack = backStack)
|
||||
}
|
||||
|
||||
private fun String.toReadingPositionObject(): ReadingPosition? {
|
||||
val index = Regex(""""firstVisibleItemIndex"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
|
||||
val offset = Regex(""""firstVisibleItemScrollOffset"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
|
||||
val sentenceIndex = Regex(""""readAloudSentenceIndex"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
|
||||
return if (index != null && offset != null) ReadingPosition(index, offset, sentenceIndex) else null
|
||||
}
|
||||
|
||||
private val ReadingPositionObjectRegex = Regex("""[{][^{}]*[}]""")
|
||||
private const val MaxStoredReadingBackStack = 10
|
||||
|
||||
private fun String.toSearchPrefixes(): List<String> =
|
||||
SearchPrefixRegex.findAll(lowercase()).map { it.value }.distinct().toList()
|
||||
|
||||
@@ -797,6 +914,10 @@ private val SearchPrefixRegex = Regex("""[\p{L}\p{N}]+""")
|
||||
|
||||
private const val ActiveReadingFileIdFlag = "active_reading_file_id"
|
||||
private const val ThemeModeFlag = "theme_mode"
|
||||
private const val ReaderFontSettingsFlag = "reader_font_settings"
|
||||
private const val ReaderFullScreenFlag = "reader_full_screen"
|
||||
private const val ReaderPagingOnlyFlag = "reader_paging_only"
|
||||
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
|
||||
private const val LibraryFilterFlag = "library_filter"
|
||||
private const val AppLocaleTagFlag = "app_locale_tag"
|
||||
private const val DownloadsWasScannedFlag = "downloads_was_scanned"
|
||||
@@ -4,6 +4,7 @@ import android.Manifest
|
||||
import android.app.AlertDialog
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Color
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
@@ -23,6 +24,8 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.core.view.WindowInsetsControllerCompat
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -55,7 +58,7 @@ class MainActivity : ComponentActivity(), AndroidLibraryDirectoryChooser {
|
||||
}
|
||||
notificationPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
|
||||
super.onCreate(savedInstanceState)
|
||||
WindowCompat.setDecorFitsSystemWindows(window, true)
|
||||
configureEdgeToEdgeBookWindow()
|
||||
initToreadPlatform(this, this)
|
||||
rememberPlatformOpenBookIntent(intent)
|
||||
|
||||
@@ -67,6 +70,11 @@ class MainActivity : ComponentActivity(), AndroidLibraryDirectoryChooser {
|
||||
requestNotificationPermissionIfNeeded()
|
||||
}
|
||||
|
||||
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
||||
super.onWindowFocusChanged(hasFocus)
|
||||
if (hasFocus) hideNavigationBar()
|
||||
}
|
||||
|
||||
override suspend fun chooseDirectory(): String? {
|
||||
val result = CompletableDeferred<String?>()
|
||||
runOnUiThread {
|
||||
@@ -175,6 +183,27 @@ class MainActivity : ComponentActivity(), AndroidLibraryDirectoryChooser {
|
||||
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
}
|
||||
|
||||
private fun configureEdgeToEdgeBookWindow() {
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
window.navigationBarColor = Color.TRANSPARENT
|
||||
window.statusBarColor = Color.TRANSPARENT
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
window.navigationBarDividerColor = Color.TRANSPARENT
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
window.isNavigationBarContrastEnforced = false
|
||||
window.isStatusBarContrastEnforced = false
|
||||
}
|
||||
hideNavigationBar()
|
||||
}
|
||||
|
||||
private fun hideNavigationBar() {
|
||||
WindowCompat.getInsetsController(window, window.decorView).apply {
|
||||
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
|
||||
hide(WindowInsetsCompat.Type.navigationBars())
|
||||
}
|
||||
}
|
||||
|
||||
private fun downloadsPath(): String =
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)?.absolutePath
|
||||
?: Environment.getExternalStorageDirectory().absolutePath
|
||||
@@ -185,7 +214,7 @@ private fun AndroidAppWindow(content: @Composable () -> Unit) {
|
||||
androidx.compose.foundation.layout.Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical)),
|
||||
.windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Top)),
|
||||
) {
|
||||
content()
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.speech.tts.TextToSpeech
|
||||
import android.speech.tts.UtteranceProgressListener
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
@@ -72,7 +73,6 @@ actual object ReadAloudPlatform {
|
||||
}
|
||||
|
||||
private object AndroidReadAloudEngine {
|
||||
private const val UtterancePrefix = "read-aloud-"
|
||||
private const val BeforePausePrefix = "read-aloud-before-"
|
||||
private const val SpeakPrefix = "read-aloud-speak-"
|
||||
private const val AfterPausePrefix = "read-aloud-after-"
|
||||
@@ -97,13 +97,27 @@ private object AndroidReadAloudEngine {
|
||||
private var ttsEngineId: String? = null
|
||||
private var engineProbe: TextToSpeech? = null
|
||||
private var voiceProbe: TextToSpeech? = null
|
||||
private var ttsGeneration = 0L
|
||||
private var engineProbeGeneration = 0L
|
||||
private var voiceProbeGeneration = 0L
|
||||
private var shouldSpeakWhenReady = false
|
||||
private var fileId: String? = null
|
||||
private var bookTitle: String = ""
|
||||
private var sentences: List<ReadAloudSentence> = emptyList()
|
||||
private var currentIndex: Int = 0
|
||||
private var playbackGeneration = 0L
|
||||
private var blockNumber = 0L
|
||||
private var activeUtteranceId: String? = null
|
||||
private var blocks: Iterator<ReadAloudBlock>? = null
|
||||
private val queuedBlocks = linkedMapOf<String, ReadAloudBlock>()
|
||||
|
||||
private fun beforeId(id: String) = "$BeforePausePrefix$id"
|
||||
private fun afterId(id: String) = "$AfterPausePrefix$id"
|
||||
|
||||
@Synchronized
|
||||
fun prepare(context: Context, fileId: String, title: String, queue: List<ReadAloudSentence>, startIndex: Int) {
|
||||
shouldSpeakWhenReady = false
|
||||
cancelQueue()
|
||||
this.fileId = fileId
|
||||
bookTitle = title
|
||||
sentences = queue
|
||||
@@ -116,6 +130,7 @@ private object AndroidReadAloudEngine {
|
||||
ensureTts(context.applicationContext)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun play(context: Context) {
|
||||
if (sentences.isEmpty()) return
|
||||
ensureTts(context.applicationContext)
|
||||
@@ -124,6 +139,7 @@ private object AndroidReadAloudEngine {
|
||||
mutableState.value = mutableState.value.copy(
|
||||
active = true,
|
||||
playing = true,
|
||||
buffering = true,
|
||||
sentenceIndex = sentences.getOrNull(currentIndex)?.index,
|
||||
)
|
||||
return
|
||||
@@ -131,12 +147,14 @@ private object AndroidReadAloudEngine {
|
||||
speakCurrent()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun stop() {
|
||||
shouldSpeakWhenReady = false
|
||||
tts?.stop()
|
||||
cancelQueue()
|
||||
mutableState.value = ReadAloudState()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun skip(delta: Int) {
|
||||
if (sentences.isEmpty()) return
|
||||
val wasPlaying = mutableState.value.playing
|
||||
@@ -153,51 +171,53 @@ private object AndroidReadAloudEngine {
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun refreshSettings(context: Context) {
|
||||
val appContext = context.applicationContext
|
||||
val textReplacements = userTextReplacements(appContext)
|
||||
mutableSettingsState.value = mutableSettingsState.value.copy(
|
||||
textReplacements = textReplacements,
|
||||
textReplacements = userTextReplacements(appContext),
|
||||
loading = true,
|
||||
message = null,
|
||||
)
|
||||
val generation = ++engineProbeGeneration
|
||||
engineProbe?.shutdown()
|
||||
var probe: TextToSpeech? = null
|
||||
probe = TextToSpeech(appContext) { status ->
|
||||
if (status != TextToSpeech.SUCCESS) {
|
||||
mutableSettingsState.value = mutableSettingsState.value.copy(
|
||||
loading = false,
|
||||
message = strings.couldNotLoadAndroidTtsEngines,
|
||||
)
|
||||
probe?.shutdown()
|
||||
if (engineProbe === probe) engineProbe = null
|
||||
return@TextToSpeech
|
||||
}
|
||||
|
||||
val engines = probe?.engines.orEmpty()
|
||||
.map { ReadAloudEngineOption(id = it.name, label = it.label ?: it.name) }
|
||||
.sortedBy { it.label.lowercase() }
|
||||
var selectedEngine = selectedEngineId(appContext).takeIf { saved ->
|
||||
saved != null && engines.any { it.id == saved }
|
||||
}
|
||||
if (selectedEngine == null && !appContext.readAloudPrefs().getBoolean(EngineChoiceMadeKey, false)) {
|
||||
selectedEngine = engines.firstOrNull { it.isRhVoice() }?.id
|
||||
if (selectedEngine != null) {
|
||||
appContext.readAloudPrefs().edit().putString(SelectedEngineKey, selectedEngine).apply()
|
||||
}
|
||||
}
|
||||
mutableSettingsState.value = mutableSettingsState.value.copy(
|
||||
engines = engines,
|
||||
selectedEngineId = selectedEngine,
|
||||
textReplacements = textReplacements,
|
||||
)
|
||||
probe?.shutdown()
|
||||
if (engineProbe === probe) engineProbe = null
|
||||
refreshVoices(appContext, selectedEngine)
|
||||
engineProbe = TextToSpeech(appContext) { status ->
|
||||
// Dispatch even if onInit fires synchronously inside the constructor.
|
||||
scope.launch(Dispatchers.Main) { handleEngineProbeInit(appContext, generation, status) }
|
||||
}
|
||||
engineProbe = probe
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun handleEngineProbeInit(context: Context, generation: Long, status: Int) {
|
||||
if (generation != engineProbeGeneration) return
|
||||
val probe = engineProbe ?: return
|
||||
if (status != TextToSpeech.SUCCESS) {
|
||||
mutableSettingsState.value = mutableSettingsState.value.copy(
|
||||
loading = false, message = strings.couldNotLoadAndroidTtsEngines,
|
||||
)
|
||||
probe.shutdown()
|
||||
engineProbe = null
|
||||
return
|
||||
}
|
||||
val engines = probe.engines.orEmpty()
|
||||
.map { ReadAloudEngineOption(id = it.name, label = it.label ?: it.name) }
|
||||
.sortedBy { it.label.lowercase() }
|
||||
var selectedEngine = selectedEngineId(context)
|
||||
if (selectedEngine == null && !context.readAloudPrefs().getBoolean(EngineChoiceMadeKey, false)) {
|
||||
selectedEngine = engines.firstOrNull { it.isRhVoice() }?.id
|
||||
if (selectedEngine != null) {
|
||||
context.readAloudPrefs().edit().putString(SelectedEngineKey, selectedEngine).apply()
|
||||
}
|
||||
}
|
||||
mutableSettingsState.value = mutableSettingsState.value.copy(
|
||||
engines = engines, selectedEngineId = selectedEngine,
|
||||
)
|
||||
probe.shutdown()
|
||||
engineProbe = null
|
||||
refreshVoices(context, selectedEngine)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun selectEngine(context: Context, engineId: String?) {
|
||||
val appContext = context.applicationContext
|
||||
appContext.readAloudPrefs()
|
||||
@@ -206,6 +226,7 @@ private object AndroidReadAloudEngine {
|
||||
.putStringOrRemove(SelectedEngineKey, engineId)
|
||||
.remove(SelectedVoiceKey)
|
||||
.apply()
|
||||
val wasPlaying = mutableState.value.playing
|
||||
resetTts()
|
||||
mutableSettingsState.value = mutableSettingsState.value.copy(
|
||||
selectedEngineId = engineId,
|
||||
@@ -215,8 +236,10 @@ private object AndroidReadAloudEngine {
|
||||
message = null,
|
||||
)
|
||||
refreshVoices(appContext, engineId)
|
||||
if (wasPlaying) play(appContext)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun selectVoice(context: Context, voiceId: String?) {
|
||||
val appContext = context.applicationContext
|
||||
appContext.readAloudPrefs()
|
||||
@@ -236,25 +259,35 @@ private object AndroidReadAloudEngine {
|
||||
if (tts != null && ttsEngineId == selectedEngine) return
|
||||
resetTts()
|
||||
ttsEngineId = selectedEngine
|
||||
val generation = ttsGeneration
|
||||
tts = if (selectedEngine == null) {
|
||||
TextToSpeech(appContext) { status ->
|
||||
handleTtsInit(appContext, status)
|
||||
scope.launch(Dispatchers.Main) { handleTtsInit(appContext, generation, status) }
|
||||
}
|
||||
} else {
|
||||
TextToSpeech(
|
||||
appContext,
|
||||
{ status -> handleTtsInit(appContext, status) },
|
||||
{ status -> scope.launch(Dispatchers.Main) { handleTtsInit(appContext, generation, status) } },
|
||||
selectedEngine,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleTtsInit(context: Context, status: Int) {
|
||||
@Synchronized
|
||||
private fun handleTtsInit(context: Context, generation: Long, status: Int) {
|
||||
if (generation != ttsGeneration) return
|
||||
ttsReady = status == TextToSpeech.SUCCESS
|
||||
if (ttsReady) {
|
||||
applyTtsConfiguration(context)
|
||||
Log.i("YereedTts", "Initialized requested engine=${ttsEngineId ?: "system default"}, voice=${tts?.voice?.name}")
|
||||
tts?.setOnUtteranceProgressListener(object : UtteranceProgressListener() {
|
||||
override fun onStart(utteranceId: String?) = Unit
|
||||
override fun onStart(utteranceId: String?) {
|
||||
handleUtteranceStart(utteranceId)
|
||||
}
|
||||
|
||||
override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) {
|
||||
handleUtteranceRange(utteranceId, start)
|
||||
}
|
||||
|
||||
override fun onDone(utteranceId: String?) {
|
||||
handleUtteranceDone(utteranceId)
|
||||
@@ -262,11 +295,11 @@ private object AndroidReadAloudEngine {
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onError(utteranceId: String?) {
|
||||
stop()
|
||||
handleUtteranceError(utteranceId)
|
||||
}
|
||||
|
||||
override fun onError(utteranceId: String?, errorCode: Int) {
|
||||
stop()
|
||||
handleUtteranceError(utteranceId)
|
||||
}
|
||||
})
|
||||
if (shouldSpeakWhenReady) {
|
||||
@@ -278,51 +311,94 @@ private object AndroidReadAloudEngine {
|
||||
}
|
||||
}
|
||||
|
||||
private fun cancelQueue() {
|
||||
// Invalidate callbacks before stopping: a late event must not affect the new queue.
|
||||
playbackGeneration++
|
||||
blocks = null
|
||||
activeUtteranceId = null
|
||||
queuedBlocks.clear()
|
||||
tts?.stop()
|
||||
}
|
||||
|
||||
private fun speakCurrent() {
|
||||
val sentence = sentences.getOrNull(currentIndex) ?: run {
|
||||
stop()
|
||||
return
|
||||
}
|
||||
cancelQueue()
|
||||
mutableState.value = ReadAloudState(active = true, playing = true, sentenceIndex = sentence.index)
|
||||
saveSentencePosition(sentence)
|
||||
val params = Bundle().apply {
|
||||
putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "$SpeakPrefix$currentIndex")
|
||||
}
|
||||
val queueMode = if (sentence.pauseBeforeMillis > 0) {
|
||||
tts?.playSilentUtterance(sentence.pauseBeforeMillis, TextToSpeech.QUEUE_FLUSH, "$BeforePausePrefix$currentIndex")
|
||||
TextToSpeech.QUEUE_ADD
|
||||
} else {
|
||||
TextToSpeech.QUEUE_FLUSH
|
||||
}
|
||||
tts?.speak(sentence.spokenText, queueMode, params, "$SpeakPrefix$currentIndex")
|
||||
if (sentence.pauseAfterMillis > 0) {
|
||||
tts?.playSilentUtterance(sentence.pauseAfterMillis, TextToSpeech.QUEUE_ADD, "$AfterPausePrefix$currentIndex")
|
||||
}
|
||||
blocks = readAloudBlocks(
|
||||
sentences, currentIndex, minOf(4_000, TextToSpeech.getMaxSpeechInputLength()),
|
||||
).iterator()
|
||||
fillQueue()
|
||||
}
|
||||
|
||||
private fun handleUtteranceDone(utteranceId: String?) {
|
||||
if (!mutableState.value.playing) return
|
||||
val speakIndex = utteranceId?.removePrefix(SpeakPrefix)?.takeIf { it != utteranceId }?.toIntOrNull()
|
||||
val afterIndex = utteranceId?.removePrefix(AfterPausePrefix)?.takeIf { it != utteranceId }?.toIntOrNull()
|
||||
val finishedIndex = afterIndex ?: speakIndex ?: return
|
||||
if (finishedIndex != currentIndex) return
|
||||
val currentSentence = sentences.getOrNull(currentIndex) ?: run {
|
||||
stop()
|
||||
return
|
||||
}
|
||||
if (speakIndex != null && currentSentence.pauseAfterMillis > 0) {
|
||||
sentences.getOrNull(currentIndex + 1)?.let { nextSentence ->
|
||||
mutableState.value = mutableState.value.copy(sentenceIndex = nextSentence.index)
|
||||
saveSentencePosition(nextSentence)
|
||||
private fun fillQueue() {
|
||||
val engine = tts ?: return
|
||||
val remaining = blocks ?: return
|
||||
// Keep the current block and two successors submitted, before playback ends.
|
||||
while (queuedBlocks.size < 3 && remaining.hasNext()) {
|
||||
val block = remaining.next()
|
||||
val id = "$SpeakPrefix$playbackGeneration-${blockNumber++}"
|
||||
queuedBlocks[id] = block
|
||||
val beforeResult = if (block.pauseBeforeMillis > 0) {
|
||||
engine.playSilentUtterance(block.pauseBeforeMillis, TextToSpeech.QUEUE_ADD, beforeId(id))
|
||||
} else TextToSpeech.SUCCESS
|
||||
val speechResult = engine.speak(block.text, TextToSpeech.QUEUE_ADD, Bundle(), id)
|
||||
val afterResult = if (block.pauseAfterMillis > 0) {
|
||||
engine.playSilentUtterance(block.pauseAfterMillis, TextToSpeech.QUEUE_ADD, afterId(id))
|
||||
} else TextToSpeech.SUCCESS
|
||||
if (beforeResult == TextToSpeech.ERROR || speechResult == TextToSpeech.ERROR ||
|
||||
afterResult == TextToSpeech.ERROR) {
|
||||
stop()
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
if (currentIndex < sentences.lastIndex) {
|
||||
currentIndex += 1
|
||||
speakCurrent()
|
||||
} else {
|
||||
stop()
|
||||
if (queuedBlocks.isEmpty()) stop()
|
||||
}
|
||||
|
||||
private fun updatePosition(position: Int) {
|
||||
if (currentIndex == position && mutableState.value.sentenceIndex == sentences[position].index) return
|
||||
currentIndex = position
|
||||
val sentence = sentences[position]
|
||||
mutableState.value = mutableState.value.copy(sentenceIndex = sentence.index)
|
||||
saveSentencePosition(sentence)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun handleUtteranceStart(utteranceId: String?) {
|
||||
if (!mutableState.value.playing) return
|
||||
val block = queuedBlocks[utteranceId] ?: return
|
||||
activeUtteranceId = utteranceId
|
||||
updatePosition(block.ranges.first().position)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun handleUtteranceRange(utteranceId: String?, offset: Int) {
|
||||
if (!mutableState.value.playing || utteranceId != activeUtteranceId) return
|
||||
val block = queuedBlocks[utteranceId] ?: return
|
||||
updatePosition(block.positionAt(offset))
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun handleUtteranceDone(utteranceId: String?) {
|
||||
if (!mutableState.value.playing || utteranceId == null) return
|
||||
val isAfter = utteranceId.startsWith(AfterPausePrefix)
|
||||
val id = if (isAfter) utteranceId.removePrefix(AfterPausePrefix) else utteranceId
|
||||
val block = queuedBlocks[id] ?: return
|
||||
if (!isAfter) {
|
||||
if (activeUtteranceId == id) updatePosition(block.ranges.last().position)
|
||||
if (block.pauseAfterMillis > 0) return
|
||||
}
|
||||
queuedBlocks.remove(id)
|
||||
fillQueue()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun handleUtteranceError(utteranceId: String?) {
|
||||
val id = utteranceId?.removePrefix(BeforePausePrefix)?.removePrefix(AfterPausePrefix)
|
||||
if (queuedBlocks.containsKey(id)) stop()
|
||||
}
|
||||
|
||||
fun notificationTitle(): String = bookTitle.ifBlank { strings.readAloud }
|
||||
@@ -341,34 +417,28 @@ private object AndroidReadAloudEngine {
|
||||
}
|
||||
|
||||
private fun refreshVoices(context: Context, engineId: String?) {
|
||||
val generation = ++voiceProbeGeneration
|
||||
voiceProbe?.shutdown()
|
||||
var probe: TextToSpeech? = null
|
||||
probe = if (engineId == null) {
|
||||
TextToSpeech(context) { status ->
|
||||
handleVoiceProbeInit(context, probe, status)
|
||||
}
|
||||
} else {
|
||||
TextToSpeech(
|
||||
context,
|
||||
{ status -> handleVoiceProbeInit(context, probe, status) },
|
||||
engineId,
|
||||
)
|
||||
}
|
||||
voiceProbe = probe
|
||||
voiceProbe = TextToSpeech(context, { status ->
|
||||
scope.launch(Dispatchers.Main) { handleVoiceProbeInit(context, generation, status) }
|
||||
}, engineId)
|
||||
}
|
||||
|
||||
private fun handleVoiceProbeInit(context: Context, probe: TextToSpeech?, status: Int) {
|
||||
@Synchronized
|
||||
private fun handleVoiceProbeInit(context: Context, generation: Long, status: Int) {
|
||||
if (generation != voiceProbeGeneration) return
|
||||
val probe = voiceProbe ?: return
|
||||
if (status != TextToSpeech.SUCCESS) {
|
||||
mutableSettingsState.value = mutableSettingsState.value.copy(
|
||||
loading = false,
|
||||
message = strings.couldNotLoadSelectedTtsVoices,
|
||||
)
|
||||
probe?.shutdown()
|
||||
probe.shutdown()
|
||||
if (voiceProbe === probe) voiceProbe = null
|
||||
return
|
||||
}
|
||||
|
||||
val voices = probe?.voices.orEmpty()
|
||||
val voices = probe.voices.orEmpty()
|
||||
.mapNotNull { voice ->
|
||||
val locale = voice.locale ?: return@mapNotNull null
|
||||
ReadAloudVoiceOption(
|
||||
@@ -396,7 +466,7 @@ private object AndroidReadAloudEngine {
|
||||
if (savedVoice == null && selectedVoiceId(context) != null) {
|
||||
context.readAloudPrefs().edit().remove(SelectedVoiceKey).apply()
|
||||
}
|
||||
probe?.shutdown()
|
||||
probe.shutdown()
|
||||
if (voiceProbe === probe) voiceProbe = null
|
||||
}
|
||||
|
||||
@@ -413,10 +483,11 @@ private object AndroidReadAloudEngine {
|
||||
}
|
||||
|
||||
private fun resetTts() {
|
||||
ttsGeneration++
|
||||
shouldSpeakWhenReady = false
|
||||
ttsReady = false
|
||||
ttsEngineId = null
|
||||
tts?.stop()
|
||||
cancelQueue()
|
||||
tts?.shutdown()
|
||||
tts = null
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -24,12 +25,16 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleEventObserver
|
||||
import androidx.lifecycle.compose.LocalLifecycleOwner
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
private const val DefaultToastDurationMillis = 1_600L
|
||||
private const val DeleteUndoDurationMillis = 5_000L
|
||||
private const val ScanResultToastDurationMillis = 5_000L
|
||||
private const val BackgroundLibraryPageSize = 50
|
||||
|
||||
private data class AppToastData(
|
||||
@@ -46,6 +51,11 @@ private data class PendingLibraryDelete(
|
||||
val restore: () -> Unit,
|
||||
)
|
||||
|
||||
internal data class LibraryItemRefreshRequest(
|
||||
val id: Long,
|
||||
val fileId: String,
|
||||
)
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun App() {
|
||||
@@ -95,25 +105,27 @@ fun App() {
|
||||
}
|
||||
|
||||
MaterialTheme(colorScheme = if (useDark) darkReaderColorScheme() else lightReaderColorScheme()) {
|
||||
Box(Modifier.fillMaxSize()) {
|
||||
Surface(Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
|
||||
if (localePreferenceLoaded) {
|
||||
BookReaderApp(
|
||||
onThemeToggle = {
|
||||
val next = themeMode.next()
|
||||
themeMode = next
|
||||
showToast(strings.themeChanged(next))
|
||||
scope.launch {
|
||||
saveThemeMode(next)
|
||||
}
|
||||
},
|
||||
onShowToast = ::showToast,
|
||||
)
|
||||
} else {
|
||||
LoadingScreen(strings.loadingOpeningBook)
|
||||
CompositionLocalProvider(LocalReaderThemeIsDark provides useDark) {
|
||||
Box(Modifier.fillMaxSize()) {
|
||||
Surface(Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
|
||||
if (localePreferenceLoaded) {
|
||||
BookReaderApp(
|
||||
onThemeToggle = {
|
||||
val next = themeMode.next()
|
||||
themeMode = next
|
||||
showToast(strings.themeChanged(next))
|
||||
scope.launch {
|
||||
saveThemeMode(next)
|
||||
}
|
||||
},
|
||||
onShowToast = ::showToast,
|
||||
)
|
||||
} else {
|
||||
LoadingScreen(strings.loadingOpeningBook)
|
||||
}
|
||||
}
|
||||
AppToast(toast, modifier = Modifier.align(Alignment.BottomCenter))
|
||||
}
|
||||
AppToast(toast, modifier = Modifier.align(Alignment.BottomCenter))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,19 +177,98 @@ private fun BookReaderApp(
|
||||
) -> Unit,
|
||||
) {
|
||||
var state by remember { mutableStateOf<AppState>(AppState.LoadingStartup) }
|
||||
var libraryBackState by remember { mutableStateOf<AppState.Library?>(null) }
|
||||
var activeScan by remember { mutableStateOf<LibraryScanProgress?>(null) }
|
||||
var scanJob by remember { mutableStateOf<Job?>(null) }
|
||||
var pendingDelete by remember { mutableStateOf<PendingLibraryDelete?>(null) }
|
||||
var pendingDeleteJob by remember { mutableStateOf<Job?>(null) }
|
||||
var hiddenDeletedFileIds by remember { mutableStateOf<Set<String>>(emptySet()) }
|
||||
var nextDeleteId by remember { mutableStateOf(0L) }
|
||||
var nextLibraryItemRefreshId by remember { mutableStateOf(0L) }
|
||||
var libraryItemRefreshRequest by remember { mutableStateOf<LibraryItemRefreshRequest?>(null) }
|
||||
var imageViewer by remember { mutableStateOf<ViewedBookImage?>(null) }
|
||||
var resumeGeneration by remember { mutableStateOf(0L) }
|
||||
var downloadsRescanRequestGeneration by remember { mutableStateOf(0L) }
|
||||
val lifecycleOwner = LocalLifecycleOwner.current
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
fun refreshLibraryItem(fileId: String) {
|
||||
nextLibraryItemRefreshId += 1
|
||||
libraryItemRefreshRequest = LibraryItemRefreshRequest(nextLibraryItemRefreshId, fileId)
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
state = loadStartupState()
|
||||
}
|
||||
|
||||
DisposableEffect(lifecycleOwner) {
|
||||
val observer = LifecycleEventObserver { _, event ->
|
||||
if (event == Lifecycle.Event.ON_RESUME) {
|
||||
resumeGeneration += 1
|
||||
}
|
||||
}
|
||||
lifecycleOwner.lifecycle.addObserver(observer)
|
||||
onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
while (true) {
|
||||
delay(500)
|
||||
val requestResult = runCatching { loadPlatformOpenBookRequest() }
|
||||
if (requestResult.isFailure) {
|
||||
val scanPath = when (val current = state) {
|
||||
is AppState.Library -> current.scanPath
|
||||
is AppState.Scan -> current.scanPath
|
||||
is AppState.Reader -> current.scanPath
|
||||
is AppState.BookInfo -> current.scanPath
|
||||
is AppState.Error, AppState.LoadingStartup -> defaultLibraryScanPath().orEmpty()
|
||||
}
|
||||
state = AppState.Library(
|
||||
emptyList(),
|
||||
scanPath,
|
||||
requestResult.exceptionOrNull()?.message ?: strings.couldNotOpenLibrary,
|
||||
loadLibraryFilter(),
|
||||
)
|
||||
continue
|
||||
}
|
||||
val request = requestResult.getOrNull() ?: continue
|
||||
val scanPath = when (val current = state) {
|
||||
is AppState.Library -> current.scanPath
|
||||
is AppState.Scan -> current.scanPath
|
||||
is AppState.Reader -> current.scanPath
|
||||
is AppState.BookInfo -> current.scanPath
|
||||
is AppState.Error, AppState.LoadingStartup -> defaultLibraryScanPath().orEmpty()
|
||||
}
|
||||
val nextState = runCatching {
|
||||
val book = parseBookInBackground(request.bytes, request.displayName)
|
||||
saveActiveReadingFileId(request.id)
|
||||
AppState.Reader(
|
||||
fileId = request.id,
|
||||
book = book,
|
||||
libraryItems = emptyList(),
|
||||
scanPath = scanPath,
|
||||
selectedFilter = when (val current = state) {
|
||||
is AppState.Library -> current.selectedFilter
|
||||
is AppState.Scan -> current.selectedFilter
|
||||
is AppState.Reader -> current.selectedFilter
|
||||
is AppState.BookInfo -> current.selectedFilter
|
||||
is AppState.Error, AppState.LoadingStartup -> loadLibraryFilter()
|
||||
},
|
||||
)
|
||||
}.getOrElse {
|
||||
AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotOpen(request.displayName), loadLibraryFilter())
|
||||
}
|
||||
libraryBackState = when (val current = state) {
|
||||
is AppState.Library -> current
|
||||
is AppState.Scan -> AppState.Library(current.items, current.scanPath, current.message, current.selectedFilter)
|
||||
is AppState.Reader, is AppState.BookInfo -> libraryBackState
|
||||
is AppState.Error, AppState.LoadingStartup -> null
|
||||
}
|
||||
state = nextState
|
||||
refreshLibraryItem(request.id)
|
||||
}
|
||||
}
|
||||
|
||||
val activeBookState = when (val current = state) {
|
||||
is AppState.Reader -> current.fileId to current.libraryItems.isEmpty()
|
||||
is AppState.BookInfo -> current.fileId to current.libraryItems.isEmpty()
|
||||
@@ -276,12 +367,23 @@ private fun BookReaderApp(
|
||||
libraryItems = current.libraryItems,
|
||||
scanPath = current.scanPath,
|
||||
message = current.message,
|
||||
selectedFilter = current.selectedFilter,
|
||||
)
|
||||
is AppState.Reader -> {
|
||||
scope.launch { saveActiveReadingFileId(null) }
|
||||
AppState.Library(current.libraryItems, current.scanPath, current.message)
|
||||
refreshLibraryItem(current.fileId)
|
||||
val backState = libraryBackState?.copy(message = current.message)
|
||||
?: AppState.Library(
|
||||
current.libraryItems,
|
||||
current.scanPath,
|
||||
current.message,
|
||||
current.selectedFilter,
|
||||
)
|
||||
libraryBackState = null
|
||||
downloadsRescanRequestGeneration += 1
|
||||
backState
|
||||
}
|
||||
is AppState.Scan -> AppState.Library(current.items, current.scanPath, current.message)
|
||||
is AppState.Scan -> AppState.Library(current.items, current.scanPath, current.message, current.selectedFilter)
|
||||
is AppState.Error -> AppState.LoadingStartup
|
||||
is AppState.Library, AppState.LoadingStartup -> current
|
||||
}
|
||||
@@ -299,7 +401,23 @@ private fun BookReaderApp(
|
||||
onBack = ::navigateBack,
|
||||
)
|
||||
|
||||
fun startScan(path: String) {
|
||||
fun setLibraryFilter(filter: LibraryFilter) {
|
||||
state = when (val current = state) {
|
||||
is AppState.Library -> current.copy(selectedFilter = filter)
|
||||
is AppState.Scan -> current.copy(selectedFilter = filter)
|
||||
else -> current
|
||||
}
|
||||
libraryBackState = libraryBackState?.copy(selectedFilter = filter)
|
||||
}
|
||||
|
||||
fun saveAndSetLibraryFilter(filter: LibraryFilter) {
|
||||
setLibraryFilter(filter)
|
||||
scope.launch {
|
||||
saveLibraryFilter(filter)
|
||||
}
|
||||
}
|
||||
|
||||
fun startScan(path: String, userInitiated: Boolean) {
|
||||
if (scanJob?.isActive == true) return
|
||||
activeScan = LibraryScanProgress(0, 0, 0, 0)
|
||||
scanJob = scope.launch {
|
||||
@@ -319,19 +437,34 @@ private fun BookReaderApp(
|
||||
},
|
||||
onFailure = { it.message ?: strings.scanFailed },
|
||||
)
|
||||
val importedFiles = report.getOrNull()?.importedFiles
|
||||
when {
|
||||
importedFiles == null -> Unit
|
||||
importedFiles > 0 -> {
|
||||
onShowToast(
|
||||
strings.newBooksAdded(importedFiles),
|
||||
strings.show,
|
||||
{ saveAndSetLibraryFilter(LibraryFilter.RecentlyAdded) },
|
||||
ScanResultToastDurationMillis,
|
||||
)
|
||||
}
|
||||
userInitiated -> {
|
||||
onShowToast(strings.noNewBooksFound, null, null, DefaultToastDurationMillis)
|
||||
}
|
||||
}
|
||||
scanJob = null
|
||||
activeScan = null
|
||||
state = when (val current = state) {
|
||||
is AppState.Library -> if (report.getOrNull()?.hasLibraryChanges() == true) {
|
||||
loadLibraryState(message, path)
|
||||
} else {
|
||||
current.copy(scanPath = path, message = message)
|
||||
}
|
||||
is AppState.Scan -> if (report.getOrNull()?.hasLibraryChanges() == true) {
|
||||
loadLibraryState(message, path)
|
||||
} else {
|
||||
AppState.Library(current.items, path, message)
|
||||
}
|
||||
is AppState.Library -> current.copy(
|
||||
scanPath = path,
|
||||
message = message,
|
||||
)
|
||||
is AppState.Scan -> AppState.Library(
|
||||
current.items,
|
||||
path,
|
||||
message,
|
||||
selectedFilter = current.selectedFilter,
|
||||
)
|
||||
AppState.LoadingStartup -> loadLibraryState(message, path)
|
||||
is AppState.Reader -> current.copy(message = message)
|
||||
is AppState.BookInfo -> current.copy(message = message)
|
||||
@@ -340,53 +473,91 @@ private fun BookReaderApp(
|
||||
}
|
||||
}
|
||||
|
||||
when (val current = state) {
|
||||
AppState.LoadingStartup -> LoadingScreen(strings.loadingOpeningBook)
|
||||
is AppState.Library -> LibraryScreen(
|
||||
state = current,
|
||||
activeScan = activeScan,
|
||||
hiddenFileIds = hiddenDeletedFileIds + pendingDelete?.request?.fileId?.let(::setOf).orEmpty(),
|
||||
onStateChange = { state = it },
|
||||
onNavigateToScan = { state = AppState.Scan(current.items, current.scanPath, current.message) },
|
||||
onStartScan = ::startScan,
|
||||
onDeleteRequested = ::requestDelete,
|
||||
)
|
||||
is AppState.Scan -> ScanScreen(
|
||||
state = current,
|
||||
activeScan = activeScan,
|
||||
onStateChange = { state = it },
|
||||
onStartScan = { path ->
|
||||
startScan(path)
|
||||
state = AppState.Library(current.items, path, strings.scanning)
|
||||
},
|
||||
)
|
||||
is AppState.Reader -> BookView(
|
||||
fileId = current.fileId,
|
||||
book = current.book,
|
||||
onImageOpen = { imageViewer = it },
|
||||
onThemeToggle = onThemeToggle,
|
||||
onBookInfo = {
|
||||
state = AppState.BookInfo(
|
||||
fileId = current.fileId,
|
||||
book = current.book,
|
||||
libraryItems = current.libraryItems,
|
||||
scanPath = current.scanPath,
|
||||
message = current.message,
|
||||
)
|
||||
},
|
||||
onDeleted = { message ->
|
||||
state = AppState.Library(emptyList(), current.scanPath, message)
|
||||
},
|
||||
onDeleteRequested = ::requestDelete,
|
||||
onBack = ::navigateBack,
|
||||
)
|
||||
is AppState.BookInfo -> BookInfoScreen(
|
||||
fileId = current.fileId,
|
||||
book = current.book,
|
||||
onImageOpen = { imageViewer = it },
|
||||
onBack = ::navigateBack,
|
||||
)
|
||||
is AppState.Error -> ErrorScreen(current.message, onBack = ::navigateBack)
|
||||
LaunchedEffect(resumeGeneration, downloadsRescanRequestGeneration, state !is AppState.LoadingStartup) {
|
||||
if (state is AppState.LoadingStartup || activeScan != null || scanJob?.isActive == true) return@LaunchedEffect
|
||||
if (!loadScanDownloadsAutomatically() || !loadDownloadsWasScanned()) return@LaunchedEffect
|
||||
val path = downloadsScanPath() ?: return@LaunchedEffect
|
||||
state = state.withMessage(strings.scanningDownloads)
|
||||
startScan(path, userInitiated = false)
|
||||
}
|
||||
|
||||
Box(Modifier.fillMaxSize()) {
|
||||
val currentLibraryState = when (val current = state) {
|
||||
is AppState.Library -> current
|
||||
is AppState.Reader, is AppState.BookInfo -> libraryBackState
|
||||
else -> null
|
||||
}
|
||||
currentLibraryState?.let { libraryState ->
|
||||
LibraryScreen(
|
||||
state = libraryState,
|
||||
activeScan = activeScan,
|
||||
itemRefreshRequest = libraryItemRefreshRequest,
|
||||
hiddenFileIds = hiddenDeletedFileIds + pendingDelete?.request?.fileId?.let(::setOf).orEmpty(),
|
||||
onStateChange = { next ->
|
||||
val currentState = state
|
||||
if (next is AppState.Reader && currentState is AppState.Library) {
|
||||
libraryBackState = currentState
|
||||
}
|
||||
state = next
|
||||
},
|
||||
onLibraryFilterChange = ::setLibraryFilter,
|
||||
onNavigateToScan = {
|
||||
libraryBackState = null
|
||||
state = AppState.Scan(
|
||||
libraryState.items,
|
||||
libraryState.scanPath,
|
||||
libraryState.message,
|
||||
libraryState.selectedFilter,
|
||||
)
|
||||
},
|
||||
onDeleteRequested = ::requestDelete,
|
||||
)
|
||||
}
|
||||
|
||||
when (val current = state) {
|
||||
AppState.LoadingStartup -> LoadingScreen(strings.loadingOpeningBook)
|
||||
is AppState.Library -> Unit
|
||||
is AppState.Scan -> ScanScreen(
|
||||
state = current,
|
||||
activeScan = activeScan,
|
||||
onStateChange = { state = it },
|
||||
onStartScan = { path ->
|
||||
startScan(path, userInitiated = true)
|
||||
state = AppState.Library(current.items, path, strings.scanning, current.selectedFilter)
|
||||
},
|
||||
)
|
||||
is AppState.Reader -> BookView(
|
||||
fileId = current.fileId,
|
||||
book = current.book,
|
||||
onImageOpen = { imageViewer = it },
|
||||
onThemeToggle = onThemeToggle,
|
||||
onBookChanged = ::refreshLibraryItem,
|
||||
onBookInfo = {
|
||||
state = AppState.BookInfo(
|
||||
fileId = current.fileId,
|
||||
book = current.book,
|
||||
libraryItems = current.libraryItems,
|
||||
scanPath = current.scanPath,
|
||||
message = current.message,
|
||||
selectedFilter = current.selectedFilter,
|
||||
)
|
||||
},
|
||||
onDeleted = { message ->
|
||||
state = libraryBackState?.copy(message = message)
|
||||
?: AppState.Library(emptyList(), current.scanPath, message, current.selectedFilter)
|
||||
libraryBackState = null
|
||||
},
|
||||
onDeleteRequested = ::requestDelete,
|
||||
onBack = ::navigateBack,
|
||||
)
|
||||
is AppState.BookInfo -> BookInfoScreen(
|
||||
fileId = current.fileId,
|
||||
book = current.book,
|
||||
onImageOpen = { imageViewer = it },
|
||||
onBack = ::navigateBack,
|
||||
)
|
||||
is AppState.Error -> ErrorScreen(current.message, onBack = ::navigateBack)
|
||||
}
|
||||
}
|
||||
|
||||
imageViewer?.let { image ->
|
||||
@@ -404,5 +575,11 @@ internal data class ViewedBookImage(
|
||||
val title: String,
|
||||
)
|
||||
|
||||
private fun LibraryScanReport.hasLibraryChanges(): Boolean =
|
||||
importedFiles > 0
|
||||
private fun AppState.withMessage(message: String): AppState =
|
||||
when (this) {
|
||||
is AppState.Library -> copy(message = message)
|
||||
is AppState.Scan -> copy(message = message)
|
||||
is AppState.Reader -> copy(message = message)
|
||||
is AppState.BookInfo -> copy(message = message)
|
||||
is AppState.Error, AppState.LoadingStartup -> this
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package net.sergeych.toread
|
||||
|
||||
import net.sergeych.toread.fb2.Fb2Book
|
||||
import net.sergeych.toread.fb2.Fb2Format
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
internal sealed interface AppState {
|
||||
data object LoadingStartup : AppState
|
||||
@@ -9,12 +11,14 @@ internal sealed interface AppState {
|
||||
val items: List<LibraryItem>,
|
||||
val scanPath: String,
|
||||
val message: String? = null,
|
||||
val selectedFilter: LibraryFilter = LibraryFilter.MyLibrary,
|
||||
) : AppState
|
||||
|
||||
data class Scan(
|
||||
val items: List<LibraryItem>,
|
||||
val scanPath: String,
|
||||
val message: String? = null,
|
||||
val selectedFilter: LibraryFilter = LibraryFilter.MyLibrary,
|
||||
) : AppState
|
||||
|
||||
data class Reader(
|
||||
@@ -23,6 +27,7 @@ internal sealed interface AppState {
|
||||
val libraryItems: List<LibraryItem>,
|
||||
val scanPath: String,
|
||||
val message: String? = null,
|
||||
val selectedFilter: LibraryFilter = LibraryFilter.MyLibrary,
|
||||
) : AppState
|
||||
|
||||
data class BookInfo(
|
||||
@@ -31,6 +36,7 @@ internal sealed interface AppState {
|
||||
val libraryItems: List<LibraryItem>,
|
||||
val scanPath: String,
|
||||
val message: String? = null,
|
||||
val selectedFilter: LibraryFilter = LibraryFilter.MyLibrary,
|
||||
) : AppState
|
||||
|
||||
data class Error(val message: String) : AppState
|
||||
@@ -42,45 +48,63 @@ internal suspend fun loadStartupState(): AppState {
|
||||
} catch (t: Throwable) {
|
||||
return AppState.Error(t.message ?: strings.couldNotOpenLibrary)
|
||||
}
|
||||
loadPlatformOpenBookRequest()?.let { request ->
|
||||
val libraryFilter = loadLibraryFilter()
|
||||
val platformRequest = runCatching { loadPlatformOpenBookRequest() }.getOrElse {
|
||||
return AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotOpenLibrary, libraryFilter)
|
||||
}
|
||||
platformRequest?.let { request ->
|
||||
return runCatching {
|
||||
val book = parseBookInBackground(request.bytes, request.displayName)
|
||||
saveActiveReadingFileId(request.id)
|
||||
AppState.Reader(
|
||||
fileId = request.id,
|
||||
book = Fb2Format.parse(request.bytes, request.displayName),
|
||||
book = book,
|
||||
libraryItems = emptyList(),
|
||||
scanPath = scanPath,
|
||||
selectedFilter = libraryFilter,
|
||||
)
|
||||
}.getOrElse {
|
||||
AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotOpen(request.displayName))
|
||||
AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotOpen(request.displayName), libraryFilter)
|
||||
}
|
||||
}
|
||||
val activeFileId = loadActiveReadingFileId() ?: return AppState.Library(emptyList(), scanPath)
|
||||
val activeFileId = loadActiveReadingFileId() ?: return AppState.Library(emptyList(), scanPath, selectedFilter = libraryFilter)
|
||||
val item = loadLibraryItem(activeFileId)
|
||||
if (item == null) {
|
||||
saveActiveReadingFileId(null)
|
||||
return AppState.Library(emptyList(), scanPath)
|
||||
return AppState.Library(emptyList(), scanPath, selectedFilter = libraryFilter)
|
||||
}
|
||||
return runCatching {
|
||||
val bytes = openLibraryBook(activeFileId) ?: error(strings.bookFileNotAvailable)
|
||||
AppState.Reader(
|
||||
fileId = activeFileId,
|
||||
book = Fb2Format.parse(bytes, item.storageUri ?: item.title),
|
||||
book = parseBookInBackground(bytes, item.storageUri ?: item.title),
|
||||
libraryItems = emptyList(),
|
||||
scanPath = scanPath,
|
||||
selectedFilter = libraryFilter,
|
||||
)
|
||||
}.getOrElse {
|
||||
saveActiveReadingFileId(null)
|
||||
AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotReopenLastBook())
|
||||
AppState.Library(emptyList(), scanPath, it.message ?: strings.couldNotReopenLastBook(), libraryFilter)
|
||||
}
|
||||
}
|
||||
|
||||
internal suspend fun loadLibraryState(message: String? = null, scanPath: String? = null): AppState =
|
||||
internal suspend fun loadLibraryState(
|
||||
message: String? = null,
|
||||
scanPath: String? = null,
|
||||
selectedFilter: LibraryFilter? = null,
|
||||
): AppState =
|
||||
runCatching {
|
||||
AppState.Library(
|
||||
items = emptyList(),
|
||||
scanPath = scanPath ?: defaultLibraryScanPath().orEmpty(),
|
||||
message = message,
|
||||
selectedFilter = selectedFilter ?: loadLibraryFilter(),
|
||||
)
|
||||
}.getOrElse {
|
||||
AppState.Error(it.message ?: strings.couldNotOpenLibrary)
|
||||
}
|
||||
|
||||
internal suspend fun parseBookInBackground(input: ByteArray, fileName: String? = null): Fb2Book =
|
||||
withContext(Dispatchers.Default) {
|
||||
Fb2Format.parse(input, fileName)
|
||||
}
|
||||
@@ -92,6 +92,16 @@ internal fun BookInfoScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
InfoSection(strings.sourceFile) {
|
||||
if (extras == null) {
|
||||
Text(strings.loading, style = MaterialTheme.typography.bodyMedium)
|
||||
} else {
|
||||
DetailLine(strings.fileName, extras?.sourceFileName?.ifBlank { null } ?: strings.notSpecified)
|
||||
DetailLine(strings.filePath, extras?.sourceFilePath?.ifBlank { null } ?: strings.notSpecified)
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
InfoSection(strings.lastReadingPosition) {
|
||||
val position = extras?.lastReadingPosition
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.sergeych.toread
|
||||
import net.sergeych.toread.fb2.Fb2Binary
|
||||
import net.sergeych.toread.fb2.Fb2Book
|
||||
import net.sergeych.toread.storage.BookReadingStatus
|
||||
import kotlin.math.abs
|
||||
|
||||
data class LibraryItem(
|
||||
val fileId: String,
|
||||
@@ -61,9 +62,27 @@ data class ReadingPosition(
|
||||
val itemIndex: Int,
|
||||
val scrollOffset: Int,
|
||||
val readAloudSentenceIndex: Int? = null,
|
||||
val backStack: List<ReadingPosition> = emptyList(),
|
||||
)
|
||||
|
||||
data class ReaderFontSettings(
|
||||
val fontSizeSp: Float = defaultReaderFontSizeSp(),
|
||||
val lineHeightSp: Float = DefaultReaderLineHeightSp,
|
||||
val fontWeight: Int = defaultReaderFontWeight(),
|
||||
val fontName: String? = null,
|
||||
val lightBackgroundArgb: Long? = null,
|
||||
val lightTextArgb: Long? = null,
|
||||
val darkBackgroundArgb: Long? = null,
|
||||
val darkTextArgb: Long? = null,
|
||||
val lightCustomBackgroundArgb: Long? = null,
|
||||
val lightCustomTextArgb: Long? = null,
|
||||
val darkCustomBackgroundArgb: Long? = null,
|
||||
val darkCustomTextArgb: Long? = null,
|
||||
)
|
||||
|
||||
data class BookInfoExtras(
|
||||
val sourceFileName: String? = null,
|
||||
val sourceFilePath: String? = null,
|
||||
val bookmarks: List<BookmarkInfo> = emptyList(),
|
||||
val notes: List<NoteInfo> = emptyList(),
|
||||
val lastReadingPosition: ReadingPosition? = null,
|
||||
@@ -130,7 +149,9 @@ expect suspend fun markLibraryFavorite(fileId: String, favorite: Boolean): Boole
|
||||
|
||||
expect suspend fun shareLibraryBookFile(fileId: String): Boolean
|
||||
|
||||
expect suspend fun viewLibraryBookFile(fileId: String): Boolean
|
||||
expect suspend fun viewLibraryBookFile(folder: String, fileName: String): Boolean
|
||||
|
||||
expect fun openPlatformUrl(url: String): Boolean
|
||||
|
||||
expect suspend fun loadBookInfoExtras(fileId: String): BookInfoExtras
|
||||
|
||||
@@ -142,10 +163,26 @@ expect suspend fun loadThemeMode(): ThemeMode
|
||||
|
||||
expect suspend fun saveThemeMode(mode: ThemeMode)
|
||||
|
||||
expect suspend fun loadReaderFontSettings(): ReaderFontSettings
|
||||
|
||||
expect suspend fun saveReaderFontSettings(settings: ReaderFontSettings)
|
||||
|
||||
expect suspend fun loadReaderFullScreen(): Boolean
|
||||
|
||||
expect suspend fun saveReaderFullScreen(fullScreen: Boolean)
|
||||
|
||||
expect suspend fun loadReaderPagingOnly(): Boolean
|
||||
|
||||
expect suspend fun saveReaderPagingOnly(enabled: Boolean)
|
||||
|
||||
expect suspend fun loadScanDownloadsAutomatically(): Boolean
|
||||
|
||||
expect suspend fun saveScanDownloadsAutomatically(enabled: Boolean)
|
||||
|
||||
internal expect suspend fun loadLibraryFilter(): LibraryFilter
|
||||
|
||||
internal expect suspend fun saveLibraryFilter(filter: LibraryFilter)
|
||||
|
||||
expect suspend fun loadAppLocaleTag(): String?
|
||||
|
||||
expect suspend fun saveAppLocaleTag(localeTag: String?)
|
||||
@@ -170,3 +207,198 @@ internal fun Fb2Book.libraryCoverBinary(): Fb2Binary? {
|
||||
val image = coverImages.firstOrNull() ?: bodyImages.firstOrNull()
|
||||
return image?.let(::binaryFor)
|
||||
}
|
||||
|
||||
internal fun canShareLibraryBookFile(): Boolean =
|
||||
getPlatform().name.startsWith("Android")
|
||||
|
||||
internal suspend fun shareLibraryBook(fileId: String): String =
|
||||
if (shareLibraryBookFile(fileId)) strings.shareOpened() else strings.couldNotShareBook()
|
||||
|
||||
internal fun canViewLibraryBookFile(): Boolean =
|
||||
getPlatform().name.startsWith("Java")
|
||||
|
||||
internal suspend fun viewLibraryBook(fileId: String): String {
|
||||
val location = loadLibraryItem(fileId)?.storageUri?.toLibraryBookFileLocation()
|
||||
?: return strings.couldNotOpenFileLocation()
|
||||
return if (viewLibraryBookFile(location.folder, location.fileName)) {
|
||||
strings.openedFileLocation()
|
||||
} else {
|
||||
strings.couldNotOpenFileLocation()
|
||||
}
|
||||
}
|
||||
|
||||
private data class LibraryBookFileLocation(
|
||||
val folder: String,
|
||||
val fileName: String,
|
||||
)
|
||||
|
||||
private fun String.toLibraryBookFileLocation(): LibraryBookFileLocation? {
|
||||
val separatorIndex = maxOf(lastIndexOf('/'), lastIndexOf('\\'))
|
||||
if (separatorIndex < 0 || separatorIndex >= lastIndex) return null
|
||||
return LibraryBookFileLocation(
|
||||
folder = if (separatorIndex == 0) substring(0, 1) else substring(0, separatorIndex),
|
||||
fileName = substring(separatorIndex + 1),
|
||||
)
|
||||
}
|
||||
|
||||
internal const val DefaultReaderLineHeightSp = 26f
|
||||
internal const val ReaderFontSizeStepSp = 1f
|
||||
internal const val ReaderLineHeightStepSp = 1f
|
||||
|
||||
private const val MinReaderFontSizeSp = 14f
|
||||
private const val MaxReaderFontSizeSp = 34f
|
||||
private const val MinReaderLineHeightSp = 18f
|
||||
private const val MaxReaderLineHeightSp = 48f
|
||||
private const val MinReaderFontWeight = 100
|
||||
private const val MaxReaderFontWeight = 900
|
||||
private val ReaderFontWeightSteps = listOf(100, 200, 300, 400, 500, 600, 700, 800, 900)
|
||||
|
||||
internal fun defaultReaderFontSettings(): ReaderFontSettings = ReaderFontSettings()
|
||||
|
||||
internal fun defaultReaderFontSizeSp(): Float =
|
||||
if (getPlatform().name.startsWith("Android")) 20f else 18f
|
||||
|
||||
internal fun defaultReaderFontWeight(): Int =
|
||||
if (getPlatform().name.startsWith("Android")) 350 else 400
|
||||
|
||||
internal fun ReaderFontSettings.coerced(): ReaderFontSettings =
|
||||
copy(
|
||||
fontSizeSp = fontSizeSp.coerceIn(MinReaderFontSizeSp, MaxReaderFontSizeSp),
|
||||
lineHeightSp = lineHeightSp.coerceIn(MinReaderLineHeightSp, MaxReaderLineHeightSp),
|
||||
fontWeight = fontWeight.coerceIn(MinReaderFontWeight, MaxReaderFontWeight),
|
||||
fontName = fontName?.takeIf { it.isNotBlank() },
|
||||
lightBackgroundArgb = lightBackgroundArgb?.coerceArgb(),
|
||||
lightTextArgb = lightTextArgb?.coerceArgb(),
|
||||
darkBackgroundArgb = darkBackgroundArgb?.coerceArgb(),
|
||||
darkTextArgb = darkTextArgb?.coerceArgb(),
|
||||
lightCustomBackgroundArgb = lightCustomBackgroundArgb?.coerceArgb(),
|
||||
lightCustomTextArgb = lightCustomTextArgb?.coerceArgb(),
|
||||
darkCustomBackgroundArgb = darkCustomBackgroundArgb?.coerceArgb(),
|
||||
darkCustomTextArgb = darkCustomTextArgb?.coerceArgb(),
|
||||
)
|
||||
|
||||
internal fun ReaderFontSettings.adjustFontSize(steps: Int): ReaderFontSettings {
|
||||
val current = coerced()
|
||||
val nextFontSize = (current.fontSizeSp + steps * ReaderFontSizeStepSp)
|
||||
.coerceIn(MinReaderFontSizeSp, MaxReaderFontSizeSp)
|
||||
val scale = if (current.fontSizeSp > 0f) nextFontSize / current.fontSizeSp else 1f
|
||||
return current.copy(
|
||||
fontSizeSp = nextFontSize,
|
||||
lineHeightSp = (current.lineHeightSp * scale).coerceIn(MinReaderLineHeightSp, MaxReaderLineHeightSp),
|
||||
)
|
||||
}
|
||||
|
||||
internal fun ReaderFontSettings.adjustLineHeight(steps: Int): ReaderFontSettings =
|
||||
coerced().let { current ->
|
||||
current.copy(
|
||||
lineHeightSp = (current.lineHeightSp + steps * ReaderLineHeightStepSp)
|
||||
.coerceIn(MinReaderLineHeightSp, MaxReaderLineHeightSp),
|
||||
)
|
||||
}
|
||||
|
||||
internal fun ReaderFontSettings.adjustFontWeight(steps: Int): ReaderFontSettings {
|
||||
val current = coerced()
|
||||
if (steps == 0) return current
|
||||
|
||||
var nextWeight = current.fontWeight
|
||||
repeat(abs(steps)) {
|
||||
nextWeight = current.fontWeight + if (steps > 0) 50 else -50
|
||||
if( nextWeight == 100 || nextWeight == 900 ) return@repeat
|
||||
}
|
||||
return current.copy(fontWeight = nextWeight)
|
||||
}
|
||||
|
||||
internal fun ReaderFontSettings.toStorageValue(): String =
|
||||
coerced().let { current ->
|
||||
listOf(
|
||||
current.fontSizeSp.toString(),
|
||||
current.lineHeightSp.toString(),
|
||||
current.fontWeight.toString(),
|
||||
current.fontName.orEmpty(),
|
||||
current.lightBackgroundArgb?.toStorageArgb().orEmpty(),
|
||||
current.lightTextArgb?.toStorageArgb().orEmpty(),
|
||||
current.darkBackgroundArgb?.toStorageArgb().orEmpty(),
|
||||
current.darkTextArgb?.toStorageArgb().orEmpty(),
|
||||
current.lightCustomBackgroundArgb?.toStorageArgb().orEmpty(),
|
||||
current.lightCustomTextArgb?.toStorageArgb().orEmpty(),
|
||||
current.darkCustomBackgroundArgb?.toStorageArgb().orEmpty(),
|
||||
current.darkCustomTextArgb?.toStorageArgb().orEmpty(),
|
||||
).joinToString("|")
|
||||
}
|
||||
|
||||
internal fun readerFontSettingsFromStorageValue(value: String?): ReaderFontSettings {
|
||||
val defaults = defaultReaderFontSettings()
|
||||
val parts = value?.split("|") ?: return defaults
|
||||
return ReaderFontSettings(
|
||||
fontSizeSp = parts.getOrNull(0)?.toFloatOrNull() ?: defaults.fontSizeSp,
|
||||
lineHeightSp = parts.getOrNull(1)?.toFloatOrNull() ?: defaults.lineHeightSp,
|
||||
fontWeight = parts.getOrNull(2)?.toIntOrNull() ?: defaults.fontWeight,
|
||||
fontName = parts.getOrNull(3)?.takeIf { it.isNotBlank() } ?: defaults.fontName,
|
||||
lightBackgroundArgb = parts.getOrNull(4)?.storageArgbOrNull(),
|
||||
lightTextArgb = parts.getOrNull(5)?.storageArgbOrNull(),
|
||||
darkBackgroundArgb = parts.getOrNull(6)?.storageArgbOrNull(),
|
||||
darkTextArgb = parts.getOrNull(7)?.storageArgbOrNull(),
|
||||
lightCustomBackgroundArgb = parts.getOrNull(8)?.storageArgbOrNull(),
|
||||
lightCustomTextArgb = parts.getOrNull(9)?.storageArgbOrNull(),
|
||||
darkCustomBackgroundArgb = parts.getOrNull(10)?.storageArgbOrNull(),
|
||||
darkCustomTextArgb = parts.getOrNull(11)?.storageArgbOrNull(),
|
||||
).coerced()
|
||||
}
|
||||
|
||||
internal fun ReaderFontSettings.withThemeBackgroundArgb(useDarkTheme: Boolean, argb: Long?): ReaderFontSettings =
|
||||
if (useDarkTheme) copy(darkBackgroundArgb = argb?.coerceArgb()) else copy(lightBackgroundArgb = argb?.coerceArgb())
|
||||
|
||||
internal fun ReaderFontSettings.withThemeTextArgb(useDarkTheme: Boolean, argb: Long?): ReaderFontSettings =
|
||||
if (useDarkTheme) copy(darkTextArgb = argb?.coerceArgb()) else copy(lightTextArgb = argb?.coerceArgb())
|
||||
|
||||
internal fun ReaderFontSettings.withThemeCustomBackgroundArgb(useDarkTheme: Boolean, argb: Long?): ReaderFontSettings =
|
||||
if (useDarkTheme) {
|
||||
copy(darkCustomBackgroundArgb = argb?.coerceArgb(), darkBackgroundArgb = argb?.coerceArgb())
|
||||
} else {
|
||||
copy(lightCustomBackgroundArgb = argb?.coerceArgb(), lightBackgroundArgb = argb?.coerceArgb())
|
||||
}
|
||||
|
||||
internal fun ReaderFontSettings.withThemeCustomTextArgb(useDarkTheme: Boolean, argb: Long?): ReaderFontSettings =
|
||||
if (useDarkTheme) {
|
||||
copy(darkCustomTextArgb = argb?.coerceArgb(), darkTextArgb = argb?.coerceArgb())
|
||||
} else {
|
||||
copy(lightCustomTextArgb = argb?.coerceArgb(), lightTextArgb = argb?.coerceArgb())
|
||||
}
|
||||
|
||||
internal fun ReaderFontSettings.themeBackgroundArgb(useDarkTheme: Boolean): Long? =
|
||||
if (useDarkTheme) darkBackgroundArgb else lightBackgroundArgb
|
||||
|
||||
internal fun ReaderFontSettings.themeTextArgb(useDarkTheme: Boolean): Long? =
|
||||
if (useDarkTheme) darkTextArgb else lightTextArgb
|
||||
|
||||
internal fun ReaderFontSettings.themeCustomBackgroundArgb(useDarkTheme: Boolean): Long? =
|
||||
if (useDarkTheme) darkCustomBackgroundArgb else lightCustomBackgroundArgb
|
||||
|
||||
internal fun ReaderFontSettings.themeCustomTextArgb(useDarkTheme: Boolean): Long? =
|
||||
if (useDarkTheme) darkCustomTextArgb else lightCustomTextArgb
|
||||
|
||||
internal fun ReaderFontSettings.resetThemeColorScheme(useDarkTheme: Boolean): ReaderFontSettings =
|
||||
if (useDarkTheme) {
|
||||
copy(
|
||||
darkBackgroundArgb = null,
|
||||
darkTextArgb = null,
|
||||
)
|
||||
} else {
|
||||
copy(
|
||||
lightBackgroundArgb = null,
|
||||
lightTextArgb = null,
|
||||
)
|
||||
}
|
||||
|
||||
private fun Long.coerceArgb(): Long =
|
||||
coerceIn(0x00000000L, 0xFFFFFFFFL)
|
||||
|
||||
private fun Long.toStorageArgb(): String =
|
||||
coerceArgb().toString(16).padStart(8, '0').uppercase()
|
||||
|
||||
private fun String.storageArgbOrNull(): Long? =
|
||||
trim()
|
||||
.removePrefix("#")
|
||||
.takeIf { it.length == 8 }
|
||||
?.toLongOrNull(16)
|
||||
?.coerceArgb()
|
||||
@@ -26,12 +26,24 @@ import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.BookmarkAdd
|
||||
import androidx.compose.material.icons.filled.BookmarkRemove
|
||||
import androidx.compose.material.icons.filled.Check
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.DoneAll
|
||||
import androidx.compose.material.icons.filled.Favorite
|
||||
import androidx.compose.material.icons.filled.FavoriteBorder
|
||||
import androidx.compose.material.icons.filled.FolderOpen
|
||||
import androidx.compose.material.icons.filled.Info
|
||||
import androidx.compose.material.icons.filled.KeyboardArrowDown
|
||||
import androidx.compose.material.icons.filled.Language
|
||||
import androidx.compose.material.icons.filled.MenuBook
|
||||
import androidx.compose.material.icons.filled.MoreVert
|
||||
import androidx.compose.material.icons.filled.RemoveDone
|
||||
import androidx.compose.material.icons.filled.Search
|
||||
import androidx.compose.material.icons.filled.Share
|
||||
import androidx.compose.material.icons.filled.VisibilityOff
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.Checkbox
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
@@ -71,7 +83,6 @@ import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import net.sergeych.toread.fb2.Fb2Format
|
||||
import net.sergeych.toread.storage.BookReadingStatus
|
||||
import kotlin.math.roundToInt
|
||||
import kotlinx.coroutines.delay
|
||||
@@ -82,10 +93,11 @@ import kotlinx.coroutines.launch
|
||||
internal fun LibraryScreen(
|
||||
state: AppState.Library,
|
||||
activeScan: LibraryScanProgress?,
|
||||
itemRefreshRequest: LibraryItemRefreshRequest?,
|
||||
hiddenFileIds: Set<String>,
|
||||
onStateChange: (AppState) -> Unit,
|
||||
onLibraryFilterChange: (LibraryFilter) -> Unit,
|
||||
onNavigateToScan: () -> Unit,
|
||||
onStartScan: (String) -> Unit,
|
||||
onDeleteRequested: (
|
||||
request: LibraryDeleteRequest,
|
||||
remove: () -> Unit,
|
||||
@@ -95,24 +107,20 @@ internal fun LibraryScreen(
|
||||
val scope = rememberCoroutineScope()
|
||||
val focusManager = LocalFocusManager.current
|
||||
var busy by remember { mutableStateOf(false) }
|
||||
var openingBook by remember { mutableStateOf(false) }
|
||||
var message by remember(state.message) { mutableStateOf(state.message) }
|
||||
var items by remember(state.items) { mutableStateOf(state.items) }
|
||||
var nextOffset by remember(state.items) { mutableStateOf(state.items.size) }
|
||||
var loadingPage by remember(state.items) { mutableStateOf(false) }
|
||||
var endReached by remember(state.items) { mutableStateOf(false) }
|
||||
var loadingLibrary by remember(state.items) { mutableStateOf(false) }
|
||||
var recentlyAddedItems by remember(state.items) { mutableStateOf<List<LibraryItem>>(emptyList()) }
|
||||
var wasScanning by remember { mutableStateOf(false) }
|
||||
var settingsMenuOpen by remember { mutableStateOf(false) }
|
||||
var localeMenuOpen by remember { mutableStateOf(false) }
|
||||
var autoScanDownloads by remember { mutableStateOf(true) }
|
||||
var autoScanSettingLoaded by remember { mutableStateOf(false) }
|
||||
var backgroundDownloadsRescanStarted by remember { mutableStateOf(false) }
|
||||
var searchText by remember { mutableStateOf("") }
|
||||
var searchFocused by remember { mutableStateOf(false) }
|
||||
var searchResults by remember { mutableStateOf<List<LibraryItem>>(emptyList()) }
|
||||
var searching by remember { mutableStateOf(false) }
|
||||
var selectedFilter by remember(state.scanPath) { mutableStateOf(LibraryFilter.ReadingNow) }
|
||||
var filterChosenByUser by remember(state.scanPath) { mutableStateOf(false) }
|
||||
var selectedFilter by remember(state.scanPath, state.selectedFilter) { mutableStateOf(state.selectedFilter) }
|
||||
val coverCache = remember { mutableStateMapOf<String, LibraryCover?>() }
|
||||
val searchActive = searchText.isNotBlank()
|
||||
val libraryItems = items.filterNot { it.fileId in hiddenFileIds }
|
||||
@@ -121,38 +129,28 @@ internal fun LibraryScreen(
|
||||
val recentlyAdded = recentlyAddedItems.filterNot { it.fileId in hiddenFileIds }
|
||||
val visibleItems = selectedFilter.apply(sourceItems, recentlyAdded, searchActive)
|
||||
.withoutDuplicateFileIds()
|
||||
val canLoadMore = !searchActive && selectedFilter.usesPagedLibrary && !endReached
|
||||
val showShareAction = canShareLibraryBookFile()
|
||||
val showViewFileAction = canViewLibraryBookFile()
|
||||
|
||||
suspend fun loadPage(reset: Boolean = false) {
|
||||
if (loadingPage) return
|
||||
loadingPage = true
|
||||
suspend fun loadLibrary() {
|
||||
if (loadingLibrary) return
|
||||
loadingLibrary = true
|
||||
val previousItems = items
|
||||
if (reset) {
|
||||
nextOffset = 0
|
||||
endReached = false
|
||||
}
|
||||
val offset = if (reset) 0 else nextOffset
|
||||
try {
|
||||
val limit = if (reset) maxOf(LibraryPageSize, previousItems.size) else LibraryPageSize
|
||||
val page = loadLibraryItemsPage(limit, offset)
|
||||
if (reset) {
|
||||
if (page != previousItems) {
|
||||
items = page
|
||||
}
|
||||
val visibleFileIds = page.mapTo(mutableSetOf()) { it.fileId }
|
||||
coverCache.keys.toList().forEach { fileId ->
|
||||
if (fileId !in visibleFileIds) coverCache.remove(fileId)
|
||||
}
|
||||
} else {
|
||||
items = items.appendNewLibraryItems(page)
|
||||
val loadedItems = loadLibraryItems()
|
||||
if (loadedItems != previousItems) {
|
||||
items = loadedItems
|
||||
}
|
||||
val visibleFileIds = loadedItems.mapTo(mutableSetOf()) { it.fileId }
|
||||
recentlyAddedItems.mapTo(visibleFileIds) { it.fileId }
|
||||
searchResults.mapTo(visibleFileIds) { it.fileId }
|
||||
coverCache.keys.toList().forEach { fileId ->
|
||||
if (fileId !in visibleFileIds) coverCache.remove(fileId)
|
||||
}
|
||||
nextOffset = offset + page.size
|
||||
endReached = page.size < limit
|
||||
} catch (t: Throwable) {
|
||||
message = t.message ?: strings.couldNotLoadLibrary
|
||||
endReached = true
|
||||
} finally {
|
||||
loadingPage = false
|
||||
loadingLibrary = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,6 +159,13 @@ internal fun LibraryScreen(
|
||||
recentlyAddedItems = loadRecentlyAddedLibraryItems(since, RecentlyAddedLimit)
|
||||
}
|
||||
|
||||
suspend fun refreshLibraryItem(fileId: String) {
|
||||
val updatedItem = loadLibraryItem(fileId) ?: return
|
||||
items = items.replaceLibraryItem(updatedItem)
|
||||
searchResults = searchResults.replaceLibraryItem(updatedItem)
|
||||
recentlyAddedItems = recentlyAddedItems.replaceLibraryItem(updatedItem)
|
||||
}
|
||||
|
||||
fun refresh(nextMessage: String? = message) {
|
||||
message = nextMessage
|
||||
scope.launch {
|
||||
@@ -169,7 +174,7 @@ internal fun LibraryScreen(
|
||||
searchResults = searchLibraryItems(searchText, SearchResultLimit)
|
||||
searching = false
|
||||
} else {
|
||||
loadPage(reset = true)
|
||||
loadLibrary()
|
||||
loadRecentlyAdded()
|
||||
}
|
||||
}
|
||||
@@ -191,7 +196,7 @@ internal fun LibraryScreen(
|
||||
searchResults = searchLibraryItems(searchText, SearchResultLimit)
|
||||
searching = false
|
||||
} else {
|
||||
loadPage(reset = true)
|
||||
loadLibrary()
|
||||
loadRecentlyAdded()
|
||||
}
|
||||
} else {
|
||||
@@ -215,7 +220,7 @@ internal fun LibraryScreen(
|
||||
searchResults = searchLibraryItems(searchText, SearchResultLimit)
|
||||
searching = false
|
||||
} else {
|
||||
loadPage(reset = true)
|
||||
loadLibrary()
|
||||
loadRecentlyAdded()
|
||||
}
|
||||
} else {
|
||||
@@ -255,12 +260,16 @@ internal fun LibraryScreen(
|
||||
}
|
||||
|
||||
LaunchedEffect(state.scanPath, state.message) {
|
||||
if (items.isEmpty() && !endReached) {
|
||||
loadPage(reset = true)
|
||||
if (items.isEmpty()) {
|
||||
loadLibrary()
|
||||
loadRecentlyAdded()
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(itemRefreshRequest) {
|
||||
itemRefreshRequest?.let { refreshLibraryItem(it.fileId) }
|
||||
}
|
||||
|
||||
LaunchedEffect(searchText) {
|
||||
val query = searchText
|
||||
if (query.isBlank()) {
|
||||
@@ -293,34 +302,8 @@ internal fun LibraryScreen(
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(searchActive, loadingPage, endReached, libraryItems, recentlyAdded) {
|
||||
val libraryDataLoaded = endReached || libraryItems.isNotEmpty() || recentlyAdded.isNotEmpty()
|
||||
if (!filterChosenByUser && !searchActive && !loadingPage && libraryDataLoaded) {
|
||||
selectedFilter = defaultLibraryFilter(libraryItems, recentlyAdded)
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
autoScanDownloads = loadScanDownloadsAutomatically()
|
||||
autoScanSettingLoaded = true
|
||||
}
|
||||
|
||||
LaunchedEffect(autoScanSettingLoaded, autoScanDownloads, activeScan == null) {
|
||||
if (
|
||||
!autoScanSettingLoaded ||
|
||||
!autoScanDownloads ||
|
||||
backgroundDownloadsRescanStarted ||
|
||||
activeScan != null
|
||||
) {
|
||||
return@LaunchedEffect
|
||||
}
|
||||
if (loadDownloadsWasScanned()) {
|
||||
downloadsScanPath()?.let { path ->
|
||||
backgroundDownloadsRescanStarted = true
|
||||
message = strings.scanningDownloads
|
||||
onStartScan(path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(activeScan != null) {
|
||||
@@ -328,17 +311,18 @@ internal fun LibraryScreen(
|
||||
wasScanning = true
|
||||
while (true) {
|
||||
delay(2_000)
|
||||
loadPage(reset = true)
|
||||
loadLibrary()
|
||||
loadRecentlyAdded()
|
||||
}
|
||||
} else if (wasScanning) {
|
||||
wasScanning = false
|
||||
loadPage(reset = true)
|
||||
loadLibrary()
|
||||
loadRecentlyAdded()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
Box(Modifier.fillMaxSize()) {
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = {
|
||||
@@ -351,8 +335,11 @@ internal fun LibraryScreen(
|
||||
LibraryFilterSelect(
|
||||
selected = selectedFilter,
|
||||
onSelected = {
|
||||
filterChosenByUser = true
|
||||
selectedFilter = it
|
||||
onLibraryFilterChange(it)
|
||||
scope.launch {
|
||||
saveLibraryFilter(it)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -402,6 +389,9 @@ internal fun LibraryScreen(
|
||||
HorizontalDivider()
|
||||
Box {
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.Language, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.locale) },
|
||||
trailingIcon = {
|
||||
Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null)
|
||||
@@ -445,6 +435,9 @@ internal fun LibraryScreen(
|
||||
}
|
||||
HorizontalDivider()
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.Info, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.appVersion(AppVersionDisplay)) },
|
||||
enabled = false,
|
||||
onClick = {},
|
||||
@@ -460,37 +453,37 @@ internal fun LibraryScreen(
|
||||
Icon(Icons.Filled.Add, contentDescription = strings.scanFolder)
|
||||
}
|
||||
},
|
||||
) {
|
||||
BoxWithConstraints(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(it)
|
||||
.onPreviewKeyEvent { event ->
|
||||
if (event.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false
|
||||
when {
|
||||
event.key == Key.Escape && searchText.isNotBlank() -> {
|
||||
clearSearch()
|
||||
true
|
||||
}
|
||||
event.key == Key.Escape && searchFocused -> {
|
||||
closeSearch()
|
||||
true
|
||||
}
|
||||
event.key.isEnterKey() && searchFocused && searchText.isBlank() -> {
|
||||
closeSearch()
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
.background(readerBackground()),
|
||||
) {
|
||||
BoxWithConstraints(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(it)
|
||||
.onPreviewKeyEvent { event ->
|
||||
if (event.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false
|
||||
when {
|
||||
event.key == Key.Escape && searchText.isNotBlank() -> {
|
||||
clearSearch()
|
||||
true
|
||||
}
|
||||
event.key == Key.Escape && searchFocused -> {
|
||||
closeSearch()
|
||||
true
|
||||
}
|
||||
event.key.isEnterKey() && searchFocused && searchText.isBlank() -> {
|
||||
closeSearch()
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
.background(readerBackground()),
|
||||
) {
|
||||
val wide = maxWidth >= 800.dp
|
||||
if (visibleItems.isEmpty() && (loadingPage || searching)) {
|
||||
if (visibleItems.isEmpty() && (loadingLibrary || searching)) {
|
||||
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
CircularProgressIndicator()
|
||||
}
|
||||
} else if (visibleItems.isEmpty() && !canLoadMore) {
|
||||
} else if (visibleItems.isEmpty()) {
|
||||
if (searchActive) {
|
||||
EmptySearchPane(modifier = Modifier.fillMaxSize().padding(if (wide) 24.dp else 14.dp))
|
||||
} else {
|
||||
@@ -509,10 +502,11 @@ internal fun LibraryScreen(
|
||||
onOpen = {
|
||||
scope.launch {
|
||||
busy = true
|
||||
openingBook = true
|
||||
try {
|
||||
val next = runCatching {
|
||||
val bytes = openLibraryBook(item.fileId) ?: error(strings.bookFileNotAvailable)
|
||||
val book = Fb2Format.parse(bytes, item.storageUri ?: item.title)
|
||||
val book = parseBookInBackground(bytes, item.storageUri ?: item.title)
|
||||
var readerLibraryItems = visibleItems
|
||||
refreshLibraryItemFromParsedBook(item.fileId, book)?.let { updatedItem ->
|
||||
items = items.replaceLibraryItem(updatedItem)
|
||||
@@ -520,7 +514,7 @@ internal fun LibraryScreen(
|
||||
readerLibraryItems = readerLibraryItems.replaceLibraryItem(updatedItem)
|
||||
coverCache[updatedItem.fileId] = loadLibraryItemCover(updatedItem.fileId)
|
||||
}
|
||||
if (item.readingStatus == BookReadingStatus.NEW &&
|
||||
if (item.readingStatus.shouldBecomeReadingOnOpen() &&
|
||||
markLibraryReadingStatus(item.fileId, BookReadingStatus.READING)
|
||||
) {
|
||||
val readingItem = loadLibraryItem(item.fileId)
|
||||
@@ -536,12 +530,19 @@ internal fun LibraryScreen(
|
||||
libraryItems = readerLibraryItems,
|
||||
scanPath = state.scanPath,
|
||||
message = message,
|
||||
selectedFilter = state.selectedFilter,
|
||||
)
|
||||
}.getOrElse {
|
||||
AppState.Library(visibleItems, state.scanPath, it.message ?: strings.couldNotOpenBook)
|
||||
AppState.Library(
|
||||
visibleItems,
|
||||
state.scanPath,
|
||||
it.message ?: strings.couldNotOpenBook,
|
||||
state.selectedFilter,
|
||||
)
|
||||
}
|
||||
onStateChange(next)
|
||||
} finally {
|
||||
openingBook = false
|
||||
busy = false
|
||||
}
|
||||
}
|
||||
@@ -607,13 +608,32 @@ internal fun LibraryScreen(
|
||||
}
|
||||
}
|
||||
},
|
||||
onShare = {
|
||||
scope.launch {
|
||||
busy = true
|
||||
try {
|
||||
message = shareLibraryBook(item.fileId)
|
||||
} finally {
|
||||
busy = false
|
||||
}
|
||||
}
|
||||
},
|
||||
onViewFile = {
|
||||
scope.launch {
|
||||
busy = true
|
||||
try {
|
||||
message = viewLibraryBook(item.fileId)
|
||||
} finally {
|
||||
busy = false
|
||||
}
|
||||
}
|
||||
},
|
||||
onDelete = {
|
||||
val previousItems = items
|
||||
val previousSearchResults = searchResults
|
||||
val previousRecentlyAddedItems = recentlyAddedItems
|
||||
val previousCover = coverCache[item.fileId]
|
||||
val hadCover = coverCache.containsKey(item.fileId)
|
||||
val previousNextOffset = nextOffset
|
||||
onDeleteRequested(
|
||||
LibraryDeleteRequest(item.fileId, item.title),
|
||||
{
|
||||
@@ -622,7 +642,6 @@ internal fun LibraryScreen(
|
||||
searchResults = searchResults.filterNot { it.fileId == item.fileId }
|
||||
recentlyAddedItems = recentlyAddedItems.filterNot { it.fileId == item.fileId }
|
||||
coverCache.remove(item.fileId)
|
||||
nextOffset = (nextOffset - 1).coerceAtLeast(items.size)
|
||||
},
|
||||
{
|
||||
items = previousItems
|
||||
@@ -631,7 +650,6 @@ internal fun LibraryScreen(
|
||||
if (hadCover) {
|
||||
coverCache[item.fileId] = previousCover
|
||||
}
|
||||
nextOffset = previousNextOffset
|
||||
},
|
||||
)
|
||||
},
|
||||
@@ -643,26 +661,14 @@ internal fun LibraryScreen(
|
||||
item = item,
|
||||
coverCache = coverCache,
|
||||
enabled = !busy,
|
||||
showShareAction = showShareAction,
|
||||
showViewFileAction = showViewFileAction,
|
||||
actions = itemActions(item),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
libraryRows(visibleItems)
|
||||
|
||||
if (canLoadMore) {
|
||||
item(key = "load-more") {
|
||||
LaunchedEffect(nextOffset, items.size) {
|
||||
if (!loadingPage) loadPage()
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier.fillMaxWidth().padding(18.dp),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
CircularProgressIndicator(modifier = Modifier.width(24.dp).height(24.dp), strokeWidth = 2.dp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
activeScan?.let { progress ->
|
||||
@@ -673,6 +679,10 @@ internal fun LibraryScreen(
|
||||
.padding(horizontal = if (wide) 24.dp else 14.dp, vertical = 14.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (openingBook) {
|
||||
LoadingOverlay(strings.loadingOpeningBook)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -862,6 +872,8 @@ private fun LibraryRow(
|
||||
item: LibraryItem,
|
||||
coverCache: MutableMap<String, LibraryCover?>,
|
||||
enabled: Boolean,
|
||||
showShareAction: Boolean,
|
||||
showViewFileAction: Boolean,
|
||||
actions: LibraryItemActions,
|
||||
) {
|
||||
var menuOpen by remember { mutableStateOf(false) }
|
||||
@@ -920,6 +932,9 @@ private fun LibraryRow(
|
||||
}
|
||||
DropdownMenu(expanded = menuOpen, onDismissRequest = { menuOpen = false }) {
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.MenuBook, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.open) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
@@ -929,6 +944,9 @@ private fun LibraryRow(
|
||||
HorizontalDivider()
|
||||
if (item.readingStatus != BookReadingStatus.READ) {
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.DoneAll, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.markAsRead) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
@@ -938,6 +956,9 @@ private fun LibraryRow(
|
||||
}
|
||||
if (item.readingStatus == BookReadingStatus.READ) {
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.RemoveDone, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.markAsUnread) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
@@ -947,6 +968,9 @@ private fun LibraryRow(
|
||||
}
|
||||
if (item.readingStatus != BookReadingStatus.TO_READ) {
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.BookmarkAdd, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.markToRead) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
@@ -956,6 +980,9 @@ private fun LibraryRow(
|
||||
}
|
||||
if (item.readingStatus != BookReadingStatus.NEW) {
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.BookmarkRemove, contentDescription = null)
|
||||
},
|
||||
text = { Text(if (item.readingStatus == BookReadingStatus.TO_READ) strings.removeToRead else strings.removeMarks) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
@@ -964,6 +991,12 @@ private fun LibraryRow(
|
||||
)
|
||||
}
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
if (item.favorite) Icons.Filled.Favorite else Icons.Filled.FavoriteBorder,
|
||||
contentDescription = null,
|
||||
)
|
||||
},
|
||||
text = { Text(if (item.favorite) strings.removeFavorite else strings.addFavorite) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
@@ -971,14 +1004,47 @@ private fun LibraryRow(
|
||||
},
|
||||
)
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.VisibilityOff, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.notInterested) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
actions.onNotInterested()
|
||||
},
|
||||
)
|
||||
if (showShareAction || showViewFileAction) {
|
||||
HorizontalDivider()
|
||||
}
|
||||
if (showShareAction) {
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.Share, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.share) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
actions.onShare()
|
||||
},
|
||||
)
|
||||
}
|
||||
if (showViewFileAction) {
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.FolderOpen, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.viewFile) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
actions.onViewFile()
|
||||
},
|
||||
)
|
||||
}
|
||||
HorizontalDivider()
|
||||
DropdownMenuItem(
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.Delete, contentDescription = null)
|
||||
},
|
||||
text = { Text(strings.delete) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
@@ -999,12 +1065,14 @@ private data class LibraryItemActions(
|
||||
val onMarkToRead: () -> Unit,
|
||||
val onNotInterested: () -> Unit,
|
||||
val onFavoriteChange: (Boolean) -> Unit,
|
||||
val onShare: () -> Unit,
|
||||
val onViewFile: () -> Unit,
|
||||
val onDelete: () -> Unit,
|
||||
)
|
||||
|
||||
private enum class LibraryFilter(val usesPagedLibrary: Boolean = true) {
|
||||
internal enum class LibraryFilter {
|
||||
ReadingNow,
|
||||
RecentlyAdded(usesPagedLibrary = false),
|
||||
RecentlyAdded,
|
||||
MyLibrary,
|
||||
ToRead,
|
||||
Favorites,
|
||||
@@ -1119,16 +1187,6 @@ private val LibraryFilter.label: String
|
||||
LibraryFilter.NotInterested -> strings.notInterested
|
||||
}
|
||||
|
||||
private fun defaultLibraryFilter(
|
||||
libraryItems: List<LibraryItem>,
|
||||
recentlyAddedItems: List<LibraryItem>,
|
||||
): LibraryFilter =
|
||||
when {
|
||||
libraryItems.any { it.readingStatus == BookReadingStatus.READING } -> LibraryFilter.ReadingNow
|
||||
recentlyAddedItems.any { it.readingStatus == BookReadingStatus.NEW } -> LibraryFilter.RecentlyAdded
|
||||
else -> LibraryFilter.MyLibrary
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun LibraryCover(
|
||||
item: LibraryItem,
|
||||
@@ -1193,15 +1251,12 @@ private fun Long.formatBytes(): String =
|
||||
private fun List<LibraryItem>.replaceLibraryItem(item: LibraryItem): List<LibraryItem> =
|
||||
map { current -> if (current.fileId == item.fileId) item else current }
|
||||
|
||||
internal fun List<LibraryItem>.appendNewLibraryItems(page: List<LibraryItem>): List<LibraryItem> {
|
||||
if (isEmpty()) return page.withoutDuplicateFileIds()
|
||||
val fileIds = mapTo(mutableSetOf()) { it.fileId }
|
||||
return this + page.filter { fileIds.add(it.fileId) }
|
||||
}
|
||||
|
||||
internal fun List<LibraryItem>.withoutDuplicateFileIds(): List<LibraryItem> =
|
||||
distinctBy { it.fileId }
|
||||
|
||||
internal fun BookReadingStatus.shouldBecomeReadingOnOpen(): Boolean =
|
||||
this == BookReadingStatus.NEW || this == BookReadingStatus.NOT_INTERESTED
|
||||
|
||||
private fun Key.isEnterKey(): Boolean = this == Key.Enter || this == Key.NumPadEnter
|
||||
|
||||
private fun LibraryScanProgress.toCatalogScanMessage(): String {
|
||||
@@ -1214,7 +1269,6 @@ private fun LibraryScanProgress.toCatalogScanMessage(): String {
|
||||
return strings.scannedProgress(scannedFiles, total, percent)
|
||||
}
|
||||
|
||||
private const val LibraryPageSize: Int = 50
|
||||
private const val SearchResultLimit: Int = 100
|
||||
private const val RecentlyAddedLimit: Int = 50
|
||||
private const val RecentlyAddedWindowMillis: Long = 30L * 60L * 60L * 1000L
|
||||
@@ -44,6 +44,7 @@ internal open class AppStrings {
|
||||
open val undo = "Undo"
|
||||
open val retry = "Retry"
|
||||
open val done = "Done"
|
||||
open val cancel = "Cancel"
|
||||
|
||||
open val libraryError = "Library error"
|
||||
open val couldNotOpenLibrary = "Could not open library."
|
||||
@@ -117,6 +118,9 @@ internal open class AppStrings {
|
||||
open val words = "Words"
|
||||
open val sections = "Sections"
|
||||
open val images = "Images"
|
||||
open val sourceFile = "Source file"
|
||||
open val fileName = "File name"
|
||||
open val filePath = "Path"
|
||||
open val lastReadingPosition = "Last Reading Position"
|
||||
open val noSavedPosition = "No saved position"
|
||||
open val listItem = "List item"
|
||||
@@ -130,9 +134,35 @@ internal open class AppStrings {
|
||||
open val noImage = "No image"
|
||||
|
||||
open val readerTheme = "Theme"
|
||||
open val tableOfContents = "Contents"
|
||||
open val backToLastPosition = "Back"
|
||||
open val readAloud = "Read aloud"
|
||||
open val readerMenu = "Book reader menu"
|
||||
open val info = "Info..."
|
||||
open val readerSettings = "Settings"
|
||||
open val fullScreen = "Full screen"
|
||||
open val readerFontSizeIncrease = "Increase font size"
|
||||
open val readerFontSizeDecrease = "Decrease font size"
|
||||
open val readerLineHeightIncrease = "Increase line spacing"
|
||||
open val readerLineHeightDecrease = "Decrease line spacing"
|
||||
open val readerFontWeightIncrease = "Increase font weight"
|
||||
open val readerFontWeightDecrease = "Decrease font weight"
|
||||
open val resetReaderFontSettings = "Reset font settings"
|
||||
open val revertColorScheme = "Revert color scheme"
|
||||
open val readerBackgroundColor = "Reader background color"
|
||||
open val readerTextColor = "Reader text color"
|
||||
open val extraReaderSettings = "Extra reader settings"
|
||||
open val customColor = "Custom color"
|
||||
open val red = "R"
|
||||
open val green = "G"
|
||||
open val blue = "B"
|
||||
open val readerColors = "Colors"
|
||||
open val readerTypography = "Typography"
|
||||
open val readerLayout = "Layout"
|
||||
open val readerNavigation = "Navigation"
|
||||
open val readerPagingOnly = "Paging only (no free scroll)"
|
||||
open val readerProgress = "Progress"
|
||||
open val closeReaderSettings = "Close reader settings"
|
||||
open val share = "Share"
|
||||
open val viewFile = "View file"
|
||||
open val previousSentence = "Previous sentence"
|
||||
@@ -145,6 +175,9 @@ internal open class AppStrings {
|
||||
open val offlineVoice = "Offline voice"
|
||||
open val autoRussian = "Auto Russian"
|
||||
open val loadingVoices = "Loading voices..."
|
||||
open val preparingSpeech = "Preparing speech…"
|
||||
open val offlineSpeechModelMissing = "Download the offline speech model first; see the desktop setup guide."
|
||||
open val speechPlaybackFailed = "Speech playback failed. Check the offline model and audio output, then try again."
|
||||
|
||||
open val imageCopied = "Image copied"
|
||||
open val copyFailed = "Copy failed"
|
||||
@@ -173,6 +206,10 @@ internal open class AppStrings {
|
||||
open fun couldNotReopenLastBook(): String = "Could not reopen last book."
|
||||
open fun scanReport(scanned: Int, imported: Int, skipped: Int, failed: Int): String =
|
||||
"Scanned $scanned, imported $imported, skipped $skipped, failed $failed."
|
||||
open val noNewBooksFound = "Nothing new has been found."
|
||||
open val show = "Show"
|
||||
open fun newBooksAdded(count: Int): String =
|
||||
if (count == 1) "1 new book added." else "$count new books added."
|
||||
open fun rescanReport(scanned: Int, updated: Int, failed: Int): String =
|
||||
"Rescanned $scanned, updated $updated, failed $failed."
|
||||
open fun checkingFile(currentFile: String?, scanned: Int, imported: Int, skipped: Int, failed: Int): String =
|
||||
@@ -236,6 +273,7 @@ internal object RussianStrings : AppStrings() {
|
||||
override val undo = "Отменить"
|
||||
override val retry = "Повторить"
|
||||
override val done = "Готово"
|
||||
override val cancel = "Отмена"
|
||||
|
||||
override val libraryError = "Ошибка библиотеки"
|
||||
override val couldNotOpenLibrary = "Не удалось открыть библиотеку."
|
||||
@@ -243,7 +281,7 @@ internal object RussianStrings : AppStrings() {
|
||||
override val couldNotOpenBook = "Не удалось открыть книгу."
|
||||
override val couldNotUpdateBook = "Не удалось обновить книгу."
|
||||
override val bookFileNotAvailable = "Файл книги недоступен."
|
||||
override val scanFailed = "Сканирование не удалось."
|
||||
override val scanFailed = "Импорт не удался."
|
||||
override val searchFailed = "Поиск не удался."
|
||||
override val libraryRescanFailed = "Повторное сканирование библиотеки не удалось."
|
||||
override val rescanningLibrary = "Пересканируем библиотеку..."
|
||||
@@ -264,7 +302,7 @@ internal object RussianStrings : AppStrings() {
|
||||
override val closeSearch = "Закрыть поиск"
|
||||
override val clearSearch = "Очистить поиск"
|
||||
override val noMatches = "Ничего не найдено"
|
||||
override val scanFolderOrChooseFilter = "Просканируйте папку или выберите другой фильтр библиотеки."
|
||||
override val scanFolderOrChooseFilter = "Импортируйте папку или выберите другой фильтр библиотеки."
|
||||
override val favorite = "Избранное"
|
||||
override val unknownAuthor = "Автор неизвестен"
|
||||
override val noMetadata = "Нет метаданных"
|
||||
@@ -287,7 +325,7 @@ internal object RussianStrings : AppStrings() {
|
||||
override val filterToRead = "К чтению"
|
||||
override val filterRead = "Прочитанные"
|
||||
|
||||
override val scan = "Сканирование"
|
||||
override val scan = "Импорт"
|
||||
override val rootFolder = "Корневая папка"
|
||||
override val choose = "Выбрать"
|
||||
override val logPrefix = "Лог"
|
||||
@@ -309,6 +347,9 @@ internal object RussianStrings : AppStrings() {
|
||||
override val words = "Слова"
|
||||
override val sections = "Разделы"
|
||||
override val images = "Иллюстрации"
|
||||
override val sourceFile = "Исходный файл"
|
||||
override val fileName = "Имя файла"
|
||||
override val filePath = "Путь"
|
||||
override val lastReadingPosition = "Последняя позиция чтения"
|
||||
override val noSavedPosition = "Позиция не сохранена"
|
||||
override val listItem = "Элемент списка"
|
||||
@@ -322,9 +363,35 @@ internal object RussianStrings : AppStrings() {
|
||||
override val noImage = "Нет изображения"
|
||||
|
||||
override val readerTheme = "Тема"
|
||||
override val tableOfContents = "Содержание"
|
||||
override val backToLastPosition = "Назад"
|
||||
override val readAloud = "Читать вслух"
|
||||
override val readerMenu = "Меню чтения"
|
||||
override val info = "Информация..."
|
||||
override val readerSettings = "Настройки"
|
||||
override val fullScreen = "На весь экран"
|
||||
override val readerFontSizeIncrease = "Увеличить шрифт"
|
||||
override val readerFontSizeDecrease = "Уменьшить шрифт"
|
||||
override val readerLineHeightIncrease = "Увеличить межстрочный интервал"
|
||||
override val readerLineHeightDecrease = "Уменьшить межстрочный интервал"
|
||||
override val readerFontWeightIncrease = "Увеличить насыщенность шрифта"
|
||||
override val readerFontWeightDecrease = "Уменьшить насыщенность шрифта"
|
||||
override val resetReaderFontSettings = "Сбросить настройки шрифта"
|
||||
override val revertColorScheme = "Вернуть цветовую схему"
|
||||
override val readerBackgroundColor = "Цвет фона чтения"
|
||||
override val readerTextColor = "Цвет текста чтения"
|
||||
override val extraReaderSettings = "Дополнительные настройки"
|
||||
override val customColor = "Свой цвет"
|
||||
override val red = "К"
|
||||
override val green = "З"
|
||||
override val blue = "С"
|
||||
override val readerColors = "Цвета"
|
||||
override val readerTypography = "Типографика"
|
||||
override val readerLayout = "Макет"
|
||||
override val readerNavigation = "Навигация"
|
||||
override val readerPagingOnly = "Постраничный режим (без прокрутки)"
|
||||
override val readerProgress = "Прогресс"
|
||||
override val closeReaderSettings = "Закрыть настройки чтения"
|
||||
override val share = "Поделиться"
|
||||
override val viewFile = "Показать файл"
|
||||
override val previousSentence = "Предыдущее предложение"
|
||||
@@ -337,6 +404,9 @@ internal object RussianStrings : AppStrings() {
|
||||
override val offlineVoice = "Офлайн-голос"
|
||||
override val autoRussian = "Авто: русский"
|
||||
override val loadingVoices = "Загружаем голоса..."
|
||||
override val preparingSpeech = "Подготавливаем речь…"
|
||||
override val offlineSpeechModelMissing = "Сначала загрузите офлайн-модель речи по инструкции для компьютера."
|
||||
override val speechPlaybackFailed = "Не удалось воспроизвести речь. Проверьте офлайн-модель и вывод звука и попробуйте снова."
|
||||
|
||||
override val imageCopied = "Изображение скопировано"
|
||||
override val copyFailed = "Не удалось скопировать"
|
||||
@@ -365,6 +435,14 @@ internal object RussianStrings : AppStrings() {
|
||||
override fun couldNotReopenLastBook(): String = "Не удалось открыть последнюю книгу."
|
||||
override fun scanReport(scanned: Int, imported: Int, skipped: Int, failed: Int): String =
|
||||
"Проверено: $scanned, импортировано: $imported, пропущено: $skipped, ошибок: $failed."
|
||||
override val noNewBooksFound = "Ничего нового не найдено."
|
||||
override val show = "Показать"
|
||||
override fun newBooksAdded(count: Int): String =
|
||||
when {
|
||||
count % 10 == 1 && count % 100 != 11 -> "$count новая книга добавлена."
|
||||
count % 10 in 2..4 && count % 100 !in 12..14 -> "$count новые книги добавлены."
|
||||
else -> "$count новых книг добавлено."
|
||||
}
|
||||
override fun rescanReport(scanned: Int, updated: Int, failed: Int): String =
|
||||
"Пересканировано: $scanned, обновлено: $updated, ошибок: $failed."
|
||||
override fun checkingFile(currentFile: String?, scanned: Int, imported: Int, skipped: Int, failed: Int): String =
|
||||
@@ -378,9 +456,9 @@ internal object RussianStrings : AppStrings() {
|
||||
}
|
||||
override fun importedSkippedFailed(imported: Int, skipped: Int, failed: Int): String =
|
||||
"Импортировано: $imported, пропущено: $skipped, ошибок: $failed"
|
||||
override fun scannedBooks(scanned: Int): String = "Просканировано книг: $scanned"
|
||||
override fun scannedBooks(scanned: Int): String = "Импортировано книг: $scanned"
|
||||
override fun scannedProgress(scanned: Int, total: Int, percent: Int): String =
|
||||
"Просканировано $scanned из $total, готово $percent%"
|
||||
"Импорт: $scanned из $total, готово $percent%"
|
||||
override fun noBooksIn(filterLabel: String): String = "Нет книг: ${filterLabel.lowercase()}"
|
||||
override fun bookMenuFor(title: String): String = "Меню книги: $title"
|
||||
override fun markedAsRead(title: String?): String =
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package net.sergeych.toread
|
||||
|
||||
internal data class ReadAloudBlockRange(val position: Int, val start: Int, val endExclusive: Int)
|
||||
|
||||
internal data class ReadAloudBlock(
|
||||
val text: String,
|
||||
val ranges: List<ReadAloudBlockRange>,
|
||||
val pauseBeforeMillis: Long,
|
||||
val pauseAfterMillis: Long,
|
||||
) {
|
||||
fun positionAt(offset: Int): Int =
|
||||
ranges.lastOrNull { it.start <= offset }?.position ?: ranges.first().position
|
||||
}
|
||||
|
||||
/** Paragraph-sized requests. Android's hard limit counts UTF-16 characters, never UTF-8 bytes.
|
||||
* Whole sentences are retained whenever they fit. An oversized sentence is split at whitespace,
|
||||
* or, for an oversized word, between Unicode code points (never inside a surrogate pair).
|
||||
*/
|
||||
internal fun readAloudBlocks(
|
||||
sentences: List<ReadAloudSentence>,
|
||||
startIndex: Int,
|
||||
maxLength: Int,
|
||||
): Sequence<ReadAloudBlock> = sequence {
|
||||
require(maxLength >= 2)
|
||||
var text = StringBuilder()
|
||||
var ranges = mutableListOf<ReadAloudBlockRange>()
|
||||
var before = 0L
|
||||
var after = 0L
|
||||
var item: Int? = null
|
||||
|
||||
suspend fun SequenceScope<ReadAloudBlock>.flush() {
|
||||
if (ranges.isEmpty()) return
|
||||
yield(ReadAloudBlock(text.toString(), ranges.toList(), before, after))
|
||||
text = StringBuilder()
|
||||
ranges = mutableListOf()
|
||||
before = 0L
|
||||
after = 0L
|
||||
}
|
||||
|
||||
for (position in startIndex.coerceAtLeast(0) until sentences.size) {
|
||||
val sentence = sentences[position]
|
||||
if (item != sentence.itemIndex || sentence.pauseBeforeMillis > 0 || after > 0) flush()
|
||||
item = sentence.itemIndex
|
||||
var offset = 0
|
||||
val spoken = sentence.spokenText
|
||||
while (offset < spoken.length) {
|
||||
// Do not split a sentence just to fill the previous block.
|
||||
if (text.isNotEmpty() && text.length + 1 + spoken.length - offset > maxLength) flush()
|
||||
if (ranges.isEmpty()) before = if (offset == 0) sentence.pauseBeforeMillis else 0L
|
||||
var end = minOf(spoken.length, offset + maxLength)
|
||||
if (end < spoken.length) {
|
||||
if (spoken[end - 1].isHighSurrogate() && spoken[end].isLowSurrogate()) end--
|
||||
val whitespace = (end - 1 downTo offset + 1).firstOrNull { spoken[it].isWhitespace() }
|
||||
if (whitespace != null) end = whitespace
|
||||
}
|
||||
if (text.isNotEmpty()) text.append(' ')
|
||||
val start = text.length
|
||||
text.append(spoken.substring(offset, end))
|
||||
ranges += ReadAloudBlockRange(position, start, text.length)
|
||||
offset = end
|
||||
while (offset < spoken.length && spoken[offset].isWhitespace()) offset++
|
||||
after = if (offset == spoken.length) sentence.pauseAfterMillis else 0L
|
||||
if (offset < spoken.length || text.length >= maxLength || after > 0) flush()
|
||||
}
|
||||
}
|
||||
flush()
|
||||
}
|
||||
@@ -17,6 +17,7 @@ data class ReadAloudState(
|
||||
val active: Boolean = false,
|
||||
val playing: Boolean = false,
|
||||
val sentenceIndex: Int? = null,
|
||||
val buffering: Boolean = false,
|
||||
)
|
||||
|
||||
data class ReadAloudEngineOption(
|
||||
@@ -45,6 +46,7 @@ data class ReadAloudSettingsState(
|
||||
val textReplacements: List<ReadAloudTextReplacement> = emptyList(),
|
||||
val loading: Boolean = false,
|
||||
val message: String? = null,
|
||||
val usesSystemDefaults: Boolean = true,
|
||||
)
|
||||
|
||||
expect object ReadAloudPlatform {
|
||||
|
||||
@@ -57,7 +57,7 @@ internal fun ScanScreen(
|
||||
CenterAlignedTopAppBar(
|
||||
title = { Text(strings.scan) },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = { onStateChange(AppState.Library(state.items, scanPath, message)) }) {
|
||||
IconButton(onClick = { onStateChange(AppState.Library(state.items, scanPath, message, state.selectedFilter)) }) {
|
||||
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = strings.backToLibrary)
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.sergeych.toread
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
@@ -31,8 +32,39 @@ import kotlin.math.roundToInt
|
||||
|
||||
@Composable
|
||||
internal fun LoadingScreen(message: String) {
|
||||
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(14.dp)) {
|
||||
Box(
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.background),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
LoadingPanel(message)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun LoadingOverlay(message: String, modifier: Modifier = Modifier) {
|
||||
Box(
|
||||
modifier
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.background.copy(alpha = 0.58f)),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
LoadingPanel(message)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun LoadingPanel(message: String) {
|
||||
Card(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.padding(horizontal = 20.dp, vertical = 18.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
CircularProgressIndicator()
|
||||
Text(message, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
@@ -109,6 +141,14 @@ internal fun themedTopAppBarColors(): TopAppBarColors =
|
||||
@Composable
|
||||
internal fun readerBackground(): Brush = SolidColor(MaterialTheme.colorScheme.background)
|
||||
|
||||
internal fun Long.toComposeColor(): Color =
|
||||
Color(
|
||||
alpha = ((this shr 24) and 0xFF).toInt(),
|
||||
red = ((this shr 16) and 0xFF).toInt(),
|
||||
green = ((this shr 8) and 0xFF).toInt(),
|
||||
blue = (this and 0xFF).toInt(),
|
||||
)
|
||||
|
||||
@Composable
|
||||
internal fun readerImageBackgroundColor(): Color {
|
||||
val surface = MaterialTheme.colorScheme.surface
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package net.sergeych.toread
|
||||
|
||||
import androidx.compose.runtime.compositionLocalOf
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
internal val LocalReaderThemeIsDark = compositionLocalOf { false }
|
||||
|
||||
internal fun ThemeMode.next(): ThemeMode =
|
||||
when (this) {
|
||||
ThemeMode.SYSTEM -> ThemeMode.LIGHT
|
||||
|
||||
@@ -5,37 +5,6 @@ import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class LibraryScreenItemListTest {
|
||||
@Test
|
||||
fun appendNewLibraryItemsSkipsItemsAlreadyInMemory() {
|
||||
val existing = listOf(
|
||||
libraryItem("file-1", "First"),
|
||||
libraryItem("file-2", "Second"),
|
||||
)
|
||||
val page = listOf(
|
||||
libraryItem("file-2", "Second duplicate"),
|
||||
libraryItem("file-3", "Third"),
|
||||
)
|
||||
|
||||
val merged = existing.appendNewLibraryItems(page)
|
||||
|
||||
assertEquals(listOf("file-1", "file-2", "file-3"), merged.map { it.fileId })
|
||||
assertEquals("Second", merged[1].title)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun appendNewLibraryItemsDeduplicatesFirstPage() {
|
||||
val page = listOf(
|
||||
libraryItem("file-1", "First"),
|
||||
libraryItem("file-1", "First duplicate"),
|
||||
libraryItem("file-2", "Second"),
|
||||
)
|
||||
|
||||
val merged = emptyList<LibraryItem>().appendNewLibraryItems(page)
|
||||
|
||||
assertEquals(listOf("file-1", "file-2"), merged.map { it.fileId })
|
||||
assertEquals("First", merged.first().title)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun myLibraryShowsAllBooksExceptNotInterested() {
|
||||
val items = listOf(
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package net.sergeych.toread
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ReadAloudBlocksTest {
|
||||
private fun sentence(index: Int, item: Int, text: String, before: Long = 0, after: Long = 0) =
|
||||
ReadAloudSentence(index, item, 0, text.length, text, pauseBeforeMillis = before, pauseAfterMillis = after)
|
||||
|
||||
@Test
|
||||
fun paragraphsKeepContextAndRangesUseSpokenText() {
|
||||
val queue = listOf(
|
||||
sentence(0, 1, "Первое.").copy(spokenText = "Пе́рвое."),
|
||||
sentence(1, 1, "Второе."), sentence(2, 2, "Другой абзац."),
|
||||
)
|
||||
val blocks = readAloudBlocks(queue, 0, 4_000).toList()
|
||||
assertEquals(listOf("Пе́рвое. Второе.", "Другой абзац."), blocks.map { it.text })
|
||||
assertEquals(0, blocks[0].positionAt(0))
|
||||
assertEquals(1, blocks[0].positionAt(9))
|
||||
assertEquals(listOf("Второе.", "Другой абзац."), readAloudBlocks(queue, 1, 4_000).map { it.text }.toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longParagraphBreaksBetweenSentencesAndCountsCharacters() {
|
||||
val queue = (0..9).map { sentence(it, 1, "я".repeat(998) + ".") }
|
||||
val blocks = readAloudBlocks(queue, 0, 4_000).toList()
|
||||
assertEquals(listOf(3_999, 3_999, 1_999), blocks.map { it.text.length })
|
||||
assertEquals((0..9).toList(), blocks.flatMap { it.ranges }.map { it.position })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun oversizedSentenceSplitsAtWordsAndNeverCutsSurrogatePairs() {
|
||||
val words = List(20) { "Привет😀" }
|
||||
val blocks = readAloudBlocks(listOf(sentence(0, 0, words.joinToString(" "))), 0, 30).toList()
|
||||
assertEquals(words.joinToString(" "), blocks.joinToString(" ") { it.text })
|
||||
assertTrue(blocks.all { it.text.length <= 30 })
|
||||
val cyrillic = "я".repeat(9_001) + "."
|
||||
val cyrillicBlocks = readAloudBlocks(listOf(sentence(0, 0, cyrillic)), 0, 4_000).toList()
|
||||
assertEquals(cyrillic, cyrillicBlocks.joinToString("") { it.text })
|
||||
assertEquals(listOf(4_000, 4_000, 1_002), cyrillicBlocks.map { it.text.length })
|
||||
val giantWord = "😀".repeat(4_001)
|
||||
val split = readAloudBlocks(listOf(sentence(0, 0, giantWord)), 0, 3_999).toList()
|
||||
assertEquals(giantWord, split.joinToString("") { it.text })
|
||||
assertTrue(split.all { it.text.length <= 3_999 && !it.text.last().isHighSurrogate() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pausesAndStartingPositionArePreserved() {
|
||||
val queue = listOf(sentence(0, 0, "Skip."), sentence(1, 0, "Heading.", 1000, 600),
|
||||
sentence(2, 1, "Wait.", after = 350), sentence(3, 1, "Go."))
|
||||
val blocks = readAloudBlocks(queue, 1, 4_000).toList()
|
||||
assertEquals(listOf("Heading.", "Wait.", "Go."), blocks.map { it.text })
|
||||
assertEquals(listOf(1000L, 0L, 0L), blocks.map { it.pauseBeforeMillis })
|
||||
assertEquals(listOf(600L, 350L, 0L), blocks.map { it.pauseAfterMillis })
|
||||
assertEquals(listOf(1, 2, 3), blocks.map { it.ranges.first().position })
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,13 @@ import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import net.sergeych.toread.fb2.Fb2Block
|
||||
import net.sergeych.toread.fb2.Fb2Book
|
||||
import net.sergeych.toread.fb2.Fb2Epigraph
|
||||
import net.sergeych.toread.fb2.Fb2EpigraphBlock
|
||||
import net.sergeych.toread.fb2.Fb2ImageRef
|
||||
import net.sergeych.toread.fb2.Fb2Poem
|
||||
import net.sergeych.toread.fb2.Fb2PoemBlock
|
||||
import net.sergeych.toread.fb2.Fb2Section
|
||||
import net.sergeych.toread.fb2.Fb2Stanza
|
||||
import net.sergeych.toread.fb2.Fb2Text
|
||||
import net.sergeych.toread.fb2.Fb2TextSpan
|
||||
|
||||
@@ -146,6 +152,227 @@ class ReadAloudContentPlanTest {
|
||||
assertEquals("/б 'z /1.", plan.sentences.single().spokenText)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun poemVersesAreIncludedInReadAloudPlan() {
|
||||
val plan = buildReaderContentPlan(
|
||||
Fb2Book(
|
||||
title = "Book",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
blocks = listOf(
|
||||
Fb2Block.Poem(
|
||||
Fb2Poem(
|
||||
blocks = listOf(
|
||||
Fb2PoemBlock.Stanza(
|
||||
Fb2Stanza(
|
||||
verses = listOf(
|
||||
text("Line one."),
|
||||
text("Line two."),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(listOf("Line one.", "Line two."), plan.sentences.map { it.text })
|
||||
assertEquals(1, plan.elements.filterIsInstance<ReaderElement.Poem>().size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun epigraphsAreIncludedInReaderPlan() {
|
||||
val plan = buildReaderContentPlan(
|
||||
Fb2Book(
|
||||
title = "Book",
|
||||
bodyEpigraphs = listOf(
|
||||
Fb2Epigraph(
|
||||
blocks = listOf(Fb2EpigraphBlock.Paragraph(text("Body quote."))),
|
||||
textAuthors = listOf(text("Body author.")),
|
||||
),
|
||||
),
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
blocks = listOf(
|
||||
Fb2Block.Epigraph(
|
||||
Fb2Epigraph(
|
||||
blocks = listOf(Fb2EpigraphBlock.Paragraph(text("Section quote."))),
|
||||
textAuthors = listOf(text("Section author.")),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(2, plan.elements.filterIsInstance<ReaderElement.Epigraph>().size)
|
||||
assertEquals(
|
||||
listOf("Body quote.", "Body author.", "Section quote.", "Section author."),
|
||||
plan.sentences.map { it.text },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wrapperOnlyTopSectionDoesNotIndentChildParagraphs() {
|
||||
val plan = buildReaderContentPlan(
|
||||
Fb2Book(
|
||||
title = "Book",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Wrapper title",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Chapter",
|
||||
blocks = listOf(paragraph("Chapter text.")),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(0, plan.elements.filterIsInstance<ReaderElement.Paragraph>().single().depth)
|
||||
assertEquals(
|
||||
listOf(0, 0),
|
||||
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().map { it.depth },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun titleOnlyPartSectionDoesNotIndentChaptersAfterFrontMatter() {
|
||||
val plan = buildReaderContentPlan(
|
||||
Fb2Book(
|
||||
title = "Book",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Copyright",
|
||||
blocks = listOf(paragraph("Copyright text.")),
|
||||
),
|
||||
Fb2Section(
|
||||
title = "Part one",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Chapter one",
|
||||
blocks = listOf(paragraph("Chapter text.")),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
listOf(0, 0),
|
||||
plan.elements.filterIsInstance<ReaderElement.Paragraph>().map { it.depth },
|
||||
)
|
||||
assertEquals(
|
||||
listOf(0, 0, 0),
|
||||
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().map { it.depth },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun imageOnlyPartStructureDoesNotIndentWholeBook() {
|
||||
val plan = buildReaderContentPlan(
|
||||
Fb2Book(
|
||||
title = "Book",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Wrapper title",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
images = listOf(Fb2ImageRef("#front.jpg")),
|
||||
),
|
||||
Fb2Section(
|
||||
title = "Part one",
|
||||
blocks = listOf(Fb2Block.Image(Fb2ImageRef("#part.png"))),
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Chapter one",
|
||||
blocks = listOf(paragraph("Chapter text.")),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(0, plan.elements.filterIsInstance<ReaderElement.Paragraph>().single().depth)
|
||||
assertEquals(
|
||||
listOf(0, 0, 0),
|
||||
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().map { it.depth },
|
||||
)
|
||||
assertEquals(listOf(0, 0, 0, 0), plan.tocEntries.map { it.depth })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun realNestedSectionsKeepRelativeIndent() {
|
||||
val plan = buildReaderContentPlan(
|
||||
Fb2Book(
|
||||
title = "Book",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Part",
|
||||
blocks = listOf(paragraph("Part text.")),
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Nested chapter",
|
||||
blocks = listOf(paragraph("Nested text.")),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
listOf(0, 1),
|
||||
plan.elements.filterIsInstance<ReaderElement.Paragraph>().map { it.depth },
|
||||
)
|
||||
assertEquals(
|
||||
listOf(0, 1),
|
||||
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().map { it.depth },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun contentPlanIncludesClickableTableOfContentsEntries() {
|
||||
val plan = buildReaderContentPlan(
|
||||
Fb2Book(
|
||||
title = "Book",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Part",
|
||||
sections = listOf(
|
||||
Fb2Section(
|
||||
title = "Chapter",
|
||||
blocks = listOf(paragraph("Chapter text.")),
|
||||
),
|
||||
),
|
||||
),
|
||||
Fb2Section(blocks = listOf(paragraph("Untitled text."))),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(listOf("Part", "Chapter", "Section 2"), plan.tocEntries.map { it.title })
|
||||
assertEquals(listOf(0, 0, 0), plan.tocEntries.map { it.depth })
|
||||
assertEquals(
|
||||
plan.elements.filterIsInstance<ReaderElement.SectionTitle>().first(),
|
||||
plan.elements[plan.tocEntries.first().itemIndex],
|
||||
)
|
||||
}
|
||||
|
||||
private fun paragraph(text: String): Fb2Block.Paragraph =
|
||||
Fb2Block.Paragraph(Fb2Text(listOf(Fb2TextSpan(text))))
|
||||
Fb2Block.Paragraph(text(text))
|
||||
|
||||
private fun text(text: String): Fb2Text =
|
||||
Fb2Text(listOf(Fb2TextSpan(text)))
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package net.sergeych.toread
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ReaderLinkDetectionTest {
|
||||
@Test
|
||||
fun detectsWebUrlsAndTrimsSentencePunctuation() {
|
||||
val text = "See http://example.org/path?q=1, then https://site.test/end."
|
||||
|
||||
val urls = text.webUrlRanges().map { text.substring(it) }
|
||||
|
||||
assertEquals(listOf("http://example.org/path?q=1", "https://site.test/end"), urls)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ignoresNonWebUrls() {
|
||||
val text = "ftp://files.test https://secure.test"
|
||||
|
||||
val urls = text.webUrlRanges().map { text.substring(it) }
|
||||
|
||||
assertEquals(listOf("https://secure.test"), urls)
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import java.awt.datatransfer.Transferable
|
||||
import java.awt.datatransfer.UnsupportedFlavorException
|
||||
import java.io.File
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.net.URI
|
||||
import javax.imageio.ImageIO
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
@@ -36,6 +37,14 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
private var pendingOpenBookFile: File? = null
|
||||
|
||||
fun rememberPlatformOpenBookArguments(args: Array<String>) {
|
||||
pendingOpenBookFile = args.asSequence()
|
||||
.map(::File)
|
||||
.firstOrNull { it.isFile && it.name.isSupportedBookFile() }
|
||||
}
|
||||
|
||||
actual fun loadDefaultBookBytes(): ByteArray? {
|
||||
var current = File(System.getProperty("user.dir")).absoluteFile
|
||||
while (true) {
|
||||
@@ -70,7 +79,26 @@ actual suspend fun copyImageToClipboard(bytes: ByteArray, mimeType: String, labe
|
||||
|
||||
actual fun defaultLibraryScanPath(): String? = findProjectRoot()?.let { File(it, "test_books").absolutePath }
|
||||
|
||||
actual suspend fun loadPlatformOpenBookRequest(): PlatformOpenBookRequest? = null
|
||||
actual suspend fun loadPlatformOpenBookRequest(): PlatformOpenBookRequest? = withContext(Dispatchers.IO) {
|
||||
val file = pendingOpenBookFile ?: return@withContext null
|
||||
pendingOpenBookFile = null
|
||||
val bytes = file.readBytes()
|
||||
val importResult = openLibraryDatabase().useLibrary { db ->
|
||||
LibraryScanner(db, ::appendLibraryLog).importExternalFileAndGetResult(
|
||||
bytes = bytes,
|
||||
displayName = file.name,
|
||||
storageUri = file.absolutePath,
|
||||
sizeBytes = file.length(),
|
||||
lastModifiedMillis = file.lastModified(),
|
||||
)
|
||||
}
|
||||
appendLibraryLog("platform open imported=${importResult.imported} fileId=${importResult.fileId} path=${file.absolutePath}")
|
||||
PlatformOpenBookRequest(
|
||||
id = importResult.fileId,
|
||||
displayName = file.name,
|
||||
bytes = bytes,
|
||||
)
|
||||
}
|
||||
|
||||
actual suspend fun chooseLibraryScanDirectory(): String? = withContext(Dispatchers.IO) {
|
||||
val chooser = JFileChooser(defaultLibraryScanPath())
|
||||
@@ -293,28 +321,76 @@ actual suspend fun markLibraryFavorite(fileId: String, favorite: Boolean): Boole
|
||||
|
||||
actual suspend fun shareLibraryBookFile(fileId: String): Boolean = false
|
||||
|
||||
actual suspend fun viewLibraryBookFile(fileId: String): Boolean = withContext(Dispatchers.IO) {
|
||||
actual suspend fun viewLibraryBookFile(folder: String, fileName: String): Boolean = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
if (!Desktop.isDesktopSupported()) return@withContext false
|
||||
val file = openLibraryDatabase().useLibrary { db ->
|
||||
db.files.get(fileId)?.storageUri?.let(::File)?.takeIf { it.isFile }
|
||||
} ?: return@withContext false
|
||||
val desktop = Desktop.getDesktop()
|
||||
if (desktop.isSupported(Desktop.Action.BROWSE_FILE_DIR)) {
|
||||
desktop.browseFileDirectory(file)
|
||||
} else {
|
||||
desktop.open(file.parentFile ?: file)
|
||||
}
|
||||
true
|
||||
val directory = File(folder).takeIf { it.isDirectory } ?: return@withContext false
|
||||
val file = File(directory, fileName).takeIf { it.isFile } ?: return@withContext false
|
||||
selectOrOpenBookFile(directory, file)
|
||||
}.getOrDefault(false)
|
||||
}
|
||||
|
||||
actual fun openPlatformUrl(url: String): Boolean =
|
||||
runCatching {
|
||||
if (!Desktop.isDesktopSupported() || !Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
|
||||
return@runCatching false
|
||||
}
|
||||
Desktop.getDesktop().browse(URI(url))
|
||||
true
|
||||
}.getOrDefault(false)
|
||||
|
||||
private fun selectOrOpenBookFile(directory: File, file: File): Boolean {
|
||||
if (isLinuxDesktop() && trySelectLinuxFile(file)) return true
|
||||
if (!Desktop.isDesktopSupported()) return false
|
||||
|
||||
val desktop = Desktop.getDesktop()
|
||||
return when {
|
||||
desktop.isSupported(Desktop.Action.BROWSE_FILE_DIR) -> {
|
||||
desktop.browseFileDirectory(file)
|
||||
true
|
||||
}
|
||||
desktop.isSupported(Desktop.Action.OPEN) -> {
|
||||
desktop.open(directory)
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
private fun isLinuxDesktop(): Boolean =
|
||||
System.getProperty("os.name").orEmpty().lowercase(Locale.ROOT).contains("linux")
|
||||
|
||||
private fun trySelectLinuxFile(file: File): Boolean =
|
||||
runCatching {
|
||||
val process = ProcessBuilder(
|
||||
"dbus-send",
|
||||
"--session",
|
||||
"--dest=org.freedesktop.FileManager1",
|
||||
"--type=method_call",
|
||||
"/org/freedesktop/FileManager1",
|
||||
"org.freedesktop.FileManager1.ShowItems",
|
||||
"array:string:${file.toURI()}",
|
||||
"string:",
|
||||
).start()
|
||||
if (process.waitFor(2, TimeUnit.SECONDS)) {
|
||||
process.exitValue() == 0
|
||||
} else {
|
||||
process.destroyForcibly()
|
||||
false
|
||||
}
|
||||
}.getOrDefault(false)
|
||||
|
||||
actual suspend fun loadBookInfoExtras(fileId: String): BookInfoExtras = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
val file = db.files.get(fileId) ?: return@useLibrary BookInfoExtras()
|
||||
val clusterId = file.bodyClusterId ?: return@useLibrary BookInfoExtras()
|
||||
val sourceFileName = file.originalFilename ?: file.storageUri?.substringAfterLast(File.separatorChar)
|
||||
val clusterId = file.bodyClusterId ?: return@useLibrary BookInfoExtras(
|
||||
sourceFileName = sourceFileName,
|
||||
sourceFilePath = file.storageUri,
|
||||
)
|
||||
val readingPosition = db.readingStates.getForBodyCluster(clusterId)?.anchor?.formatHintsJson?.toReadingPosition()
|
||||
BookInfoExtras(
|
||||
sourceFileName = sourceFileName,
|
||||
sourceFilePath = file.storageUri,
|
||||
bookmarks = db.bookmarks.listForBodyCluster(clusterId).map {
|
||||
BookmarkInfo(
|
||||
title = it.title,
|
||||
@@ -361,6 +437,42 @@ actual suspend fun saveThemeMode(mode: ThemeMode) = withContext(Dispatchers.IO)
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadReaderFontSettings(): ReaderFontSettings = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
readerFontSettingsFromStorageValue(db.getAppFlag(ReaderFontSettingsFlag))
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun saveReaderFontSettings(settings: ReaderFontSettings) = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.setAppFlag(ReaderFontSettingsFlag, settings.toStorageValue())
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadReaderFullScreen(): Boolean = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(ReaderFullScreenFlag)?.toBooleanStrictOrNull() ?: false
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun saveReaderFullScreen(fullScreen: Boolean) = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.setAppFlag(ReaderFullScreenFlag, fullScreen.toString())
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadReaderPagingOnly(): Boolean = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(ReaderPagingOnlyFlag)?.toBooleanStrictOrNull() ?: false
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun saveReaderPagingOnly(enabled: Boolean) = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.setAppFlag(ReaderPagingOnlyFlag, enabled.toString())
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadScanDownloadsAutomatically(): Boolean = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
|
||||
@@ -373,6 +485,29 @@ actual suspend fun saveScanDownloadsAutomatically(enabled: Boolean) = withContex
|
||||
}
|
||||
}
|
||||
|
||||
internal actual suspend fun loadLibraryFilter(): LibraryFilter = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
val saved = db.getAppFlag(LibraryFilterFlag)
|
||||
when {
|
||||
saved == null -> {
|
||||
appendLibraryLog("warning library filter was not saved; using ${LibraryFilter.MyLibrary.name}")
|
||||
LibraryFilter.MyLibrary
|
||||
}
|
||||
else -> runCatching { LibraryFilter.valueOf(saved) }
|
||||
.getOrElse {
|
||||
appendLibraryLog("warning invalid saved library filter '$saved'; using ${LibraryFilter.MyLibrary.name}")
|
||||
LibraryFilter.MyLibrary
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal actual suspend fun saveLibraryFilter(filter: LibraryFilter) = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.setAppFlag(LibraryFilterFlag, filter.name)
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadAppLocaleTag(): String? = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(AppLocaleTagFlag)?.takeIf { it.isNotBlank() }
|
||||
@@ -577,16 +712,34 @@ private fun ReadingPosition.toFormatHintsJson(): String =
|
||||
buildString {
|
||||
append("""{"firstVisibleItemIndex":$itemIndex,"firstVisibleItemScrollOffset":$scrollOffset""")
|
||||
readAloudSentenceIndex?.let { append(""","readAloudSentenceIndex":$it""") }
|
||||
if (backStack.isNotEmpty()) {
|
||||
append(""","backStack":[""")
|
||||
append(backStack.take(MaxStoredReadingBackStack).joinToString(",") { it.toFormatHintsJson() })
|
||||
append("]")
|
||||
}
|
||||
append("}")
|
||||
}
|
||||
|
||||
private fun String.toReadingPosition(): ReadingPosition? {
|
||||
val position = toReadingPositionObject() ?: return null
|
||||
val backStackJson = Regex(""""backStack"\s*:\s*\[(.*)]""").find(this)?.groupValues?.getOrNull(1)
|
||||
val backStack = backStackJson
|
||||
?.let { ReadingPositionObjectRegex.findAll(it).mapNotNull { match -> match.value.toReadingPositionObject() }.toList() }
|
||||
.orEmpty()
|
||||
.take(MaxStoredReadingBackStack)
|
||||
return position.copy(backStack = backStack)
|
||||
}
|
||||
|
||||
private fun String.toReadingPositionObject(): ReadingPosition? {
|
||||
val index = Regex(""""firstVisibleItemIndex"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
|
||||
val offset = Regex(""""firstVisibleItemScrollOffset"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
|
||||
val sentenceIndex = Regex(""""readAloudSentenceIndex"\s*:\s*(\d+)""").find(this)?.groupValues?.getOrNull(1)?.toIntOrNull()
|
||||
return if (index != null && offset != null) ReadingPosition(index, offset, sentenceIndex) else null
|
||||
}
|
||||
|
||||
private val ReadingPositionObjectRegex = Regex("""[{][^{}]*[}]""")
|
||||
private const val MaxStoredReadingBackStack = 10
|
||||
|
||||
private fun String.toSearchPrefixes(): List<String> =
|
||||
SearchPrefixRegex.findAll(lowercase()).map { it.value }.distinct().toList()
|
||||
|
||||
@@ -603,8 +756,15 @@ private fun runCommand(vararg command: String): String? =
|
||||
}
|
||||
}.getOrNull()
|
||||
|
||||
private fun String.isSupportedBookFile(): Boolean =
|
||||
endsWith(".fb2", ignoreCase = true) || endsWith(".fb2.zip", ignoreCase = true)
|
||||
|
||||
private const val ActiveReadingFileIdFlag = "active_reading_file_id"
|
||||
private const val ThemeModeFlag = "theme_mode"
|
||||
private const val ReaderFontSettingsFlag = "reader_font_settings"
|
||||
private const val ReaderFullScreenFlag = "reader_full_screen"
|
||||
private const val ReaderPagingOnlyFlag = "reader_paging_only"
|
||||
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
|
||||
private const val LibraryFilterFlag = "library_filter"
|
||||
private const val AppLocaleTagFlag = "app_locale_tag"
|
||||
private const val DownloadsWasScannedFlag = "downloads_was_scanned"
|
||||
@@ -1,20 +1,54 @@
|
||||
package net.sergeych.toread
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.sergeych.toread.tts.DesktopReadAloudPlayer
|
||||
import net.sergeych.toread.tts.SpeechAudio
|
||||
import net.sergeych.toread.tts.RuAccentProcess
|
||||
import net.sergeych.toread.tts.SileroProcess
|
||||
import net.sergeych.toread.tts.SupertonicTts
|
||||
import net.sergeych.toread.tts.playDesktopAudio
|
||||
|
||||
actual object ReadAloudPlatform {
|
||||
actual val isSupported: Boolean = false
|
||||
private val mutableState = MutableStateFlow(ReadAloudState())
|
||||
actual val state: StateFlow<ReadAloudState> = mutableState
|
||||
private val mutableSettingsState = MutableStateFlow(ReadAloudSettingsState())
|
||||
actual val settingsState: StateFlow<ReadAloudSettingsState> = mutableSettingsState
|
||||
actual val isSupported: Boolean = true
|
||||
private val inferenceLock = Mutex()
|
||||
private var synthesizer: SupertonicTts? = null
|
||||
private val accentor = if (RuAccentProcess.isInstalled()) RuAccentProcess() else null
|
||||
private val silero = if (SileroProcess.isInstalled()) SileroProcess() else null
|
||||
private val player = DesktopReadAloudPlayer(
|
||||
scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate),
|
||||
synthesize = { text, voice ->
|
||||
withContext(Dispatchers.IO) {
|
||||
inferenceLock.withLock {
|
||||
val context = currentCoroutineContext()
|
||||
context.ensureActive()
|
||||
val engine = synthesizer ?: SupertonicTts().also { synthesizer = it }
|
||||
context.ensureActive()
|
||||
SpeechAudio(engine.synthesize(text, voice, checkCancelled = { context.ensureActive() }), engine.sampleRate)
|
||||
}
|
||||
}
|
||||
},
|
||||
playAudio = ::playDesktopAudio,
|
||||
installed = { SupertonicTts.isInstalled() },
|
||||
accentuate = accentor?.let { processor -> { text -> processor.accentuate(text) } },
|
||||
silero = silero?.let { processor -> { text, voice -> processor.synthesize(text, voice) } },
|
||||
)
|
||||
actual val state: StateFlow<ReadAloudState> = player.state
|
||||
actual val settingsState: StateFlow<ReadAloudSettingsState> = player.settingsState
|
||||
|
||||
actual fun prepare(fileId: String, bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int) = Unit
|
||||
actual fun play() = Unit
|
||||
actual fun stop() = Unit
|
||||
actual fun skip(delta: Int) = Unit
|
||||
actual fun refreshSettings() = Unit
|
||||
actual fun selectEngine(engineId: String?) = Unit
|
||||
actual fun selectVoice(voiceId: String?) = Unit
|
||||
actual fun prepare(fileId: String, bookTitle: String, sentences: List<ReadAloudSentence>, startIndex: Int) =
|
||||
player.prepare(sentences, startIndex)
|
||||
actual fun play() = player.play()
|
||||
actual fun stop() = player.stop()
|
||||
actual fun skip(delta: Int) = player.skip(delta)
|
||||
actual fun refreshSettings() = player.refreshSettings()
|
||||
actual fun selectEngine(engineId: String?) = player.selectEngine(engineId)
|
||||
actual fun selectVoice(voiceId: String?) = player.selectVoice(voiceId)
|
||||
}
|
||||
@@ -3,11 +3,14 @@ package net.sergeych.toread
|
||||
import androidx.compose.ui.window.Window
|
||||
import androidx.compose.ui.window.application
|
||||
|
||||
fun main() = application {
|
||||
Window(
|
||||
onCloseRequest = ::exitApplication,
|
||||
title = strings.appName,
|
||||
) {
|
||||
App()
|
||||
fun main(args: Array<String>) {
|
||||
rememberPlatformOpenBookArguments(args)
|
||||
application {
|
||||
Window(
|
||||
onCloseRequest = ::exitApplication,
|
||||
title = strings.appName,
|
||||
) {
|
||||
App()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package net.sergeych.toread.tts
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.File
|
||||
import javax.sound.sampled.AudioFileFormat
|
||||
import javax.sound.sampled.AudioFormat
|
||||
import javax.sound.sampled.AudioInputStream
|
||||
import javax.sound.sampled.AudioSystem
|
||||
import javax.sound.sampled.LineEvent
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import kotlin.coroutines.resume
|
||||
|
||||
internal fun SpeechAudio.pcm16(): ByteArray = ByteArray(samples.size * 2).also { bytes ->
|
||||
samples.forEachIndexed { index, sample ->
|
||||
val pcm = (sample.takeIf { it.isFinite() }?.coerceIn(-1f, 1f)?.times(32767) ?: 0f).toInt()
|
||||
bytes[index * 2] = pcm.toByte()
|
||||
bytes[index * 2 + 1] = (pcm shr 8).toByte()
|
||||
}
|
||||
}
|
||||
|
||||
internal suspend fun playDesktopAudio(audio: SpeechAudio) = withContext(Dispatchers.IO) {
|
||||
val format = AudioFormat(audio.sampleRate.toFloat(), 16, 1, true, false)
|
||||
val pcm = audio.pcm16()
|
||||
val clip = AudioSystem.getClip()
|
||||
try {
|
||||
currentCoroutineContext().ensureActive()
|
||||
clip.open(format, pcm, 0, pcm.size)
|
||||
suspendCancellableCoroutine<Unit> { continuation ->
|
||||
val completed = AtomicBoolean(false)
|
||||
clip.addLineListener { event ->
|
||||
if (event.type == LineEvent.Type.STOP && completed.compareAndSet(false, true)) {
|
||||
continuation.resume(Unit)
|
||||
}
|
||||
}
|
||||
continuation.invokeOnCancellation {
|
||||
clip.stop()
|
||||
clip.close()
|
||||
}
|
||||
if (continuation.isActive) clip.start()
|
||||
}
|
||||
} finally {
|
||||
clip.stop()
|
||||
clip.close()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun SpeechAudio.writeWav(file: File) {
|
||||
file.parentFile?.mkdirs()
|
||||
val format = AudioFormat(sampleRate.toFloat(), 16, 1, true, false)
|
||||
AudioInputStream(ByteArrayInputStream(pcm16()), format, samples.size.toLong()).use {
|
||||
AudioSystem.write(it, AudioFileFormat.Type.WAVE, file)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun joinSpeechAudio(parts: List<SpeechAudio>): SpeechAudio {
|
||||
require(parts.isNotEmpty()) { "No speech to play" }
|
||||
if (parts.size == 1) return parts.single()
|
||||
val rate = parts.first().sampleRate
|
||||
require(parts.all { it.sampleRate == rate }) { "Mismatched speech sample rates" }
|
||||
val pause = rate / 10
|
||||
val samples = FloatArray(parts.sumOf { it.samples.size } + pause * (parts.size - 1))
|
||||
var offset = 0
|
||||
parts.forEach { part -> part.samples.copyInto(samples, offset); offset += part.samples.size + pause }
|
||||
return SpeechAudio(samples, rate)
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package net.sergeych.toread.tts
|
||||
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import net.sergeych.toread.ReadAloudEngineOption
|
||||
import net.sergeych.toread.ReadAloudSentence
|
||||
import net.sergeych.toread.ReadAloudSettingsState
|
||||
import net.sergeych.toread.ReadAloudState
|
||||
import net.sergeych.toread.ReadAloudVoiceOption
|
||||
import net.sergeych.toread.strings
|
||||
|
||||
internal data class SpeechAudio(val samples: FloatArray, val sampleRate: Int)
|
||||
|
||||
/** Commands and state changes run on the UI dispatcher; inference/audio run off-thread.
|
||||
* A playback job owns its lookahead, so stop/skip/voice changes cannot publish stale audio.
|
||||
*/
|
||||
internal class DesktopReadAloudPlayer(
|
||||
private val scope: CoroutineScope,
|
||||
private val synthesize: suspend (String, String) -> SpeechAudio,
|
||||
private val playAudio: suspend (SpeechAudio) -> Unit,
|
||||
private val installed: () -> Boolean,
|
||||
private val accentuate: (suspend (String) -> String)? = null,
|
||||
private val silero: (suspend (String, String) -> SpeechAudio)? = null,
|
||||
) {
|
||||
private fun voices(engine: String) = if (engine == "silero")
|
||||
SileroProcess.voices.map { ReadAloudVoiceOption(it, it.replaceFirstChar(Char::uppercase), "ru (English: F1)", false) }
|
||||
else SupertonicTts.voices.map { ReadAloudVoiceOption(it, it, "en / ru", false) }
|
||||
private val defaultEngine = if (silero != null) "silero" else if (accentuate != null) "supertonic-3-ruaccent" else "supertonic-3"
|
||||
private val mutableState = MutableStateFlow(ReadAloudState())
|
||||
val state = mutableState.asStateFlow()
|
||||
private val mutableSettings = MutableStateFlow(ReadAloudSettingsState(
|
||||
engines = (if (silero != null) listOf(ReadAloudEngineOption("silero", "Silero (RU) / Supertonic (EN)")) else emptyList()) +
|
||||
listOf(ReadAloudEngineOption("supertonic-3", "Supertonic 3")) +
|
||||
if (accentuate != null) listOf(ReadAloudEngineOption("supertonic-3-ruaccent", "Supertonic 3 + RUAccent")) else emptyList(),
|
||||
selectedEngineId = defaultEngine,
|
||||
voices = voices(defaultEngine),
|
||||
selectedVoiceId = if (silero != null) "xenia" else "F1",
|
||||
usesSystemDefaults = false,
|
||||
))
|
||||
val settingsState = mutableSettings.asStateFlow()
|
||||
private var queue = emptyList<ReadAloudSentence>()
|
||||
private var index = 0
|
||||
private var playback: Job? = null
|
||||
|
||||
fun prepare(sentences: List<ReadAloudSentence>, startIndex: Int) {
|
||||
stop()
|
||||
queue = sentences.toList()
|
||||
index = if (queue.isEmpty()) 0 else startIndex.coerceIn(queue.indices)
|
||||
mutableState.value = ReadAloudState(active = queue.isNotEmpty(), sentenceIndex = queue.getOrNull(index)?.index)
|
||||
}
|
||||
|
||||
fun play() {
|
||||
if (queue.isEmpty()) return
|
||||
playback?.cancel()
|
||||
mutableSettings.value = mutableSettings.value.copy(message = null)
|
||||
if (!installed()) {
|
||||
mutableState.value = mutableState.value.copy(active = true, playing = false, buffering = false)
|
||||
mutableSettings.value = mutableSettings.value.copy(message = strings.offlineSpeechModelMissing)
|
||||
return
|
||||
}
|
||||
val voice = mutableSettings.value.selectedVoiceId ?: "F1"
|
||||
val useSilero = mutableSettings.value.selectedEngineId == "silero"
|
||||
val useAccents = mutableSettings.value.selectedEngineId == "supertonic-3-ruaccent"
|
||||
suspend fun render(text: String): SpeechAudio {
|
||||
if (useSilero) return if (SupertonicTts.languageOf(text) == "ru") silero!!.invoke(text, voice)
|
||||
else synthesize(text, "F1")
|
||||
val prepared = if (useAccents) accentuate!!.invoke(text) else text
|
||||
// Accent marks increase character count; re-chunk before entering the TTS model.
|
||||
return joinSpeechAudio(SupertonicTts.chunks(SupertonicTts.russianStressToUnicode(prepared)).map { synthesize(it, voice) })
|
||||
}
|
||||
val chunks = queue.asSequence().withIndex().drop(index).flatMap { (position, sentence) ->
|
||||
val parts = SupertonicTts.chunks(sentence.spokenText)
|
||||
parts.asSequence().mapIndexed { chunkIndex, text ->
|
||||
Chunk(position, sentence, text, chunkIndex == 0, chunkIndex == parts.lastIndex)
|
||||
}
|
||||
}.iterator()
|
||||
mutableState.value = mutableState.value.copy(active = true, playing = true, buffering = true)
|
||||
playback = scope.launch {
|
||||
try {
|
||||
coroutineScope {
|
||||
// Keep at most one audio chunk ahead of the currently playing chunk.
|
||||
var next = if (chunks.hasNext()) chunks.next() else null
|
||||
var pending = next?.let { chunk -> async { render(chunk.text) } }
|
||||
while (next != null) {
|
||||
val current = next
|
||||
mutableState.value = mutableState.value.copy(buffering = pending?.isCompleted != true)
|
||||
val audio = pending!!.await()
|
||||
next = if (chunks.hasNext()) chunks.next() else null
|
||||
pending = next?.let { chunk -> async { render(chunk.text) } }
|
||||
index = current.position
|
||||
mutableState.value = ReadAloudState(active = true, playing = true, sentenceIndex = current.sentence.index)
|
||||
if (current.first) delay(current.sentence.pauseBeforeMillis)
|
||||
playAudio(audio)
|
||||
if (current.last) delay(current.sentence.pauseAfterMillis)
|
||||
}
|
||||
}
|
||||
mutableState.value = ReadAloudState()
|
||||
} catch (cancelled: CancellationException) {
|
||||
throw cancelled
|
||||
} catch (error: Exception) {
|
||||
System.err.println("Desktop TTS failed: ${error.javaClass.simpleName}: ${error.message}")
|
||||
mutableState.value = mutableState.value.copy(playing = false, buffering = false)
|
||||
mutableSettings.value = mutableSettings.value.copy(message = strings.speechPlaybackFailed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
playback?.cancel()
|
||||
playback = null
|
||||
mutableState.value = ReadAloudState()
|
||||
}
|
||||
|
||||
fun skip(delta: Int) {
|
||||
if (queue.isEmpty()) return
|
||||
val wasPlaying = mutableState.value.playing
|
||||
playback?.cancel()
|
||||
index = (index.toLong() + delta).coerceIn(0, queue.lastIndex.toLong()).toInt()
|
||||
mutableState.value = ReadAloudState(active = true, sentenceIndex = queue[index].index)
|
||||
if (wasPlaying) play()
|
||||
}
|
||||
|
||||
fun selectVoice(id: String?) {
|
||||
val voice = id ?: if (mutableSettings.value.selectedEngineId == "silero") "xenia" else "F1"
|
||||
if (mutableSettings.value.voices.none { it.id == voice } || voice == mutableSettings.value.selectedVoiceId) return
|
||||
mutableSettings.value = mutableSettings.value.copy(selectedVoiceId = voice)
|
||||
if (mutableState.value.playing) play()
|
||||
}
|
||||
|
||||
fun selectEngine(id: String?) {
|
||||
val engine = id ?: defaultEngine
|
||||
if (mutableSettings.value.engines.none { it.id == engine } || engine == mutableSettings.value.selectedEngineId) return
|
||||
val options = voices(engine)
|
||||
mutableSettings.value = mutableSettings.value.copy(selectedEngineId = engine, voices = options,
|
||||
selectedVoiceId = mutableSettings.value.selectedVoiceId?.takeIf { id -> options.any { it.id == id } }
|
||||
?: if (engine == "silero") "xenia" else "F1")
|
||||
if (mutableState.value.playing) play()
|
||||
}
|
||||
|
||||
fun refreshSettings() {
|
||||
mutableSettings.value = mutableSettings.value.copy(message = if (installed()) null else strings.offlineSpeechModelMissing)
|
||||
}
|
||||
|
||||
private data class Chunk(
|
||||
val position: Int,
|
||||
val sentence: ReadAloudSentence,
|
||||
val text: String,
|
||||
val first: Boolean,
|
||||
val last: Boolean,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package net.sergeych.toread.tts
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import java.io.BufferedReader
|
||||
import java.io.BufferedWriter
|
||||
import java.io.File
|
||||
|
||||
/** Optional desktop-only CPU accentor. Persistent local process, no HTTP or remote text service. */
|
||||
internal class RuAccentProcess(private val directory: File = installationDirectory()) : AutoCloseable {
|
||||
private val mapper = ObjectMapper()
|
||||
private val lock = Mutex()
|
||||
@Volatile private var worker: Worker? = null
|
||||
|
||||
init {
|
||||
Runtime.getRuntime().addShutdownHook(Thread { close() })
|
||||
}
|
||||
|
||||
suspend fun accentuate(text: String): String {
|
||||
// Respect explicitly supplied stress; English needs no Russian processor.
|
||||
if (SupertonicTts.languageOf(text) == "en" || '\u0301' in text || Regex("\\+[аеёиоуыэюяАЕЁИОУЫЭЮЯ]").containsMatchIn(text)) return text
|
||||
return lock.withLock {
|
||||
try {
|
||||
currentCoroutineContext().ensureActive()
|
||||
val current = worker?.takeIf { it.process.isAlive } ?: startWorker().also { worker = it }
|
||||
withTimeout(90_000) {
|
||||
coroutineScope {
|
||||
val response = async(Dispatchers.IO) {
|
||||
if (!current.ready) {
|
||||
val ready = current.output.readLine() ?: error("RuAccent exited while loading")
|
||||
check(mapper.readTree(ready)["ready"]?.asBoolean() == true) { "RuAccent did not initialize" }
|
||||
current.ready = true
|
||||
}
|
||||
current.input.write(mapper.writeValueAsString(mapOf("text" to text)))
|
||||
current.input.newLine()
|
||||
current.input.flush()
|
||||
val line = current.output.readLine() ?: error("RuAccent exited during inference")
|
||||
val result = mapper.readTree(line)
|
||||
check(result["error"] == null) { "RuAccent inference failed" }
|
||||
result["text"]?.asText()?.takeIf { it.isNotBlank() } ?: error("Empty RuAccent response")
|
||||
}
|
||||
try {
|
||||
response.await()
|
||||
} finally {
|
||||
// A blocked pipe read cannot be interrupted reliably; killing the
|
||||
// cancelled worker unblocks it before the next request acquires the lock.
|
||||
if (!isActive) current.process.destroyForcibly()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error: Exception) {
|
||||
close()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startWorker(): Worker {
|
||||
val runtime = mapper.readTree(File(directory, "runtime.json"))
|
||||
val process = ProcessBuilder(runtime["python"].asText(), "-u", File(directory, "worker.py").path, directory.path)
|
||||
.redirectError(ProcessBuilder.Redirect.INHERIT)
|
||||
.apply {
|
||||
environment()["HF_HUB_OFFLINE"] = "1"
|
||||
environment()["TRANSFORMERS_OFFLINE"] = "1"
|
||||
environment()["HF_HUB_DISABLE_TELEMETRY"] = "1"
|
||||
}.start()
|
||||
return Worker(process, process.inputStream.bufferedReader(Charsets.UTF_8), process.outputStream.bufferedWriter(Charsets.UTF_8))
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
worker?.process?.destroyForcibly()
|
||||
worker = null
|
||||
}
|
||||
|
||||
private class Worker(val process: Process, val output: BufferedReader, val input: BufferedWriter, var ready: Boolean = false)
|
||||
|
||||
companion object {
|
||||
fun installationDirectory(): File = System.getenv("YEREED_RUACCENT_HOME")?.let(::File)
|
||||
?: File(SupertonicTts.modelDirectory().parentFile, "ruaccent")
|
||||
fun isInstalled(): Boolean = File(installationDirectory(), "runtime.json").isFile && File(installationDirectory(), "worker.py").isFile
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package net.sergeych.toread.tts
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import java.io.BufferedReader
|
||||
import java.io.BufferedWriter
|
||||
import java.io.File
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.util.Base64
|
||||
|
||||
/** Desktop-only CPU Silero synthesizer. Persistent local process, no HTTP or remote text service. */
|
||||
internal class SileroProcess(private val directory: File = installationDirectory()) : AutoCloseable {
|
||||
private val mapper = ObjectMapper()
|
||||
private val lock = Mutex()
|
||||
@Volatile private var worker: Worker? = null
|
||||
|
||||
init {
|
||||
Runtime.getRuntime().addShutdownHook(Thread { close() })
|
||||
}
|
||||
|
||||
suspend fun synthesize(text: String, voice: String): SpeechAudio {
|
||||
return lock.withLock {
|
||||
try {
|
||||
currentCoroutineContext().ensureActive()
|
||||
val current = worker?.takeIf { it.process.isAlive } ?: startWorker().also { worker = it }
|
||||
withTimeout(90_000) {
|
||||
coroutineScope {
|
||||
val response = async(Dispatchers.IO) {
|
||||
if (!current.ready) {
|
||||
val ready = current.output.readLine() ?: error("Silero exited while loading")
|
||||
check(mapper.readTree(ready)["ready"]?.asBoolean() == true) { "Silero did not initialize" }
|
||||
current.ready = true
|
||||
}
|
||||
current.input.write(mapper.writeValueAsString(mapOf("text" to text, "voice" to voice)))
|
||||
current.input.newLine()
|
||||
current.input.flush()
|
||||
val line = current.output.readLine() ?: error("Silero exited during inference")
|
||||
val result = mapper.readTree(line)
|
||||
check(result["error"] == null) { "Silero inference failed" }
|
||||
val bytes = Base64.getDecoder().decode(result["audio"].asText())
|
||||
check(bytes.isNotEmpty() && bytes.size % 4 == 0) { "Invalid Silero audio" }
|
||||
val buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer()
|
||||
val samples = FloatArray(buffer.remaining()).also { buffer.get(it) }
|
||||
check(samples.all { it.isFinite() }) { "Non-finite Silero audio" }
|
||||
SpeechAudio(samples, result["sampleRate"].asInt().also { check(it == 48000) })
|
||||
}
|
||||
try {
|
||||
response.await()
|
||||
} finally {
|
||||
// A blocked pipe read cannot be interrupted reliably; killing the
|
||||
// cancelled worker unblocks it before the next request acquires the lock.
|
||||
if (!isActive) current.process.destroyForcibly()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error: Exception) {
|
||||
close()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startWorker(): Worker {
|
||||
val runtime = mapper.readTree(File(directory, "runtime.json"))
|
||||
val script = SileroProcess::class.java.getResourceAsStream("/tts/silero-worker.py")!!.use { it.reader().readText() }
|
||||
val process = ProcessBuilder(runtime["python"].asText(), "-u", "-c", script, directory.path)
|
||||
.redirectError(ProcessBuilder.Redirect.INHERIT)
|
||||
.apply {
|
||||
environment()["HF_HUB_OFFLINE"] = "1"
|
||||
environment()["TRANSFORMERS_OFFLINE"] = "1"
|
||||
environment()["HF_HUB_DISABLE_TELEMETRY"] = "1"
|
||||
}.start()
|
||||
return Worker(process, process.inputStream.bufferedReader(Charsets.UTF_8), process.outputStream.bufferedWriter(Charsets.UTF_8))
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
worker?.process?.destroyForcibly()
|
||||
worker = null
|
||||
}
|
||||
|
||||
private class Worker(val process: Process, val output: BufferedReader, val input: BufferedWriter, var ready: Boolean = false)
|
||||
|
||||
companion object {
|
||||
val voices = listOf("xenia", "baya", "aidar", "kseniya", "eugene")
|
||||
fun installationDirectory(): File = System.getenv("YEREED_SILERO_HOME")?.let(::File)
|
||||
?: File(SupertonicTts.modelDirectory().parentFile, "silero-v5_5-ru")
|
||||
fun isInstalled(): Boolean = File(installationDirectory(), "runtime.json").isFile && File(installationDirectory(), "model.pt").length() > 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package net.sergeych.toread.tts
|
||||
|
||||
import ai.onnxruntime.OnnxTensor
|
||||
import ai.onnxruntime.OrtEnvironment
|
||||
import ai.onnxruntime.OrtSession
|
||||
import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import java.io.File
|
||||
import java.nio.FloatBuffer
|
||||
import java.text.Normalizer
|
||||
import java.util.Random
|
||||
|
||||
/** CPU-only, single-utterance adaptation of Supertone's MIT Java example.
|
||||
* See docs/licenses/supertonic-code-MIT.txt. Call from one worker at a time.
|
||||
*/
|
||||
internal class SupertonicTts(val directory: File = modelDirectory()) : AutoCloseable {
|
||||
private val mapper = ObjectMapper()
|
||||
private val environment = OrtEnvironment.getEnvironment()
|
||||
private val config = mapper.readTree(File(directory, "onnx/tts.json"))
|
||||
private val indexer = mapper.readTree(File(directory, "onnx/unicode_indexer.json"))
|
||||
.map { it.asLong() }.toLongArray()
|
||||
val sampleRate: Int = config["ae"]["sample_rate"].asInt()
|
||||
private val compression = config["ttl"]["chunk_compress_factor"].asInt()
|
||||
private val chunkSize = config["ae"]["base_chunk_size"].asInt() * compression
|
||||
private val latentDimension = config["ttl"]["latent_dim"].asInt() * compression
|
||||
private val sessions = mutableListOf<OrtSession>()
|
||||
private val styles = mutableMapOf<String, JsonNode>()
|
||||
|
||||
init {
|
||||
try {
|
||||
OrtSession.SessionOptions().use { options ->
|
||||
// The standard (non-GPU) ONNX artifact uses CPUExecutionProvider only.
|
||||
options.setIntraOpNumThreads(4.coerceAtMost(Runtime.getRuntime().availableProcessors()))
|
||||
options.setInterOpNumThreads(1)
|
||||
for (name in listOf("duration_predictor", "text_encoder", "vector_estimator", "vocoder")) {
|
||||
sessions += environment.createSession(File(directory, "onnx/$name.onnx").path, options)
|
||||
}
|
||||
}
|
||||
} catch (error: Throwable) {
|
||||
close()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
fun synthesize(
|
||||
text: String,
|
||||
voice: String = "F1",
|
||||
language: String = languageOf(text),
|
||||
seed: Long? = null,
|
||||
checkCancelled: () -> Unit = {},
|
||||
): FloatArray {
|
||||
require(voice in voices) { "Unknown voice: $voice" }
|
||||
require(language in listOf("en", "ru", "na")) { "Unsupported language: $language" }
|
||||
require(text.isNotBlank() && text.length <= MAX_CHARS) { "Expected 1–$MAX_CHARS characters" }
|
||||
checkCancelled()
|
||||
val prepared = prepareText(text, language)
|
||||
// Unknown symbols become spaces, never invalid (-1) embedding indices.
|
||||
val ids = prepared.codePoints().toArray().map { point ->
|
||||
indexer.getOrNull(point)?.takeIf { it >= 0 } ?: indexer[' '.code]
|
||||
}.toLongArray()
|
||||
val style = styles.getOrPut(voice) { mapper.readTree(File(directory, "voice_styles/$voice.json")) }
|
||||
NativeResources().use { resources ->
|
||||
fun tensor(value: Any) = resources.keep(OnnxTensor.createTensor(environment, value))
|
||||
fun styleTensor(name: String): OnnxTensor {
|
||||
val node = style[name]
|
||||
val values = node["data"].flatMap { batch -> batch.flatMap { row -> row.map { it.floatValue() } } }
|
||||
return resources.keep(OnnxTensor.createTensor(
|
||||
environment, FloatBuffer.wrap(values.toFloatArray()), node["dims"].map { it.asLong() }.toLongArray(),
|
||||
))
|
||||
}
|
||||
val textIds = tensor(arrayOf(ids))
|
||||
val textMask = tensor(arrayOf(arrayOf(FloatArray(ids.size) { 1f })))
|
||||
val styleDp = styleTensor("style_dp")
|
||||
val styleTtl = styleTensor("style_ttl")
|
||||
val duration = sessions[0].run(mapOf(
|
||||
"text_ids" to textIds, "text_mask" to textMask, "style_dp" to styleDp,
|
||||
)).use { result -> (result[0] as OnnxTensor).floatBuffer.get(0) / 1.05f }
|
||||
check(duration.isFinite() && duration > 0 && duration <= 60) { "Invalid speech duration: $duration" }
|
||||
checkCancelled()
|
||||
val sampleCount = (duration * sampleRate).toInt()
|
||||
val latentLength = (sampleCount + chunkSize - 1) / chunkSize
|
||||
val random = seed?.let(::Random) ?: Random()
|
||||
var latent = arrayOf(Array(latentDimension) { FloatArray(latentLength) { random.nextGaussian().toFloat() } })
|
||||
val latentMask = tensor(arrayOf(arrayOf(FloatArray(latentLength) { 1f })))
|
||||
val totalSteps = tensor(floatArrayOf(8f))
|
||||
val encoded = resources.keep(sessions[1].run(mapOf(
|
||||
"text_ids" to textIds, "text_mask" to textMask, "style_ttl" to styleTtl,
|
||||
)))
|
||||
val embedding = encoded[0] as OnnxTensor
|
||||
repeat(8) { step ->
|
||||
checkCancelled()
|
||||
OnnxTensor.createTensor(environment, latent).use { noisy ->
|
||||
OnnxTensor.createTensor(environment, floatArrayOf(step.toFloat())).use { currentStep ->
|
||||
sessions[2].run(mapOf(
|
||||
"noisy_latent" to noisy, "text_emb" to embedding, "style_ttl" to styleTtl,
|
||||
"latent_mask" to latentMask, "text_mask" to textMask,
|
||||
"current_step" to currentStep, "total_step" to totalSteps,
|
||||
)).use { result ->
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
latent = result[0].value as Array<Array<FloatArray>>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
checkCancelled()
|
||||
OnnxTensor.createTensor(environment, latent).use { finalLatent ->
|
||||
return sessions[3].run(mapOf("latent" to finalLatent)).use { result ->
|
||||
val samples = (result[0] as OnnxTensor).floatBuffer
|
||||
check(samples.remaining() >= sampleCount) { "Truncated speech waveform" }
|
||||
FloatArray(sampleCount).also { samples.get(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
sessions.asReversed().forEach { runCatching { it.close() } }
|
||||
sessions.clear()
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val MAX_CHARS = 300
|
||||
val voices = listOf("F1", "F2", "F3", "F4", "F5", "M1", "M2", "M3", "M4", "M5")
|
||||
|
||||
fun modelDirectory(): File {
|
||||
val override = System.getProperty("yereed.tts.modelDir") ?: System.getenv("YEREED_TTS_MODEL_DIR")
|
||||
if (!override.isNullOrBlank()) return File(override)
|
||||
generateSequence(File(System.getProperty("user.dir")).absoluteFile) { it.parentFile }.forEach { root ->
|
||||
File(root, ".local/tts/supertonic-3").takeIf { it.isDirectory }?.let { return it }
|
||||
}
|
||||
return File(System.getProperty("user.home"), ".toread/tts/supertonic-3")
|
||||
}
|
||||
|
||||
fun isInstalled(directory: File = modelDirectory()): Boolean =
|
||||
(listOf("onnx/tts.json", "onnx/unicode_indexer.json") +
|
||||
listOf("duration_predictor", "text_encoder", "vector_estimator", "vocoder").map { "onnx/$it.onnx" } +
|
||||
voices.map { "voice_styles/$it.json" }).all { File(directory, it).length() > 0 }
|
||||
|
||||
fun languageOf(text: String): String {
|
||||
val russian = text.any { it in 'А'..'я' || it == 'ё' || it == 'Ё' }
|
||||
val english = text.any { it in 'a'..'z' || it in 'A'..'Z' }
|
||||
return when {
|
||||
russian && english -> "na"
|
||||
russian -> "ru"
|
||||
else -> "en"
|
||||
}
|
||||
}
|
||||
|
||||
/** Bounded chunks preserve every non-whitespace character, including long unbroken words. */
|
||||
fun chunks(text: String): List<String> = buildList {
|
||||
var remaining = text.trim()
|
||||
while (remaining.length > MAX_CHARS) {
|
||||
var end = remaining.lastIndexOfAny(charArrayOf(' ', '\n', '\t'), MAX_CHARS)
|
||||
if (end <= 0) end = MAX_CHARS
|
||||
if (Character.isHighSurrogate(remaining[end - 1])) end--
|
||||
while (end > 0 && Character.getType(remaining[end]) == Character.NON_SPACING_MARK.toInt()) end--
|
||||
require(end > 0) { "Oversized combining-character sequence" }
|
||||
add(remaining.substring(0, end).trim())
|
||||
remaining = remaining.substring(end).trimStart()
|
||||
}
|
||||
if (remaining.isNotEmpty()) add(remaining)
|
||||
}
|
||||
|
||||
internal fun prepareText(text: String, language: String): String {
|
||||
var normalized = Normalizer.normalize(russianStressToUnicode(text), Normalizer.Form.NFKD)
|
||||
.replace(Regex("[–‑—]"), "-")
|
||||
.replace(Regex("[“”]"), "\"")
|
||||
.replace(Regex("[‘’´`]"), "'")
|
||||
.replace(Regex("[_\\[\\]|/#→←<>]"), " ")
|
||||
.replace(Regex("[♥☆♡©\\\\]"), "")
|
||||
.replace(Regex("[\\x{1F000}-\\x{1FAFF}\\x{2600}-\\x{27BF}]"), "")
|
||||
if (language == "en") normalized = normalized.replace("@", " at ")
|
||||
.replace("e.g.,", "for example, ").replace("i.e.,", "that is, ")
|
||||
normalized = normalized.replace(Regex("\\s+"), " ").trim()
|
||||
.replace(Regex(" +([,.!?;:'])"), "$1")
|
||||
if (normalized.isEmpty() || normalized.last() !in ".!?;:,'\")]}…»") normalized += "."
|
||||
return "<$language>$normalized</$language>"
|
||||
}
|
||||
|
||||
/** RUAccent/Silero notation -> Cyrillic vowel plus U+0301, never a Latin lookalike. */
|
||||
internal fun russianStressToUnicode(text: String): String =
|
||||
text.replace(Regex("\\+([аеёиоуыэюяАЕЁИОУЫЭЮЯ])")) { it.groupValues[1] + "\u0301" }
|
||||
}
|
||||
}
|
||||
|
||||
private class NativeResources : AutoCloseable {
|
||||
private val values = mutableListOf<AutoCloseable>()
|
||||
fun <T : AutoCloseable> keep(value: T): T = value.also { values += it }
|
||||
override fun close() = values.asReversed().forEach { runCatching { it.close() } }
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package net.sergeych.toread.tts
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
import kotlin.system.measureNanoTime
|
||||
|
||||
/** Reproducible audition without opening the library, altering books, or contacting a service. */
|
||||
fun main(args: Array<String>) = runBlocking {
|
||||
val output = File(args.firstOrNull() ?: "build/tts-samples")
|
||||
if ("--silero" in args) {
|
||||
check(SileroProcess.isInstalled()) { "Install the Silero desktop runtime first." }
|
||||
SileroProcess().use { engine ->
|
||||
for ((label, text) in listOf(
|
||||
"library" to "На двери висел старый замок, а вдали виднелся замок на холме.",
|
||||
"yo" to "Ежик шел по лесу. Ребенок пьет теплое молоко.",
|
||||
)) {
|
||||
val audio = engine.synthesize(text, "xenia")
|
||||
check(audio.samples.any { kotlin.math.abs(it) > 0.001f })
|
||||
val file = File(output, "silero-desktop-$label.wav")
|
||||
audio.writeWav(file)
|
||||
println("Silero desktop worker: ${file.absolutePath}; ${audio.samples.size.toDouble() / audio.sampleRate}s")
|
||||
if ("--play" in args) playDesktopAudio(audio)
|
||||
}
|
||||
}
|
||||
return@runBlocking
|
||||
}
|
||||
check(SupertonicTts.isInstalled()) { "Run python3 scripts/download-tts-model.py first." }
|
||||
lateinit var engine: SupertonicTts
|
||||
val loadTime = measureNanoTime { engine = SupertonicTts() } / 1e9
|
||||
println("Supertonic 3; CPU only; up to 4 threads; 8 steps; model load: %.3f s".format(Locale.ROOT, loadTime))
|
||||
val stressTrial = "--stress" in args
|
||||
val ruaccentTrial = "--ruaccent" in args
|
||||
val russianText = "В библиотеке было тихо. Она открыла книгу и начала читать. За окном шёл осенний дождь. На двери висел старый замок, а вдали виднелся замок на холме. Завтра, подумала она, мы наконец доберёмся до моря."
|
||||
val accentor = if (ruaccentTrial) RuAccentProcess() else null
|
||||
val samples = if (ruaccentTrial) linkedMapOf("ru-plain" to russianText, "ru-ruaccent" to russianText) else if (stressTrial) linkedMapOf(
|
||||
"ru-plain" to "Перед нами старый замок. Они стояли около замка.",
|
||||
"ru-castle" to "П+еред н+ами ст+арый з+амок. Он+и сто+яли +около з+амка.",
|
||||
"ru-lock" to "П+еред н+ами ст+арый зам+ок. Он+и сто+яли +около замк+а.",
|
||||
) else linkedMapOf(
|
||||
"en" to "The library was quiet. She opened the book and began to read. Outside, the autumn rain tapped softly against the window. Tomorrow, she thought, they would finally reach the coast.",
|
||||
"ru" to russianText,
|
||||
)
|
||||
engine.use {
|
||||
try {
|
||||
for (voice in if (stressTrial || ruaccentTrial) listOf("F1") else listOf("F1", "M1")) for ((label, text) in samples) {
|
||||
val language = if (stressTrial || ruaccentTrial) "ru" else label
|
||||
val accentStart = System.nanoTime()
|
||||
val accented = if (label == "ru-ruaccent") accentor!!.accentuate(text) else text
|
||||
if (label == "ru-ruaccent") println("RuAccent (including first load): %.3f s".format(Locale.ROOT, (System.nanoTime() - accentStart) / 1e9))
|
||||
val spoken = SupertonicTts.russianStressToUnicode(accented)
|
||||
lateinit var audio: SpeechAudio
|
||||
val elapsed = measureNanoTime {
|
||||
audio = joinSpeechAudio(SupertonicTts.chunks(spoken).map { chunk ->
|
||||
SpeechAudio(engine.synthesize(chunk, voice, language, seed = 42), engine.sampleRate)
|
||||
})
|
||||
} / 1e9
|
||||
val duration = audio.samples.size.toDouble() / audio.sampleRate
|
||||
check(audio.samples.all { it.isFinite() } && audio.samples.any { kotlin.math.abs(it) > 0.001f }) {
|
||||
"Invalid or silent model output"
|
||||
}
|
||||
val file = File(output, "$label-$voice.wav")
|
||||
audio.writeWav(file)
|
||||
File(output, "$label-$voice.txt").writeText(spoken + "\n")
|
||||
println("$label/$voice: %.3f s synthesis, %.3f s audio, RTF %.3f; %s".format(
|
||||
Locale.ROOT, elapsed, duration, elapsed / duration, file.absolutePath,
|
||||
))
|
||||
if ("--play" in args) playDesktopAudio(audio)
|
||||
}
|
||||
} finally {
|
||||
accentor?.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Supertone Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Local stdin/stdout JSON protocol. Book text never leaves this process tree."""
|
||||
import contextlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
|
||||
os.environ.update(HF_HUB_OFFLINE="1", TRANSFORMERS_OFFLINE="1", HF_HUB_DISABLE_TELEMETRY="1", TOKENIZERS_PARALLELISM="false")
|
||||
|
||||
|
||||
def load(model):
|
||||
# Bound ORT worker counts before RUAccent imports its session constructor.
|
||||
import onnxruntime as ort
|
||||
original = ort.InferenceSession
|
||||
|
||||
def cpu_session(path, *args, **kwargs):
|
||||
options = ort.SessionOptions()
|
||||
options.intra_op_num_threads = 4
|
||||
options.inter_op_num_threads = 1
|
||||
kwargs["sess_options"] = options
|
||||
kwargs["providers"] = ["CPUExecutionProvider"]
|
||||
return original(path, *args, **kwargs)
|
||||
|
||||
ort.InferenceSession = cpu_session
|
||||
from ruaccent import RUAccent
|
||||
accentor = RUAccent()
|
||||
accentor.load(omograph_model_size="turbo3.1", use_dictionary=True, device="CPU", workdir=str(model), tiny_mode=False)
|
||||
return accentor
|
||||
|
||||
|
||||
def main():
|
||||
with contextlib.redirect_stdout(sys.stderr):
|
||||
accentor = load(Path(sys.argv[1]))
|
||||
if "--check" in sys.argv:
|
||||
with contextlib.redirect_stdout(sys.stderr):
|
||||
result = accentor.process_all("На двери висит замок.")
|
||||
print(result, flush=True)
|
||||
return
|
||||
print(json.dumps({"ready": True}), flush=True)
|
||||
for line in sys.stdin:
|
||||
try:
|
||||
text = json.loads(line)["text"]
|
||||
if not isinstance(text, str) or len(text) > 2000:
|
||||
raise ValueError("Expected text of at most 2000 characters")
|
||||
start = time.monotonic()
|
||||
with contextlib.redirect_stdout(sys.stderr):
|
||||
accented = accentor.process_all(text)
|
||||
print(json.dumps({"text": accented, "seconds": time.monotonic() - start}, ensure_ascii=False), flush=True)
|
||||
except Exception as error:
|
||||
# Do not echo private input or exception text into application logs.
|
||||
print(json.dumps({"error": type(error).__name__}), flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Persistent CPU-only Silero worker; local JSON lines, float32 audio in base64."""
|
||||
import array
|
||||
import base64
|
||||
import contextlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
||||
os.environ["TORCH_HOME"] = str(Path(sys.argv[1]).parent / "torch")
|
||||
|
||||
|
||||
def main():
|
||||
with contextlib.redirect_stdout(sys.stderr):
|
||||
import torch
|
||||
torch.set_num_threads(4)
|
||||
torch.set_num_interop_threads(1)
|
||||
model = torch.package.PackageImporter(str(Path(sys.argv[1]) / "model.pt")).load_pickle("tts_models", "model")
|
||||
model.to(torch.device("cpu"))
|
||||
print(json.dumps({"ready": True}), flush=True)
|
||||
for line in sys.stdin:
|
||||
try:
|
||||
request = json.loads(line)
|
||||
text = request["text"]
|
||||
voice = request["voice"]
|
||||
if not isinstance(text, str) or not 0 < len(text) <= 2000 or voice not in model.speakers:
|
||||
raise ValueError("Invalid speech request")
|
||||
# Preserve explicit reader/book stress in Silero's native notation.
|
||||
text = re.sub(r"([аеёиоуыэюяАЕЁИОУЫЭЮЯ])\u0301", r"+\1", text)
|
||||
with contextlib.redirect_stdout(sys.stderr), torch.inference_mode():
|
||||
audio = model.apply_tts(text=text, speaker=voice, sample_rate=48000,
|
||||
put_accent=True, put_yo=True,
|
||||
put_stress_homo=True, put_yo_homo=True).detach().cpu().flatten()
|
||||
if not audio.numel() or not torch.isfinite(audio).all():
|
||||
raise ValueError("Invalid model audio")
|
||||
samples = array.array("f", audio.tolist())
|
||||
if sys.byteorder != "little":
|
||||
samples.byteswap()
|
||||
print(json.dumps({"sampleRate": 48000, "audio": base64.b64encode(samples.tobytes()).decode("ascii")}), flush=True)
|
||||
except Exception as error:
|
||||
print(json.dumps({"error": type(error).__name__}), flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,186 @@
|
||||
package net.sergeych.toread.tts
|
||||
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.awaitCancellation
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.test.advanceUntilIdle
|
||||
import kotlinx.coroutines.test.runCurrent
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.toread.ReadAloudSentence
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class DesktopReadAloudPlayerTest {
|
||||
private fun sentence(index: Int, text: String) = ReadAloudSentence(index, index, 0, text.length, text)
|
||||
private fun audio(id: Int) = SpeechAudio(floatArrayOf(id.toFloat()), 24000)
|
||||
|
||||
@Test
|
||||
fun sileroDefaultsToXeniaRoutesEnglishAndResetsVoiceOnEngineChange() = runTest {
|
||||
val calls = mutableListOf<String>()
|
||||
var accented = false
|
||||
val player = DesktopReadAloudPlayer(this, { text, voice -> calls += "super:$voice:$text"; audio(1) },
|
||||
{}, { true }, { accented = true; it }, { text, voice -> calls += "silero:$voice:$text"; audio(1) })
|
||||
assertEquals("silero", player.settingsState.value.selectedEngineId)
|
||||
assertEquals("xenia", player.settingsState.value.selectedVoiceId)
|
||||
player.prepare(listOf(sentence(0, "Привет."), sentence(1, "Hello."), sentence(2, "Привет, John.")), 0)
|
||||
player.play()
|
||||
advanceUntilIdle()
|
||||
assertEquals(listOf("silero:xenia:Привет.", "super:F1:Hello.", "super:F1:Привет, John."), calls)
|
||||
assertFalse(accented)
|
||||
player.selectVoice("F1")
|
||||
assertEquals("xenia", player.settingsState.value.selectedVoiceId)
|
||||
player.selectEngine("supertonic-3")
|
||||
assertEquals("F1", player.settingsState.value.selectedVoiceId)
|
||||
player.selectEngine("silero")
|
||||
assertEquals("xenia", player.settingsState.value.selectedVoiceId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun switchingAwayFromSileroCancelsPendingSpeech() = runTest {
|
||||
var cancelled = false
|
||||
var played = 0
|
||||
val player = DesktopReadAloudPlayer(this, { _, _ -> audio(1) }, { played++ }, { true },
|
||||
silero = { _, _ -> try { awaitCancellation() } finally { cancelled = true } })
|
||||
player.prepare(listOf(sentence(0, "Привет.")), 0)
|
||||
player.play()
|
||||
runCurrent()
|
||||
player.selectEngine("supertonic-3")
|
||||
advanceUntilIdle()
|
||||
assertTrue(cancelled)
|
||||
assertEquals(1, played)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stopDuringSynthesisDiscardsAudioAndDoesNotReportAnError() = runTest {
|
||||
var started = false
|
||||
var played = false
|
||||
val player = DesktopReadAloudPlayer(this, { _, _ -> started = true; awaitCancellation() }, { played = true }, { true })
|
||||
player.prepare(listOf(sentence(7, "First.")), 0)
|
||||
player.play()
|
||||
runCurrent()
|
||||
assertTrue(started)
|
||||
assertTrue(player.state.value.buffering)
|
||||
player.stop()
|
||||
runCurrent()
|
||||
assertFalse(played)
|
||||
assertFalse(player.state.value.active)
|
||||
assertNull(player.settingsState.value.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun skipCancelsOldSynthesisAndStartsRequestedSentence() = runTest {
|
||||
val played = mutableListOf<Int>()
|
||||
val player = DesktopReadAloudPlayer(this, { text, _ ->
|
||||
if (text == "First.") awaitCancellation() else audio(2)
|
||||
}, { played += it.samples[0].toInt(); awaitCancellation() }, { true })
|
||||
player.prepare(listOf(sentence(10, "First."), sentence(20, "Second.")), 0)
|
||||
player.play()
|
||||
runCurrent()
|
||||
player.skip(1)
|
||||
runCurrent()
|
||||
assertEquals(listOf(2), played)
|
||||
assertEquals(20, player.state.value.sentenceIndex)
|
||||
player.stop()
|
||||
runCurrent()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun voiceChangeCancelsPlaybackAndRestartsCurrentSentence() = runTest {
|
||||
val voices = mutableListOf<String>()
|
||||
var cancelledAudio = 0
|
||||
val player = DesktopReadAloudPlayer(this, { _, voice -> voices += voice; audio(1) }, {
|
||||
try { awaitCancellation() } finally { cancelledAudio++ }
|
||||
}, { true })
|
||||
player.prepare(listOf(sentence(3, "Hello.")), 0)
|
||||
player.play()
|
||||
runCurrent()
|
||||
player.selectVoice("M1")
|
||||
runCurrent()
|
||||
assertEquals(listOf("F1", "M1"), voices)
|
||||
assertEquals(1, cancelledAudio)
|
||||
assertEquals(3, player.state.value.sentenceIndex)
|
||||
player.stop()
|
||||
runCurrent()
|
||||
assertEquals(2, cancelledAudio)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun lookaheadDoesNotAdvanceHighlightBeforePlayback() = runTest {
|
||||
val synthesized = mutableListOf<String>()
|
||||
val player = DesktopReadAloudPlayer(this, { text, _ -> synthesized += text; audio(1) }, {
|
||||
awaitCancellation()
|
||||
}, { true })
|
||||
player.prepare(listOf(sentence(4, "First."), sentence(8, "Second."), sentence(12, "Third.")), 0)
|
||||
player.play()
|
||||
runCurrent()
|
||||
assertEquals(listOf("First.", "Second."), synthesized)
|
||||
assertEquals(4, player.state.value.sentenceIndex)
|
||||
player.stop()
|
||||
runCurrent()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fullQueueUsesSpokenTextAndFinishes() = runTest {
|
||||
val synthesized = mutableListOf<String>()
|
||||
val player = DesktopReadAloudPlayer(this, { text, _ -> synthesized += text; audio(1) }, { delay(10) }, { true })
|
||||
player.prepare(listOf(sentence(0, "1").copy(spokenText = "One.", pauseBeforeMillis = 20), sentence(1, "Two.")), 0)
|
||||
player.play()
|
||||
advanceUntilIdle()
|
||||
assertEquals(listOf("One.", "Two."), synthesized)
|
||||
assertFalse(player.state.value.playing)
|
||||
assertFalse(player.state.value.active)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun accentStageCanBeComparedWithPlainEngineAndNeverChangesOriginalText() = runTest {
|
||||
val inputs = mutableListOf<String>()
|
||||
val original = sentence(0, "замок")
|
||||
val player = DesktopReadAloudPlayer(this, { text, _ -> inputs += text; audio(1) }, {}, { true }, { "з+амок" })
|
||||
player.prepare(listOf(original), 0)
|
||||
player.play()
|
||||
advanceUntilIdle()
|
||||
assertEquals(listOf("за\u0301мок"), inputs)
|
||||
assertEquals("замок", original.spokenText)
|
||||
player.selectEngine("supertonic-3")
|
||||
player.play()
|
||||
advanceUntilIdle()
|
||||
assertEquals(listOf("за\u0301мок", "замок"), inputs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun accentedTextIsRechunkedToFitTtsWithoutDiscardingMarks() = runTest {
|
||||
val inputs = mutableListOf<String>()
|
||||
val marked = "а\u0301 ".repeat(110).trim()
|
||||
val player = DesktopReadAloudPlayer(this, { text, _ -> inputs += text; audio(1) }, {}, { true }, { marked })
|
||||
player.prepare(listOf(sentence(0, "а ".repeat(110))), 0)
|
||||
player.play()
|
||||
advanceUntilIdle()
|
||||
assertTrue(inputs.size > 1)
|
||||
assertTrue(inputs.all { it.length <= 300 })
|
||||
assertEquals(marked.replace(" ", ""), inputs.joinToString("").replace(" ", ""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun missingModelAndInferenceErrorsRemainVisibleAndRetryable() = runTest {
|
||||
var installed = false
|
||||
var calls = 0
|
||||
val player = DesktopReadAloudPlayer(this, { _, _ -> calls++; error("bad model") }, {}, { installed })
|
||||
player.prepare(listOf(sentence(0, "Hello.")), 0)
|
||||
player.play()
|
||||
assertEquals(0, calls)
|
||||
assertFalse(player.state.value.playing)
|
||||
assertNotNull(player.settingsState.value.message)
|
||||
installed = true
|
||||
player.play()
|
||||
advanceUntilIdle()
|
||||
assertEquals(1, calls)
|
||||
assertTrue(player.state.value.active)
|
||||
assertFalse(player.state.value.buffering)
|
||||
assertNotNull(player.settingsState.value.message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package net.sergeych.toread.tts
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class SupertonicTextTest {
|
||||
@Test
|
||||
fun longSentencesAndUnbrokenWordsAreBoundedWithoutLosingText() {
|
||||
for (text in listOf("Слово ".repeat(150), "a".repeat(601), "a".repeat(299) + "😀" + "b".repeat(301), "а".repeat(300) + "\u0301" + "а".repeat(50))) {
|
||||
val chunks = SupertonicTts.chunks(text)
|
||||
assertTrue(chunks.all { it.length <= 300 && it.isNotBlank() })
|
||||
assertEquals(text.filterNot(Char::isWhitespace), chunks.joinToString("").filterNot(Char::isWhitespace))
|
||||
assertTrue(chunks.none { Character.isHighSurrogate(it.last()) || Character.isLowSurrogate(it.first()) })
|
||||
assertTrue(chunks.none { it.first() == '\u0301' })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun languageSelectionHandlesRussianEnglishAndMixedText() {
|
||||
assertEquals("ru", SupertonicTts.languageOf("Ёж шёл домой."))
|
||||
assertEquals("en", SupertonicTts.languageOf("The library."))
|
||||
assertEquals("na", SupertonicTts.languageOf("Он сказал Hello."))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun preprocessingDoesNotInsertPredictedStress() {
|
||||
assertEquals("<ru>На двери висит замок.</ru>", SupertonicTts.prepareText("На двери висит замок", "ru"))
|
||||
assertEquals("<en>Hello!</en>", SupertonicTts.prepareText("Hello !", "en"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ruaccentStressBecomesCombiningAcuteOnCyrillicVowels() {
|
||||
assertEquals("за\u0301мок замо\u0301к Ё\u0301лка", SupertonicTts.russianStressToUnicode("з+амок зам+ок +Ёлка"))
|
||||
assertEquals("2+2 +a +б а\u0301", SupertonicTts.russianStressToUnicode("2+2 +a +б а\u0301"))
|
||||
assertEquals("<ru>За\u0301мок.</ru>", SupertonicTts.prepareText("З+амок", "ru"))
|
||||
assertEquals("<ru>За\u0301мок.</ru>", SupertonicTts.prepareText("За\u0301мок", "ru"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pcmConversionClipsAndHandlesNonFiniteSamples() {
|
||||
val bytes = SpeechAudio(floatArrayOf(-2f, 2f, Float.NaN), 44100).pcm16()
|
||||
assertEquals(listOf(1, 128, 255, 127, 0, 0), bytes.map { it.toInt() and 255 })
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,10 @@ actual suspend fun markLibraryFavorite(fileId: String, favorite: Boolean): Boole
|
||||
|
||||
actual suspend fun shareLibraryBookFile(fileId: String): Boolean = false
|
||||
|
||||
actual suspend fun viewLibraryBookFile(fileId: String): Boolean = false
|
||||
actual suspend fun viewLibraryBookFile(folder: String, fileName: String): Boolean = false
|
||||
|
||||
actual fun openPlatformUrl(url: String): Boolean =
|
||||
window.open(url, target = "_blank") != null
|
||||
|
||||
actual suspend fun loadBookInfoExtras(fileId: String): BookInfoExtras = BookInfoExtras()
|
||||
|
||||
@@ -73,10 +76,48 @@ actual suspend fun loadThemeMode(): ThemeMode = ThemeMode.SYSTEM
|
||||
|
||||
actual suspend fun saveThemeMode(mode: ThemeMode) = Unit
|
||||
|
||||
actual suspend fun loadReaderFontSettings(): ReaderFontSettings =
|
||||
readerFontSettingsFromStorageValue(window.localStorage.getItem(ReaderFontSettingsStorageKey))
|
||||
|
||||
actual suspend fun saveReaderFontSettings(settings: ReaderFontSettings) {
|
||||
window.localStorage.setItem(ReaderFontSettingsStorageKey, settings.toStorageValue())
|
||||
}
|
||||
|
||||
actual suspend fun loadReaderFullScreen(): Boolean =
|
||||
window.localStorage.getItem(ReaderFullScreenStorageKey)?.toBooleanStrictOrNull() ?: false
|
||||
|
||||
actual suspend fun saveReaderFullScreen(fullScreen: Boolean) {
|
||||
window.localStorage.setItem(ReaderFullScreenStorageKey, fullScreen.toString())
|
||||
}
|
||||
|
||||
actual suspend fun loadReaderPagingOnly(): Boolean =
|
||||
window.localStorage.getItem(ReaderPagingOnlyStorageKey)?.toBooleanStrictOrNull() ?: false
|
||||
|
||||
actual suspend fun saveReaderPagingOnly(enabled: Boolean) {
|
||||
window.localStorage.setItem(ReaderPagingOnlyStorageKey, enabled.toString())
|
||||
}
|
||||
|
||||
actual suspend fun loadScanDownloadsAutomatically(): Boolean = true
|
||||
|
||||
actual suspend fun saveScanDownloadsAutomatically(enabled: Boolean) = Unit
|
||||
|
||||
internal actual suspend fun loadLibraryFilter(): LibraryFilter =
|
||||
when (val saved = window.localStorage.getItem(LibraryFilterStorageKey)) {
|
||||
null -> {
|
||||
warnLibraryFilterFallback("library filter was not saved")
|
||||
LibraryFilter.MyLibrary
|
||||
}
|
||||
else -> runCatching { LibraryFilter.valueOf(saved) }
|
||||
.getOrElse {
|
||||
warnLibraryFilterFallback("invalid saved library filter '$saved'")
|
||||
LibraryFilter.MyLibrary
|
||||
}
|
||||
}
|
||||
|
||||
internal actual suspend fun saveLibraryFilter(filter: LibraryFilter) {
|
||||
window.localStorage.setItem(LibraryFilterStorageKey, filter.name)
|
||||
}
|
||||
|
||||
actual suspend fun loadAppLocaleTag(): String? =
|
||||
window.localStorage.getItem(AppLocaleStorageKey)?.takeIf { it.isNotBlank() }
|
||||
|
||||
@@ -110,7 +151,15 @@ actual fun watchPlatformDarkTheme(onChange: (Boolean) -> Unit): () -> Unit {
|
||||
|
||||
actual fun libraryLogPath(): String? = null
|
||||
|
||||
private fun warnLibraryFilterFallback(reason: String) {
|
||||
println("warning $reason; using ${LibraryFilter.MyLibrary.name}")
|
||||
}
|
||||
|
||||
private const val AppLocaleStorageKey = "toread.appLocaleTag"
|
||||
private const val LibraryFilterStorageKey = "toread.libraryFilter"
|
||||
private const val ReaderFontSettingsStorageKey = "toread.readerFontSettings"
|
||||
private const val ReaderFullScreenStorageKey = "toread.readerFullScreen"
|
||||
private const val ReaderPagingOnlyStorageKey = "toread.readerPagingOnly"
|
||||
|
||||
actual fun formatLibraryLastReadTime(millis: Long): String {
|
||||
val totalMinutes = millis / 60_000L
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
# Desktop CPU speech trial
|
||||
|
||||
Desktop Read aloud defaults to **Silero v5_5_ru / Xenia** for Russian when its
|
||||
local runtime is installed. Silero runs in a persistent CPU-only Python process
|
||||
and handles its own stress and ё restoration. English and mixed-script chunks
|
||||
use Supertonic 3 / F1 through ONNX Runtime's CPU provider. Mixed passages retain
|
||||
Supertonic's Russian pronunciation limitations. Plain Supertonic is retained as an audition baseline. Initial listening feedback
|
||||
found its Russian word stress unacceptable. An optional RUAccent CPU stage now
|
||||
adds stress and restores ё before synthesis; its +vowel notation is converted to
|
||||
a Cyrillic vowel followed by U+0301. Manual minimal-pair listening showed stress
|
||||
changes, but this is not a comprehensive pronunciation-quality evaluation.
|
||||
Android retains its existing system TTS implementation.
|
||||
|
||||
## Run
|
||||
|
||||
From the repository root:
|
||||
|
||||
```sh
|
||||
python3 scripts/download-tts-model.py
|
||||
./gradlew :composeApp:run
|
||||
```
|
||||
|
||||
The script downloads about 401 MB into the ignored `.local/tts/supertonic-3`
|
||||
directory. Downloads are revision-pinned and verified against the committed file
|
||||
manifest. It reuses valid files and replaces incomplete downloads only after hash
|
||||
verification. No Python packages are needed. The model license is saved alongside
|
||||
its weights.
|
||||
|
||||
Open a book and choose **Read aloud** from the reader menu. Playback highlights the
|
||||
current sentence, follows the reading position, and exposes Stop, previous/next
|
||||
sentence, and voice settings. Silero offers Xenia (default), Baya, Aidar, Kseniya
|
||||
and Eugene. The Supertonic engines offer F1–F5 and M1–M5. Engine and voice choices
|
||||
last for this application session; each launch starts with Xenia if installed.
|
||||
|
||||
The model loads lazily on first playback. One chunk is prepared ahead while audio
|
||||
plays. Stop, skipping, and voice changes cancel the old playback and lookahead;
|
||||
Supertonic inference checks cancellation between model stages/denoising steps;
|
||||
cancelling Silero inference terminates its worker, which restarts on demand. Long
|
||||
sentences are split at whitespace into at most 300-character chunks. English and
|
||||
Russian are selected from their scripts; mixed-script chunks use the model's
|
||||
language-agnostic `na` mode. This heuristic is not general language detection.
|
||||
|
||||
For a model installed elsewhere, set `YEREED_TTS_MODEL_DIR` (or JVM property
|
||||
`yereed.tts.modelDir`) to the directory containing `onnx/` and `voice_styles/`.
|
||||
Without an override the app searches parent directories for the workspace model,
|
||||
then falls back to `~/.toread/tts/supertonic-3`. Packaged applications can use that
|
||||
home-directory location. The application itself never downloads models or sends
|
||||
text over the network. The first Gradle build and model setup require downloads.
|
||||
|
||||
## Optional RUAccent stage (desktop trial)
|
||||
|
||||
```sh
|
||||
python3 scripts/setup-ruaccent.py
|
||||
./gradlew :composeApp:run
|
||||
```
|
||||
|
||||
This creates a private Python 3.12 runtime under `.local/ruaccent-runtime` and
|
||||
fetches about 726 MB of revision-pinned RUAccent assets into `.local/tts/ruaccent`.
|
||||
It does not change system Python. The bootstrap supports Linux x86_64 directly;
|
||||
on other systems install `uv` first. The dependency versions and model hashes
|
||||
are recorded under `scripts/`. This desktop trial uses RUAccent 1.5.8.3,
|
||||
`turbo3.1`, the full dictionary/rule pipeline, and CPU execution with four threads.
|
||||
This is a desktop quality experiment, not an Android footprint recommendation.
|
||||
|
||||
After setup, restart the reader. **Supertonic 3 + RUAccent** becomes the default
|
||||
engine; select **Supertonic 3** in read-aloud settings for a direct comparison.
|
||||
English bypasses the accentor. Already marked Russian chunks are preserved.
|
||||
The accentor is a persistent local child process communicating over stdin/stdout;
|
||||
Hugging Face and Transformers run in offline mode. Book contents are never sent
|
||||
to a server. Set `YEREED_RUACCENT_HOME` to override its installation directory.
|
||||
Stop/skip kills a pending accentor request when necessary; the next request
|
||||
restarts the worker. Displayed book text and stored files are unchanged.
|
||||
|
||||
The model recognizes U+0301, and preprocessing preserves it. For example,
|
||||
`з+амок` becomes `за́мок`, not a Latin accented-vowel substitution. Predicted
|
||||
stress can still be wrong; model-token support does not guarantee compliance.
|
||||
|
||||
## Audition / benchmark
|
||||
|
||||
```sh
|
||||
./gradlew :composeApp:ttsSample
|
||||
# Also play the four samples through the desktop audio device:
|
||||
./gradlew :composeApp:ttsSample -PttsPlay=true
|
||||
# Controlled manual stress pairs, same words/noise seed:
|
||||
./gradlew :composeApp:ttsSample -PttsStress=true -PttsPlay=true
|
||||
# Original plain Russian passage, then RUAccent + Supertonic:
|
||||
./gradlew :composeApp:ttsSample -PttsRuAccent=true -PttsPlay=true
|
||||
```
|
||||
|
||||
WAV files and their exact unaccented texts go to `composeApp/build/tts-samples/`.
|
||||
The sample task uses the same inference and audio code as the reader without
|
||||
opening or changing the library. It prints model-load time, synthesis time,
|
||||
audio duration, and real-time factor (generation seconds / audio seconds).
|
||||
|
||||
Initial measurements on an Intel Core Ultra 9 275HX, four CPU threads, eight
|
||||
steps, speed 1.05: 1.075 seconds model load; English/Russian F1/M1 samples generated
|
||||
11.6–15.3 seconds of audio in 2.4–3.3 seconds (RTF 0.20–0.24). These are short
|
||||
local samples, not phone/battery or long-book benchmarks. Synthesis completion
|
||||
and non-silent waveforms do not establish pronunciation quality.
|
||||
|
||||
## Dependencies and provenance
|
||||
|
||||
- [Supertonic source](https://github.com/supertone-oss-archive/supertonic): the
|
||||
Kotlin inference code adapts the MIT Java example; attribution is in
|
||||
[licenses/supertonic-code-MIT.txt](licenses/supertonic-code-MIT.txt).
|
||||
- [Supertonic 3 model](https://huggingface.co/supertone-oss-archive/supertonic-3):
|
||||
revision `aafc6e32416a594460b32413efc49d7fe4ce6d46`, OpenRAIL-M weights.
|
||||
Upstream development/support ended in September 2026.
|
||||
- ONNX Runtime 1.23.1, standard CPU artifact; no CUDA/GPU provider.
|
||||
- JSON loading uses Jackson; PCM playback uses Java Sound.
|
||||
|
||||
Run `./gradlew :composeApp:jvmTest` for text-boundary, PCM conversion, queue,
|
||||
lookahead, cancellation, voice-change, and missing-model/error tests, together
|
||||
with existing desktop/common tests. Model inference is exercised separately by
|
||||
`ttsSample`, so ordinary tests do not download weights or use speakers.
|
||||
|
||||
The isolated virtual-display capture did not provide a usable UI image; rendered
|
||||
GUI verification remains incomplete. No further screen access is used.
|
||||
|
||||
RUAccent measurements on the same host: about 5.9 seconds cold load, 30–43 ms
|
||||
for the two warm short passages, and about 2.1 GiB resident memory for the full
|
||||
accentor worker alone. The combined passage took 2.98 seconds of TTS synthesis
|
||||
for 16.42 seconds of audio, excluding accentor startup. In that passage RUAccent
|
||||
incorrectly chose `за́мок` for the door lock; on a shorter explicit door-lock
|
||||
sentence it chose `замо́к`. These are limited observations, not an accuracy score.
|
||||
|
||||
## Silero Russian audition
|
||||
|
||||
User listening feedback rejected Supertonic's Russian quality. A separate CPU
|
||||
audition uses official Silero `v5_5_ru`, with its own stress, ё, stress-homograph
|
||||
and ё-homograph processing enabled. It does not use RUAccent. The same model is
|
||||
now the preferred desktop Russian engine. The Russian model is CC BY-NC-SA 4.0, unlike Silero's MIT CIS
|
||||
base models; see [upstream](https://github.com/snakers4/silero-models).
|
||||
|
||||
Setup from the repository root (requires `uv`; our existing local copy is
|
||||
`.local/tools/uv`):
|
||||
|
||||
```sh
|
||||
export UV_CACHE_DIR="$PWD/.local/uv-cache"
|
||||
export UV_PYTHON_INSTALL_DIR="$PWD/.local/python"
|
||||
export UV_SYSTEM_CERTS=true
|
||||
.local/tools/uv venv --python 3.12.14 .local/silero-runtime
|
||||
.local/tools/uv pip sync --python .local/silero-runtime/bin/python scripts/silero-requirements.txt --index-url https://download.pytorch.org/whl/cpu
|
||||
mkdir -p .local/tts/silero-v5_5-ru
|
||||
curl -fL https://models.silero.ai/models/tts/ru/v5_5_ru.pt -o .local/tts/silero-v5_5-ru/model.pt.part
|
||||
mv .local/tts/silero-v5_5-ru/model.pt.part .local/tts/silero-v5_5-ru/model.pt
|
||||
.local/silero-runtime/bin/python scripts/silero-sample.py
|
||||
python3 - <<'PY'
|
||||
from pathlib import Path
|
||||
import json
|
||||
root = Path.cwd()
|
||||
(root / '.local/tts/silero-v5_5-ru/runtime.json').write_text(json.dumps({
|
||||
'python': str(root / '.local/silero-runtime/bin/python')
|
||||
}) + '\n')
|
||||
PY
|
||||
# Restart the reader after setup, or check its actual Kotlin/Python bridge:
|
||||
./gradlew :composeApp:ttsSample -PttsSilero=true
|
||||
```
|
||||
|
||||
Outputs: `composeApp/build/tts-samples/silero-*.wav`, matching input `.txt`
|
||||
files, and `silero-benchmark.json` with timings, settings and the downloaded
|
||||
model's SHA-256. The script uses CPU-only PyTorch, four threads and 48 kHz PCM.
|
||||
Default voices are Xenia, Baya and Aidar; `--voices xenia` limits the audition.
|
||||
Each reads the earlier library passage and a separate unaccented ё/context
|
||||
challenge. Pronunciation quality must be judged by listening.
|
||||
|
||||
The desktop integration discovers `silero-v5_5-ru` beside the Supertonic model
|
||||
directory; `YEREED_SILERO_HOME` can override it. `runtime.json` points to the
|
||||
isolated Python executable. The worker script is bundled in the application;
|
||||
model loading and inference are local. Both models are needed for bilingual use.
|
||||
|
||||
Initial audition on the same desktop: model load 0.38 seconds; six samples
|
||||
generated 13.2–15.5 seconds of audio in 0.36–0.82 seconds (RTF 0.027–0.059,
|
||||
including the first synthesis). All outputs were finite and non-silent.
|
||||
Downloaded model SHA-256:
|
||||
`50081637b602126ee06cb3bc8a744d25651d2da149ee8864b9a379bfdd934437`.
|
||||
@@ -20,7 +20,7 @@ The common API is `Fb2Format.parse(input: ByteArray, fileName: String? = null)`.
|
||||
Import detection:
|
||||
|
||||
- A file is treated as ZIP when its bytes start with the ZIP local-file signature `PK\003\004` or the provided filename ends with `.zip`.
|
||||
- Otherwise bytes are decoded according to the XML declaration when supported. UTF-8 is the default, and unsupported or missing encodings fall back to UTF-8. `windows-1251` is supported for legacy FB2 files.
|
||||
- Otherwise bytes are decoded according to the XML declaration when supported. UTF-8 is the default, and unsupported or missing encodings fall back to UTF-8. `windows-1251` and `windows-1252` are supported for legacy FB2 files.
|
||||
- In ZIP archives, the first entry ending with `.fb2` is used. If no such entry exists, the first non-directory entry is used.
|
||||
|
||||
ZIP support:
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Supertone Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,5 +1,5 @@
|
||||
[versions]
|
||||
agp = "8.11.2"
|
||||
agp = "8.13.2"
|
||||
android-compileSdk = "36"
|
||||
android-minSdk = "26"
|
||||
android-targetSdk = "36"
|
||||
@@ -19,8 +19,12 @@ ktor = "3.4.3"
|
||||
logback = "1.5.32"
|
||||
material3 = "1.11.0-alpha07"
|
||||
h2 = "2.4.240"
|
||||
onnxruntime = "1.23.1"
|
||||
jackson = "2.18.6"
|
||||
|
||||
[libraries]
|
||||
onnxruntime = { module = "com.microsoft.onnxruntime:onnxruntime", version.ref = "onnxruntime" }
|
||||
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
|
||||
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
||||
kotlin-testJunit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
|
||||
junit = { module = "junit:junit", version.ref = "junit" }
|
||||
@@ -41,6 +45,7 @@ compose-components-resources = { module = "org.jetbrains.compose.components:comp
|
||||
compose-uiToolingPreview = { module = "org.jetbrains.compose.ui:ui-tooling-preview", version.ref = "composeMultiplatform" }
|
||||
kotlinx-coroutinesSwing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
|
||||
kotlinx-coroutinesCore = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
|
||||
kotlinx-coroutinesTest = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
|
||||
h2 = { module = "com.h2database:h2", version.ref = "h2" }
|
||||
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
|
||||
ktor-serverCore = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
|
||||
|
||||
|
Before Width: | Height: | Size: 215 KiB |
|
Before Width: | Height: | Size: 199 KiB |
|
Before Width: | Height: | Size: 179 KiB |
|
Before Width: | Height: | Size: 171 KiB |
|
After Width: | Height: | Size: 208 KiB |
|
After Width: | Height: | Size: 392 KiB |
|
After Width: | Height: | Size: 398 KiB |
|
After Width: | Height: | Size: 212 KiB |
|
After Width: | Height: | Size: 203 KiB |
|
After Width: | Height: | Size: 365 KiB |
|
After Width: | Height: | Size: 262 KiB |
|
After Width: | Height: | Size: 407 KiB |
|
After Width: | Height: | Size: 280 KiB |
|
After Width: | Height: | Size: 136 KiB |
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Download the pinned Supertonic 3 CPU model. No packages or credentials required."""
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
MANIFEST = json.loads((ROOT / "scripts/supertonic-3-model.json").read_text())
|
||||
|
||||
|
||||
def valid(path, entry):
|
||||
if not path.is_file() or path.stat().st_size != entry["size"]:
|
||||
return False
|
||||
digest = hashlib.sha256() if "sha256" in entry else hashlib.sha1()
|
||||
if "git_blob" in entry:
|
||||
digest.update(f"blob {entry['size']}\0".encode())
|
||||
with path.open("rb") as source:
|
||||
for chunk in iter(lambda: source.read(1024 * 1024), b""):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest() == entry.get("sha256", entry.get("git_blob"))
|
||||
|
||||
|
||||
def download(manifest, destination):
|
||||
total = sum(entry["size"] for entry in manifest["files"])
|
||||
print(f"{manifest['repository']}: {total / 1e6:.1f} MB; destination: {destination}", flush=True)
|
||||
base = f"https://huggingface.co/{manifest['repository']}/resolve/{manifest['revision']}"
|
||||
for entry in manifest["files"]:
|
||||
target = destination / entry["path"]
|
||||
if valid(target, entry):
|
||||
print(f"Verified {entry['path']}", flush=True)
|
||||
continue
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
temporary = target.with_suffix(target.suffix + ".part")
|
||||
print(f"Downloading {entry['path']} ({entry['size'] / 1e6:.1f} MB)", flush=True)
|
||||
try:
|
||||
with urllib.request.urlopen(f"{base}/{entry['path']}", timeout=60) as source, temporary.open("wb") as output:
|
||||
while chunk := source.read(1024 * 1024):
|
||||
output.write(chunk)
|
||||
if not valid(temporary, entry):
|
||||
raise RuntimeError(f"Checksum mismatch: {entry['path']}")
|
||||
temporary.replace(target)
|
||||
finally:
|
||||
temporary.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def main():
|
||||
destination = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT / ".local/tts/supertonic-3"
|
||||
print("Supertonic model license: OpenRAIL-M (saved as LICENSE alongside the model).", flush=True)
|
||||
download(MANIFEST, destination)
|
||||
print("Ready. Run ./gradlew :composeApp:run and use Read aloud in a book.", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,231 @@
|
||||
{
|
||||
"repository": "ruaccent/accentuator",
|
||||
"revision": "b78ae5ea1e62beaf138bed1865cd8c3b0b5ca855",
|
||||
"files": [
|
||||
{
|
||||
"path": "README.md",
|
||||
"size": 2660,
|
||||
"git_blob": "7f12645cf20ed21742fd63a3b799efd8c7007734"
|
||||
},
|
||||
{
|
||||
"path": "dictionary/accents.json.gz",
|
||||
"size": 20954156,
|
||||
"sha256": "aa460ebba90de00fbbf3d41d121961f605b98667e45efb7920f127473b15515e"
|
||||
},
|
||||
{
|
||||
"path": "dictionary/accents_nn.json.gz",
|
||||
"size": 845996,
|
||||
"sha256": "8395664000b80c1afe09bfea3650945b0933482b8e3dee5bb9d429eb18c44935"
|
||||
},
|
||||
{
|
||||
"path": "dictionary/omographs.json.gz",
|
||||
"size": 219047,
|
||||
"sha256": "04a9e81c68d65f65ba493fe0110f99e79087548c2beeec3032e2b66e28706f36"
|
||||
},
|
||||
{
|
||||
"path": "dictionary/rule_engine/accents.json",
|
||||
"size": 694782,
|
||||
"git_blob": "91690536e14e133fe4017941a466fde663b96011"
|
||||
},
|
||||
{
|
||||
"path": "dictionary/rule_engine/forms.json",
|
||||
"size": 2669,
|
||||
"git_blob": "6686cd615fb55c09976550aeba3ad6687ecc32dd"
|
||||
},
|
||||
{
|
||||
"path": "dictionary/yo_homographs.json.gz",
|
||||
"size": 5747,
|
||||
"sha256": "c4ee777bbbab87f9eac838f370ad92974e079d02b21903e480c54b5f0c8c60d1"
|
||||
},
|
||||
{
|
||||
"path": "dictionary/yo_words.json.gz",
|
||||
"size": 548914,
|
||||
"sha256": "a19fa89a964a0691d9fe4ee384783e3934904891843d8f59a1c480d67947a82a"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rulemma/rulemma.dat",
|
||||
"size": 16703198,
|
||||
"sha256": "bf2b3ef3ff7a0aa6e4250aa4e9c8ed568e25f825deebdb12dee1b46b785ba9fc"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rulemma/rulemma.py",
|
||||
"size": 11166,
|
||||
"git_blob": "a182b7d1694b13e8ac6807e2701ad17bf4064bbe"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rupostagger/__init__.py",
|
||||
"size": 111,
|
||||
"git_blob": "01bd02ccfc68c851542390a6d0a684481f897bb9"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rupostagger/database/ruword2tags.db",
|
||||
"size": 168816640,
|
||||
"sha256": "a06848e656bef642aafb4440c03554fa78f2f32dde92ea66f3f86ce9977b167e"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rupostagger/rupostagger.config",
|
||||
"size": 276,
|
||||
"git_blob": "4794511fc1b5ef4bc649fab5eafdf6b775206b72"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rupostagger/rupostagger.model",
|
||||
"size": 2417464,
|
||||
"sha256": "21b7b0bfd7427b5fdc1604052176db8aa3b139b3ce03be440cfce48536f8e5ef"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rupostagger/rupostagger.py",
|
||||
"size": 7118,
|
||||
"git_blob": "198a8db45678a9578dfad5bbf01d3efd8e95a7c8"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rupostagger/rusyllab.py",
|
||||
"size": 18975,
|
||||
"git_blob": "7f606a48ff2a5de6b6388a45caf1d41bd6089611"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rupostagger/ruword2tags.dat",
|
||||
"size": 9683765,
|
||||
"sha256": "dde47b5f1d48ff899887ac07812dcabd2966e48e84646f3065bfd06627c2af58"
|
||||
},
|
||||
{
|
||||
"path": "koziev/rupostagger/ruword2tags.py",
|
||||
"size": 18086,
|
||||
"git_blob": "71098637747f19e44788506775e4cd1e6c5e7183"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_accent/big.onnx",
|
||||
"size": 2285217,
|
||||
"sha256": "47e69d9ae19f2a82e21b1c70f6a4bbfb1abc5759e98b2e67d009c5e9d7af18c9"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_accent/config.json",
|
||||
"size": 841,
|
||||
"git_blob": "0d1656a1fb93fcea726186c8bfad977c6e27c4a5"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_accent/model.onnx",
|
||||
"size": 803402,
|
||||
"sha256": "4e393144e45626f6f1062a0784ef06f921b97321a8e7b87ac2a09a892286500a"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_accent/ort_config.json",
|
||||
"size": 727,
|
||||
"git_blob": "5a8fc726323dda820ae4e794e4d0d514de07eee2"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_accent/special_tokens_map.json",
|
||||
"size": 99,
|
||||
"git_blob": "1543e951b7c6f9c246a5e441d7162b2c65ab48a5"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_accent/tokenizer_config.json",
|
||||
"size": 257,
|
||||
"git_blob": "5ea10565e634adcd37089652fbf6bb2cf05c1530"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_accent/vocab.txt",
|
||||
"size": 140,
|
||||
"git_blob": "1e5a3d765a6f5a686ed3c6294efb85e9e00a431a"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_omograph/turbo3.1/added_tokens.json",
|
||||
"size": 279418,
|
||||
"git_blob": "23620bf92f2ad9e1243202cff127617fe9415c9a"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_omograph/turbo3.1/config.json",
|
||||
"size": 723,
|
||||
"git_blob": "2ff7dbeb01fffcb05f65ad430b08205fe6668693"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_omograph/turbo3.1/merges.txt",
|
||||
"size": 1213606,
|
||||
"git_blob": "2d4658a914a3760fbf1a3a9c72b42eefcf8d8ded"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_omograph/turbo3.1/model.onnx",
|
||||
"size": 359306923,
|
||||
"sha256": "2cb6a174c4cdb45bd3132b4f7c8a3779fc4b6869863180ed7d0e421bcd453dbd"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_omograph/turbo3.1/special_tokens_map.json",
|
||||
"size": 280,
|
||||
"git_blob": "d5698132694f4f1bcff08fa7d937b1701812598e"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_omograph/turbo3.1/tokenizer.json",
|
||||
"size": 5532343,
|
||||
"git_blob": "31ac709e3c4625c6bf80b7c97066a9cde4d9ba89"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_omograph/turbo3.1/tokenizer_config.json",
|
||||
"size": 492,
|
||||
"git_blob": "15a19568f93d1b292c7ff6d4ef0fdac13bd65b53"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_omograph/turbo3.1/vocab.json",
|
||||
"size": 1555282,
|
||||
"git_blob": "ede65202ec9ef483ff166bb31f76615f30048db7"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_stress_usage_predictor/config.json",
|
||||
"size": 822,
|
||||
"git_blob": "df189246d87953bead249c2ee82e3985a96be8f8"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_stress_usage_predictor/model.onnx",
|
||||
"size": 116473561,
|
||||
"sha256": "3d547500637b4ddfec8880ed6d1405fd50ee9d3f0131ef8a2a69dcf961dbefeb"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_stress_usage_predictor/special_tokens_map.json",
|
||||
"size": 125,
|
||||
"git_blob": "a8b3208c2884c4efb86e49300fdd3dc877220cdf"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_stress_usage_predictor/tokenizer.json",
|
||||
"size": 2413536,
|
||||
"git_blob": "b4d5ccba4a193e0330537378bb0bba47026bd364"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_stress_usage_predictor/tokenizer_config.json",
|
||||
"size": 368,
|
||||
"git_blob": "d7d40c5e58b2c6ba58974571f617abe23221eb6c"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_stress_usage_predictor/vocab.txt",
|
||||
"size": 1080667,
|
||||
"git_blob": "211012a925fc51581997b6d3ef047bcda9a842f4"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_yo_homograph_resolver/config.json",
|
||||
"size": 625,
|
||||
"git_blob": "6615961d500dfe1fbbf302d1bbd4832b7e27f398"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_yo_homograph_resolver/model.onnx",
|
||||
"size": 14332169,
|
||||
"sha256": "42cc85bf0c4b319dfe3d89fa17b162a92fd5e1c651a657cb3d5f44978d4e70ac"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_yo_homograph_resolver/special_tokens_map.json",
|
||||
"size": 125,
|
||||
"git_blob": "a8b3208c2884c4efb86e49300fdd3dc877220cdf"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_yo_homograph_resolver/tokenizer.json",
|
||||
"size": 126952,
|
||||
"git_blob": "bbab1a4bc4dfa68697338718621489e77d03e70a"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_yo_homograph_resolver/tokenizer_config.json",
|
||||
"size": 401,
|
||||
"git_blob": "7d8a1ab4371909b9dcaabfcf93b9bbd56045605a"
|
||||
},
|
||||
{
|
||||
"path": "nn/nn_yo_homograph_resolver/vocab.txt",
|
||||
"size": 48576,
|
||||
"git_blob": "32bc54d43a46178e70dcefb85626f52e9ddbc98b"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
certifi==2026.7.22
|
||||
charset-normalizer==3.5.1
|
||||
filelock==4.0.4
|
||||
flatbuffers==25.12.19
|
||||
fsspec==2026.9.0
|
||||
hf-xet==1.6.0
|
||||
huggingface-hub==0.36.2
|
||||
idna==3.20
|
||||
numpy==2.5.3
|
||||
onnxruntime==1.30.0
|
||||
packaging==26.3
|
||||
protobuf==7.36.2
|
||||
python-crfsuite==0.9.12
|
||||
pyyaml==6.0.3
|
||||
razdel==0.5.0
|
||||
regex==2026.9.10
|
||||
requests==2.34.2
|
||||
ruaccent==1.5.8.3
|
||||
safetensors==0.8.0
|
||||
sentencepiece==0.2.2
|
||||
tokenizers==0.22.2
|
||||
tqdm==4.70.1
|
||||
transformers==4.57.6
|
||||
typing-extensions==4.16.0
|
||||
urllib3==2.8.0
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Workspace-local RuAccent CPU setup. No sudo or changes to system Python."""
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import runpy
|
||||
import shutil
|
||||
import subprocess
|
||||
import urllib.request
|
||||
import zipfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def main():
|
||||
uv = shutil.which("uv") or str(ROOT / ".local/tools/uv")
|
||||
if not Path(uv).is_file():
|
||||
if platform.system() != "Linux" or platform.machine() != "x86_64":
|
||||
raise SystemExit("Install uv first on this platform, then rerun this setup script.")
|
||||
with urllib.request.urlopen("https://pypi.org/pypi/uv/0.12.19/json", timeout=60) as source:
|
||||
metadata = json.load(source)
|
||||
wheel = next(x for x in metadata["urls"] if x["filename"].endswith("manylinux_2_17_x86_64.manylinux2014_x86_64.whl"))
|
||||
with urllib.request.urlopen(wheel["url"], timeout=60) as source:
|
||||
content = source.read()
|
||||
expected = "a63d18a0aa38ee9f21a5406afbbaeb41303bcd954be9d6b7c1b95ac275e53958"
|
||||
if hashlib.sha256(content).hexdigest() != expected:
|
||||
raise RuntimeError("uv checksum mismatch")
|
||||
with zipfile.ZipFile(io.BytesIO(content)) as archive:
|
||||
name = next(x for x in archive.namelist() if x.endswith("/uv"))
|
||||
Path(uv).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(uv).write_bytes(archive.read(name))
|
||||
Path(uv).chmod(0o755)
|
||||
environment = dict(os.environ, UV_CACHE_DIR=str(ROOT / ".local/uv-cache"),
|
||||
UV_PYTHON_INSTALL_DIR=str(ROOT / ".local/python"), UV_SYSTEM_CERTS="true")
|
||||
venv = ROOT / ".local/ruaccent-runtime"
|
||||
python = venv / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
|
||||
if not python.is_file():
|
||||
subprocess.run([uv, "venv", "--python", "3.12.14", str(venv)], env=environment, check=True)
|
||||
subprocess.run([uv, "pip", "sync", "--python", str(python), str(ROOT / "scripts/ruaccent-requirements.txt")], env=environment, check=True)
|
||||
model = ROOT / ".local/tts/ruaccent"
|
||||
manifest = json.loads((ROOT / "scripts/ruaccent-model.json").read_text())
|
||||
runpy.run_path(str(ROOT / "scripts/download-tts-model.py"))["download"](manifest, model)
|
||||
# RUAccent 1.5.8.3 expects its rule-engine files beside the installed package.
|
||||
module_path = subprocess.check_output([str(python), "-c", "import importlib.util; print(importlib.util.find_spec('ruaccent').submodule_search_locations[0])"], text=True).strip()
|
||||
shutil.copytree(model / "koziev", Path(module_path) / "koziev", dirs_exist_ok=True)
|
||||
worker = ROOT / "composeApp/src/jvmMain/resources/tts/ruaccent-worker.py"
|
||||
shutil.copyfile(worker, model / "worker.py")
|
||||
# Verify a wholly offline initialization before declaring the installation ready.
|
||||
subprocess.run([str(python), str(model / "worker.py"), str(model), "--check"], check=True, env=environment)
|
||||
(model / "runtime.json").write_text(json.dumps({"python": str(python), "model": "turbo3.1", "revision": manifest["revision"]}, indent=2) + "\n")
|
||||
print("RuAccent ready: CPU, turbo3.1, full dictionary; no network during inference.", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,10 @@
|
||||
filelock==3.32.3
|
||||
fsspec==2026.7.0
|
||||
jinja2==3.1.6
|
||||
markupsafe==3.0.3
|
||||
mpmath==1.3.0
|
||||
networkx==3.6.1
|
||||
setuptools==78.1.0
|
||||
sympy==1.14.0
|
||||
torch==2.8.0+cpu
|
||||
typing-extensions==4.16.0
|
||||
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Audition official Silero v5_5_ru locally on CPU, without changing the reader."""
|
||||
import argparse
|
||||
import array
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
import wave
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
||||
os.environ.setdefault("TORCH_HOME", str(ROOT / ".local/torch"))
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--voices", nargs="+", default=["xenia", "baya", "aidar"])
|
||||
args = parser.parse_args()
|
||||
import torch
|
||||
|
||||
torch.set_num_threads(4)
|
||||
torch.set_num_interop_threads(1)
|
||||
torch.manual_seed(42)
|
||||
model_path = ROOT / ".local/tts/silero-v5_5-ru/model.pt"
|
||||
output = ROOT / "composeApp/build/tts-samples"
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
start = time.perf_counter()
|
||||
model = torch.package.PackageImporter(str(model_path)).load_pickle("tts_models", "model")
|
||||
model.to(torch.device("cpu"))
|
||||
loaded = time.perf_counter() - start
|
||||
print(f"Silero v5_5_ru, CPU, 4 threads; load {loaded:.3f}s; voices {model.speakers}", flush=True)
|
||||
samples = {
|
||||
"ru": "В библиотеке было тихо. Она открыла книгу и начала читать. За окном шёл осенний дождь. На двери висел старый замок, а вдали виднелся замок на холме. Завтра, подумала она, мы наконец доберёмся до моря.",
|
||||
"ru-yo": "Елка росла у дома. Ежик шел по лесу. Ребенок пьет теплое молоко. Все уже пришли. Он прочитал все. Она все еще ждет. Он узнает об этом завтра. Он узнает ее по голосу.",
|
||||
}
|
||||
report = {"model": "v5_5_ru", "sha256": hashlib.sha256(model_path.read_bytes()).hexdigest(),
|
||||
"torch": torch.__version__, "device": "cpu", "threads": 4, "load_seconds": loaded,
|
||||
"put_accent": True, "put_yo": True, "put_stress_homo": True, "put_yo_homo": True, "samples": []}
|
||||
for voice in args.voices:
|
||||
for label, text in samples.items():
|
||||
start = time.perf_counter()
|
||||
with torch.inference_mode():
|
||||
audio = model.apply_tts(text=text, speaker=voice, sample_rate=48000,
|
||||
put_accent=True, put_yo=True,
|
||||
put_stress_homo=True, put_yo_homo=True).detach().cpu().flatten()
|
||||
elapsed = time.perf_counter() - start
|
||||
assert audio.numel() and torch.isfinite(audio).all() and audio.abs().max() > 0.001
|
||||
duration = audio.numel() / 48000
|
||||
pcm = array.array("h", (audio.clamp(-1, 1) * 32767).round().to(torch.int16).tolist())
|
||||
if sys.byteorder != "little":
|
||||
pcm.byteswap()
|
||||
path = output / f"silero-{label}-{voice}.wav"
|
||||
with wave.open(str(path), "wb") as wav:
|
||||
wav.setparams((1, 2, 48000, 0, "NONE", "not compressed"))
|
||||
wav.writeframes(pcm.tobytes())
|
||||
path.with_suffix(".txt").write_text(text + "\n")
|
||||
report["samples"].append({"file": path.name, "synthesis_seconds": elapsed,
|
||||
"audio_seconds": duration, "rtf": elapsed / duration})
|
||||
print(f"{path.name}: {elapsed:.3f}s synthesis, {duration:.3f}s audio, RTF {elapsed / duration:.3f}", flush=True)
|
||||
(output / "silero-benchmark.json").write_text(json.dumps(report, indent=2) + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,91 @@
|
||||
{
|
||||
"repository": "supertone-oss-archive/supertonic-3",
|
||||
"revision": "aafc6e32416a594460b32413efc49d7fe4ce6d46",
|
||||
"files": [
|
||||
{
|
||||
"path": "LICENSE",
|
||||
"size": 15007,
|
||||
"git_blob": "1e2cbe6cef94c8cdf3ed1fcebc0f5317ca7ad5a1"
|
||||
},
|
||||
{
|
||||
"path": "onnx/duration_predictor.onnx",
|
||||
"size": 3700147,
|
||||
"sha256": "c3eb91414d5ff8a7a239b7fe9e34e7e2bf8a8140d8375ffb14718b1c639325db"
|
||||
},
|
||||
{
|
||||
"path": "onnx/text_encoder.onnx",
|
||||
"size": 36416150,
|
||||
"sha256": "c7befd5ea8c3119769e8a6c1486c4edc6a3bc8365c67621c881bbb774b9902ff"
|
||||
},
|
||||
{
|
||||
"path": "onnx/tts.json",
|
||||
"size": 8253,
|
||||
"git_blob": "28575b0793c92607a0f0a292550df9bd17709a79"
|
||||
},
|
||||
{
|
||||
"path": "onnx/unicode_indexer.json",
|
||||
"size": 277676,
|
||||
"git_blob": "3aaf4d33024aff0be455633c24b3636b3a810150"
|
||||
},
|
||||
{
|
||||
"path": "onnx/vector_estimator.onnx",
|
||||
"size": 256534781,
|
||||
"sha256": "883ac868ea0275ef0e991524dc64f16b3c0376efd7c320af6b53f5b780d7c61c"
|
||||
},
|
||||
{
|
||||
"path": "onnx/vocoder.onnx",
|
||||
"size": 101424195,
|
||||
"sha256": "085de76dd8e8d5836d6ca66826601f615939218f90e519f70ee8a36ed2a4c4ba"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/F1.json",
|
||||
"size": 292046,
|
||||
"git_blob": "421365f307ed1535d8da16845031d0e9fa3e60c5"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/F2.json",
|
||||
"size": 292423,
|
||||
"git_blob": "b09a3a433df8a4856d1d7d64736a56ddf37a4ea3"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/F3.json",
|
||||
"size": 290794,
|
||||
"git_blob": "a366f94c77c4b440404071f1a52bed3ccf83a5ff"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/F4.json",
|
||||
"size": 291808,
|
||||
"git_blob": "39c78a52795320c6101b7eaf984c0e2352fbdc18"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/F5.json",
|
||||
"size": 291479,
|
||||
"git_blob": "06983f8816ad00a8eeb7efd37a33d457587e16f8"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/M1.json",
|
||||
"size": 291748,
|
||||
"git_blob": "bddfff85a7c4c140c11620bd7005d86681dccdac"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/M2.json",
|
||||
"size": 292055,
|
||||
"git_blob": "602ff6c4d6d48cdb71c37461f7c175d71fe8b34b"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/M3.json",
|
||||
"size": 290198,
|
||||
"git_blob": "000b60436b77619b933fdc33628ad737595e6794"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/M4.json",
|
||||
"size": 291522,
|
||||
"git_blob": "aae253ae81185fa06a6469c3f1cdff04ed121ddf"
|
||||
},
|
||||
{
|
||||
"path": "voice_styles/M5.json",
|
||||
"size": 291469,
|
||||
"git_blob": "0536b252698830f8536c077edcb94e1ce4768ab6"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -15,7 +15,9 @@ data class Fb2Book(
|
||||
val documentInfo: Fb2DocumentInfo = Fb2DocumentInfo(),
|
||||
val bodyTitle: List<String> = emptyList(),
|
||||
val bodyImages: List<Fb2ImageRef> = emptyList(),
|
||||
val bodyEpigraphs: List<Fb2Epigraph> = emptyList(),
|
||||
val sections: List<Fb2Section> = emptyList(),
|
||||
val notes: Map<String, Fb2Section> = emptyMap(),
|
||||
val binaries: List<Fb2Binary> = emptyList(),
|
||||
) {
|
||||
fun binaryFor(image: Fb2ImageRef): Fb2Binary? =
|
||||
@@ -46,6 +48,7 @@ data class Fb2DocumentInfo(
|
||||
)
|
||||
|
||||
data class Fb2Section(
|
||||
val id: String? = null,
|
||||
val title: String? = null,
|
||||
val paragraphs: List<String> = emptyList(),
|
||||
val images: List<Fb2ImageRef> = emptyList(),
|
||||
@@ -69,10 +72,50 @@ data class Fb2Binary(
|
||||
sealed interface Fb2Block {
|
||||
data class Paragraph(val content: Fb2Text) : Fb2Block
|
||||
data class Subtitle(val content: Fb2Text) : Fb2Block
|
||||
data class Epigraph(val epigraph: Fb2Epigraph) : Fb2Block
|
||||
data class Cite(val cite: Fb2Cite) : Fb2Block
|
||||
data class Poem(val poem: Fb2Poem) : Fb2Block
|
||||
data class Image(val image: Fb2ImageRef) : Fb2Block
|
||||
data object EmptyLine : Fb2Block
|
||||
}
|
||||
|
||||
data class Fb2Epigraph(
|
||||
val blocks: List<Fb2EpigraphBlock> = emptyList(),
|
||||
val textAuthors: List<Fb2Text> = emptyList(),
|
||||
)
|
||||
|
||||
data class Fb2Cite(
|
||||
val blocks: List<Fb2EpigraphBlock> = emptyList(),
|
||||
val textAuthors: List<Fb2Text> = emptyList(),
|
||||
)
|
||||
|
||||
sealed interface Fb2EpigraphBlock {
|
||||
data class Paragraph(val content: Fb2Text) : Fb2EpigraphBlock
|
||||
data class Subtitle(val content: Fb2Text) : Fb2EpigraphBlock
|
||||
data class Poem(val poem: Fb2Poem) : Fb2EpigraphBlock
|
||||
data class Cite(val cite: Fb2Cite) : Fb2EpigraphBlock
|
||||
data object EmptyLine : Fb2EpigraphBlock
|
||||
}
|
||||
|
||||
data class Fb2Poem(
|
||||
val title: List<Fb2Text> = emptyList(),
|
||||
val epigraphs: List<Fb2Epigraph> = emptyList(),
|
||||
val blocks: List<Fb2PoemBlock> = emptyList(),
|
||||
val textAuthors: List<Fb2Text> = emptyList(),
|
||||
val date: String? = null,
|
||||
)
|
||||
|
||||
sealed interface Fb2PoemBlock {
|
||||
data class Subtitle(val content: Fb2Text) : Fb2PoemBlock
|
||||
data class Stanza(val stanza: Fb2Stanza) : Fb2PoemBlock
|
||||
}
|
||||
|
||||
data class Fb2Stanza(
|
||||
val title: List<Fb2Text> = emptyList(),
|
||||
val subtitle: Fb2Text? = null,
|
||||
val verses: List<Fb2Text> = emptyList(),
|
||||
)
|
||||
|
||||
data class Fb2Text(
|
||||
val spans: List<Fb2TextSpan>,
|
||||
) {
|
||||
@@ -83,6 +126,8 @@ data class Fb2Text(
|
||||
data class Fb2TextSpan(
|
||||
val text: String,
|
||||
val styles: Set<Fb2TextStyle> = emptySet(),
|
||||
val href: String? = null,
|
||||
val linkType: String? = null,
|
||||
)
|
||||
|
||||
enum class Fb2TextStyle {
|
||||
|
||||
@@ -6,6 +6,7 @@ internal object Fb2XmlEncoding {
|
||||
fun decodeXml(bytes: ByteArray): String =
|
||||
when (declaredEncoding(bytes)?.lowercase()) {
|
||||
"windows-1251" -> decodeWindows1251(bytes)
|
||||
"windows-1252" -> decodeWindows1252(bytes)
|
||||
else -> bytes.decodeToString()
|
||||
}
|
||||
|
||||
@@ -30,6 +31,12 @@ internal object Fb2XmlEncoding {
|
||||
}
|
||||
}
|
||||
|
||||
private fun decodeWindows1252(bytes: ByteArray): String = buildString(bytes.size) {
|
||||
bytes.forEach { byte ->
|
||||
append(windows1252Char(byte.toInt() and 0xff))
|
||||
}
|
||||
}
|
||||
|
||||
private fun windows1251Char(value: Int): Char =
|
||||
when (value) {
|
||||
in 0x00..0x7f -> value.toChar()
|
||||
@@ -100,4 +107,43 @@ internal object Fb2XmlEncoding {
|
||||
in 0xc0..0xff -> (0x0410 + value - 0xc0).toChar()
|
||||
else -> '\uFFFD'
|
||||
}
|
||||
|
||||
private fun windows1252Char(value: Int): Char =
|
||||
when (value) {
|
||||
in 0x00..0x7f -> value.toChar()
|
||||
0x80 -> '\u20ac'
|
||||
0x81 -> '\uFFFD'
|
||||
0x82 -> '\u201a'
|
||||
0x83 -> '\u0192'
|
||||
0x84 -> '\u201e'
|
||||
0x85 -> '\u2026'
|
||||
0x86 -> '\u2020'
|
||||
0x87 -> '\u2021'
|
||||
0x88 -> '\u02c6'
|
||||
0x89 -> '\u2030'
|
||||
0x8a -> '\u0160'
|
||||
0x8b -> '\u2039'
|
||||
0x8c -> '\u0152'
|
||||
0x8d -> '\uFFFD'
|
||||
0x8e -> '\u017d'
|
||||
0x8f -> '\uFFFD'
|
||||
0x90 -> '\uFFFD'
|
||||
0x91 -> '\u2018'
|
||||
0x92 -> '\u2019'
|
||||
0x93 -> '\u201c'
|
||||
0x94 -> '\u201d'
|
||||
0x95 -> '\u2022'
|
||||
0x96 -> '\u2013'
|
||||
0x97 -> '\u2014'
|
||||
0x98 -> '\u02dc'
|
||||
0x99 -> '\u2122'
|
||||
0x9a -> '\u0161'
|
||||
0x9b -> '\u203a'
|
||||
0x9c -> '\u0153'
|
||||
0x9d -> '\uFFFD'
|
||||
0x9e -> '\u017e'
|
||||
0x9f -> '\u0178'
|
||||
in 0xa0..0xff -> value.toChar()
|
||||
else -> '\uFFFD'
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,15 @@ internal object Fb2XmlMapper {
|
||||
?: throw Fb2ParseException("FB2 document is missing description/title-info")
|
||||
val documentInfo = description.first("document-info")
|
||||
|
||||
val body = root.children("body").firstOrNull { it.attributes["name"] != "notes" }
|
||||
val bodies = root.children("body")
|
||||
val body = bodies.firstOrNull { it.attributes["name"] != "notes" }
|
||||
?: root.first("body")
|
||||
val notes = bodies
|
||||
.filter { it.attributes["name"] == "notes" }
|
||||
.flatMap { it.children("section") }
|
||||
.map(::sectionFrom)
|
||||
.mapNotNull { section -> section.id?.let { it to section } }
|
||||
.toMap()
|
||||
|
||||
return Fb2Book(
|
||||
title = titleInfo.firstText("book-title")
|
||||
@@ -32,7 +39,9 @@ internal object Fb2XmlMapper {
|
||||
documentInfo = documentInfoFrom(documentInfo),
|
||||
bodyTitle = body?.first("title")?.children("p")?.mapNotNull { it.text().ifBlank { null } }.orEmpty(),
|
||||
bodyImages = body?.children("image")?.mapNotNull(::imageRefFrom).orEmpty(),
|
||||
bodyEpigraphs = body?.children("epigraph")?.map(::epigraphFrom).orEmpty(),
|
||||
sections = body?.children("section")?.map(::sectionFrom).orEmpty(),
|
||||
notes = notes,
|
||||
binaries = root.children("binary").mapNotNull(::binaryFrom),
|
||||
)
|
||||
}
|
||||
@@ -124,6 +133,9 @@ internal object Fb2XmlMapper {
|
||||
when (child.localName) {
|
||||
"p" -> Fb2Block.Paragraph(textFrom(child))
|
||||
"subtitle" -> Fb2Block.Subtitle(textFrom(child))
|
||||
"epigraph" -> Fb2Block.Epigraph(epigraphFrom(child))
|
||||
"cite" -> Fb2Block.Cite(citeFrom(child))
|
||||
"poem" -> Fb2Block.Poem(poemFrom(child))
|
||||
"image" -> imageRefFrom(child)?.let(Fb2Block::Image)
|
||||
"empty-line" -> Fb2Block.EmptyLine
|
||||
else -> null
|
||||
@@ -131,6 +143,7 @@ internal object Fb2XmlMapper {
|
||||
}
|
||||
|
||||
return Fb2Section(
|
||||
id = element.attributes["id"]?.takeIf { it.isNotBlank() },
|
||||
title = element.first("title")?.text()?.ifBlank { null },
|
||||
paragraphs = blocks.filterIsInstance<Fb2Block.Paragraph>()
|
||||
.mapNotNull { it.content.plainText.ifBlank { null } },
|
||||
@@ -140,14 +153,82 @@ internal object Fb2XmlMapper {
|
||||
)
|
||||
}
|
||||
|
||||
private fun textFrom(element: XmlElement): Fb2Text =
|
||||
Fb2Text(spansFrom(element.nodes).mergeAdjacent())
|
||||
private fun poemFrom(element: XmlElement): Fb2Poem =
|
||||
Fb2Poem(
|
||||
title = titleFrom(element.first("title")),
|
||||
epigraphs = element.children("epigraph").map(::epigraphFrom),
|
||||
blocks = element.nodes.mapNotNull { node ->
|
||||
val child = (node as? XmlNode.ElementNode)?.element ?: return@mapNotNull null
|
||||
when (child.localName) {
|
||||
"subtitle" -> Fb2PoemBlock.Subtitle(textFrom(child))
|
||||
"stanza" -> Fb2PoemBlock.Stanza(stanzaFrom(child))
|
||||
else -> null
|
||||
}
|
||||
},
|
||||
textAuthors = element.children("text-author").map(::textFrom),
|
||||
date = element.first("date")?.text()?.ifBlank { null },
|
||||
)
|
||||
|
||||
private fun spansFrom(nodes: List<XmlNode>, styles: Set<Fb2TextStyle> = emptySet()): List<Fb2TextSpan> =
|
||||
private fun epigraphFrom(element: XmlElement): Fb2Epigraph =
|
||||
Fb2Epigraph(
|
||||
blocks = element.nodes.mapNotNull { node ->
|
||||
val child = (node as? XmlNode.ElementNode)?.element ?: return@mapNotNull null
|
||||
when (child.localName) {
|
||||
"p" -> Fb2EpigraphBlock.Paragraph(textFrom(child))
|
||||
"poem" -> Fb2EpigraphBlock.Poem(poemFrom(child))
|
||||
"cite" -> Fb2EpigraphBlock.Cite(citeFrom(child))
|
||||
"empty-line" -> Fb2EpigraphBlock.EmptyLine
|
||||
else -> null
|
||||
}
|
||||
},
|
||||
textAuthors = element.children("text-author").map(::textFrom),
|
||||
)
|
||||
|
||||
private fun citeFrom(element: XmlElement): Fb2Cite =
|
||||
Fb2Cite(
|
||||
blocks = element.nodes.mapNotNull { node ->
|
||||
val child = (node as? XmlNode.ElementNode)?.element ?: return@mapNotNull null
|
||||
when (child.localName) {
|
||||
"p" -> Fb2EpigraphBlock.Paragraph(textFrom(child))
|
||||
"subtitle" -> Fb2EpigraphBlock.Subtitle(textFrom(child))
|
||||
"poem" -> Fb2EpigraphBlock.Poem(poemFrom(child))
|
||||
"empty-line" -> Fb2EpigraphBlock.EmptyLine
|
||||
else -> null
|
||||
}
|
||||
},
|
||||
textAuthors = element.children("text-author").map(::textFrom),
|
||||
)
|
||||
|
||||
private fun stanzaFrom(element: XmlElement): Fb2Stanza =
|
||||
Fb2Stanza(
|
||||
title = titleFrom(element.first("title")),
|
||||
subtitle = element.first("subtitle")?.let(::textFrom),
|
||||
verses = element.children("v").map(::textFrom),
|
||||
)
|
||||
|
||||
private fun titleFrom(element: XmlElement?): List<Fb2Text> =
|
||||
element?.children("p")?.map(::textFrom).orEmpty()
|
||||
|
||||
private fun textFrom(element: XmlElement): Fb2Text =
|
||||
Fb2Text(spansFrom(element.nodes).mergeAdjacent().trimBoundaryWhitespace())
|
||||
|
||||
private fun spansFrom(
|
||||
nodes: List<XmlNode>,
|
||||
styles: Set<Fb2TextStyle> = emptySet(),
|
||||
href: String? = null,
|
||||
linkType: String? = null,
|
||||
): List<Fb2TextSpan> =
|
||||
nodes.flatMap { node ->
|
||||
when (node) {
|
||||
is XmlNode.TextNode -> listOfNotNull(
|
||||
node.text.takeIf { it.isNotEmpty() }?.let { Fb2TextSpan(it, styles) }
|
||||
node.text.takeIf { it.isNotEmpty() }?.let {
|
||||
Fb2TextSpan(
|
||||
text = it,
|
||||
styles = styles,
|
||||
href = href,
|
||||
linkType = linkType,
|
||||
)
|
||||
}
|
||||
)
|
||||
is XmlNode.ElementNode -> {
|
||||
val element = node.element
|
||||
@@ -160,7 +241,13 @@ internal object Fb2XmlMapper {
|
||||
"sub" -> styles + Fb2TextStyle.Subscript
|
||||
else -> styles
|
||||
}
|
||||
spansFrom(element.nodes, nestedStyles)
|
||||
val nestedHref = element.attributeLocal("href")
|
||||
?.takeIf { element.localName == "a" && it.isNotBlank() }
|
||||
?: href
|
||||
val nestedLinkType = element.attributes["type"]
|
||||
?.takeIf { element.localName == "a" && it.isNotBlank() }
|
||||
?: linkType
|
||||
spansFrom(element.nodes, nestedStyles, nestedHref, nestedLinkType)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,7 +257,11 @@ internal object Fb2XmlMapper {
|
||||
val merged = mutableListOf<Fb2TextSpan>()
|
||||
forEach { span ->
|
||||
val previous = merged.lastOrNull()
|
||||
if (previous != null && previous.styles == span.styles) {
|
||||
if (previous != null &&
|
||||
previous.styles == span.styles &&
|
||||
previous.href == span.href &&
|
||||
previous.linkType == span.linkType
|
||||
) {
|
||||
merged[merged.lastIndex] = previous.copy(text = previous.text + span.text)
|
||||
} else {
|
||||
merged += span
|
||||
@@ -179,6 +270,14 @@ internal object Fb2XmlMapper {
|
||||
return merged
|
||||
}
|
||||
|
||||
private fun List<Fb2TextSpan>.trimBoundaryWhitespace(): List<Fb2TextSpan> {
|
||||
if (isEmpty()) return emptyList()
|
||||
val trimmed = toMutableList()
|
||||
trimmed[0] = trimmed[0].copy(text = trimmed[0].text.trimStart())
|
||||
trimmed[trimmed.lastIndex] = trimmed.last().copy(text = trimmed.last().text.trimEnd())
|
||||
return trimmed.filter { it.text.isNotEmpty() }
|
||||
}
|
||||
|
||||
private fun StringBuilder.appendSection(section: Fb2Section) {
|
||||
append("<section>")
|
||||
section.title?.takeIf { it.isNotBlank() }?.let {
|
||||
|
||||
@@ -60,6 +60,23 @@ class Fb2FormatTest {
|
||||
assertEquals("Привет, мир.", book.sections.single().paragraphs.single())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesWindows1252PlainXml() {
|
||||
val book = Fb2Format.parse(windows1252Xml.encodeWindows1252(), "legacy.fb2")
|
||||
|
||||
assertEquals("The “Test” Book", book.title)
|
||||
assertEquals("It’s 25€ for café — OK.", book.sections.single().paragraphs.single())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesWindows1252StoredZip() {
|
||||
val zip = Fb2Zip.createStoredZip("legacy.fb2", windows1252Xml.encodeWindows1252())
|
||||
val book = Fb2Format.parse(zip, "legacy.fb2.zip")
|
||||
|
||||
assertEquals("The “Test” Book", book.title)
|
||||
assertEquals("It’s 25€ for café — OK.", book.sections.single().paragraphs.single())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fallsBackToUtf8ForUnknownEncoding() {
|
||||
val xml = sampleXml.replace("encoding=\"UTF-8\"", "encoding=\"KOI8-R\"")
|
||||
@@ -81,6 +98,53 @@ class Fb2FormatTest {
|
||||
assertEquals("pic.png", (section.blocks[3] as Fb2Block.Image).image.binaryId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesPoemsWithStanzasVersesAndInlineStyles() {
|
||||
val book = Fb2Format.parseXml(poemXml)
|
||||
val poem = (book.sections.single().blocks.single() as Fb2Block.Poem).poem
|
||||
|
||||
assertEquals("Song", poem.title.single().plainText)
|
||||
val stanza = (poem.blocks.single() as Fb2PoemBlock.Stanza).stanza
|
||||
assertEquals("First stanza", stanza.title.single().plainText)
|
||||
assertEquals("Softly", stanza.subtitle?.plainText)
|
||||
assertEquals(listOf("Line one", "Line two."), stanza.verses.map { it.plainText })
|
||||
assertEquals(setOf(Fb2TextStyle.Emphasis), stanza.verses[0].spans.single().styles)
|
||||
assertEquals("Poet", poem.textAuthors.single().plainText)
|
||||
assertEquals("1910", poem.date)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesBodySectionAndPoemEpigraphs() {
|
||||
val book = Fb2Format.parseXml(epigraphXml)
|
||||
|
||||
assertEquals("Body quote", (book.bodyEpigraphs.single().blocks.single() as Fb2EpigraphBlock.Paragraph).content.plainText)
|
||||
|
||||
val sectionBlocks = book.sections.single().blocks
|
||||
val sectionEpigraph = (sectionBlocks[0] as Fb2Block.Epigraph).epigraph
|
||||
assertEquals("Section quote", (sectionEpigraph.blocks[0] as Fb2EpigraphBlock.Paragraph).content.plainText)
|
||||
assertEquals("Author", sectionEpigraph.textAuthors.single().plainText)
|
||||
|
||||
val cite = (sectionEpigraph.blocks[1] as Fb2EpigraphBlock.Cite).cite
|
||||
assertEquals("Cited line", (cite.blocks.single() as Fb2EpigraphBlock.Paragraph).content.plainText)
|
||||
assertEquals("Cited author", cite.textAuthors.single().plainText)
|
||||
|
||||
val poem = (sectionBlocks[1] as Fb2Block.Poem).poem
|
||||
val poemEpigraph = poem.epigraphs.single()
|
||||
assertEquals("Poem quote", (poemEpigraph.blocks.single() as Fb2EpigraphBlock.Paragraph).content.plainText)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsesNoteLinksAndNotesBody() {
|
||||
val book = Fb2Format.parseXml(notesXml)
|
||||
val paragraph = book.sections.single().blocks.single() as Fb2Block.Paragraph
|
||||
val link = paragraph.content.spans.single { it.href != null }
|
||||
|
||||
assertEquals("[1]", link.text)
|
||||
assertEquals("#n_1", link.href)
|
||||
assertEquals("note", link.linkType)
|
||||
assertEquals("Note text.", (book.notes.getValue("n_1").blocks.single() as Fb2Block.Paragraph).content.plainText)
|
||||
}
|
||||
|
||||
private val sampleXml = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
@@ -134,6 +198,78 @@ class Fb2FormatTest {
|
||||
</FictionBook>
|
||||
""".trimIndent()
|
||||
|
||||
private val poemXml = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<description>
|
||||
<title-info>
|
||||
<author><nickname>A</nickname></author>
|
||||
<book-title>Poetry</book-title>
|
||||
<lang>en</lang>
|
||||
</title-info>
|
||||
<document-info>
|
||||
<author><nickname>Toread</nickname></author>
|
||||
<date>2026-05-12</date>
|
||||
<id>poetry</id>
|
||||
<version>1.0</version>
|
||||
</document-info>
|
||||
</description>
|
||||
<body>
|
||||
<section>
|
||||
<poem>
|
||||
<title><p>Song</p></title>
|
||||
<stanza>
|
||||
<title><p>First stanza</p></title>
|
||||
<subtitle>Softly</subtitle>
|
||||
<v>
|
||||
<emphasis>Line one</emphasis>
|
||||
</v>
|
||||
<v>Line two.</v>
|
||||
</stanza>
|
||||
<text-author>Poet</text-author>
|
||||
<date>1910</date>
|
||||
</poem>
|
||||
</section>
|
||||
</body>
|
||||
</FictionBook>
|
||||
""".trimIndent()
|
||||
|
||||
private val epigraphXml = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<description>
|
||||
<title-info>
|
||||
<author><nickname>A</nickname></author>
|
||||
<book-title>Epigraphs</book-title>
|
||||
<lang>en</lang>
|
||||
</title-info>
|
||||
<document-info>
|
||||
<author><nickname>Toread</nickname></author>
|
||||
<date>2026-05-12</date>
|
||||
<id>epigraphs</id>
|
||||
<version>1.0</version>
|
||||
</document-info>
|
||||
</description>
|
||||
<body>
|
||||
<epigraph><p>Body quote</p></epigraph>
|
||||
<section>
|
||||
<epigraph>
|
||||
<p>Section quote</p>
|
||||
<cite>
|
||||
<p>Cited line</p>
|
||||
<text-author>Cited author</text-author>
|
||||
</cite>
|
||||
<text-author>Author</text-author>
|
||||
</epigraph>
|
||||
<poem>
|
||||
<epigraph><p>Poem quote</p></epigraph>
|
||||
<stanza><v>Line.</v></stanza>
|
||||
</poem>
|
||||
</section>
|
||||
</body>
|
||||
</FictionBook>
|
||||
""".trimIndent()
|
||||
|
||||
private val windows1251Xml = """
|
||||
<?xml version="1.0" encoding="windows-1251"?>
|
||||
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
@@ -154,6 +290,55 @@ class Fb2FormatTest {
|
||||
</FictionBook>
|
||||
""".trimIndent()
|
||||
|
||||
private val windows1252Xml = """
|
||||
<?xml version="1.0" encoding="windows-1252"?>
|
||||
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<description>
|
||||
<title-info>
|
||||
<author><nickname>Author</nickname></author>
|
||||
<book-title>The “Test” Book</book-title>
|
||||
<lang>en</lang>
|
||||
</title-info>
|
||||
<document-info>
|
||||
<author><nickname>Toread</nickname></author>
|
||||
<date>2026-05-12</date>
|
||||
<id>legacy-1252</id>
|
||||
<version>1.0</version>
|
||||
</document-info>
|
||||
</description>
|
||||
<body><section><p>It’s 25€ for café — OK.</p></section></body>
|
||||
</FictionBook>
|
||||
""".trimIndent()
|
||||
|
||||
private val notesXml = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">
|
||||
<description>
|
||||
<title-info>
|
||||
<author><nickname>A</nickname></author>
|
||||
<book-title>Notes</book-title>
|
||||
<lang>en</lang>
|
||||
</title-info>
|
||||
<document-info>
|
||||
<author><nickname>Toread</nickname></author>
|
||||
<date>2026-05-12</date>
|
||||
<id>notes</id>
|
||||
<version>1.0</version>
|
||||
</document-info>
|
||||
</description>
|
||||
<body>
|
||||
<section>
|
||||
<p>Main<a l:href="#n_1" type="note">[1]</a>.</p>
|
||||
</section>
|
||||
</body>
|
||||
<body name="notes">
|
||||
<section id="n_1">
|
||||
<p>Note text.</p>
|
||||
</section>
|
||||
</body>
|
||||
</FictionBook>
|
||||
""".trimIndent()
|
||||
|
||||
private fun String.encodeWindows1251(): ByteArray =
|
||||
ByteArray(length) { index ->
|
||||
val char = this[index]
|
||||
@@ -166,4 +351,23 @@ class Fb2FormatTest {
|
||||
}
|
||||
value.toByte()
|
||||
}
|
||||
|
||||
private fun String.encodeWindows1252(): ByteArray =
|
||||
ByteArray(length) { index ->
|
||||
val char = this[index]
|
||||
val value = when {
|
||||
char.code <= 0x7f -> char.code
|
||||
char.code in 0xa0..0xff -> char.code
|
||||
char == '€' -> 0x80
|
||||
char == '‘' -> 0x91
|
||||
char == '’' -> 0x92
|
||||
char == '“' -> 0x93
|
||||
char == '”' -> 0x94
|
||||
char == '–' -> 0x96
|
||||
char == '—' -> 0x97
|
||||
char == '™' -> 0x99
|
||||
else -> error("Test character $char is not mapped to windows-1252")
|
||||
}
|
||||
value.toByte()
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,12 @@ package net.sergeych.toread.storage.jdbc
|
||||
|
||||
import net.sergeych.toread.fb2.Fb2Block
|
||||
import net.sergeych.toread.fb2.Fb2Book
|
||||
import net.sergeych.toread.fb2.Fb2Cite
|
||||
import net.sergeych.toread.fb2.Fb2Epigraph
|
||||
import net.sergeych.toread.fb2.Fb2EpigraphBlock
|
||||
import net.sergeych.toread.fb2.Fb2Format
|
||||
import net.sergeych.toread.fb2.Fb2Poem
|
||||
import net.sergeych.toread.fb2.Fb2PoemBlock
|
||||
import net.sergeych.toread.fb2.Fb2Section
|
||||
import net.sergeych.toread.storage.BodyClusterRecord
|
||||
import net.sergeych.toread.storage.BookBodyRecord
|
||||
@@ -21,6 +26,11 @@ data class LibraryScanSummary(
|
||||
val currentFile: String? = null,
|
||||
)
|
||||
|
||||
data class LibraryImportResult(
|
||||
val fileId: String,
|
||||
val imported: Boolean,
|
||||
)
|
||||
|
||||
class LibraryScanner(
|
||||
private val database: H2LibraryDatabase,
|
||||
private val log: (String) -> Unit = {},
|
||||
@@ -80,17 +90,29 @@ class LibraryScanner(
|
||||
storageUri: String,
|
||||
sizeBytes: Long?,
|
||||
lastModifiedMillis: Long?,
|
||||
): Boolean {
|
||||
): Boolean =
|
||||
importExternalFileAndGetResult(bytes, displayName, storageUri, sizeBytes, lastModifiedMillis).imported
|
||||
|
||||
fun importExternalFileAndGetResult(
|
||||
bytes: ByteArray,
|
||||
displayName: String,
|
||||
storageUri: String,
|
||||
sizeBytes: Long?,
|
||||
lastModifiedMillis: Long?,
|
||||
): LibraryImportResult {
|
||||
val rawSha256 = bytes.sha256Hex()
|
||||
if (sizeBytes != null && database.files.findByOriginalFilenameSizeAndRawSha256(displayName, sizeBytes, rawSha256).isNotEmpty()) {
|
||||
return false
|
||||
if (sizeBytes != null) {
|
||||
database.files.findByOriginalFilenameSizeAndRawSha256(displayName, sizeBytes, rawSha256).firstOrNull()?.let {
|
||||
return LibraryImportResult(fileId = it.id, imported = false)
|
||||
}
|
||||
}
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
database.files.findPrimaryDuplicateTarget(bodyClusterId = null, bodyId = null, rawSha256 = rawSha256)?.let { duplicateTarget ->
|
||||
val fileId = "file-${UUID.randomUUID()}"
|
||||
database.files.upsert(
|
||||
BookFileRecord(
|
||||
id = "file-${UUID.randomUUID()}",
|
||||
id = fileId,
|
||||
bookId = duplicateTarget.bookId,
|
||||
bodyId = duplicateTarget.bodyId,
|
||||
bodyClusterId = duplicateTarget.bodyClusterId,
|
||||
@@ -109,7 +131,7 @@ class LibraryScanner(
|
||||
updatedAt = now,
|
||||
)
|
||||
)
|
||||
return true
|
||||
return LibraryImportResult(fileId = fileId, imported = true)
|
||||
}
|
||||
|
||||
val book = Fb2Format.parse(bytes, displayName)
|
||||
@@ -124,6 +146,7 @@ class LibraryScanner(
|
||||
database.files.findPrimaryDuplicateTarget(bodyClusterId = clusterId, bodyId = bodyId, rawSha256 = rawSha256)
|
||||
}
|
||||
val bookId = duplicateTarget?.bookId ?: "book-${UUID.randomUUID()}"
|
||||
val fileId = "file-${UUID.randomUUID()}"
|
||||
|
||||
database.transaction {
|
||||
if (duplicateTarget == null || duplicateTarget.bookId == null) {
|
||||
@@ -172,7 +195,7 @@ class LibraryScanner(
|
||||
}
|
||||
files.upsert(
|
||||
BookFileRecord(
|
||||
id = "file-${UUID.randomUUID()}",
|
||||
id = fileId,
|
||||
bookId = bookId,
|
||||
bodyId = bodyId,
|
||||
bodyClusterId = clusterId,
|
||||
@@ -192,7 +215,7 @@ class LibraryScanner(
|
||||
)
|
||||
)
|
||||
}
|
||||
return true
|
||||
return LibraryImportResult(fileId = fileId, imported = true)
|
||||
}
|
||||
|
||||
private fun importLinkedFile(file: File): Boolean {
|
||||
@@ -246,7 +269,7 @@ private fun String.bookMimeType(): String =
|
||||
if (endsWith(".zip", ignoreCase = true)) "application/zip" else "application/x-fictionbook+xml"
|
||||
|
||||
private fun Fb2Book.canonicalText(): String =
|
||||
sections.flatMap { it.textBlocks() }
|
||||
(bodyEpigraphs.flatMap { it.textBlocks() } + sections.flatMap { it.textBlocks() })
|
||||
.joinToString(separator = "\n") { it.normalizeForBodyHash() }
|
||||
.trim()
|
||||
|
||||
@@ -256,8 +279,11 @@ private fun Fb2Section.textBlocks(): List<String> {
|
||||
blocks.forEach { block ->
|
||||
when (block) {
|
||||
Fb2Block.EmptyLine -> Unit
|
||||
is Fb2Block.Cite -> addAll(block.cite.textBlocks())
|
||||
is Fb2Block.Epigraph -> addAll(block.epigraph.textBlocks())
|
||||
is Fb2Block.Image -> Unit
|
||||
is Fb2Block.Paragraph -> add(block.content.plainText)
|
||||
is Fb2Block.Poem -> addAll(block.poem.textBlocks())
|
||||
is Fb2Block.Subtitle -> add(block.content.plainText)
|
||||
}
|
||||
}
|
||||
@@ -266,6 +292,42 @@ private fun Fb2Section.textBlocks(): List<String> {
|
||||
return current + sections.flatMap { it.textBlocks() }
|
||||
}
|
||||
|
||||
private fun Fb2Poem.textBlocks(): List<String> = buildList {
|
||||
title.forEach { add(it.plainText) }
|
||||
epigraphs.forEach { addAll(it.textBlocks()) }
|
||||
blocks.forEach { block ->
|
||||
when (block) {
|
||||
is Fb2PoemBlock.Stanza -> {
|
||||
block.stanza.title.forEach { add(it.plainText) }
|
||||
block.stanza.subtitle?.let { add(it.plainText) }
|
||||
block.stanza.verses.forEach { add(it.plainText) }
|
||||
}
|
||||
is Fb2PoemBlock.Subtitle -> add(block.content.plainText)
|
||||
}
|
||||
}
|
||||
textAuthors.forEach { add(it.plainText) }
|
||||
date?.let { add(it) }
|
||||
}
|
||||
|
||||
private fun Fb2Epigraph.textBlocks(): List<String> = buildList {
|
||||
blocks.forEach { addAll(it.textBlocks()) }
|
||||
textAuthors.forEach { add(it.plainText) }
|
||||
}
|
||||
|
||||
private fun Fb2Cite.textBlocks(): List<String> = buildList {
|
||||
blocks.forEach { addAll(it.textBlocks()) }
|
||||
textAuthors.forEach { add(it.plainText) }
|
||||
}
|
||||
|
||||
private fun Fb2EpigraphBlock.textBlocks(): List<String> =
|
||||
when (this) {
|
||||
Fb2EpigraphBlock.EmptyLine -> emptyList()
|
||||
is Fb2EpigraphBlock.Cite -> cite.textBlocks()
|
||||
is Fb2EpigraphBlock.Paragraph -> listOf(content.plainText)
|
||||
is Fb2EpigraphBlock.Poem -> poem.textBlocks()
|
||||
is Fb2EpigraphBlock.Subtitle -> listOf(content.plainText)
|
||||
}
|
||||
|
||||
private fun String.normalizeForBodyHash(): String =
|
||||
lowercase()
|
||||
.replace('\u00ad'.toString(), "")
|
||||
|
||||
@@ -15,13 +15,14 @@ class LibraryScannerTest {
|
||||
val db = H2LibraryDatabase.openMemory("scansFb2ZipSubtreeIntoLinkedLibraryRecords")
|
||||
try {
|
||||
val summary = LibraryScanner(db).scanSubtree(File(root, "test_books"))
|
||||
val expectedFiles = LibraryScanner.countSupportedBookFiles(File(root, "test_books"))
|
||||
|
||||
assertEquals(1, summary.scannedFiles)
|
||||
assertEquals(1, summary.importedFiles)
|
||||
assertEquals(expectedFiles, summary.scannedFiles)
|
||||
assertEquals(expectedFiles, summary.importedFiles)
|
||||
assertEquals(0, summary.skippedFiles)
|
||||
assertEquals(0, summary.failedFiles)
|
||||
|
||||
val file = db.files.list().single()
|
||||
val file = db.files.list().first { it.storageUri?.endsWith(".fb2.zip") == true }
|
||||
assertEquals("fb2.zip", file.format)
|
||||
assertTrue(file.storageUri?.endsWith(".fb2.zip") == true)
|
||||
val book = assertNotNull(file.bookId?.let(db.books::get))
|
||||
@@ -31,7 +32,7 @@ class LibraryScannerTest {
|
||||
|
||||
val second = LibraryScanner(db).scanSubtree(File(root, "test_books"))
|
||||
assertEquals(0, second.importedFiles)
|
||||
assertEquals(1, second.skippedFiles)
|
||||
assertEquals(expectedFiles, second.skippedFiles)
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
@@ -92,6 +93,36 @@ class LibraryScannerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun importExternalFileResultReturnsExistingFileForExactRepeat() {
|
||||
val root = findProjectRoot()
|
||||
val bytes = File(root, "test_books/Maraini_Zapiski-Terezy-Numy.G7vc8A.872381.fb2.zip").readBytes()
|
||||
val db = H2LibraryDatabase.openMemory("importExternalFileResultReturnsExistingFileForExactRepeat")
|
||||
try {
|
||||
val scanner = LibraryScanner(db)
|
||||
val first = scanner.importExternalFileAndGetResult(
|
||||
bytes = bytes,
|
||||
displayName = "opened.fb2.zip",
|
||||
storageUri = "/tmp/opened.fb2.zip",
|
||||
sizeBytes = bytes.size.toLong(),
|
||||
lastModifiedMillis = 1L,
|
||||
)
|
||||
val second = scanner.importExternalFileAndGetResult(
|
||||
bytes = bytes,
|
||||
displayName = "opened.fb2.zip",
|
||||
storageUri = "/tmp/opened.fb2.zip",
|
||||
sizeBytes = bytes.size.toLong(),
|
||||
lastModifiedMillis = 1L,
|
||||
)
|
||||
|
||||
assertTrue(first.imported)
|
||||
assertEquals(false, second.imported)
|
||||
assertEquals(first.fileId, second.fileId)
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
|
||||
private fun findProjectRoot(): File {
|
||||
var current = File(System.getProperty("user.dir")).absoluteFile
|
||||
while (true) {
|
||||
|
||||