diff --git a/CHANGELOG.md b/CHANGELOG.md index 10eb749..0b153e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,24 @@ What changed in each release of LLeMbas for Android. Versions follow SemVer; until 1.0.0 the app is in alpha and releases come off `main`. +## [Unreleased] + +### Fixed + +- **Listening went on and on in a quiet room.** With the instance's speech-to-text the sheet + decided "somebody is speaking" and "it is quiet again" by two fixed loudness levels. A phone + whose room noise sat above the quiet level never heard a pause, so it listened until the + two-minute limit; and words the speech-to-text invented from silence counted as speech. Now + the room's own level is measured from the first moment and followed all the time, and speech + and quiet are judged against it: nothing said ends after five seconds, a question ends a + second and a half after you stop, a click or a knock is not speech, and the limit is one + minute. Silence is no longer sent to be written down while you have said nothing. +- The phone's own recogniser (Voice input › Phone) is asked for the same pause, and is watched: + one that never answers no longer leaves the sheet listening for good. +- An app that uses the system's recogniser while LLeMbas is the assistant no longer waits the + full fifteen seconds every time: it ends at the pause after speech, or says that nothing was + said. + ## [1.0.0-beta.9] — 2026-11-13 For LLeMbas server 2.0.0-beta.12 or later. diff --git a/app/src/main/java/eu/llembas/android/assist/AssistController.kt b/app/src/main/java/eu/llembas/android/assist/AssistController.kt index 0d0105c..a74f850 100644 --- a/app/src/main/java/eu/llembas/android/assist/AssistController.kt +++ b/app/src/main/java/eu/llembas/android/assist/AssistController.kt @@ -136,6 +136,9 @@ class AssistController(private val context: Context, private val scope: Coroutin private val route = HeadsetRoute(context) private var following: Job? = null private var hearing: Job? = null + + /** The ear for the end of speech, while the instance's speech-to-text listens. */ + private var ear: EndOfSpeech? = null private var usingPhone = false private var screenText: String? = null private var stt: Boolean? = null @@ -256,8 +259,10 @@ class AssistController(private val context: Context, private val scope: Coroutin } }, ) + watchPhone() } else { recorder.preferred = headset + ear = EndOfSpeech(TICK_MS) recorder.start() launch { recorder.level.collect { l -> _state.update { it.copy(level = l) } } } launch { writeDownAsSaid() } @@ -271,7 +276,8 @@ class AssistController(private val context: Context, private val scope: Coroutin /** * The instance's speech-to-text takes a whole recording, so words "as they are said" are the * recording so far, written down again every moment -- one request at a time, only when half - * a second more has been said, and not past the first minute. + * a second more has been said, not past the first minute -- and not before the microphone + * has heard anything above the room: silence written down is an invented sentence. */ private suspend fun writeDownAsSaid() { val api = context.lembas.instances.client() ?: return @@ -279,44 +285,70 @@ class AssistController(private val context: Context, private val scope: Coroutin while (scope.isActive) { delay(PARTIAL_EVERY_MS) val seconds = recorder.seconds - if (seconds - sent < 0.5f || seconds > 60f) continue + if (seconds - sent < 0.5f || seconds > 60f || ear?.heardSomething != true) continue sent = seconds val words = runCatching { api.transcribe(recorder.snapshot()) }.getOrNull() if (!words.isNullOrBlank() && _state.value.listening) _state.update { it.copy(heard = words) } } } - /** Wait for a pause after speech (true) -- or for nothing said at all, which gives up (false). */ + /** + * Wait for a pause after speech (true) -- or for nothing said at all, which gives up (false). + * [EndOfSpeech] decides, from the microphone's level against the room's own and the words + * written down so far. + */ private suspend fun untilQuiet(): Boolean { - var spoke = false - var quietFor = 0L - var waited = 0L - // The words written down so far are the second ear, for a quiet microphone: words the - // instance heard mean speech, and the same words for a while mean it ended. - var words = "" - var sameWordsFor = 0L + val ear = ear ?: return false while (true) { delay(TICK_MS) - waited += TICK_MS - val level = recorder.level.value - if (level >= SPEAKING) { - spoke = true - quietFor = 0 - } else if (level < QUIET) { - quietFor += TICK_MS + when (ear.tick(recorder.level.value, _state.value.heard)) { + EndOfSpeech.Verdict.Said -> return true + EndOfSpeech.Verdict.Nothing -> return false + EndOfSpeech.Verdict.Listening -> Unit } + } + } + + /** + * The phone's recogniser finds the end of speech itself -- and is watched all the same + * ([phoneWatch]): one that never calls back would leave the sheet listening for good. + */ + private suspend fun watchPhone() { + var waited = 0L + var words = "" + var sameFor = 0L + var asked = false + while (_state.value.listening) { + delay(TICK_MS) + waited += TICK_MS val heard = _state.value.heard - if (heard.isNotBlank()) spoke = true if (heard != words) { words = heard - sameWordsFor = 0 - } else if (heard.isNotBlank()) { - sameWordsFor += TICK_MS + sameFor = 0 + } else { + sameFor += TICK_MS + } + when (phoneWatch(waited, heard.isNotBlank(), sameFor)) { + PhoneWatch.Wait -> Unit + + PhoneWatch.Finish -> if (!asked) { + asked = true + phone.finish() + } + + PhoneWatch.Take -> { + phone.stop() + _state.update { it.copy(listening = false, level = 0f, heard = "") } + askOrStop(heard) + return + } + + PhoneWatch.GiveUp -> { + phone.stop() + _state.update { it.copy(listening = false, level = 0f, heard = "", conversation = false) } + return + } } - if (spoke && quietFor >= PAUSE_MS) return true - if (sameWordsFor >= SAME_WORDS_MS) return true - if (!spoke && waited >= NOTHING_SAID_MS) return false - if (waited >= LONGEST_MS) return spoke } } @@ -600,16 +632,9 @@ class AssistController(private val context: Context, private val scope: Coroutin private const val TITLE_WAIT_MS = 20_000L private const val TITLE_ASK_MS = 4_000L - // Listening, for the instance's speech-to-text (the phone's recogniser finds the end of - // speech itself). Levels are the RMS of 16-bit samples, 0..1: a room is under 0.01, - // a voice near the phone 0.02 and up. Not tuned on many phones: the stop button always works. - private const val TICK_MS = 100L - private const val SPEAKING = 0.02f - private const val QUIET = 0.01f - private const val PAUSE_MS = 1_800L - private const val SAME_WORDS_MS = 4_500L - private const val NOTHING_SAID_MS = 8_000L - private const val LONGEST_MS = 120_000L + // Listening: how often the end of speech is looked for (EndOfSpeech has the rules and + // their numbers), and how often the recording so far is written down. + private const val TICK_MS = EndOfSpeech.TICK_MS private const val PARTIAL_EVERY_MS = 1_200L /** A reply's words, for a voice: its text steps, without the markup. */ diff --git a/app/src/main/java/eu/llembas/android/assist/EndOfSpeech.kt b/app/src/main/java/eu/llembas/android/assist/EndOfSpeech.kt new file mode 100644 index 0000000..d762ce1 --- /dev/null +++ b/app/src/main/java/eu/llembas/android/assist/EndOfSpeech.kt @@ -0,0 +1,161 @@ +package eu.llembas.android.assist + +/** + * When to stop listening, for the instance's speech-to-text (which takes a whole recording and + * finds no end of speech itself). Fed the microphone's level once a tick ([tick]); apart from + * Android, so it is tested with sequences of levels. + * + * 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 + * 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; + * - 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, + * and words count as speech for a quiet voice -- but only when the microphone heard at least + * something above the floor, because a speech-to-text model asked to write down silence + * invents a sentence; + * - and never longer than [LONGEST_MS]. + */ +class EndOfSpeech(private val tickMs: Long = TICK_MS) { + enum class Verdict { Listening, Said, Nothing } + + /** The room's own level, as followed so far; 0 until the microphone gave any sound. */ + var floor = 0f + private set + + /** Whether somebody spoke. */ + var spoke = false + private set + + /** + * Whether the microphone heard anything above the room at all: only then is the recording + * worth writing down while it runs. + */ + val heardSomething: Boolean get() = spoke || stirred >= STIRRED_TICKS + + private val recent = ArrayDeque() + private var waited = 0L + private val loud = ArrayDeque() + private var stirred = 0 + private var quietFor = 0L + private var words = "" + private var sameWordsFor = 0L + + /** The level from which a sound is speech beginning, and the level under which it is a pause. */ + private fun speechFrom(): Float = maxOf(floor * SPEECH_RATIO, floor + SPEECH_MARGIN) + + private fun pauseUnder(): Float = maxOf(floor * PAUSE_RATIO, floor + PAUSE_MARGIN) + + /** 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) + if (heard != words) { + words = heard + sameWordsFor = 0 + } else if (heard.isNotBlank()) { + sameWordsFor += tickMs + } + if (heard.isNotBlank() && stirred >= STIRRED_TICKS) spoke = true + return when { + spoke && quietFor >= PAUSE_MS -> Verdict.Said + spoke && sameWordsFor >= SAME_WORDS_MS -> Verdict.Said + !spoke && waited >= NOTHING_SAID_MS -> Verdict.Nothing + waited >= LONGEST_MS -> if (spoke) Verdict.Said else Verdict.Nothing + else -> Verdict.Listening + } + } + + private fun follow(level: Float) { + 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()) { + stirred++ + quietFor = 0 + } else { + quietFor += tickMs + } + } + + companion object { + const val TICK_MS = 100L + + /** How long the room's quietest level is looked for: two seconds. */ + private const val WINDOW_TICKS = 20 + + /** How fast the floor follows a room that got louder: a twentieth of the way each tick. */ + private const val RISE = 0.05f + + /** No microphone is quieter than this; and the most the first sound may claim as "the room". */ + private const val MIN_FLOOR = 0.0008f + private const val START_MAX = 0.02f + + // Speech begins at a little over twice the floor (and at least a small step above it, for + // a silent room); a pause is back within 1.6 times of it. Between the two nothing changes. + private const val SPEECH_RATIO = 2.2f + private const val SPEECH_MARGIN = 0.004f + private const val PAUSE_RATIO = 1.6f + 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 + + const val PAUSE_MS = 1_500L + const val SAME_WORDS_MS = 4_500L + const val NOTHING_SAID_MS = 5_000L + const val LONGEST_MS = 60_000L + } +} + +/** What to do about the phone's own recogniser, which finds the end of speech itself -- usually. */ +enum class PhoneWatch { + Wait, + + /** Ask it to stop and give what it has. */ + Finish, + + /** It did not answer the asking either: take the words it showed. */ + Take, + + /** Nothing was said, and it has not said so itself. */ + GiveUp, +} + +/** + * 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]). + * A recogniser normally ends by itself; some never call back, and the sheet would listen for good. + */ +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 + waitedMs >= EndOfSpeech.LONGEST_MS + PHONE_GRACE_MS -> if (anyWords) PhoneWatch.Take else PhoneWatch.GiveUp + waitedMs >= EndOfSpeech.LONGEST_MS -> PhoneWatch.Finish + else -> PhoneWatch.Wait +} + +/** Longer than the recogniser's own "no speech" (about five seconds on most): it gets to say so first. */ +const val PHONE_NOTHING_SAID_MS = 8_000L + +/** How long a recogniser that was asked to stop has to answer. */ +const val PHONE_GRACE_MS = 2_500L diff --git a/app/src/main/java/eu/llembas/android/assist/LembasRecognitionService.kt b/app/src/main/java/eu/llembas/android/assist/LembasRecognitionService.kt index 97082be..c99303f 100644 --- a/app/src/main/java/eu/llembas/android/assist/LembasRecognitionService.kt +++ b/app/src/main/java/eu/llembas/android/assist/LembasRecognitionService.kt @@ -19,7 +19,8 @@ import kotlinx.coroutines.launch * Speech recognition through the instance (`/v1/audio/transcriptions`). Android requires a * digital assistant to name a recognition service; this is it, and any app that asks the * system's recognizer while LLeMbas is the assistant is answered by the instance's - * speech-to-text. Listening ends when the caller says stop, or after [LIMIT_MS]. + * speech-to-text. Listening ends when the caller says stop, at a pause after speech or when + * nothing is said ([EndOfSpeech], as in the assistant's sheet), or after [LIMIT_MS]. */ class LembasRecognitionService : RecognitionService() { private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) @@ -37,7 +38,23 @@ class LembasRecognitionService : RecognitionService() { listener.readyForSpeech(Bundle()) listener.beginningOfSpeech() limit = scope.launch { - delay(LIMIT_MS) + val ear = EndOfSpeech() + var waited = 0L + while (waited < LIMIT_MS) { + delay(EndOfSpeech.TICK_MS) + waited += EndOfSpeech.TICK_MS + when (ear.tick(recorder.level.value)) { + EndOfSpeech.Verdict.Said -> break + + // Nothing said is nothing sent: silence written down is an invented sentence. + EndOfSpeech.Verdict.Nothing -> { + silent() + return@launch + } + + EndOfSpeech.Verdict.Listening -> Unit + } + } finish() } } @@ -52,6 +69,16 @@ class LembasRecognitionService : RecognitionService() { scope.launch { recorder.stop() } } + /** Nobody spoke: the caller is told so, and nothing is written down. */ + private fun silent() { + val listener = callback ?: return + callback = null + scope.launch { + recorder.stop() + listener.error(SpeechRecognizer.ERROR_SPEECH_TIMEOUT) + } + } + private fun finish() { val listener = callback ?: return callback = null diff --git a/app/src/main/java/eu/llembas/android/assist/Voice.kt b/app/src/main/java/eu/llembas/android/assist/Voice.kt index 541158e..921acc6 100644 --- a/app/src/main/java/eu/llembas/android/assist/Voice.kt +++ b/app/src/main/java/eu/llembas/android/assist/Voice.kt @@ -311,6 +311,10 @@ class PhoneRecognizer(private val context: Context) { Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) .putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) .putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true) + // How long a silence ends it: a hint, which not every recogniser takes -- the + // sheet keeps its own watch as well (phoneWatch). + .putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, EndOfSpeech.PAUSE_MS) + .putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, EndOfSpeech.PAUSE_MS) .putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.packageName), ) } diff --git a/app/src/test/java/eu/llembas/android/EndOfSpeechTest.kt b/app/src/test/java/eu/llembas/android/EndOfSpeechTest.kt new file mode 100644 index 0000000..b67672d --- /dev/null +++ b/app/src/test/java/eu/llembas/android/EndOfSpeechTest.kt @@ -0,0 +1,176 @@ +package eu.llembas.android + +import eu.llembas.android.assist.EndOfSpeech +import eu.llembas.android.assist.EndOfSpeech.Verdict +import eu.llembas.android.assist.PHONE_GRACE_MS +import eu.llembas.android.assist.PHONE_NOTHING_SAID_MS +import eu.llembas.android.assist.PhoneWatch +import eu.llembas.android.assist.phoneWatch +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** When the sheet stops listening, from sequences of microphone levels (one each 100 ms). */ +class EndOfSpeechTest { + /** How it ended and after how many milliseconds. */ + private data class Ended(val verdict: Verdict, val ms: Long) + + /** + * Feed [start], then [rest] over and over, until a verdict; [words] are what the instance + * has written down by each tick. + */ + private fun listen(start: List, rest: List, words: (Int) -> String = { "" }): Ended { + val ear = EndOfSpeech() + var tick = 0 + while (tick < 5_000) { + val level = if (tick < start.size) start[tick] else rest[(tick - start.size) % rest.size] + val verdict = ear.tick(level, words(tick)) + tick++ + if (verdict != Verdict.Listening) return Ended(verdict, tick * EndOfSpeech.TICK_MS) + } + error("it never stopped listening") + } + + /** A steady noise around [level]: no two moments quite the same. */ + private fun noise(level: Float): List = listOf(1.0f, 0.9f, 1.1f, 0.95f, 1.15f, 0.85f, 1.05f).map { it * level } + + /** Two seconds of a voice whose loudest moments are [peak]: it rises and falls between words. */ + private fun voice(peak: Float): List { + val second = listOf(0.6f, 1.0f, 0.8f, 0.3f, 0.9f, 1.0f, 0.5f, 0.7f, 1.0f, 0.4f) + return (second + second).map { it * peak } + } + + private fun still(level: Float, ticks: Int): List = 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, ended: Ended) { + 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) + } + + @Test + fun aSilentRoomWithNothingSaidGivesUpSoon() { + assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(emptyList(), still(0.001f, 1))) + } + + @Test + fun aNoisyRoomWithNothingSaidGivesUpAsSoon() { + // Louder than any fixed "this is a voice" level could allow for: the noise is the floor. + assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(emptyList(), noise(0.03f))) + assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(emptyList(), noise(0.012f))) + } + + @Test + fun aMicrophoneThatGivesNothingAtAllGivesUpToo() { + assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(emptyList(), still(0f, 1))) + } + + @Test + fun speechThenAPauseEndsAfterThePause() { + val start = still(0.001f, 5) + voice(0.06f) + assertEndsAfter(start, listen(start, still(0.001f, 1))) + } + + @Test + fun speechInANoisyRoomEndsAfterThePauseToo() { + val start = noise(0.03f) + voice(0.12f) + assertEndsAfter(start, listen(start, noise(0.03f))) + } + + @Test + fun aRoomThatIsNeverQuietByAFixedLevelStillEnds() { + // The room sits at 0.015: above what used to count as quiet, so it listened to the limit. + val start = still(0.015f, 5) + voice(0.06f) + assertEndsAfter(start, listen(start, still(0.015f, 1))) + } + + @Test + fun speakingAtOnceIsHeard() { + // No quiet moment first to measure the room by: the first dip between words gives it. + for (peak in listOf(0.05f, 0.08f, 0.3f)) { + val start = voice(peak) + assertEndsAfter(start, listen(start, still(0.002f, 1))) + } + } + + @Test + fun aShortWordIsSpeech() { + val start = still(0.001f, 10) + listOf(0.03f, 0.05f, 0.04f, 0.01f) + assertEndsAfter(start, listen(start, still(0.001f, 1))) + } + + @Test + fun aClickIsNotSpeech() { + assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(still(0.001f, 5) + listOf(0.3f), still(0.001f, 1))) + val twice = still(0.001f, 5) + listOf(0.3f, 0.3f) + still(0.001f, 20) + listOf(0.3f, 0.3f) + assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(twice, still(0.001f, 1))) + } + + @Test + fun aLongQuestionIsNotCutShort() { + var start = still(0.001f, 5) + repeat(10) { start = start + voice(0.06f) } + assertEndsAfter(start, listen(start, still(0.001f, 1))) + } + + @Test + fun soundThatNeverPausesEndsAtTheLimit() { + val ended = listen(emptyList(), listOf(0.01f, 0.2f, 0.01f, 0.2f, 0.2f, 0.2f, 0.01f)) + assertEquals(Ended(Verdict.Said, EndOfSpeech.LONGEST_MS), ended) + } + + @Test + fun theSameWordsForAWhileEndIt() { + // Music that never pauses, and the words stopped coming: that is the end. + val ended = listen(still(0.001f, 5), listOf(0.01f, 0.2f, 0.01f, 0.2f, 0.2f, 0.2f, 0.01f)) { tick -> if (tick > 20) "play something" else "" } + assertEquals(Verdict.Said, ended.verdict) + assertTrue(ended.ms < 8_000) + } + + @Test + fun wordsInventedFromSilenceAreNotSpeech() { + // A speech-to-text model handed silence writes "Thank you." -- in a silent room and in a + // noisy one the microphone heard nothing above the room, so nothing was said. + val invented = { tick: Int -> if (tick > 12) "Thank you." else "" } + assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(emptyList(), still(0.001f, 1), invented)) + assertEquals(Ended(Verdict.Nothing, EndOfSpeech.NOTHING_SAID_MS), listen(emptyList(), noise(0.03f), invented)) + } + + @Test + fun aQuietVoiceCountsOnceItsWordsCome() { + // Too soft to be sure of by loudness alone, but above the room, and the instance wrote words down. + val start = still(0.001f, 5) + still(0.0035f, 20) + assertEndsAfter(start, listen(start, still(0.001f, 1)) { tick -> if (tick > 15) "what time is it" else "" }) + } + + @Test + fun theFloorFollowsTheRoom() { + val ear = EndOfSpeech() + assertFalse(ear.heardSomething) + repeat(30) { ear.tick(noise(0.03f)[it % 7]) } + // Near the quietest of the noise, not at a fixed number. + assertTrue(ear.floor > 0.02f && ear.floor < 0.03f) + assertFalse(ear.heardSomething) + assertFalse(ear.spoke) + voice(0.12f).forEach { ear.tick(it) } + assertTrue(ear.spoke) + assertTrue(ear.heardSomething) + } + + @Test + fun thePhonesRecogniserIsWatched() { + // It normally ends by itself; the watch is for one that does not. + assertEquals(PhoneWatch.Wait, phoneWatch(3_000, anyWords = false, sameForMs = 3_000)) + assertEquals(PhoneWatch.GiveUp, phoneWatch(PHONE_NOTHING_SAID_MS, anyWords = false, sameForMs = PHONE_NOTHING_SAID_MS)) + // Words still coming: left alone. + assertEquals(PhoneWatch.Wait, phoneWatch(20_000, anyWords = true, sameForMs = 400)) + // The same words for a while: asked to stop, then taken as they are. + assertEquals(PhoneWatch.Finish, phoneWatch(20_000, anyWords = true, sameForMs = EndOfSpeech.SAME_WORDS_MS)) + assertEquals(PhoneWatch.Take, phoneWatch(20_000, anyWords = true, sameForMs = EndOfSpeech.SAME_WORDS_MS + PHONE_GRACE_MS)) + // And never past the limit. + 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)) + } +}