Skip to content

Commit

Permalink
Fix setting speaker ID for Android TTS Engine. (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Jan 15, 2024
1 parent 229853b commit f4e3f45
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class MainActivity : AppCompatActivity() {
// modelDir = "vits-zh-aishell3"
// modelName = "vits-aishell3.onnx"
// ruleFsts = "vits-zh-aishell3/rule.fst"
// lexcion = "lexicon.txt"
// lexicon = "lexicon.txt"

if (dataDir != null) {
val newDir = copyDataDir(modelDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
Expand All @@ -32,6 +33,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.k2fsa.sherpa.onnx.tts.engine.ui.theme.SherpaOnnxTtsEngineTheme
Expand Down Expand Up @@ -65,22 +67,31 @@ class MainActivity : ComponentActivity() {
}
var testText by remember { mutableStateOf("") }

OutlinedTextField(value = testText,
OutlinedTextField(
value = testText,
onValueChange = { testText = it },
label = { Text ("Test text") },
modifier = Modifier.fillMaxWidth().wrapContentHeight().padding(16.dp),
label = { Text("Test text") },
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.padding(16.dp),
singleLine = false,
)

val numSpeakers = TtsEngine.tts!!.numSpeakers()
if (numSpeakers > 1) {
Row {
Text("Speaker ID: (0-${numSpeakers - 1})")
Slider(
value = TtsEngine.speakerIdState.value.toFloat(),
onValueChange = { TtsEngine.speakerId = it.toInt() },
valueRange = 0.0f..(numSpeakers - 1).toFloat(),
steps = 1
OutlinedTextField(
value = TtsEngine.speakerIdState.value.toString(),
onValueChange = {
if (it.isEmpty() || it.isBlank()) {
TtsEngine.speakerId = 0
} else {
TtsEngine.speakerId = it.toString().toInt()
}
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object TtsEngine {
// modelDir = "vits-zh-aishell3"
// modelName = "vits-aishell3.onnx"
// ruleFsts = "vits-zh-aishell3/rule.fst"
// lexcion = "lexicon.txt"
// lexicon = "lexicon.txt"
// lang = "zho"

if (dataDir != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ class TtsService : TextToSpeechService() {
override fun onLoadLanguage(_lang: String?, _country: String?, _variant: String?): Int {
val lang = _lang ?: ""

if (lang == TtsEngine.lang) {
return if (lang == TtsEngine.lang) {
TtsEngine.createTts(application)
return TextToSpeech.LANG_AVAILABLE
TextToSpeech.LANG_AVAILABLE
} else {
return TextToSpeech.LANG_NOT_SUPPORTED
TextToSpeech.LANG_NOT_SUPPORTED
}
}

Expand Down

0 comments on commit f4e3f45

Please sign in to comment.