知了 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): someone typing during a pause no longer joins the sentences either side into one
wquguru38334d588446
diff --git a/android/cli/src/main/kotlin/cicada/cli/Tests.kt b/android/cli/src/main/kotlin/cicada/cli/Tests.ktindex 4c89a14..573cf66 100644--- a/android/cli/src/main/kotlin/cicada/cli/Tests.kt+++ b/android/cli/src/main/kotlin/cicada/cli/Tests.kt@@ -11,6 +11,7 @@ import java.util.concurrent.atomic.AtomicIntegerimport kotlin.concurrent.threadimport kotlin.math.PIimport kotlin.math.abs+import kotlin.math.expimport kotlin.math.sinobject Tests {@@ -54,6 +55,7 @@ object Tests {"cleansTranslations" to ::cleansTranslations,"joinsCutSentences" to ::joinsCutSentences,"segmentsOnPauses" to ::segmentsOnPauses,+ "keyClicksAreNotSpeech" to ::keyClicksAreNotSpeech,"cutsLongSpeechAtItsQuietestMoment" to ::cutsLongSpeech,"ignoresClicks" to ::ignoresClicks,"wavRoundTrip" to ::wavRoundTrip,@@ -420,6 +422,14 @@ object Tests {(sin(2 * PI * 300 * it / 16000) * env * amp * 32767).toInt().toShort()}private fun silence(seconds: Double) = ShortArray((seconds * 16000).toInt())+ /// Typing: a key click every 0.2 s, 6 ms of decaying crackle peaking near -12 dBFS (scripts/vad-clips.py).+ private fun keys(seconds: Double) = silence(seconds).also { out ->+ var at = 1600+ while (at < out.size - 96) {+ for (k in 0 until 96) out[at + k] = (0.25 * exp(-k / 24.0) * (if (k % 2 == 1) 1 else -1) * 32767).toInt().toShort()+ at += 3200+ }+ }/// A sine at `freq` that swells and fades four times a second like syllables.private fun hz(freq: Double, seconds: Double, amp: Double) = ShortArray((seconds * 16000).toInt()) {(sin(2 * PI * freq * it / 16000) * (0.55 + 0.45 * sin(2 * PI * 4 * it / 16000)) * amp * 32767).toInt().toShort()@@ -457,6 +467,13 @@ object Tests {check(abs(list[2].start - (list[1].start + list[1].seconds)) < 0.001, "the rest carries on without a gap")}+ fun keyClicksAreNotSpeech() {+ val list = cut(tone(2.0), keys(1.5), tone(1.5), silence(1.0))+ equal(2, list.size, "clicks in a pause don't hold it open")+ check(abs(list[1].start - 3.35) < 0.06, "second start, got ${list[1].start}")+ equal(0, cut(silence(0.5), keys(3.0), silence(1.0)).size, "typing alone makes no segment")+ }+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")diff --git a/android/core/src/main/kotlin/cicada/Segmenter.kt b/android/core/src/main/kotlin/cicada/Segmenter.ktindex dae248b..a97522a 100644--- a/android/core/src/main/kotlin/cicada/Segmenter.kt+++ b/android/core/src/main/kotlin/cicada/Segmenter.kt@@ -16,6 +16,8 @@ class Segmenter {companion object {const val RATE = 16000private const val FRAME = 480 // 30 ms+ private const val STEP = 160 // 10 ms: a key click is judged in these, as the Mac's VoiceActivity does+ private const val ONSET = 3 // steps of sound before it counts as speech@Volatile var threshold = 0.01 // the Mac AudioRouter's gate (about -40 dBFS)@Volatile var pause = 0.6@Volatile var maxLength = 8.0@@ -37,6 +39,9 @@ class Segmenter {private val current = ShortList() // the open segmentprivate val levels = ArrayList<Double>() // per frame of `current`private val highs = ArrayList<Double>() // per frame of `current`: its level over 100 Hz+ private val speech = ArrayList<Boolean>() // per frame of `current`: whether it held speech (isSpeech)+ private var sounding = 0 // 10 ms steps of sound in a row so far+ private var carried = false // the sound that made the last frame speech began in the frame beforeprivate var hx = 0.0 // the high-pass's last input and output, carried from frame to frameprivate var hy = 0.0private val before = ArrayDeque<Triple<ShortArray, Double, Double>>() // recent quiet frames and their levels, for the pre-roll@@ -63,10 +68,30 @@ class Segmenter {if (current.size > 0) close(current.size - quietFrames * FRAME + (TAIL * RATE).toInt())}+ /// Whether the frame holds speech: sound that has lasted 30 ms, judged in 10 ms steps against the+ /// line, so a lone key click (a few ms) neither opens a segment nor breaks the pause that ends one+ /// (VoiceActivity.swift's onset); the pre-roll then reaches back to where that sound began.+ private fun isSpeech(frame: ShortArray): Boolean {+ val line = threshold+ var said = false+ var early = sounding > 0+ var i = 0+ while (i < frame.size) {+ val end = minOf(frame.size, i + STEP)+ var sum = 0.0+ for (k in i until end) sum += frame[k].toDouble() * frame[k]+ if (Math.sqrt(sum / (end - i)) / 32768 >= line) {+ if (++sounding >= ONSET && !said) { said = true; carried = early }+ } else { sounding = 0; early = false }+ i = end+ }+ return said+ }+private fun step(frame: ShortArray) {val level = Wav.rms(frame)val high = high(frame)- val loud = level >= threshold+ val loud = isSpeech(frame)val now = consumed / RATE.toDouble()consumed += frame.size@@ -75,13 +100,15 @@ class Segmenter {idle.addLast(level)while (idle.size > (pause * RATE / FRAME).toInt()) idle.removeFirst()before.addLast(Triple(frame, level, high))- while (before.size > PRE_ROLL * RATE / FRAME) before.removeFirst()+ while (before.size > PRE_ROLL * RATE / FRAME + 1) before.removeFirst()return}+ // One frame more when the sound began in the last quiet one.+ while (before.size > PRE_ROLL * RATE / FRAME + (if (carried) 1 else 0)) before.removeFirst()currentStart = now - before.size * FRAME / RATE.toDouble()lead = idle.minOrNull()idle.clear()- for ((f, l, h) in before) { current.addAll(f, 0, f.size); levels.add(l); highs.add(h) }+ for ((f, l, h) in before) { current.addAll(f, 0, f.size); levels.add(l); highs.add(h); speech.add(false) }before.clear()quietFrames = 0; loudFrames = 0}@@ -89,6 +116,7 @@ class Segmenter {current.addAll(frame, 0, frame.size)levels.add(level)highs.add(high)+ speech.add(loud)if (loud) { quietFrames = 0; loudFrames++ } else quietFrames++if (quietFrames * FRAME >= pause * RATE) {@@ -186,26 +214,28 @@ class Segmenter {val rest = current.slice(end, current.size)val restLevels = ArrayList(levels.subList(minOf(levels.size, end / FRAME), levels.size))val restHighs = ArrayList(highs.subList(minOf(highs.size, end / FRAME), highs.size))+ val restSpeech = ArrayList(speech.subList(minOf(speech.size, end / FRAME), speech.size))val start = currentStartval sent = ArrayList(levels.subList(0, minOf(levels.size, end / FRAME)))- val speech = if (isWord(levels)) sent.count { it >= threshold / 4 } * FRAME >= minWord * RATE+ val said = if (isWord(levels)) sent.count { it >= threshold / 4 } * FRAME >= minWord * RATEelse loudFrames * FRAME >= minSpeech * RATE && !isSteady(sent)- val heard = speech && !isRumble(sent, ArrayList(highs.subList(0, sent.size)))- current.clear(); levels.clear(); highs.clear()+ val heard = said && !isRumble(sent, ArrayList(highs.subList(0, sent.size)))+ current.clear(); levels.clear(); highs.clear(); speech.clear()quietFrames = 0; loudFrames = 0if (heard) onSegment?.invoke(Segment(++index, Math.round(start * 1000) / 1000.0, samples))// A forced cut carries on with the remainder; after a pause the remainder is silence.- if (restLevels.any { it >= threshold }) {+ if (restSpeech.any { it }) {currentStart = start + end / RATE.toDouble()lead = nullcurrent.addAll(rest, 0, rest.size)levels.addAll(restLevels)highs.addAll(restHighs)+ speech.addAll(restSpeech)var trailing = 0- for (i in levels.indices.reversed()) { if (levels[i] >= threshold) break; trailing++ }+ for (i in speech.indices.reversed()) { if (speech[i]) break; trailing++ }quietFrames = trailing- loudFrames = levels.count { it >= threshold }+ loudFrames = speech.count { it }}}}