知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.
Swift androidlinuxlive-captionsmacosmeeting-notesmenubar-appsimultaneous-interpretationspeech-recognitionsubtitlesswiftswiftuitranslationwindows
fix(android): Your own voice no longer comes out cut short when the recording ends in a pause
wquguru29b57142a657
diff --git a/android/app/src/main/kotlin/com/wquguru/cicada/DebugReceiver.kt b/android/app/src/main/kotlin/com/wquguru/cicada/DebugReceiver.ktindex 1ec5553..55042b3 100644--- a/android/app/src/main/kotlin/com/wquguru/cicada/DebugReceiver.kt+++ b/android/app/src/main/kotlin/com/wquguru/cicada/DebugReceiver.kt@@ -156,7 +156,7 @@ class DebugReceiver : BroadcastReceiver() {val pcm = Wav.read(File(app.filesDir, name).inputStream())app.background.execute {when (val r = VoiceReference.check(app.translator, pcm, app.settings.targetCode)) {- is VoiceReference.Check.Ok -> app.main.post { Log.d("debug: voiceref saved=${app.saveVoiceReference(pcm, r.text, lang)}") }+ is VoiceReference.Check.Ok -> app.main.post { Log.d("debug: voiceref saved=${app.saveVoiceReference(r.pcm, r.text, lang)}") }is VoiceReference.Check.Again -> Log.d("debug: voiceref refused: ${Strings.LF(r.reason, *r.args.toTypedArray())}")}}diff --git a/android/app/src/main/kotlin/com/wquguru/cicada/VoiceRecordActivity.kt b/android/app/src/main/kotlin/com/wquguru/cicada/VoiceRecordActivity.ktindex 32f78a6..a7bb271 100644--- a/android/app/src/main/kotlin/com/wquguru/cicada/VoiceRecordActivity.kt+++ b/android/app/src/main/kotlin/com/wquguru/cicada/VoiceRecordActivity.kt@@ -196,7 +196,8 @@ class VoiceRecordActivity : Activity() {app.main.post {if (isDestroyed) return@postwhen (result) {- is VoiceReference.Check.Ok -> { transcript = result.text; phase = Phase.Ready }+ // The trimmed clip is what's tried and kept, and its length is what's shown.+ is VoiceReference.Check.Ok -> { transcript = result.text; pcm = result.pcm; seconds = pcm.size / Segmenter.RATE.toDouble(); phase = Phase.Ready }is VoiceReference.Check.Again -> { problem = Strings.LF(result.reason, *result.args.toTypedArray()); phase = Phase.Failed }}build()diff --git a/android/cli/src/main/kotlin/cicada/cli/Main.kt b/android/cli/src/main/kotlin/cicada/cli/Main.ktindex 01a6f40..2ff917f 100644--- a/android/cli/src/main/kotlin/cicada/cli/Main.kt+++ b/android/cli/src/main/kotlin/cicada/cli/Main.kt@@ -122,7 +122,12 @@ fun main(argv: Array<String>) {val reference = opt("--reference", "").ifEmpty { null }?.let { path ->val pcm = Wav.read(File(path).inputStream())when (val r = VoiceReference.check(Translator(ling), pcm, opt("--own", "zh-CN"))) {- is VoiceReference.Check.Ok -> { println("reference: %.1f s, heard %d characters".format(pcm.size / 16000.0, r.text.length)); "data:audio/wav;base64," + java.util.Base64.getEncoder().encodeToString(Wav.bytes(pcm)) to r.text }+ is VoiceReference.Check.Ok -> {+ // What 用这个声音 keeps: the trimmed clip, written and read back as the app does.+ println("reference: %.1f s recorded, %.1f s kept, heard %d characters".format(pcm.size / 16000.0, r.pcm.size / 16000.0, r.text.length))+ VoiceReference.save(r.pcm, r.text)+ VoiceReference.reference()!!+ }is VoiceReference.Check.Again -> { println("reference refused: ${Strings.LF(r.reason, *r.args.toTypedArray())}"); exitProcess(1) }}}diff --git a/android/cli/src/main/kotlin/cicada/cli/VoiceTests.kt b/android/cli/src/main/kotlin/cicada/cli/VoiceTests.ktindex f44ede0..a8e5087 100644--- a/android/cli/src/main/kotlin/cicada/cli/VoiceTests.kt+++ b/android/cli/src/main/kotlin/cicada/cli/VoiceTests.kt@@ -18,8 +18,23 @@ object VoiceTests {"cloneRecordingIsKeptPrivatelyAndRemoved" to ::storage,"clonePaceIsTheMacs" to ::pace,"cloneRecordingIsCheckedLikeTheMac" to ::checkRecording,+ "cloneQuietAtTheEndsIsTrimmed" to ::trimmed,)+ fun trimmed() {+ val rate = Segmenter.RATE+ // 1 s before, 12 s of reading, 1.5 s after (the pause before 读完了).+ val clip = tone(12.0, pad = 0.0).let { ShortArray(rate) + it + ShortArray(rate * 3 / 2) }+ val t = VoiceReference.trim(clip)+ check(kotlin.math.abs(t.size / rate.toDouble() - 12.4) < 0.05, "12 s and 0.2 s each side, got ${t.size / rate.toDouble()}")+ equal(clip.copyOfRange(rate - rate / 5, rate - rate / 5 + 100).toList(), t.copyOfRange(0, 100).toList(), "starts 0.2 s before the reading")+ equal(0.toShort(), t.last(), "ends in the kept quiet")+ val tight = tone(5.0, pad = 0.0)+ equal(tight.size, VoiceReference.trim(tight).size, "nothing to trim")+ val silent = ShortArray(rate)+ equal(silent.size, VoiceReference.trim(silent).size, "silence is left as it is")+ }+private class Fake(val reply: String = "{}") : Transport {val streamed = mutableListOf<Pair<String, Int>>()val sent = mutableListOf<String>()@@ -120,7 +135,10 @@ object VoiceTests {equal(VoiceReference.Check.Again("读得有点慢,克隆的声音也会这么慢。请用平时说话的语速再录一次"), slow, "slow")val passage = "大家好,我是这次会议的主持人。今天我们主要讨论新产品的上线计划,包括时间安排、测试范围和市场推广。"val ok = VoiceReference.check(Translator(client(heard(passage), AppSettings())), tone(11.0), "zh-CN")- equal(VoiceReference.Check.Ok(passage), ok, "heard at an ordinary pace")+ equal(VoiceReference.Check.Ok(passage, VoiceReference.trim(tone(11.0))), ok, "heard at an ordinary pace, the trimmed clip kept")+ // 9.5 s of reading padded to 15 s by pauses: the reading is what counts.+ val padded = VoiceReference.check(Translator(client(heard(passage), AppSettings())), tone(9.5, pad = 2.75), "zh-CN")+ equal(VoiceReference.Check.Again("录音太短了,请至少朗读 %lld 秒", listOf(10)), padded, "pauses don't count")val none = VoiceReference.check(Translator(client(heard("嗯"), AppSettings())), tone(11.0), "zh-CN")equal(VoiceReference.Check.Again("没听清录音内容,请换个安静的地方再录一次"), none, "nothing heard")}diff --git a/android/core/src/main/kotlin/cicada/VoiceReference.kt b/android/core/src/main/kotlin/cicada/VoiceReference.ktindex 2fea6ca..c23bf63 100644--- a/android/core/src/main/kotlin/cicada/VoiceReference.kt+++ b/android/core/src/main/kotlin/cicada/VoiceReference.kt@@ -75,6 +75,25 @@ object VoiceReference {/// Seconds from the first to the last 20 ms window within 35 dB of the loudest one.fun speechSpan(samples: ShortArray, rate: Int = Segmenter.RATE): Double? {+ val (first, last, size) = speechWindows(samples, rate) ?: return null+ return (last - first + 1) * size.toDouble() / rate+ }++ /// Quiet kept before and after the reading by `trim`.+ const val TRIM_PAD = 0.2++ /// The recording without the quiet before and after the reading, 0.2 s kept each side (the+ /// Linux port's trim). Ming copies the clip's ending: a second of silence after the last word+ /// (the pause before 读完了) cut every cloned sentence short (measured 2026-11 against Ling:+ /// 1.9 s of speech instead of 3.6 s for the same line).+ fun trim(samples: ShortArray, rate: Int = Segmenter.RATE): ShortArray {+ val (first, last, size) = speechWindows(samples, rate) ?: return samples+ val pad = (TRIM_PAD * rate).toInt()+ return samples.copyOfRange(maxOf(0, first * size - pad), minOf(samples.size, (last + 1) * size + pad))+ }++ /// The first and last 20 ms window within 35 dB of the loudest one, and the window's length.+ private fun speechWindows(samples: ShortArray, rate: Int): Triple<Int, Int, Int>? {val size = maxOf(1, (rate * 0.02).toInt())val levels = ArrayList<Double>()var i = 0@@ -91,7 +110,7 @@ object VoiceReference {val first = levels.indexOfFirst { it >= floor }val last = levels.indexOfLast { it >= floor }if (first < 0) return null- return (last - first + 1) * size.toDouble() / rate+ return Triple(first, last, size)}/// Characters or words a second, silence at both ends left out; null when there is no speech.@@ -128,14 +147,20 @@ object VoiceReference {}/// What the recording says, as the service hears it (the Mac transcribes on the device; the- /// phone has Ming), then checked as the Mac checks it. Returns the text, or the reason to- /// record again (a Chinese UI key).+ /// phone has Ming), then checked as the Mac checks it. Returns the text and the trimmed clip+ /// to keep, or the reason to record again (a Chinese UI key). The 10 s are the reading's,+ /// silence at the ends left out.sealed class Check {- data class Ok(val text: String) : Check()+ class Ok(val text: String, val pcm: ShortArray) : Check() {+ override fun equals(other: Any?) = other is Ok && other.text == text && other.pcm.contentEquals(pcm)+ override fun hashCode() = text.hashCode()+ override fun toString() = "Ok($text, ${pcm.size} samples)"+ }data class Again(val reason: String, val args: List<Any> = emptyList()) : Check()}- fun check(translator: Translator, pcm: ShortArray, own: String): Check {+ fun check(translator: Translator, recorded: ShortArray, own: String): Check {+ val pcm = trim(recorded)val seconds = pcm.size / Segmenter.RATE.toDouble()if (seconds < MIN_SECONDS) return Check.Again("录音太短了,请至少朗读 %lld 秒", listOf(MIN_SECONDS.toInt()))val other = if (VoiceCatalog.key(own) == "en") "zh-CN" else "en"@@ -145,6 +170,6 @@ object VoiceReference {val pace = pace(text, pcm)Log.d("voice clone: ${text.length} chars, pace ${pace?.let { "%.1f".format(it) } ?: "-"}/s")if (pace != null && isSlow(text, pace)) return Check.Again("读得有点慢,克隆的声音也会这么慢。请用平时说话的语速再录一次")- return Check.Ok(text)+ return Check.Ok(text, pcm)}}