Skip to content

Commit

Permalink
fix: 采样率未获取
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Aug 26, 2023
1 parent e619172 commit fb5944b
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.CallbackState
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.SaveActionHandler
import com.github.jing332.tts_server_android.compose.widgets.DenseOutlinedField
import com.github.jing332.tts_server_android.compose.widgets.ExposedDropTextField
import com.github.jing332.tts_server_android.compose.widgets.RowToggleButtonGroup
Expand All @@ -48,7 +49,6 @@ fun BasicInfoEditScreen(
modifier: Modifier,
systts: SystemTts,
onSysttsChange: (SystemTts) -> Unit,
saveEvent: CallbackState,

showSpeechTarget: Boolean = true,
group: SystemTtsGroup = remember { appDb.systemTtsDao.getGroup(systts.groupId) }
Expand All @@ -59,7 +59,8 @@ fun BasicInfoEditScreen(
) {
val context = LocalContext.current
val speechRule by rememberUpdatedState(newValue = speechRules.find { it.ruleId == systts.speechRule.tagRuleId })
saveEvent.value = {

SaveActionHandler {
var tagName = ""
if (speechRule != null) {
runCatching {
Expand All @@ -82,6 +83,8 @@ fun BasicInfoEditScreen(
speechRule = systts.speechRule.copy(tagName = tagName)
)
)

true
}

var showParamsDialog by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package com.github.jing332.tts_server_android.compose.nav.systts.edit

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.LocalSaveCallBack
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.TtsUiFactory
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.rememberSaveCallBacks
import com.github.jing332.tts_server_android.data.entities.systts.SystemTts
import com.github.jing332.tts_server_android.model.speech.tts.PluginTTS
import kotlinx.coroutines.launch

@Composable
fun TtsEditContainerScreen(
Expand All @@ -21,13 +26,27 @@ fun TtsEditContainerScreen(
onCancel: () -> Unit,
) {
val ui = TtsUiFactory.from(systts.tts)!!
ui.FullEditScreen(
modifier,
systts = systts,
onSysttsChange = onSysttsChange,
onSave = onSave,
onCancel = onCancel
)
val callbacks = rememberSaveCallBacks()
val scope = rememberCoroutineScope()
CompositionLocalProvider(LocalSaveCallBack provides callbacks) {
ui.FullEditScreen(
modifier,
systts = systts,
onSysttsChange = onSysttsChange,
onSave = {
scope.launch {
val iterator = callbacks.listIterator(callbacks.size)
while (iterator.hasPrevious()){
val element = iterator.previous()
if (!element.onSave()) return@launch
}

onSave()
}
},
onCancel = onCancel
)
}
}

@Preview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.github.jing332.tts_server_android.compose.nav.systts.edit.BasicInfoEditScreen
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.LocalSaveCallBack
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.TtsUiFactory
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.rememberCallbackState
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.rememberSaveCallBacks
import com.github.jing332.tts_server_android.data.entities.systts.SystemTts
import com.github.jing332.tts_server_android.model.speech.tts.BgmTTS
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -30,23 +34,29 @@ fun QuickEditBottomSheet(
) {
val state = rememberModalBottomSheetState(skipPartiallyExpanded = true)
val ui = remember { TtsUiFactory.from(systts.tts)!! }
val saveEvent = rememberCallbackState()
val callbacks = rememberSaveCallBacks()
val scope = rememberCoroutineScope()
ModalBottomSheet(
modifier = Modifier.fillMaxSize(),
sheetState = state, onDismissRequest = {
saveEvent.value?.invoke()
onDismissRequest()
scope.launch {
for (callback in callbacks) {
if (!callback.onSave()) return@launch
}
onDismissRequest()
}
}) {
Column(Modifier.verticalScroll(rememberScrollState())) {
BasicInfoEditScreen(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 4.dp),
systts = systts,
onSysttsChange = onSysttsChange,
saveEvent = saveEvent,
showSpeechTarget = systts.tts !is BgmTTS
)
CompositionLocalProvider(LocalSaveCallBack provides callbacks) {
BasicInfoEditScreen(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 4.dp),
systts = systts,
onSysttsChange = onSysttsChange,
showSpeechTarget = systts.tts !is BgmTTS
)
}
ui.ParamsEditScreen(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class BgmUI : TtsUI() {
.fillMaxWidth()
.padding(horizontal = 8.dp),
systts = systts,
saveEvent = saveSignal, onSysttsChange = onSysttsChange,
onSysttsChange = onSysttsChange,
showSpeechTarget = false,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.minimumInteractiveComponentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -42,6 +44,7 @@ import com.github.jing332.tts_server_android.compose.widgets.ExposedDropTextFiel
import com.github.jing332.tts_server_android.compose.widgets.LabelSlider
import com.github.jing332.tts_server_android.data.entities.systts.SystemTts
import com.github.jing332.tts_server_android.model.speech.tts.LocalTTS
import kotlinx.coroutines.launch

class LocalTtsUI : TtsUI() {
@Composable
Expand Down Expand Up @@ -139,15 +142,20 @@ class LocalTtsUI : TtsUI() {
onSave: () -> Unit,
onCancel: () -> Unit
) {
val saveEvent = rememberCallbackState()
val callbacks = rememberSaveCallBacks()
val scope = rememberCoroutineScope()
Scaffold(
topBar = {
TtsTopAppBar(
title = { Text(stringResource(id = R.string.edit_local_tts)) },
onBackAction = onCancel,
onSaveAction = {
saveEvent.value?.invoke()
onSave()
scope.launch {
for (callback in callbacks) {
if (!callback.onSave()) return@launch
}
onSave()
}
}
)
}
Expand All @@ -158,7 +166,6 @@ class LocalTtsUI : TtsUI() {
.verticalScroll(rememberScrollState()),
systts = systts,
onSysttsChange = onSysttsChange,
saveEvent = saveEvent
)
}
}
Expand All @@ -168,7 +175,6 @@ class LocalTtsUI : TtsUI() {
modifier: Modifier,
systts: SystemTts,
onSysttsChange: (SystemTts) -> Unit,
saveEvent: CallbackState,
vm: LocalTtsViewModel = viewModel()
) {
val tts = systts.tts as LocalTTS
Expand All @@ -184,7 +190,6 @@ class LocalTtsUI : TtsUI() {
modifier = Modifier.fillMaxWidth(),
systts = systts,
onSysttsChange = onSysttsChange,
saveEvent = saveEvent,
)
AuditionTextField(modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ class MsTtsUI : TtsUI() {
.fillMaxWidth()
.padding(horizontal = 8.dp),
systts = systts,
saveEvent = saveSignal,
onSysttsChange = onSysttsChange
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.viewmodel.compose.viewModel
import com.drake.net.utils.withIO
import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.nav.systts.AuditionDialog
import com.github.jing332.tts_server_android.compose.nav.systts.edit.BasicInfoEditScreen
import com.github.jing332.tts_server_android.compose.nav.systts.edit.IntSlider
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.base.AuditionTextField
import com.github.jing332.tts_server_android.compose.nav.systts.edit.ui.base.TtsTopAppBar
import com.github.jing332.tts_server_android.compose.nav.systts.AuditionDialog
import com.github.jing332.tts_server_android.compose.widgets.ExposedDropTextField
import com.github.jing332.tts_server_android.compose.widgets.LoadingDialog
import com.github.jing332.tts_server_android.data.entities.systts.SystemTts
import com.github.jing332.tts_server_android.model.speech.tts.BaseAudioFormat
import com.github.jing332.tts_server_android.model.speech.tts.PluginTTS
import com.github.jing332.tts_server_android.ui.view.AppDialogs.displayErrorDialog
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -114,20 +115,21 @@ class PluginTtsUI : TtsUI() {
onSave: () -> Unit,
onCancel: () -> Unit,
) {
val saveEvent = rememberCallbackState()
var name by remember { mutableStateOf("") }
val scope = rememberCoroutineScope()
Scaffold(
modifier = modifier,
topBar = {
TtsTopAppBar(
title = { Text(text = stringResource(id = R.string.edit_plugin_tts)) },
onBackAction = onCancel,
onSaveAction = {
if (systts.displayName?.isBlank() == true)
onSysttsChange(systts.copy(displayName = name))
scope.launch {
if (systts.displayName?.isBlank() == true)
onSysttsChange(systts.copy(displayName = name))

saveEvent.value?.invoke()
onSave()
onSave()
}
}
)
}
Expand All @@ -139,7 +141,6 @@ class PluginTtsUI : TtsUI() {
rememberScrollState()
),
voiceName = { name = it },
saveEvent = saveEvent,
systts = systts, onSysttsChange = onSysttsChange,
)
}
Expand All @@ -150,10 +151,53 @@ class PluginTtsUI : TtsUI() {
modifier: Modifier,
systts: SystemTts,
voiceName: (String) -> Unit,
saveEvent: CallbackState,
onSysttsChange: (SystemTts) -> Unit,
vm: PluginTtsViewModel = viewModel(),
) {
val tts by rememberUpdatedState(newValue = systts.tts as PluginTTS)
val context = LocalContext.current

SaveActionHandler {
val sampleRate = try {
withIO { vm.engine.getSampleRate(tts.locale, tts.voice) ?: 16000 }
} catch (e: Exception) {
context.displayErrorDialog(
e,
context.getString(R.string.plugin_tts_get_sample_rate_failed)
)
null
}

val isNeedDecode = try {
withIO { vm.engine.isNeedDecode(tts.locale, tts.voice) }
} catch (e: Exception) {
context.displayErrorDialog(
e,
context.getString(R.string.plugin_tts_get_need_decode_failed)
)
null
}

println(sampleRate)
println(isNeedDecode)
println("---")
if (sampleRate != null && isNeedDecode != null) {
onSysttsChange(
systts.copy(
tts = tts.copy(
audioFormat = BaseAudioFormat(
sampleRate = sampleRate,
isNeedDecode = isNeedDecode
)
)
)
)

true
}else
false
}

var showLoadingDialog by remember { mutableStateOf(false) }
if (showLoadingDialog)
LoadingDialog(onDismissRequest = { showLoadingDialog = false })
Expand All @@ -164,8 +208,6 @@ class PluginTtsUI : TtsUI() {
showAuditionDialog = false
}

val tts by rememberUpdatedState(newValue = systts.tts as PluginTTS)
val context = LocalContext.current
Column(modifier) {
Column(
Modifier
Expand All @@ -175,7 +217,6 @@ class PluginTtsUI : TtsUI() {
BasicInfoEditScreen(
Modifier.fillMaxWidth(),
systts = systts,
saveEvent = saveEvent,
onSysttsChange = onSysttsChange
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.jing332.tts_server_android.compose.nav.systts.edit.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf

internal val LocalSaveCallBack =
staticCompositionLocalOf<MutableList<SaveCallBack>> { mutableListOf() }

internal fun interface SaveCallBack {
suspend fun onSave(): Boolean
}

@Composable
internal fun rememberSaveCallBacks() = remember { mutableListOf<SaveCallBack>() }

@Composable
internal fun SaveActionHandler(cb: SaveCallBack) {
val cbs = LocalSaveCallBack.current
DisposableEffect(Unit) {
println("add cb")
cbs.add(cb)
onDispose {
println("remove cb")
cbs.remove(cb)
}
}
}
Loading

0 comments on commit fb5944b

Please sign in to comment.