The sheet's end of speech (instance speech-to-text) was "level >= 0.02 is speech, level < 0.01 is quiet". A room between the two never counted as a pause, so once anything had counted as speech it listened to the 120 s limit; and words the speech-to-text made of silence counted as speech too. EndOfSpeech follows the noise floor (the quietest of the last two seconds, down at once, up slowly) and judges speech and pause against it: three loud ticks in eight begin speech, 1.5 s back near the floor end it, nothing said gives up after 5 s, words count only when the microphone heard something above the room, and the limit is 60 s. Tested with level sequences. The recording is not written down before anything was heard. The phone's recogniser gets the silence extras and a watch for one that never calls back; the recognition service ends at the pause instead of always at 15 s. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
6 files changed
+448
-37
No files matched your search
@@ -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. */
|
||||
|
||||
@@ -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<Float>()
|
||||
private var waited = 0L
|
||||
private val loud = ArrayDeque<Boolean>()
|
||||
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
|
||||
@@ -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
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user