End of speech: digital silence is a pause, a steady voice is not the room

Review S2, S3, S6.

A zero level after sound was skipped as "no microphone yet", so speech into
a headset that gates its noise to exact zeros never counted a pause and ran
to the limit. Once a floor is known a zero tick is quiet.

Listening that began in the middle of a sentence with shallow dips let the
floor creep up to the voice: no speech was ever seen and it gave up as
"nothing said" while the person talked. The floor now rises only in calm
moments (the last half second within half again of itself), a first sound
louder than any room is kept as a guess until a dip or a calm moment shows
the room, and sound that stays above the floor without being steady is
speech. Words count as speech from four ticks above the room, not two.

phoneWatch takes how long ago the recogniser last gave a sign of somebody
speaking: no words is no longer "nothing said" while it reports speech.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Fable 5.2 committed 2026-11-13 15:42:04 +00:00
1 parent cdb6a35371
commit a4a5c5dba1
2 files changed
+114 -25

No files matched your search

@@ -8,12 +8,16 @@ package eu.llembas.android.assist
* Nothing here is an absolute loudness: a room's own noise differs from phone to phone by more
* than a voice differs from a room. The **noise floor** is measured from the first sound on and
* followed all the time -- the quietest level of the last two seconds, taken at once when it
* falls and slowly when it rises, so the dips between words keep it down while somebody speaks
* and a steady noise (a fan, a car) becomes the floor within a moment. Speech and quiet are
* falls and slowly, in calm moments, when it rises: the dips between words keep it down while
* somebody speaks, and a steady noise (a fan, a car) becomes the floor within a moment. Speech and quiet are
* levels relative to it:
*
* - **speech** begins when the level is well above the floor in [ONSET_TICKS] of the last
* [ONSET_WINDOW] ticks -- a voice rises and falls, and one click or knock is not speech;
* - listening may begin in the middle of a sentence (the wake word's shape: the person goes
* straight on), and a voice that never dips far must not become "the room": the floor rises
* only while the sound is calm, and sound that stays above the floor without being steady is
* speech;
* - once somebody spoke, a **pause** of [PAUSE_MS] back near the floor ends it ([Verdict.Said]);
* - when **nothing is said**, it gives up after [NOTHING_SAID_MS] ([Verdict.Nothing]);
* - the words written down so far are a second ear: the same words for [SAME_WORDS_MS] end it,
@@ -43,6 +47,11 @@ class EndOfSpeech(private val tickMs: Long = TICK_MS) {
private var waited = 0L
private val loud = ArrayDeque<Boolean>()
private var stirred = 0
private var aboveRun = 0
/** The floor is a guess: listening began in the middle of sound louder than any room is taken to be. */
private var guessed = false
private var overGuess = 0
private var quietFor = 0L
private var words = ""
private var sameWordsFor = 0L
@@ -55,9 +64,14 @@ class EndOfSpeech(private val tickMs: Long = TICK_MS) {
/** One tick: the microphone's [level] (0..1, RMS) and the [heard] words written down so far. */
fun tick(level: Float, heard: String = ""): Verdict {
waited += tickMs
// Exact zero is a microphone that has not given anything yet (or gives nothing at all):
// it says nothing about the room.
if (level > 0f) follow(level)
// Exact zero before any sound is a microphone that has not given anything yet (or gives
// nothing at all): it says nothing about the room. After sound it is silence -- a
// headset that gates its noise gives exactly that between sentences -- and so a pause.
if (level > 0f) {
follow(level)
} else if (floor > 0f) {
judge(loud = false, above = false)
}
if (heard != words) {
words = heard
sameWordsFor = 0
@@ -78,20 +92,48 @@ class EndOfSpeech(private val tickMs: Long = TICK_MS) {
recent.addLast(level)
if (recent.size > WINDOW_TICKS) recent.removeFirst()
val lowest = recent.min()
// The first sound is the room, unless somebody is already speaking -- then a guess that
// the next dip corrects. After it: down at once, up slowly.
floor = when {
floor == 0f -> minOf(level, START_MAX)
lowest < floor -> lowest
else -> floor + (lowest - floor) * RISE
}.coerceAtLeast(MIN_FLOOR)
loud.addLast(level >= speechFrom())
if (loud.size > ONSET_WINDOW) loud.removeFirst()
if (loud.count { it } >= ONSET_TICKS) spoke = true
if (level >= pauseUnder()) {
// Steady over the whole window: a noise. Calm in the last half second: no voice now.
val steady = recent.size >= CALM_TICKS && recent.max() <= lowest * STEADY_RATIO
val last = recent.takeLast(CALM_TICKS)
val calm = last.size >= CALM_TICKS && last.max() <= last.min() * STEADY_RATIO
// The first sound is the room, unless somebody is already speaking -- then a guess,
// until a dip or a calm moment shows the room. After it: down at once; up slowly, and
// only while the sound is calm, so a voice does not lift the floor under itself.
when {
floor == 0f -> {
floor = minOf(level, START_MAX)
guessed = level > START_MAX
}
lowest < floor -> {
floor = lowest
guessed = false
}
calm -> {
floor += (lowest - floor) * RISE
guessed = false
}
}
floor = floor.coerceAtLeast(MIN_FLOOR)
judge(loud = level >= speechFrom(), above = level >= pauseUnder())
// Sound that stays above the floor and is not steady is a voice: one that never dips
// far, or -- while the floor is only a guess -- one that began before the listening did.
overGuess = if (guessed && level > floor) overGuess + 1 else 0
if ((aboveRun >= SUSTAINED_TICKS || overGuess >= SUSTAINED_TICKS) && !steady) spoke = true
}
/** One tick's sound, judged: [loud] enough to be speech beginning, or at least [above] a pause. */
private fun judge(loud: Boolean, above: Boolean) {
this.loud.addLast(loud)
if (this.loud.size > ONSET_WINDOW) this.loud.removeFirst()
if (this.loud.count { it } >= ONSET_TICKS) spoke = true
if (above) {
stirred++
aboveRun++
quietFor = 0
} else {
aboveRun = 0
quietFor += tickMs
}
}
@@ -117,7 +159,13 @@ class EndOfSpeech(private val tickMs: Long = TICK_MS) {
private const val PAUSE_MARGIN = 0.002f
private const val ONSET_TICKS = 3
private const val ONSET_WINDOW = 8
private const val STIRRED_TICKS = 2
private const val STIRRED_TICKS = 4
// Steady, calm: the loudest within half again of the quietest -- over the window, over
// the last half second. Sustained: above the floor for a second without a break.
private const val CALM_TICKS = 5
private const val STEADY_RATIO = 1.5f
private const val SUSTAINED_TICKS = 10
const val PAUSE_MS = 1_500L
const val SAME_WORDS_MS = 4_500L
@@ -142,15 +190,20 @@ enum class PhoneWatch {
/**
* The watch kept over the phone's recogniser, from how long it has listened ([waitedMs]), whether
* it has shown any words ([anyWords]) and for how long they have stayed the same ([sameForMs]).
* it has shown any words ([anyWords]), for how long they have stayed the same ([sameForMs]), and
* how long ago it last gave a sign of somebody speaking ([silentForMs]: its "speech began", a
* loud level, new words -- not every recogniser shows words while one speaks).
* A recogniser normally ends by itself; some never call back, and the sheet would listen for good.
* No words and nothing heard for a while gives up; when somebody did speak before that quiet, it
* is first asked for what it has.
*/
fun phoneWatch(waitedMs: Long, anyWords: Boolean, sameForMs: Long): PhoneWatch = when {
!anyWords && waitedMs >= PHONE_NOTHING_SAID_MS -> PhoneWatch.GiveUp
anyWords && sameForMs >= EndOfSpeech.SAME_WORDS_MS + PHONE_GRACE_MS -> PhoneWatch.Take
anyWords && sameForMs >= EndOfSpeech.SAME_WORDS_MS -> PhoneWatch.Finish
fun phoneWatch(waitedMs: Long, anyWords: Boolean, sameForMs: Long, silentForMs: Long = waitedMs): PhoneWatch = when {
waitedMs >= EndOfSpeech.LONGEST_MS + PHONE_GRACE_MS -> if (anyWords) PhoneWatch.Take else PhoneWatch.GiveUp
waitedMs >= EndOfSpeech.LONGEST_MS -> PhoneWatch.Finish
!anyWords && silentForMs >= PHONE_NOTHING_SAID_MS + PHONE_GRACE_MS -> PhoneWatch.GiveUp
!anyWords && silentForMs >= PHONE_NOTHING_SAID_MS -> if (silentForMs < waitedMs) PhoneWatch.Finish else PhoneWatch.GiveUp
anyWords && sameForMs >= EndOfSpeech.SAME_WORDS_MS + PHONE_GRACE_MS -> PhoneWatch.Take
anyWords && sameForMs >= EndOfSpeech.SAME_WORDS_MS && silentForMs >= EndOfSpeech.PAUSE_MS -> PhoneWatch.Finish
else -> PhoneWatch.Wait
}
@@ -44,12 +44,15 @@ class EndOfSpeechTest {
private fun still(level: Float, ticks: Int): List<Float> = List(ticks) { level }
/** Ended as said, a pause after [start]'s last sound: not before it was over, and not long after. */
private fun assertEndsAfter(start: List<Float>, ended: Ended) {
private fun assertEndsAfter(start: List<Float>, ended: Ended, late: Long = 400) {
val over = start.size * EndOfSpeech.TICK_MS
assertEquals(Verdict.Said, ended.verdict)
assertTrue("ended at ${ended.ms}, speech over at $over", ended.ms > over + EndOfSpeech.PAUSE_MS - 400 && ended.ms <= over + EndOfSpeech.PAUSE_MS + 400)
assertTrue("ended at ${ended.ms}, speech over at $over", ended.ms > over + EndOfSpeech.PAUSE_MS - 400 && ended.ms <= over + EndOfSpeech.PAUSE_MS + late)
}
/** A voice that never dips far: its quietest moments are six tenths of its loudest. One second. */
private fun steadyVoice(peak: Float): List<Float> = listOf(1.0f, 0.7f, 0.9f, 0.6f, 1.0f, 0.8f, 0.65f, 0.95f, 0.75f, 0.6f).map { it * peak }
@Test
fun aSilentRoomWithNothingSaidGivesUpSoon() {
assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(emptyList(), still(0.001f, 1)))
@@ -76,7 +79,8 @@ class EndOfSpeechTest {
@Test
fun speechInANoisyRoomEndsAfterThePauseToo() {
val start = noise(0.03f) + voice(0.12f)
assertEndsAfter(start, listen(start, noise(0.03f)))
// The room is louder than the first guess allows for: found a moment after the voice ends.
assertEndsAfter(start, listen(start, noise(0.03f)), late = 800)
}
@Test
@@ -95,6 +99,27 @@ class EndOfSpeechTest {
}
}
@Test
fun silenceFromAHeadsetThatGatesItsNoiseIsAPause() {
// After speech the microphone gives exact zeros: that is quiet, not "no microphone".
val start = still(0.001f, 5) + voice(0.06f)
assertEndsAfter(start, listen(start, still(0f, 1)))
val atOnce = voice(0.06f)
assertEndsAfter(atOnce, listen(atOnce, still(0f, 1)))
}
@Test
fun listeningThatBeginsInTheMiddleOfASentenceHearsIt() {
// The wake word's shape: the person goes straight on, and for eight seconds the voice
// never dips far. It must not become "the room", nor end as "nothing said" at five.
for (peak in listOf(0.035f, 0.05f, 0.1f)) {
var start = emptyList<Float>()
repeat(8) { start = start + steadyVoice(peak) }
assertEndsAfter(start, listen(start, still(0.002f, 1)))
assertEndsAfter(start, listen(start, still(0.004f, 1)))
}
}
@Test
fun aShortWordIsSpeech() {
val start = still(0.001f, 10) + listOf(0.03f, 0.05f, 0.04f, 0.01f)
@@ -173,4 +198,15 @@ class EndOfSpeechTest {
assertEquals(PhoneWatch.Finish, phoneWatch(EndOfSpeech.LONGEST_MS, anyWords = true, sameForMs = 0))
assertEquals(PhoneWatch.Take, phoneWatch(EndOfSpeech.LONGEST_MS + PHONE_GRACE_MS, anyWords = true, sameForMs = 0))
}
@Test
fun aRecogniserThatShowsNoWordsWhileOneSpeaksIsNotGivenUpOn() {
// No partial results, but it said speech began and its level is loud: somebody speaks.
assertEquals(PhoneWatch.Wait, phoneWatch(12_000, anyWords = false, sameForMs = 12_000, silentForMs = 300))
// Quiet for a good while after that: asked for what it has, then given up on.
assertEquals(PhoneWatch.Finish, phoneWatch(20_000, anyWords = false, sameForMs = 20_000, silentForMs = PHONE_NOTHING_SAID_MS))
assertEquals(PhoneWatch.GiveUp, phoneWatch(20_000, anyWords = false, sameForMs = 20_000, silentForMs = PHONE_NOTHING_SAID_MS + PHONE_GRACE_MS))
// Words that stopped changing while the level is still loud: not cut off yet.
assertEquals(PhoneWatch.Wait, phoneWatch(20_000, anyWords = true, sameForMs = EndOfSpeech.SAME_WORDS_MS, silentForMs = 200))
}
}