Skip to content

Commit

Permalink
release
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Mar 1, 2024
1 parent ef7364f commit 1434d72
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- automatic download and installation model
- batch change language
- model list reorder
- engine cache size
- number of threads per engine
2 changes: 1 addition & 1 deletion app/src/main/java/com/k2fsa/sherpa/onnx/Tts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fun getOfflineTtsConfig(
model = "$modelDir/$modelName",
lexicon = "$modelDir/$lexicon",
tokens = "$modelDir/tokens.txt",
dataDir = "$dataDir"
dataDir = dataDir
),
numThreads = 2,
debug = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ object TtsConfig {
val timeoutDestruction = mutableDataSaverStateOf(
dataSaverInterface = dataSaverPref,
key = "timeoutDestruction",
initialValue = 0
initialValue = 3
)

val cacheSize = mutableDataSaverStateOf(
dataSaverInterface = dataSaverPref,
key = "cacheSize",
initialValue = 3
)

val threadNum = mutableDataSaverStateOf(
dataSaverInterface = dataSaverPref,
key = "threadNum",
initialValue = 2
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ object ModelManager {
tokens = format(tokens),
dataDir = format(dataDir),
),
numThreads = 2,
debug = true,
provider = "cpu",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import java.util.concurrent.DelayQueue
import java.util.concurrent.Delayed
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import kotlin.math.max
import kotlin.math.min


interface ImplCache {
fun destroy()
}


class SynthesizerCache {
internal class SynthesizerCache {
companion object {
private val delayTime: Int
get() = 1000 * 60 * TtsConfig.timeoutDestruction.value
get() = 1000 * 60 * min(1, TtsConfig.timeoutDestruction.value)

private val maxCacheSize: Int
get() = max(1, TtsConfig.cacheSize.value)
}

private val delayQueue = DelayQueue<DelayedDestroyTask>()
Expand Down Expand Up @@ -44,15 +49,31 @@ class SynthesizerCache {
}
}

private fun limitSize() {
if (queueMap.size > maxCacheSize) {
val oldestEntry =
queueMap.entries.minByOrNull { it.value.getDelay(TimeUnit.MILLISECONDS) }
oldestEntry?.let {
queueMap.remove(it.key)
delayQueue.remove(it.value)
}
}
}

@Synchronized
fun cache(id: String, obj: ImplCache) {
val task =
DelayedDestroyTask(delayTime = delayTime, id, obj)
limitSize()

val task = DelayedDestroyTask(delayTime = delayTime, id, obj)
delayQueue.add(task)
queueMap[id] = task
ensureTaskRunning()
}

@Synchronized
fun getById(id: String): ImplCache? {
limitSize()

queueMap[id]?.let {
if (it.getDelay(TimeUnit.MILLISECONDS) <= 1000 * 10) { // 小于10s便重置
it.reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.k2fsa.sherpa.onnx.tts.engine.synthesizer
import android.util.Log
import com.k2fsa.sherpa.onnx.OfflineTts
import com.k2fsa.sherpa.onnx.OfflineTtsConfig
import com.k2fsa.sherpa.onnx.tts.engine.conf.TtsConfig
import kotlin.math.max

object SynthesizerManager {
const val TAG = "SynthesizerManager"
Expand All @@ -15,9 +17,11 @@ object SynthesizerManager {
Log.d(TAG, "getTTS (from cache): ${it.config}")
}

return tts ?: OfflineTts(
config = cfg
).run {
val model = cfg.model.copy(
numThreads = max(1, TtsConfig.threadNum.value)
)

return tts ?: OfflineTts(config = cfg.copy(model = model)).run {
cacheTTS(cfg.model.vits.model, this)
this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,41 @@ fun SettingsScreen() {
Scaffold { paddingValues ->
Column(Modifier.padding(paddingValues)) {
DividerPreference {
Text(stringResource(id = R.string.engine_cache))
Text(stringResource(id = R.string.engine))
}

var timeout by remember { TtsConfig.timeoutDestruction }
val timeoutStr = stringResource(id = R.string.minute_format, timeout)
SliderPreference(
valueRange = 1f..60f,
steps = 60,
title = { Text(stringResource(R.string.timeout_destruction)) },
subTitle = { Text(stringResource(R.string.timeout_destruction_summary))},
value = timeout.toFloat(),
onValueChange = { timeout = it.toInt() },
label = timeoutStr
)

var cacheSize by remember { TtsConfig.cacheSize }
val cacheSizeStr = cacheSize.toString()
SliderPreference(
valueRange = 1f..10f,
title = { Text(stringResource(R.string.cache_size)) },
subTitle = { Text(stringResource(R.string.cache_size_summary))},
value = cacheSize.toFloat(),
onValueChange = { cacheSize = it.toInt() },
label = cacheSizeStr
)

var threadNum by remember { TtsConfig.threadNum }
val threadNumStr = threadNum.toString()
SliderPreference(
valueRange = 1f..8f,
title = { Text(stringResource(R.string.thread_num)) },
subTitle = { Text(stringResource(R.string.thread_num_summary))},
value = threadNum.toFloat(),
onValueChange = { threadNum = it.toInt() },
label = threadNumStr
)
}
}
}
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<string name="desc_seekbar_remove">Seekbar remove</string>
<string name="desc_seekbar_add">Seekbar add</string>
<string name="close">Close</string>
<string name="engine_cache">Engine cache</string>
<string name="engine">Engine</string>
<string name="timeout_destruction">Timeout destruction</string>
<string name="minute_format">%1$s min</string>
<string name="timeout_destruction_summary">The model is automatically destroyed after it has not been used for a specified period of time to save running memory.</string>
Expand Down Expand Up @@ -53,4 +53,8 @@
<string name="model_install_failed">Model installation failed ❌</string>
<string name="model_installed">Model installed</string>
<string name="timed_out">Timed out</string>
<string name="cache_size">Cache size</string>
<string name="cache_size_summary">Maximum number of engine caches</string>
<string name="thread_num">Number of threads</string>
<string name="thread_num_summary">Maximum number of threads per engine</string>
</resources>

0 comments on commit 1434d72

Please sign in to comment.