Fangjun Kuang
Committed by GitHub

Fix setting speaker ID for Android TTS Engine. (#530)

@@ -184,7 +184,7 @@ class MainActivity : AppCompatActivity() { @@ -184,7 +184,7 @@ class MainActivity : AppCompatActivity() {
184 // modelDir = "vits-zh-aishell3" 184 // modelDir = "vits-zh-aishell3"
185 // modelName = "vits-aishell3.onnx" 185 // modelName = "vits-aishell3.onnx"
186 // ruleFsts = "vits-zh-aishell3/rule.fst" 186 // ruleFsts = "vits-zh-aishell3/rule.fst"
187 - // lexcion = "lexicon.txt" 187 + // lexicon = "lexicon.txt"
188 188
189 if (dataDir != null) { 189 if (dataDir != null) {
190 val newDir = copyDataDir(modelDir) 190 val newDir = copyDataDir(modelDir)
@@ -16,6 +16,7 @@ import androidx.compose.foundation.layout.fillMaxSize @@ -16,6 +16,7 @@ import androidx.compose.foundation.layout.fillMaxSize
16 import androidx.compose.foundation.layout.fillMaxWidth 16 import androidx.compose.foundation.layout.fillMaxWidth
17 import androidx.compose.foundation.layout.padding 17 import androidx.compose.foundation.layout.padding
18 import androidx.compose.foundation.layout.wrapContentHeight 18 import androidx.compose.foundation.layout.wrapContentHeight
  19 +import androidx.compose.foundation.text.KeyboardOptions
19 import androidx.compose.material3.Button 20 import androidx.compose.material3.Button
20 import androidx.compose.material3.ExperimentalMaterial3Api 21 import androidx.compose.material3.ExperimentalMaterial3Api
21 import androidx.compose.material3.MaterialTheme 22 import androidx.compose.material3.MaterialTheme
@@ -32,6 +33,7 @@ import androidx.compose.runtime.mutableStateOf @@ -32,6 +33,7 @@ import androidx.compose.runtime.mutableStateOf
32 import androidx.compose.runtime.remember 33 import androidx.compose.runtime.remember
33 import androidx.compose.runtime.setValue 34 import androidx.compose.runtime.setValue
34 import androidx.compose.ui.Modifier 35 import androidx.compose.ui.Modifier
  36 +import androidx.compose.ui.text.input.KeyboardType
35 import androidx.compose.ui.tooling.preview.Preview 37 import androidx.compose.ui.tooling.preview.Preview
36 import androidx.compose.ui.unit.dp 38 import androidx.compose.ui.unit.dp
37 import com.k2fsa.sherpa.onnx.tts.engine.ui.theme.SherpaOnnxTtsEngineTheme 39 import com.k2fsa.sherpa.onnx.tts.engine.ui.theme.SherpaOnnxTtsEngineTheme
@@ -65,10 +67,14 @@ class MainActivity : ComponentActivity() { @@ -65,10 +67,14 @@ class MainActivity : ComponentActivity() {
65 } 67 }
66 var testText by remember { mutableStateOf("") } 68 var testText by remember { mutableStateOf("") }
67 69
68 - OutlinedTextField(value = testText, 70 + OutlinedTextField(
  71 + value = testText,
69 onValueChange = { testText = it }, 72 onValueChange = { testText = it },
70 - label = { Text ("Test text") },  
71 - modifier = Modifier.fillMaxWidth().wrapContentHeight().padding(16.dp), 73 + label = { Text("Test text") },
  74 + modifier = Modifier
  75 + .fillMaxWidth()
  76 + .wrapContentHeight()
  77 + .padding(16.dp),
72 singleLine = false, 78 singleLine = false,
73 ) 79 )
74 80
@@ -76,11 +82,16 @@ class MainActivity : ComponentActivity() { @@ -76,11 +82,16 @@ class MainActivity : ComponentActivity() {
76 if (numSpeakers > 1) { 82 if (numSpeakers > 1) {
77 Row { 83 Row {
78 Text("Speaker ID: (0-${numSpeakers - 1})") 84 Text("Speaker ID: (0-${numSpeakers - 1})")
79 - Slider(  
80 - value = TtsEngine.speakerIdState.value.toFloat(),  
81 - onValueChange = { TtsEngine.speakerId = it.toInt() },  
82 - valueRange = 0.0f..(numSpeakers - 1).toFloat(),  
83 - steps = 1 85 + OutlinedTextField(
  86 + value = TtsEngine.speakerIdState.value.toString(),
  87 + onValueChange = {
  88 + if (it.isEmpty() || it.isBlank()) {
  89 + TtsEngine.speakerId = 0
  90 + } else {
  91 + TtsEngine.speakerId = it.toString().toInt()
  92 + }
  93 + },
  94 + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
84 ) 95 )
85 } 96 }
86 } 97 }
@@ -87,7 +87,7 @@ object TtsEngine { @@ -87,7 +87,7 @@ object TtsEngine {
87 // modelDir = "vits-zh-aishell3" 87 // modelDir = "vits-zh-aishell3"
88 // modelName = "vits-aishell3.onnx" 88 // modelName = "vits-aishell3.onnx"
89 // ruleFsts = "vits-zh-aishell3/rule.fst" 89 // ruleFsts = "vits-zh-aishell3/rule.fst"
90 - // lexcion = "lexicon.txt" 90 + // lexicon = "lexicon.txt"
91 // lang = "zho" 91 // lang = "zho"
92 92
93 if (dataDir != null) { 93 if (dataDir != null) {
@@ -81,11 +81,11 @@ class TtsService : TextToSpeechService() { @@ -81,11 +81,11 @@ class TtsService : TextToSpeechService() {
81 override fun onLoadLanguage(_lang: String?, _country: String?, _variant: String?): Int { 81 override fun onLoadLanguage(_lang: String?, _country: String?, _variant: String?): Int {
82 val lang = _lang ?: "" 82 val lang = _lang ?: ""
83 83
84 - if (lang == TtsEngine.lang) { 84 + return if (lang == TtsEngine.lang) {
85 TtsEngine.createTts(application) 85 TtsEngine.createTts(application)
86 - return TextToSpeech.LANG_AVAILABLE 86 + TextToSpeech.LANG_AVAILABLE
87 } else { 87 } else {
88 - return TextToSpeech.LANG_NOT_SUPPORTED 88 + TextToSpeech.LANG_NOT_SUPPORTED
89 } 89 }
90 } 90 }
91 91