Wake phrases the model can hear: greeting x name, generated pieces, sensitivity
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
"Hey Lembas" could not work well: Lembas is not a word the model knows. - The phrase is a greeting (Hey, OK, Hello) and a name (Assistant, Computer, Google, and ten ordinary names); the default is Hey Assistant and an old saved id reads as it. - Token pieces are generated, not hand-written: fetch-wakeword.py also takes the model's bpe.model from the same pinned archive, gen-wake-phrases.py encodes every word with it into wake/WakeWords.kt, CI checks the committed table against the tokenizer, and WakeWordsTest checks every piece against tokens.txt. "OK" is spelled as the model's speech wrote it, OKAY. Elizabeth and Benjamin are dropped (four or more single-letter pieces). - Each keyword line carries a boosting score and a trigger threshold (sherpa-onnx's ":boost #threshold"), chosen by a Sensitivity setting: Low 1.0/0.35, Normal 1.5/0.25, High 2.0/0.15; short phrases 0.05 stricter. numTrailingBlanks is the engine's own 1. - Settings says the phrase is English whatever the app's language is, and that a Google device nearby answers to Google too. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
26 files changed
+691
-41
No files matched your search
@@ -89,8 +89,10 @@ data class Prefs(
|
||||
val assistantConversation: Boolean = true,
|
||||
/** Listen for a wake word on the phone (WakeWordService). Off until the person turns it on. */
|
||||
val wakeWord: Boolean = false,
|
||||
/** Which of WakePhrases.all. */
|
||||
val wakePhrase: String = "hey_lembas",
|
||||
/** Which of WakePhrases.all, as `greeting_name`; an id that is no phrase any more reads as the default. */
|
||||
val wakePhrase: String = "hey_assistant",
|
||||
/** WakeSensitivity's id: low, normal or high. */
|
||||
val wakeSensitivity: String = "normal",
|
||||
/** Listen only while the screen is on. */
|
||||
val wakeScreenOnly: Boolean = true,
|
||||
/** The assistant may read the notifications when asked (with Android's notification access). */
|
||||
|
||||
@@ -53,6 +53,7 @@ import androidx.compose.material.icons.filled.Security
|
||||
import androidx.compose.material.icons.filled.SystemUpdate
|
||||
import androidx.compose.material.icons.filled.Timer
|
||||
import androidx.compose.material.icons.filled.Translate
|
||||
import androidx.compose.material.icons.filled.Tune
|
||||
import androidx.compose.material.icons.filled.Update
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.Icon
|
||||
@@ -106,10 +107,13 @@ import eu.llembas.android.update.offersBetas
|
||||
import eu.llembas.android.wake.WakeEngine
|
||||
import eu.llembas.android.wake.WakeFailure
|
||||
import eu.llembas.android.wake.WakePause
|
||||
import eu.llembas.android.wake.WakePhrase
|
||||
import eu.llembas.android.wake.WakePhrases
|
||||
import eu.llembas.android.wake.WakeSensitivity
|
||||
import eu.llembas.android.wake.WakeState
|
||||
import eu.llembas.android.wake.WakeStatus
|
||||
import eu.llembas.android.wake.WakeWordService
|
||||
import eu.llembas.android.wake.WakeWords
|
||||
import eu.llembas.android.wake.wakeTitle
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@@ -129,6 +133,7 @@ interface SettingsActions {
|
||||
fun assistantConversation(on: Boolean) {}
|
||||
fun wakeWord(on: Boolean) {}
|
||||
fun wakePhrase(id: String) {}
|
||||
fun wakeSensitivity(id: String) {}
|
||||
fun wakeScreenOnly(on: Boolean) {}
|
||||
fun batterySettings() {}
|
||||
fun notificationSummaries(on: Boolean) {}
|
||||
@@ -272,6 +277,10 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
scope.launch { store.savePrefs { it.copy(wakePhrase = id) } }
|
||||
}
|
||||
|
||||
override fun wakeSensitivity(id: String) {
|
||||
scope.launch { store.savePrefs { it.copy(wakeSensitivity = id) } }
|
||||
}
|
||||
|
||||
override fun wakeScreenOnly(on: Boolean) {
|
||||
scope.launch { store.savePrefs { it.copy(wakeScreenOnly = on) } }
|
||||
}
|
||||
@@ -373,6 +382,15 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
fun SettingsContent(state: SettingsState, actions: SettingsActions, onBack: () -> Unit = {}) {
|
||||
val palette = LocalPalette.current
|
||||
var choosingLanguage by remember { mutableStateOf(false) }
|
||||
var choosingWake by remember { mutableStateOf<WakeChoice?>(null) }
|
||||
choosingWake?.let { which ->
|
||||
ModalBottomSheet(onDismissRequest = { choosingWake = null }, sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)) {
|
||||
WakeChoices(which, state.prefs, onPick = { pick ->
|
||||
choosingWake = null
|
||||
pick(actions)
|
||||
})
|
||||
}
|
||||
}
|
||||
if (choosingLanguage) {
|
||||
ModalBottomSheet(onDismissRequest = { choosingLanguage = false }, sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)) {
|
||||
LanguageChoices(state.prefs, onPick = {
|
||||
@@ -539,7 +557,7 @@ fun SettingsContent(state: SettingsState, actions: SettingsActions, onBack: () -
|
||||
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
)
|
||||
}
|
||||
item { WakeWordGroup(state, actions) }
|
||||
item { WakeWordGroup(state, actions, onChoose = { choosingWake = it }) }
|
||||
item { NotificationSummaryGroup(state, actions) }
|
||||
item { PhoneActionsGroup(state, actions) }
|
||||
item {
|
||||
@@ -623,12 +641,16 @@ fun SettingsContent(state: SettingsState, actions: SettingsActions, onBack: () -
|
||||
}
|
||||
}
|
||||
|
||||
/** The three things about the wake word that are chosen from a list, each in a sheet. */
|
||||
enum class WakeChoice { Greeting, Name, Sensitivity }
|
||||
|
||||
/**
|
||||
* The wake word: off until turned on; the phrase (the engine's own, English); only while the
|
||||
* screen is on; and what it costs and why a phone may stop it, said before it is turned on.
|
||||
* The wake word: off until turned on; the phrase as a greeting and a name (English, words the
|
||||
* engine's model knows); how easily it is taken as heard; only while the screen is on; and what
|
||||
* it costs and why a phone may stop it, said before it is turned on.
|
||||
*/
|
||||
@Composable
|
||||
private fun WakeWordGroup(state: SettingsState, actions: SettingsActions) {
|
||||
private fun WakeWordGroup(state: SettingsState, actions: SettingsActions, onChoose: (WakeChoice) -> Unit) {
|
||||
val palette = LocalPalette.current
|
||||
val on = state.prefs.wakeWord && state.wakeAvailable
|
||||
Group(stringResource(R.string.wake_word)) {
|
||||
@@ -662,16 +684,37 @@ private fun WakeWordGroup(state: SettingsState, actions: SettingsActions) {
|
||||
)
|
||||
}
|
||||
if (state.wakeAvailable) {
|
||||
WakePhrases.all.forEach { phrase ->
|
||||
Rule()
|
||||
SettingRow(
|
||||
Icons.Filled.RecordVoiceOver,
|
||||
"“${phrase.words}”",
|
||||
showIcon = false,
|
||||
onClick = { actions.wakePhrase(phrase.id) },
|
||||
trailing = { RadioButton(selected = state.prefs.wakePhrase == phrase.id, onClick = { actions.wakePhrase(phrase.id) }) },
|
||||
)
|
||||
}
|
||||
val phrase = WakePhrases.byId(state.prefs.wakePhrase)
|
||||
Rule()
|
||||
SettingRow(
|
||||
Icons.Filled.RecordVoiceOver,
|
||||
"“${phrase.words}”",
|
||||
subtitle = stringResource(R.string.wake_phrase_line),
|
||||
onClick = { onChoose(WakeChoice.Name) },
|
||||
)
|
||||
Rule()
|
||||
SettingRow(
|
||||
Icons.Filled.RecordVoiceOver,
|
||||
stringResource(R.string.wake_greeting),
|
||||
subtitle = phrase.greeting.words,
|
||||
showIcon = false,
|
||||
onClick = { onChoose(WakeChoice.Greeting) },
|
||||
)
|
||||
Rule()
|
||||
SettingRow(
|
||||
Icons.Filled.RecordVoiceOver,
|
||||
stringResource(R.string.wake_name),
|
||||
subtitle = if (phrase.name.id == GOOGLE) phrase.name.words + " · " + stringResource(R.string.wake_name_google_line) else phrase.name.words,
|
||||
showIcon = false,
|
||||
onClick = { onChoose(WakeChoice.Name) },
|
||||
)
|
||||
Rule()
|
||||
SettingRow(
|
||||
Icons.Filled.Tune,
|
||||
stringResource(R.string.wake_sensitivity),
|
||||
subtitle = stringResource(sensitivityLabel(WakeSensitivity.byId(state.prefs.wakeSensitivity))),
|
||||
onClick = { onChoose(WakeChoice.Sensitivity) },
|
||||
)
|
||||
Rule()
|
||||
SettingRow(
|
||||
Icons.Filled.ScreenLockPortrait,
|
||||
@@ -860,6 +903,84 @@ private fun languageLabel(prefs: Prefs): String = when (prefs.language) {
|
||||
else -> AppLanguage.name(prefs.language) ?: stringResource(R.string.language_account)
|
||||
}
|
||||
|
||||
private const val GOOGLE = "google"
|
||||
|
||||
private fun sensitivityLabel(sensitivity: WakeSensitivity): Int = when (sensitivity) {
|
||||
WakeSensitivity.Low -> R.string.wake_sensitivity_low
|
||||
WakeSensitivity.Normal -> R.string.wake_sensitivity_normal
|
||||
WakeSensitivity.High -> R.string.wake_sensitivity_high
|
||||
}
|
||||
|
||||
/**
|
||||
* One of the wake word's lists, as a sheet: the greetings, the names (Google's says that a Google
|
||||
* device nearby answers to it too), or the sensitivity with what each step trades. A pick is
|
||||
* handed back as what to do with the actions.
|
||||
*/
|
||||
@Composable
|
||||
fun WakeChoices(which: WakeChoice, prefs: Prefs, onPick: ((SettingsActions) -> Unit) -> Unit) {
|
||||
val phrase = WakePhrases.byId(prefs.wakePhrase)
|
||||
val google = stringResource(R.string.wake_name_google_line)
|
||||
val title = stringResource(
|
||||
when (which) {
|
||||
WakeChoice.Greeting -> R.string.wake_greeting
|
||||
WakeChoice.Name -> R.string.wake_name
|
||||
WakeChoice.Sensitivity -> R.string.wake_sensitivity
|
||||
},
|
||||
)
|
||||
// id, words, a line under them, whether it is the one chosen.
|
||||
val choices: List<Choice> = when (which) {
|
||||
WakeChoice.Greeting -> WakeWords.greetings.map { Choice(WakePhrase(it, phrase.name).id, it.words, null, it.id == phrase.greeting.id) }
|
||||
|
||||
WakeChoice.Name -> WakeWords.names.map {
|
||||
Choice(WakePhrase(phrase.greeting, it).id, it.words, if (it.id == GOOGLE) google else null, it.id == phrase.name.id)
|
||||
}
|
||||
|
||||
WakeChoice.Sensitivity -> WakeSensitivity.entries.map {
|
||||
val line = when (it) {
|
||||
WakeSensitivity.Low -> R.string.wake_sensitivity_low_line
|
||||
WakeSensitivity.Normal -> R.string.wake_sensitivity_normal_line
|
||||
WakeSensitivity.High -> R.string.wake_sensitivity_high_line
|
||||
}
|
||||
Choice(it.id, stringResource(sensitivityLabel(it)), stringResource(line), it.id == WakeSensitivity.byId(prefs.wakeSensitivity).id)
|
||||
}
|
||||
}
|
||||
ChoiceList(title, choices) { id ->
|
||||
onPick { actions -> if (which == WakeChoice.Sensitivity) actions.wakeSensitivity(id) else actions.wakePhrase(id) }
|
||||
}
|
||||
}
|
||||
|
||||
private data class Choice(val id: String, val title: String, val subtitle: String?, val chosen: Boolean)
|
||||
|
||||
/** A sheet's list of choices: a title, then rows of words that give and a radio button that keeps its size. */
|
||||
@Composable
|
||||
private fun ChoiceList(title: String, choices: List<Choice>, onPick: (String) -> Unit) {
|
||||
val palette = LocalPalette.current
|
||||
LazyColumn(Modifier.fillMaxWidth(), contentPadding = PaddingValues(bottom = 24.dp)) {
|
||||
item {
|
||||
Text(title, style = MaterialTheme.typography.titleMedium, modifier = Modifier.padding(start = 24.dp, end = 24.dp, bottom = 8.dp))
|
||||
}
|
||||
choices.forEach { choice ->
|
||||
item(key = choice.id) {
|
||||
Row(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { onPick(choice.id) }
|
||||
.heightIn(min = 56.dp)
|
||||
.padding(horizontal = 24.dp, vertical = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text(choice.title, style = MaterialTheme.typography.bodyLarge)
|
||||
choice.subtitle?.let { Text(it, style = MaterialTheme.typography.bodyMedium, color = palette.inkMuted) }
|
||||
}
|
||||
Spacer(Modifier.width(12.dp))
|
||||
RadioButton(selected = choice.chosen, onClick = { onPick(choice.id) })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings › Language, as a sheet: the account's language (the default), the phone's, then every
|
||||
* language the app has, each named in itself so a person who cannot read the current one still
|
||||
|
||||
@@ -9,31 +9,62 @@ import kotlinx.coroutines.flow.update
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.util.Locale
|
||||
|
||||
/** One word of a wake phrase: its [id], how it is written for the person, and its spelling in the model's [pieces]. */
|
||||
data class WakeWord(val id: String, val words: String, val pieces: String)
|
||||
|
||||
/**
|
||||
* A wake word the engine ships: what the person says ([words]), the engine's spelling of it
|
||||
* ([tokens], the model's BPE pieces, worked out once with its `bpe.model` -- the phone has no
|
||||
* tokenizer), and the id Settings keeps.
|
||||
* A wake phrase: a greeting and a name ("Hey Assistant"). What the person says ([words]), the
|
||||
* engine's spelling of it ([tokens], the model's BPE pieces -- generated with its `bpe.model` by
|
||||
* `scripts/gen-wake-phrases.py`, since the phone has no tokenizer), and the [id] Settings keeps.
|
||||
*
|
||||
* Only phrases from this list can be chosen: an open-vocabulary spotter hears any phrase it can
|
||||
* spell, but how well each one works was not measured, and a free text box would invite one that
|
||||
* fires on every other sentence. English only: the model is trained on English speech.
|
||||
* Only words from [WakeWords] can be chosen: an open-vocabulary spotter hears any phrase it can
|
||||
* spell, but one it was never trained to hear (a made-up name) has to be repeated until it lands,
|
||||
* and a free text box would invite one that fires on every other sentence. English only: the
|
||||
* model is trained on English speech.
|
||||
*/
|
||||
data class WakePhrase(val id: String, val words: String, val tokens: String)
|
||||
data class WakePhrase(val greeting: WakeWord, val name: WakeWord) {
|
||||
val id: String get() = "${greeting.id}_${name.id}"
|
||||
val words: String get() = "${greeting.words} ${name.words}"
|
||||
val tokens: String get() = "${greeting.pieces} ${name.pieces}"
|
||||
}
|
||||
|
||||
object WakePhrases {
|
||||
val all = listOf(
|
||||
WakePhrase("hey_lembas", "Hey Lembas", "▁HE Y ▁LE M B AS"),
|
||||
WakePhrase("ok_lembas", "OK Lembas", "▁O K ▁LE M B AS"),
|
||||
WakePhrase("hello_lembas", "Hello Lembas", "▁HE LL O ▁LE M B AS"),
|
||||
WakePhrase("hey_assistant", "Hey Assistant", "▁HE Y ▁AS S IST ANT"),
|
||||
)
|
||||
/** "Hey Assistant". */
|
||||
val default: WakePhrase = WakePhrase(WakeWords.greetings.first { it.id == "hey" }, WakeWords.names.first { it.id == "assistant" })
|
||||
|
||||
val default: WakePhrase get() = all.first()
|
||||
val all: List<WakePhrase> = WakeWords.greetings.flatMap { greeting -> WakeWords.names.map { WakePhrase(greeting, it) } }
|
||||
|
||||
/**
|
||||
* The phrase Settings saved as [id] (`greeting_name`). An id that is no longer a phrase --
|
||||
* the "Lembas" ones of the first wake word, which the model could not hear -- is the default.
|
||||
*/
|
||||
fun byId(id: String): WakePhrase = all.firstOrNull { it.id == id } ?: default
|
||||
}
|
||||
|
||||
/**
|
||||
* How easily the phrase is taken as heard: Settings › Wake word › Sensitivity.
|
||||
*
|
||||
* The engine has two numbers per keyword (sherpa-onnx's keyword-spotting documentation): a
|
||||
* boosting score that keeps the phrase's path alive in the search ([boost], larger is easier),
|
||||
* and a trigger threshold, the least probability the heard pieces must reach ([threshold], 0 to
|
||||
* 1, lower is easier). Normal is the engine's own default threshold with the boost its
|
||||
* documentation uses for a two-word phrase; Low takes that documentation's stricter example
|
||||
* (0.35), High goes as far below the default as Low goes above it. Starting values: nobody has
|
||||
* measured them on a phone yet.
|
||||
*/
|
||||
enum class WakeSensitivity(val id: String, val boost: Float, val threshold: Float) {
|
||||
Low("low", 1.0f, 0.35f),
|
||||
Normal("normal", 1.5f, 0.25f),
|
||||
High("high", 2.0f, 0.15f),
|
||||
;
|
||||
|
||||
companion object {
|
||||
fun byId(id: String): WakeSensitivity = entries.firstOrNull { it.id == id } ?: Normal
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The engine, as the service sees it: 16 kHz mono samples in, the phrase out when it was heard.
|
||||
* Everything happens on the phone; [accept] neither records nor sends anything.
|
||||
@@ -171,8 +202,21 @@ fun wakeLine(context: Context, state: WakeState): String? = when (state) {
|
||||
else -> null
|
||||
}
|
||||
|
||||
/** The engine's keywords file for [phrase]: its pieces, then `@` and the id the engine answers with. */
|
||||
fun wakeKeywords(phrase: WakePhrase): String = "${phrase.tokens} @${phrase.id}\n"
|
||||
/**
|
||||
* The engine's keywords file for [phrase]: its pieces, `:` the boosting score, `#` the trigger
|
||||
* threshold, then `@` and the id the engine answers with (sherpa-onnx's syntax; no space after
|
||||
* `:` or `#`). A phrase of fewer than five pieces is short enough to turn up inside other speech,
|
||||
* so it has to be a little surer ([SHORT_PHRASE_STRICTER]).
|
||||
*/
|
||||
fun wakeKeywords(phrase: WakePhrase, sensitivity: WakeSensitivity = WakeSensitivity.Normal): String {
|
||||
val short = phrase.tokens.split(' ').size < SHORT_PHRASE_PIECES
|
||||
val threshold = sensitivity.threshold + if (short) SHORT_PHRASE_STRICTER else 0f
|
||||
// Locale.ROOT: a decimal comma would not be a number to the engine.
|
||||
return String.format(Locale.ROOT, "%s :%.1f #%.2f @%s\n", phrase.tokens, sensitivity.boost, threshold, phrase.id)
|
||||
}
|
||||
|
||||
private const val SHORT_PHRASE_PIECES = 5
|
||||
private const val SHORT_PHRASE_STRICTER = 0.05f
|
||||
|
||||
/**
|
||||
* Every piece of [keywords] must be in the model's token table ([tokens], `tokens.txt`: a piece
|
||||
|
||||
@@ -145,7 +145,7 @@ class WakeWordService : LifecycleService() {
|
||||
null
|
||||
} else {
|
||||
val phrase = WakePhrases.byId(prefs.wakePhrase)
|
||||
WakePlan(phrase, wakeKeywords(phrase), wakePause(on, prefs.wakeScreenOnly, saver, open > 0, mic))
|
||||
WakePlan(phrase, wakeKeywords(phrase, WakeSensitivity.byId(prefs.wakeSensitivity)), wakePause(on, prefs.wakeScreenOnly, saver, open > 0, mic))
|
||||
}
|
||||
}
|
||||
val listener = WakeListener(
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// Generated by scripts/gen-wake-phrases.py from the model's bpe.model. Do not edit: edit the lists there.
|
||||
package eu.llembas.android.wake
|
||||
|
||||
/**
|
||||
* The words a wake phrase is made of -- a greeting and a name -- each with its spelling in the
|
||||
* model's BPE pieces (`sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01`). English, as the
|
||||
* model is.
|
||||
*
|
||||
* Left out, because the tokenizer can only spell them out letter by letter:
|
||||
* Elizabeth (▁E LI Z A B E TH)
|
||||
* Benjamin (▁BE N J A M IN)
|
||||
*/
|
||||
object WakeWords {
|
||||
val greetings = listOf(
|
||||
WakeWord("hey", "Hey", "▁HE Y"),
|
||||
WakeWord("ok", "OK", "▁OKAY"),
|
||||
WakeWord("hello", "Hello", "▁HE LL O"),
|
||||
)
|
||||
|
||||
val names = listOf(
|
||||
WakeWord("assistant", "Assistant", "▁AS S IST ANT"),
|
||||
WakeWord("computer", "Computer", "▁COMP U TER"),
|
||||
WakeWord("google", "Google", "▁GO O G LE"),
|
||||
WakeWord("victoria", "Victoria", "▁VI C T OR IA"),
|
||||
WakeWord("samantha", "Samantha", "▁SA MAN TH A"),
|
||||
WakeWord("jessica", "Jessica", "▁JE S S IC A"),
|
||||
WakeWord("amanda", "Amanda", "▁A MAN D A"),
|
||||
WakeWord("olivia", "Olivia", "▁O LI VI A"),
|
||||
WakeWord("oliver", "Oliver", "▁O LI VER"),
|
||||
WakeWord("sebastian", "Sebastian", "▁SE B AS T IAN"),
|
||||
WakeWord("jonathan", "Jonathan", "▁JO N A TH AN"),
|
||||
WakeWord("alexander", "Alexander", "▁A LE X AN DER"),
|
||||
WakeWord("christopher", "Christopher", "▁ CH R IST O PH ER"),
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user