Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(android): a fan, hum or hiss no longer turns into a made-up English sentence

wquguruba91e37d3a02
diff --git a/android/cli/src/main/kotlin/cicada/cli/Tests.kt b/android/cli/src/main/kotlin/cicada/cli/Tests.kt
index e0bf378..71a26c5 100644
--- a/android/cli/src/main/kotlin/cicada/cli/Tests.kt
+++ b/android/cli/src/main/kotlin/cicada/cli/Tests.kt
@@ -401,7 +401,10 @@ object Tests {
// MARK: Segmenter
- private fun tone(seconds: Double, amp: Double = 0.3) = ShortArray((seconds * 16000).toInt()) { (sin(2 * PI * 300 * it / 16000) * amp * 32767).toInt().toShort() }
+ /// A 300 Hz tone that swells and fades four times a second like syllables; steady when not.
+ private fun tone(seconds: Double, amp: Double = 0.3, syllables: Boolean = true) = ShortArray((seconds * 16000).toInt()) {
+ (sin(2 * PI * 300 * it / 16000) * (if (syllables) 0.55 + 0.45 * sin(2 * PI * 4 * it / 16000) else 1.0) * amp * 32767).toInt().toShort()
+ }
private fun silence(seconds: Double) = ShortArray((seconds * 16000).toInt())
private fun concat(vararg parts: ShortArray): ShortArray = parts.fold(ShortArray(0)) { a, b -> a + b }
@@ -434,6 +437,8 @@ object Tests {
fun ignoresClicks() {
equal(0, cut(silence(1.0), tone(0.1), silence(1.0)).size, "under 0.25 s of sound is not speech")
equal(0, cut(tone(3.0, 0.002)).size, "below the gate is silence")
+ equal(0, cut(tone(3.0, 0.02, syllables = false), silence(1.0)).size, "a steady hum over the gate is not speech")
+ equal(1, cut(tone(3.0), silence(1.0)).size, "speech, which rises and falls, is")
}
fun wavRoundTrip() {
diff --git a/android/core/src/main/kotlin/cicada/Segmenter.kt b/android/core/src/main/kotlin/cicada/Segmenter.kt
index fabbed2..b631f9c 100644
--- a/android/core/src/main/kotlin/cicada/Segmenter.kt
+++ b/android/core/src/main/kotlin/cicada/Segmenter.kt
@@ -20,6 +20,7 @@ class Segmenter {
@Volatile var pause = 0.6
@Volatile var maxLength = 8.0
@Volatile var minSpeech = 0.25
+ @Volatile var steady = 2.0 // loud frames' 90th / 10th percentile below this: not speech
private const val PRE_ROLL = 0.15
private const val TAIL = 0.15
private const val CUT_WINDOW = 2.0
@@ -83,6 +84,16 @@ class Segmenter {
}
}
+ /// Steady sound just over the gate (a fan, hum, hiss) isn't speech: its loud frames barely vary
+ /// (90th / 10th percentile about 1.1; speech, even over such a floor, 3 and up). Given one, Ming
+ /// writes a stock sentence in the hint's language ("I'm going to show you a couple of things").
+ /// Speech buried under steady noise goes too; system audio rarely carries it.
+ internal fun isSteady(frameLevels: List<Double>): Boolean {
+ val loud = frameLevels.filter { it >= threshold }.sorted()
+ if (loud.size < 2) return false
+ return loud[(0.9 * (loud.size - 1)).toInt()] < steady * loud[(0.1 * (loud.size - 1)).toInt()]
+ }
+
/// Emits `current[0 until end]` and keeps what follows as the start of the next segment.
private fun close(endAt: Int) {
val end = endAt.coerceIn(0, current.size)
@@ -90,7 +101,7 @@ class Segmenter {
val rest = current.slice(end, current.size)
val restLevels = ArrayList(levels.subList(minOf(levels.size, end / FRAME), levels.size))
val start = currentStart
- val speech = loudFrames * FRAME >= minSpeech * RATE
+ val speech = loudFrames * FRAME >= minSpeech * RATE && !isSteady(levels.subList(0, minOf(levels.size, end / FRAME)))
current.clear(); levels.clear()
quietFrames = 0; loudFrames = 0
if (speech) onSegment?.invoke(Segment(++index, Math.round(start * 1000) / 1000.0, samples))