Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache mechanism to sherpa tts #1732

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.material3.Slider
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -46,6 +47,8 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import kotlin.math.roundToInt
import kotlinx.coroutines.delay
import kotlin.time.TimeSource

const val TAG = "sherpa-onnx-tts-engine"
Expand Down Expand Up @@ -76,6 +79,9 @@ class MainActivity : ComponentActivity() {
Log.i(TAG, "Finish initializing AudioTrack")

val preferenceHelper = PreferenceHelper(this)

TtsEngine.cacheSize = preferenceHelper.getCacheSizeInMB()

setContent {
SherpaOnnxTtsEngineTheme {
// A surface container using the 'background' color from the theme
Expand All @@ -88,6 +94,17 @@ class MainActivity : ComponentActivity() {
}) {
Box(modifier = Modifier.padding(it)) {
Column(modifier = Modifier.padding(16.dp)) {
// Track used cache size in a mutable state
var usedCacheSizeMB by remember { mutableStateOf(0) }

// LaunchedEffect to periodically update the used cache size
LaunchedEffect(Unit) {
mah92 marked this conversation as resolved.
Show resolved Hide resolved
while (true) {
usedCacheSizeMB = TtsEngine.tts?.getTotalUsedCacheSizeInMB() ?: 0
delay(5000) // Update every 5 seconds
}
}

Column {
Text("Speed " + String.format("%.1f", TtsEngine.speed))
Slider(
Expand All @@ -99,6 +116,20 @@ class MainActivity : ComponentActivity() {
valueRange = 0.2F..3.0F,
modifier = Modifier.fillMaxWidth()
)

Text("Cache Size: ${TtsEngine.cacheSize}MB (${usedCacheSizeMB}MB used)")
Slider(
value = TtsEngine.cacheSizeState.value.toFloat(),
onValueChange = { newValue ->
// Round the value to the nearest multiple of 10
val roundedValue = (newValue / 5).roundToInt() * 5
TtsEngine.cacheSize = roundedValue
preferenceHelper.setCacheSizeInMB(roundedValue)
TtsEngine.tts?.setCacheSizeInMB(roundedValue)
},
valueRange = 0f..100f,
modifier = Modifier.fillMaxWidth()
)
}

val testTextContent = getSampleText(TtsEngine.lang ?: "")
Expand Down Expand Up @@ -263,6 +294,21 @@ class MainActivity : ComponentActivity() {
Row {
Text(rtfText)
}

Button(
modifier = Modifier.padding(20.dp),
onClick = {
TtsEngine.tts?.clearCache() // Call the clearCache method
usedCacheSizeMB = 0 // Reset used cache size
Toast.makeText(
applicationContext,
"Cache cleared!",
Toast.LENGTH_SHORT
).show()
}
) {
Text("Clear")
}
}
}
}
Expand All @@ -272,6 +318,13 @@ class MainActivity : ComponentActivity() {
}
}

override fun onResume() {
super.onResume()
// Update used cache size when the app is resumed
val usedCacheSizeMB = TtsEngine.tts?.getTotalUsedCacheSizeInMB() ?: 0
Log.i(TAG, "App resumed. Used cache size: ${usedCacheSizeMB}MB")
}

override fun onDestroy() {
stopMediaPlayer()
super.onDestroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PreferenceHelper(context: Context) {
private val PREFS_NAME = "com.k2fsa.sherpa.onnx.tts.engine"
private val SPEED_KEY = "speed"
private val SID_KEY = "speaker_id"
private val CACHE_SIZE_KEY = "cache_size"

private val sharedPreferences: SharedPreferences =
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
Expand All @@ -29,4 +30,14 @@ class PreferenceHelper(context: Context) {
fun getSid(): Int {
return sharedPreferences.getInt(SID_KEY, 0)
}
}

fun setCacheSizeInMB(value: Int) {
val editor = sharedPreferences.edit()
editor.putInt(CACHE_SIZE_KEY, value)
editor.apply()
}

fun getCacheSizeInMB(): Int {
return sharedPreferences.getInt(CACHE_SIZE_KEY, 20) // Default cache size is 20MB
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object TtsEngine {


val speedState: MutableState<Float> = mutableFloatStateOf(1.0F)
val cacheSizeState: MutableState<Int> = mutableIntStateOf(0)
val speakerIdState: MutableState<Int> = mutableIntStateOf(0)

var speed: Float
Expand All @@ -33,6 +34,12 @@ object TtsEngine {
speedState.value = value
}

var cacheSize: Int
get() = cacheSizeState.value
set(value) {
cacheSizeState.value = value
}

var speakerId: Int
get() = speakerIdState.value
set(value) {
Expand Down Expand Up @@ -190,8 +197,11 @@ object TtsEngine {

speed = PreferenceHelper(context).getSpeed()
speakerId = PreferenceHelper(context).getSid()
cacheSize = PreferenceHelper(context).getCacheSizeInMB()

tts = OfflineTts(assetManager = assets, config = config)

tts?.setCacheSizeInMB(cacheSize)
}


Expand Down
1 change: 1 addition & 0 deletions sherpa-onnx/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ if(SHERPA_ONNX_ENABLE_TTS)
jieba-lexicon.cc
lexicon.cc
melo-tts-lexicon.cc
offline-tts-cache-mechanism.cc
offline-tts-character-frontend.cc
offline-tts-frontend.cc
offline-tts-impl.cc
Expand Down
Loading