Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。
Add Android system TTS backend (#24)
宋博文c5a27b2abc74
diff --git a/app/src/main/java/net/kuafuai/andee/audio/AudioIO.kt b/app/src/main/java/net/kuafuai/andee/audio/AudioIO.ktindex 96bfc94..ee4be1a 100644--- a/app/src/main/java/net/kuafuai/andee/audio/AudioIO.kt+++ b/app/src/main/java/net/kuafuai/andee/audio/AudioIO.kt@@ -339,37 +339,17 @@ class AudioIO {fun startStreamPlayback(sampleRate: Int = 24_000) {stopStreamPlayback()- val channelConfig = AudioFormat.CHANNEL_OUT_MONO- val encoding = AudioFormat.ENCODING_PCM_16BIT- val minBuf = AudioTrack.getMinBufferSize(sampleRate, channelConfig, encoding)- require(minBuf > 0) { "AudioTrack.getMinBufferSize failed: $minBuf" }- // USAGE_ASSISTANT, not USAGE_MEDIA: this is the device answering, not- // the device playing something. Declaring it as media put our own voice- // in the same bucket as whatever video the user has open, which is both- // a lie to the audio policy and the wrong thing for the volume keys to- // land on.- val attrs = AudioAttributes.Builder()- .setUsage(AudioAttributes.USAGE_ASSISTANT)- .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)- .build()- val format = AudioFormat.Builder()- .setEncoding(encoding)- .setSampleRate(sampleRate)- .setChannelMask(channelConfig)- .build()- val t = AudioTrack.Builder()- .setAudioAttributes(attrs)- .setAudioFormat(format)- .setBufferSizeInBytes(minBuf * 4)- .setTransferMode(AudioTrack.MODE_STREAM)- .build()- streamPlaySampleRate = sampleRatestreamBytesWritten = 0streamBytesQueued = 0playQueue.clear()- t.play()- streamTrack = tplaying = true+ // A sample rate of 0 means "not known yet": the track is built late,+ // from the first chunk, via [setStreamSampleRate]. System TTS is the+ // one backend that needs this — it only learns the rate after the WAV+ // has been written, so it opens at 0 and reports the real number later.+ if (sampleRate > 0) {+ createStreamTrack(sampleRate)+ }playThread = Thread {try {while (playing) {@@ -394,6 +374,55 @@ class AudioIO {}.also { it.name = "AudioIO-Player"; it.isDaemon = true; it.start() }}+ /**+ * Build the AudioTrack, once the sample rate is known. Called either from+ * [startStreamPlayback] (rate declared up front) or from+ * [setStreamSampleRate] (rate learned after opening).+ *+ * USAGE_ASSISTANT, not USAGE_MEDIA: this is the device answering, not the+ * device playing something. Declaring it as media put our own voice in the+ * same bucket as whatever video the user has open, which is both a lie to+ * the audio policy and the wrong thing for the volume keys to land on.+ */+ private fun createStreamTrack(sampleRate: Int) {+ val channelConfig = AudioFormat.CHANNEL_OUT_MONO+ val encoding = AudioFormat.ENCODING_PCM_16BIT+ val minBuf = AudioTrack.getMinBufferSize(sampleRate, channelConfig, encoding)+ require(minBuf > 0) { "AudioTrack.getMinBufferSize failed: $minBuf" }+ val attrs = AudioAttributes.Builder()+ .setUsage(AudioAttributes.USAGE_ASSISTANT)+ .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)+ .build()+ val format = AudioFormat.Builder()+ .setEncoding(encoding)+ .setSampleRate(sampleRate)+ .setChannelMask(channelConfig)+ .build()+ val t = AudioTrack.Builder()+ .setAudioAttributes(attrs)+ .setAudioFormat(format)+ .setBufferSizeInBytes(minBuf * 4)+ .setTransferMode(AudioTrack.MODE_STREAM)+ .build()+ streamPlaySampleRate = sampleRate+ t.play()+ streamTrack = t+ }++ /**+ * Tell a deferred playback session ([startStreamPlayback] with 0) what its+ * sample rate is. The track is built here; a session that already knows its+ * rate ignores the call.+ *+ * Gated on [playing] because a barge-in can stop the session on another+ * thread between the caller's `cancelled` check and this call, and a track+ * built after that has no one left to stop it.+ */+ fun setStreamSampleRate(sampleRate: Int) {+ if (!playing || sampleRate <= 0 || streamTrack != null) return+ createStreamTrack(sampleRate)+ }+/*** Hand a PCM chunk to the player. Never blocks — see [playQueue].*diff --git a/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt b/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.ktindex b4bbbad..3951701 100644--- a/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt+++ b/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt@@ -62,15 +62,24 @@ object VoiceConfig {private const val PREFS = "voice_prefs"/**- * Which service hears and speaks. See [voiceProvider].+ * The two cloud providers, valid on both the hearing and speaking+ * sides. See [voiceProvider] and [speakProvider].*- * Two values and not a boolean because the list will not stay at two:- * the picker, the self-check rows and the engines all branch on the- * string, so a third provider is a value rather than a second flag.+ * A string and not a boolean because the list is not closed: the+ * speaking side already adds [VOICE_SYSTEM], and the picker, the+ * self-check rows and the engines all branch on the string, so a new+ * provider is a value rather than a second flag.*/const val VOICE_HUOSHAN = VoiceSettings.VOICE_HUOSHANconst val VOICE_GOOGLE = VoiceSettings.VOICE_GOOGLE+ /**+ * The device's own text-to-speech engine. A speak-side only option: a+ * system engine can read aloud but has no recogniser, so it never+ * appears on the listening side.+ */+ const val VOICE_SYSTEM = "system"+/*** The Vertex endpoint, its Express-keyed surface.*@@ -480,7 +489,9 @@ object VoiceConfig {}/**- * Which service hears and speaks: [VOICE_HUOSHAN] or [VOICE_GOOGLE].+ * Which service hears: [VOICE_HUOSHAN] or [VOICE_GOOGLE]. The speaking+ * side is a separate choice — see [speakProvider] — because a system+ * engine can read aloud but has no recogniser.** **Anything that is not exactly `google` reads as 火山**, which is the* same fail-closed direction [groundingEnabled] and [notifyScope] take,@@ -520,6 +531,31 @@ object VoiceConfig {}}+ /**+ * Which service speaks: [VOICE_HUOSHAN], [VOICE_GOOGLE] or+ * [VOICE_SYSTEM]. The speaking and hearing sides are now two separate+ * choices, because a system engine reads aloud but cannot hear — so+ * [voiceProvider] (the hearing side) keeps only 火山 / Google, while+ * this one may add the device's own engine.+ *+ * An unset value falls back to [voiceProvider], so a device that+ * predates this row speaks exactly as it did before; a saved choice+ * always wins. CodeFlying is pinned to 火山 for the same reason as+ * [voiceProvider] — the hosted gateway owns the upstream choice.+ */+ fun speakProvider(context: Context): String {+ if (brainMode(context) == BRAIN_CODEFLYING) return VOICE_HUOSHAN+ val saved = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)+ .getString("speak_provider", "").orEmpty().trim().lowercase()+ return when (saved) {+ VOICE_GOOGLE -> VOICE_GOOGLE+ VOICE_SYSTEM -> VOICE_SYSTEM+ VOICE_HUOSHAN -> VOICE_HUOSHAN+ "" -> voiceProvider(context)+ else -> VOICE_HUOSHAN+ }+ }+/*** Is the 火山 account the one saved in settings, or the factory one?*@@ -646,6 +682,7 @@ object VoiceConfig {// the hint — which is the value a blank resolves to — can be// seen at all."voice_provider" to voiceProvider(context),+ "speak_provider" to speakProvider(context),"google_api_key" to p.getString("google_api_key", "").orEmpty(),"google_base_url" to p.getString("google_base_url", "").orEmpty(),"google_project" to p.getString("google_project", "").orEmpty(),@@ -934,7 +971,7 @@ object VoiceConfig {"codeflying_key","asr_endpoint", "asr_resource_id","tts_endpoint", "tts_resource_id", "tts_sample_rate",- "voice_provider",+ "voice_provider", "speak_provider","google_api_key", "google_base_url","google_project", "google_location","google_asr_model", "google_tts_model", "google_language",diff --git a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.ktindex ba8c110..b2b49d8 100644--- a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt+++ b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt@@ -907,11 +907,22 @@ object SelfCheck {*/private fun voiceKey(context: Context): Finding {val cfg = VoiceConfig.load(context)- val has = if (cfg.voiceProvider == VoiceConfig.VOICE_GOOGLE) {+ // The checklist grades "can this device hear and speak". A system+ // engine needs no key to speak, but both cloud providers do — and the+ // two sides are now chosen independently, so each has to be checked+ // against its own picker, or one green tick covers a service with no+ // key (hear = 火山, speak = Google, no Google key → silent reply).+ val speakHas = when (VoiceConfig.speakProvider(context)) {+ VoiceConfig.VOICE_SYSTEM -> true+ VoiceConfig.VOICE_GOOGLE -> cfg.googleApiKey.isNotBlank()+ else -> cfg.apiKey.isNotBlank()+ }+ val hearHas = if (cfg.voiceProvider == VoiceConfig.VOICE_GOOGLE) {cfg.googleApiKey.isNotBlank()} else {cfg.apiKey.isNotBlank()}+ val has = speakHas && hearHasreturn Finding(id = "voice_key",title = R.string.check_title_voice_key,diff --git a/app/src/main/java/net/kuafuai/andee/tts/SystemTts.kt b/app/src/main/java/net/kuafuai/andee/tts/SystemTts.ktnew file mode 100644index 0000000..c767d9b--- /dev/null+++ b/app/src/main/java/net/kuafuai/andee/tts/SystemTts.kt@@ -0,0 +1,339 @@+package net.kuafuai.andee.tts++import android.content.Context+import android.speech.tts.TextToSpeech+import android.speech.tts.UtteranceProgressListener+import android.util.Log+import net.kuafuai.andee.config.VoiceConfig+import java.io.File+import java.util.Locale+import java.util.concurrent.CountDownLatch+import java.util.concurrent.Executors+import java.util.concurrent.TimeUnit+import java.util.concurrent.atomic.AtomicBoolean++/**+ * The device's own text-to-speech engine — `android.speech.tts.TextToSpeech`.+ *+ * Unlike [HuoshanTts] and [GoogleTts], this backend is free, works with no+ * network, and needs no API key: it speaks through whatever engine the user+ * has installed and selected in the system's "text-to-speech output" settings+ * (Google TTS, a vendor engine, MultiTTS, …). It is deliberately+ * engine-agnostic — the code never names a specific package, it binds+ * whatever the system reports as the default.+ *+ * **Two things differ from the other backends.**+ *+ * The sample rate. [TtsEngine]'s contract says [TtsEngine.Listener.onOpen]+ * declares the rate before the driver can push text, because 火山 learns it+ * from an open socket and Google declares a constant. A system engine only+ * reveals its rate after the WAV has been written, so this backend opens at an+ * *unknown* rate ([onOpen] with `0`) and reports the real number through+ * [TtsEngine.Listener.onRate] once the first file is read — the "build the+ * AudioTrack late" path the issue discussion settled on, instead of+ * resampling.+ *+ * The split. Synthesis is per sentence ([splitSentences]), not per utterance:+ * the engine caps its input at a few thousand characters and an+ * unpronounceable stretch kills the whole take, so the same trade GoogleTts+ * makes — one file per sentence — is made here too.+ *+ * A device with no bindable engine (or none that will speak the reply's+ * language) is reported through [TtsEngine.Listener.onUnavailable], not+ * [TtsEngine.Listener.onError], so the driver can fall back to a keyed backend+ * rather than go quiet.+ */+class SystemTts(+ private val context: Context,+ private val listener: TtsEngine.Listener,+) : TtsEngine {++ private val text = StringBuilder()++ @Volatile+ private var cancelled = false++ /** The in-flight engine, so [cancel] can stop a sentence mid-synthesis. */+ @Volatile+ private var tts: TextToSpeech? = null++ private val io = Executors.newSingleThreadExecutor { r ->+ Thread(r, "SystemTts").apply { isDaemon = true }+ }++ override fun open() {+ io.execute {+ if (cancelled) return@execute+ val initLatch = CountDownLatch(1)+ val inited = AtomicBoolean(false)++ val engine = TextToSpeech(context) { status ->+ if (status == TextToSpeech.SUCCESS) inited.set(true)+ initLatch.countDown()+ }+ tts = engine++ // The onInit callback lands on the main looper; wait for it here.+ if (!initLatch.await(10, TimeUnit.SECONDS)) {+ Log.w(TAG, "init timeout")+ engine.shutdown()+ tts = null+ listener.onUnavailable("system tts init timeout")+ return@execute+ }+ if (cancelled) {+ engine.shutdown()+ tts = null+ return@execute+ }+ if (!inited.get()) {+ Log.w(TAG, "no system TTS engine on this device")+ engine.shutdown()+ tts = null+ listener.onUnavailable("no system tts engine")+ return@execute+ }++ // The reply follows the UI language, so ask the engine for that+ // language. A strict `isLanguageAvailable` gate is deliberately+ // *not* used here: engines report their capabilities as zh, zh-CN,+ // zh-Hans-CN … and refusing on a mismatch would reject an engine+ // that speaks the language perfectly well. Only a hard "missing+ // data" is fatal; "not supported" is left to the synthesis itself+ // to prove, so a fallback engine whose default is Chinese is not+ // wrongly gated out.+ val lang = if (VoiceConfig.uiLanguage(context) == VoiceConfig.LANG_EN) {+ Locale.US+ } else {+ Locale.SIMPLIFIED_CHINESE+ }+ val langResult = engine.setLanguage(lang)+ if (langResult == TextToSpeech.LANG_MISSING_DATA) {+ Log.w(TAG, "engine has no data for $lang")+ engine.shutdown()+ tts = null+ listener.onUnavailable("system tts: no language data for $lang")+ return@execute+ }++ // Rate unknown until the first file is read.+ listener.onOpen(0)+ }+ }++ override fun push(text: String) {+ synchronized(this) { this.text.append(text) }+ }++ override fun finish() {+ val pending = synchronized(this) { this.text.toString() }+ io.execute {+ runCatching { synth(pending) }.onFailure {+ if (cancelled) return@onFailure+ Log.e(TAG, "synth failed", it)+ listener.onError(it.message ?: "system tts failed")+ }+ }+ }++ override fun cancel() {+ cancelled = true+ // Interrupting must stop the engine, or the abandoned sentence keeps+ // the engine busy — the same rule the issue discussion spelled out.+ val engine = tts+ runCatching { engine?.stop() }+ runCatching { engine?.shutdown() }+ tts = null+ io.shutdown()+ }++ private fun synth(pending: String) {+ val engine = tts ?: run {+ listener.onError("system tts not initialized")+ return+ }+ if (cancelled) return++ try {+ val sentences = splitSentences(pending)+ // Nothing pronounceable — punctuation or whitespace. Saying nothing+ // is the correct reading of it, same as GoogleTts.+ if (sentences.isEmpty()) {+ listener.onDone()+ return+ }++ var sawAny = false+ for ((i, sentence) in sentences.withIndex()) {+ if (cancelled) return+ listener.onSentenceStart(sentence)+ val got = synthOne(engine, sentence, i)+ listener.onSentenceEnd(sentence)+ if (cancelled) return+ sawAny = sawAny || got+ // The first sentence is the test of whether this works at all.+ // A silent *later* sentence is a long pause and not our+ // business; a silent *first* one means the whole utterance is+ // about to vanish, and reporting it is what gets the words put+ // up on screen to be read instead.+ if (i == 0 && !got) {+ listener.onError("system tts returned no audio")+ return+ }+ }+ if (cancelled) return+ listener.onDone()+ } finally {+ release(engine)+ }+ }++ /**+ * Hand the engine back once the utterance is spoken. The PCM has already+ * been handed to the player by this point, so stopping the engine cannot+ * cut off what is still playing — but it does release the binder session,+ * which otherwise leaks one engine per utterance.+ */+ private fun release(engine: TextToSpeech) {+ runCatching { engine.stop() }+ runCatching { engine.shutdown() }+ if (tts === engine) tts = null+ io.shutdown()+ }++ /** One sentence to a file, read back, file deleted whatever happens. */+ private fun synthOne(engine: TextToSpeech, sentence: String, index: Int): Boolean {+ val file = File(context.cacheDir, "andee_tts_${System.nanoTime()}.wav")+ val uid = "andee_${index}_${System.nanoTime()}"+ val done = CountDownLatch(1)+ val ok = AtomicBoolean(false)++ engine.setOnUtteranceProgressListener(object : UtteranceProgressListener() {+ override fun onStart(utteranceId: String?) {}++ override fun onDone(utteranceId: String?) {+ if (utteranceId == uid) {+ ok.set(true)+ done.countDown()+ }+ }++ @Deprecated("Deprecated in Java")+ override fun onError(utteranceId: String?) {+ if (utteranceId == uid) {+ ok.set(false)+ done.countDown()+ }+ }++ override fun onError(utteranceId: String?, errorCode: Int) {+ if (utteranceId == uid) {+ ok.set(false)+ done.countDown()+ }+ }++ // Barge-in reports through onStop, not onDone/onError. Without this+ // the done latch is never released after a cancel, and the io thread+ // sits out the full 30-second await with the temp file still on+ // disk.+ override fun onStop(utteranceId: String?, interrupted: Boolean) {+ if (utteranceId == uid) {+ ok.set(false)+ done.countDown()+ }+ }+ })++ try {+ val result = engine.synthesizeToFile(sentence, null, file, uid)+ if (result != TextToSpeech.SUCCESS) return false+ if (!done.await(30, TimeUnit.SECONDS)) return false+ if (cancelled || !ok.get() || !file.exists() || file.length() == 0L) return false++ val (rate, pcm) = readWav(file)+ if (rate <= 0 || pcm.isEmpty()) return false+ // Report the real rate; the player ignores a rate it already has,+ // so one per sentence is harmless and the first is the one that+ // builds the track.+ listener.onRate(rate)+ listener.onAudio(pcm)+ return true+ } finally {+ // The temporary file must not survive the sentence; a pile of+ // *.wav is a bug, not a backlog — and a throw on this path must+ // not leave one behind.+ file.delete()+ }+ }++ /**+ * Pull the sample rate and the raw 16-bit mono PCM out of a WAV. Only the+ * common system-engine shape (PCM, 16-bit, mono) is returned; anything+ * else hands back an empty result — with a log line naming the format — so+ * the caller falls back rather than plays garbage at the wrong rate.+ */+ private fun readWav(file: File): Pair<Int, ByteArray> {+ val bytes = file.readBytes()+ if (bytes.size < 44) return 0 to ByteArray(0)++ var offset = 12+ var sampleRate = 0+ var channels = 1+ var bitsPerSample = 16+ var dataOffset = -1+ var dataSize = 0++ while (offset + 8 <= bytes.size) {+ val id = String(bytes, offset, 4, Charsets.US_ASCII)+ val size = (bytes[offset + 4].toInt() and 0xff) or+ ((bytes[offset + 5].toInt() and 0xff) shl 8) or+ ((bytes[offset + 6].toInt() and 0xff) shl 16) or+ ((bytes[offset + 7].toInt() and 0xff) shl 24)++ if (id == "fmt ") {+ if (offset + 24 > bytes.size) return 0 to ByteArray(0)+ val audioFormat =+ (bytes[offset + 8].toInt() and 0xff) or ((bytes[offset + 9].toInt() and 0xff) shl 8)+ if (audioFormat != 1) {+ Log.w(TAG, "not PCM: audioFormat=$audioFormat")+ return 0 to ByteArray(0)+ }+ channels =+ (bytes[offset + 10].toInt() and 0xff) or ((bytes[offset + 11].toInt() and 0xff) shl 8)+ sampleRate = (bytes[offset + 12].toInt() and 0xff) or+ ((bytes[offset + 13].toInt() and 0xff) shl 8) or+ ((bytes[offset + 14].toInt() and 0xff) shl 16) or+ ((bytes[offset + 15].toInt() and 0xff) shl 24)+ bitsPerSample =+ (bytes[offset + 22].toInt() and 0xff) or ((bytes[offset + 23].toInt() and 0xff) shl 8)+ } else if (id == "data") {+ dataOffset = offset + 8+ dataSize = size+ break+ }+ offset += 8 + size + (size and 1)+ }++ if (dataOffset < 0) return 0 to ByteArray(0)+ // The size is read as a signed 32-bit, so a streaming writer's+ // 0xFFFFFFFF becomes -1; clamp it to what the file actually holds.+ val available = bytes.size - dataOffset+ val pcmSize = dataSize.coerceIn(0, available)+ if (pcmSize <= 0) {+ Log.w(TAG, "wav has no data (size=$dataSize)")+ return 0 to ByteArray(0)+ }+ if (channels != 1 || bitsPerSample != 16) {+ Log.w(TAG, "unsupported wav format: rate=$sampleRate channels=$channels bits=$bitsPerSample")+ return 0 to ByteArray(0)+ }++ val pcm = bytes.copyOfRange(dataOffset, dataOffset + pcmSize)+ return sampleRate to pcm+ }++ companion object {+ private const val TAG = "SystemTts"+ }+}diff --git a/app/src/main/java/net/kuafuai/andee/tts/TtsController.kt b/app/src/main/java/net/kuafuai/andee/tts/TtsController.ktindex 5fdcc89..174b074 100644--- a/app/src/main/java/net/kuafuai/andee/tts/TtsController.kt+++ b/app/src/main/java/net/kuafuai/andee/tts/TtsController.kt@@ -69,6 +69,14 @@ class TtsController(@Volatileprivate var reported = false+ /**+ * Reset per utterance. Set when the system backend reports itself+ * unavailable and the driver falls back to a keyed engine, so a second+ * [TtsEngine.Listener.onUnavailable] does not retry forever.+ */+ @Volatile+ private var fallbackTried = false+private fun reportUnavailable(why: String) {if (reported) returnreported = true@@ -239,6 +247,7 @@ class TtsController(if (text.isBlank()) returncancelled = falsereported = false+ fallbackTried = falseexecutor.execute {try {speakImpl(text, cues)@@ -348,6 +357,34 @@ class TtsController(}}+ override fun onRate(sampleRate: Int) {+ if (cancelled) return+ audio.setStreamSampleRate(sampleRate)+ }++ override fun onUnavailable(reason: String) {+ if (cancelled) return+ if (fallbackTried) {+ // Already fell back once and the fallback is not running+ // yet — treat it as a plain failure and close the turn out.+ onError(reason)+ return+ }+ fallbackTried = true+ // The system engine cannot speak on this device. Fall back to+ // the keyed engine the listening side already names — the same+ // "fail to 火山" direction, decided here because the controller+ // is the only thing that knows what else is available, and the+ // adapter is the only thing that can find out it cannot speak.+ client = null+ val fallback: TtsEngine = when (VoiceConfig.voiceProvider(context)) {+ VoiceConfig.VOICE_GOOGLE -> GoogleTts(cfg, this)+ else -> HuoshanTts(cfg, this)+ }+ client = fallback+ fallback.open()+ }+override fun onAudio(pcm: ByteArray) {if (cancelled) returnlastProgressAt = SystemClock.elapsedRealtime()@@ -413,9 +450,13 @@ class TtsController(}}- // Which service speaks. Same fail-to-火山 rule as the listening side.- val c: TtsEngine = when (cfg.voiceProvider) {+ // Which service speaks. The listening side keeps [VoiceConfig.voiceProvider]+ // (火山 / Google only — a system engine has no recogniser to hear with);+ // the speaking side may additionally pick the device's own engine, which+ // is free and works offline. Fail direction is 火山, same as listening.+ val c: TtsEngine = when (VoiceConfig.speakProvider(context)) {VoiceConfig.VOICE_GOOGLE -> GoogleTts(cfg, listener)+ VoiceConfig.VOICE_SYSTEM -> SystemTts(context, listener)else -> HuoshanTts(cfg, listener)}client = cdiff --git a/app/src/main/java/net/kuafuai/andee/ui/SettingsUi.kt b/app/src/main/java/net/kuafuai/andee/ui/SettingsUi.ktindex ea32fda..c9b6142 100644--- a/app/src/main/java/net/kuafuai/andee/ui/SettingsUi.kt+++ b/app/src/main/java/net/kuafuai/andee/ui/SettingsUi.kt@@ -751,7 +751,9 @@ class SettingsUi(// of the same thing: 火山's are endpoints and resource ids, Google's are// a project, a location and two model names.val voiceBlock = block(panel)- rowLabel(voiceBlock, lctx.getString(R.string.settings_voice_provider))+ // 听(识别):谁把用户的话转成文字。系统引擎没有识别器,所以这一+ // 侧只有 火山/谷歌。+ rowLabel(voiceBlock, lctx.getString(R.string.settings_voice_hear_provider))var applyProvider: ((String, Boolean) -> Unit)? = nullval provider = segmented(voiceBlock,@@ -766,6 +768,23 @@ class SettingsUi(),) { applyProvider?.invoke(it, true) }+ // 说(朗读):谁把答案读出来。可以选系统自带的引擎——免费、离线。+ rowLabel(voiceBlock, lctx.getString(R.string.settings_voice_speak_provider))+ var speakPicked = ""+ val speak = segmented(+ voiceBlock,+ "speak_provider",+ listOf(+ VoiceConfig.VOICE_HUOSHAN to lctx.getString(R.string.settings_voice_provider_huoshan),+ VoiceConfig.VOICE_GOOGLE to lctx.getString(R.string.settings_voice_provider_google),+ VoiceConfig.VOICE_SYSTEM to lctx.getString(R.string.settings_voice_provider_system),+ ),+ ) {+ speakPicked = it+ applyProvider?.invoke(provider, true)+ }+ speakPicked = speak+// 火山: previously in 高级 → 火山. Rows copy that section's labels,// which still describe exactly these keys.val huoshanBlock = block(voiceBlock)@@ -864,8 +883,14 @@ class SettingsUi(note(googleBlock, lctx.getString(R.string.settings_voice_google_note))applyProvider = { p, animate ->- Glass.setVisible(huoshanBlock, p == VoiceConfig.VOICE_HUOSHAN, animate)- Glass.setVisible(googleBlock, p == VoiceConfig.VOICE_GOOGLE, animate)+ // A provider's block is shown if it is picked on *either* side. The+ // blocks hold both the hearing and speaking rows, so hiding one+ // because the *hearing* picker does not name it would bury the key+ // field for the provider the *speaking* picker just chose.+ val useHuoshan = p == VoiceConfig.VOICE_HUOSHAN || speakPicked == VoiceConfig.VOICE_HUOSHAN+ val useGoogle = p == VoiceConfig.VOICE_GOOGLE || speakPicked == VoiceConfig.VOICE_GOOGLE+ Glass.setVisible(huoshanBlock, useHuoshan, animate)+ Glass.setVisible(googleBlock, useGoogle, animate)}applyProvider(provider, false)diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xmlindex 02420a0..c993fff 100644--- a/app/src/main/res/values-en/strings.xml+++ b/app/src/main/res/values-en/strings.xml@@ -187,11 +187,13 @@<string name="settings_voice_tts_endpoint">TTS endpoint</string><string name="settings_voice_tts_resource_id">TTS resource ID</string><string name="settings_voice_tts_sample_rate">TTS sample rate</string>- <string name="settings_voice_provider">Voice provider</string><string name="settings_voice_provider_huoshan">Volcengine</string><string name="settings_voice_provider_google">Google</string>+ <string name="settings_voice_hear_provider">Hearing</string>+ <string name="settings_voice_speak_provider">Speaking</string>+ <string name="settings_voice_provider_system">System</string><string name="settings_google_api_key">Google API key</string>- <string name="settings_google_api_key_hint">Vertex Express key — hearing and speaking both use it</string>+ <string name="settings_google_api_key_hint">Vertex Express key — hearing and Google speaking both use it</string><string name="settings_google_key_builtin">Built in at package time — leave blank to use it</string><string name="settings_google_project">Project number</string><string name="settings_google_project_hint">The GCP project *number* (not its id); recognition needs it</string>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex a1c6a48..c4d8b97 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -164,11 +164,13 @@<string name="settings_voice_tts_endpoint">合成地址</string><string name="settings_voice_tts_resource_id">合成资源 ID</string><string name="settings_voice_tts_sample_rate">合成采样率</string>- <string name="settings_voice_provider">语音服务商</string><string name="settings_voice_provider_huoshan">火山</string><string name="settings_voice_provider_google">谷歌</string>+ <string name="settings_voice_hear_provider">识别(听)</string>+ <string name="settings_voice_speak_provider">朗读(说)</string>+ <string name="settings_voice_provider_system">系统</string><string name="settings_google_api_key">Google API Key</string>- <string name="settings_google_api_key_hint">Vertex Express Key,听和说都用它</string>+ <string name="settings_google_api_key_hint">Vertex Express Key,识别和 Google 朗读都用它</string><string name="settings_google_key_builtin">已内置(编译时写入),留空即用它</string><string name="settings_google_project">项目号</string><string name="settings_google_project_hint">GCP 的项目编号(不是项目名),识别要用</string>diff --git a/core/src/main/kotlin/net/kuafuai/andee/tts/GoogleTts.kt b/core/src/main/kotlin/net/kuafuai/andee/tts/GoogleTts.ktindex 1f43344..da3d971 100644--- a/core/src/main/kotlin/net/kuafuai/andee/tts/GoogleTts.kt+++ b/core/src/main/kotlin/net/kuafuai/andee/tts/GoogleTts.kt@@ -314,14 +314,10 @@ class GoogleTts(* makes — a client that pushes text sentence by sentence gets a Gemini* call per sentence there too.*- * `. ` rather than `.` in the pattern: a bare period would cut `3.5` in- * half, and a half-sentence is spoken with a falling contour that sounds- * like the end of a thought.+ * Delegates to [splitSentences], shared with `SystemTts` so both backends+ * read the same terminators.*/- private fun split(pending: String): List<String> =- pending.split(Regex("(?<=[。!?!?\\n])|(?<=\\. )"))- .map { it.trim() }- .filter { it.isNotEmpty() && it.any { ch -> ch.isLetterOrDigit() } }+ private fun split(pending: String): List<String> = splitSentences(pending)/** The API's own words for what it disliked, else a truncated body. */private fun detail(raw: String): String = runCatching {diff --git a/core/src/main/kotlin/net/kuafuai/andee/tts/TtsEngine.kt b/core/src/main/kotlin/net/kuafuai/andee/tts/TtsEngine.ktindex 7f73410..1d2b7cb 100644--- a/core/src/main/kotlin/net/kuafuai/andee/tts/TtsEngine.kt+++ b/core/src/main/kotlin/net/kuafuai/andee/tts/TtsEngine.kt@@ -8,12 +8,13 @@ package net.kuafuai.andee.tts* synthesises on demand, however many times — then [finish]. Audio comes back* through [Listener.onAudio] as raw 16-bit little-endian PCM.*- * **What a backend cannot promise is when the text arrives relative to the- * network call.** 火山 accepts text into an open socket, so it can report its- * sample rate before it has seen a word; a backend that sends one HTTP request- * per utterance only learns the rate from the response. [Listener.onOpen] is- * therefore called *before* [push] by construction — see `GoogleTts` for what- * that forces it to declare up front.+ * **What a backend cannot promise is when the text — or the sample rate —+ * arrives relative to the network call.** 火山 accepts text into an open+ * socket, so it can report its sample rate before it has seen a word; Google+ * declares a constant. A system engine only learns its rate after writing the+ * WAV, so it opens at `0` and reports the real number through+ * [Listener.onRate]. [Listener.onOpen] is called *before* [push] by+ * construction either way.*/interface TtsEngine {@@ -30,6 +31,26 @@ interface TtsEngine {*/fun onOpen(sampleRate: Int)+ /**+ * The sample rate the PCM is actually in, for a backend that could not+ * name it in [onOpen] because it only learns it after synthesising.+ * Called at most once, after [onOpen] and before the first [onAudio].+ *+ * Streaming backends (火山, Google) already declared their rate in+ * [onOpen], so this is a no-op for them and they do not override it.+ */+ fun onRate(sampleRate: Int) {}++ /**+ * This backend cannot run on this device at all — no bindable engine,+ * or no data for the language. The driver may retry once with another+ * backend; if it does not (or cannot), it treats this as [onError].+ *+ * Distinct from [onError] on purpose: a missing engine is a known,+ * expected state (a bare ROM), not a failure of a running utterance.+ */+ fun onUnavailable(reason: String) {}+fun onAudio(pcm: ByteArray)fun onSentenceStart(text: String)@@ -52,3 +73,14 @@ interface TtsEngine {fun cancel()}++/**+ * Split an utterance into sentences, keeping the terminator attached to the+ * sentence it ends. `. ` rather than `.` in the pattern, so `3.5` is not cut+ * in half. Shared by the backends that synthesise one sentence at a time, so+ * they read the same terminators rather than drift apart.+ */+fun splitSentences(text: String): List<String> =+ text.split(Regex("(?<=[。!?!?\\n])|(?<=\\. )"))+ .map { it.trim() }+ .filter { it.isNotEmpty() && it.any { ch -> ch.isLetterOrDigit() } }