# Wake word

> Know when the wearer says "Hey Glassly", without transcribing anything.

Source: https://docs.glasslyos.com/app-devs/core-concepts/microphone/wake-word/

`session.wakeWord` tells your miniapp when the wearer says "Hey Glassly". A
small keyword model on the phone listens to the glasses microphone. No audio
leaves the phone and no speech-to-text runs, so it is the cheap way to wait
until the wearer addresses you. Once it fires, subscribe to
[`session.transcription`](https://docs.glasslyos.com/app-devs/core-concepts/microphone/speech-to-text.md)
to hear what they say next.

It needs the `MICROPHONE` permission in your
[manifest](https://docs.glasslyos.com/app-devs/core-concepts/miniapp-manifest.md). Without it the phone drops
the subscription, no events arrive, and [`onPermissionError`](https://docs.glasslyos.com/app-devs/core-concepts/permissions.md#permission-errors) fires with `PERMISSION_NOT_DECLARED`.

## Listen for the wake word

```typescript
import { registerMiniapp } from "@glassly/miniapp/background";

registerMiniapp((session) => {
  session.wakeWord.on(() => {
    // Transcribe only once the wearer has addressed you.
    const stop = session.transcription.on((data) => {
      if (!data.isFinal) return;
      answer(data.text);
      stop();
    });
  });
});
```

`on` returns a function that unsubscribes. The detector runs while at least one
miniapp is subscribed and stops with the last one. `session.wakeWord.stop()`
removes every listener your miniapp added.

Each `WakeWordData` has:

| Field | Type | Notes |
|---|---|---|
| `phrase` | `string` | The phrase the detector is trained on, lowercase: `"hey glassly"`. |
| `confidence` | `number` | The detector's score, from the threshold (0.5) up to 1. |
| `timestamp` | `number` | Phone time the phrase finished, unix ms. |

## Phones without the detector

`session.wakeWord.isAvailable` is `false` on a Glassly App that predates the
wake word and on a host that ships without the model. `on` then never fires,
so match the phrase in the transcript instead:

```typescript
if (session.wakeWord.isAvailable) {
  session.wakeWord.on(() => startListening());
} else {
  session.transcription.on((data) => {
    if (/hey,? glassly/i.test(data.text)) startListening();
  });
}
```

## What to expect

- **One phrase.** The detector hears "Hey Glassly" only. Your miniapp cannot
  register its own phrase.
- **At most one event every 2 seconds.**
- **The next words are kept for you.** Speech-to-text takes a moment to start
  after you subscribe. For 4 seconds after a detection the phone keeps the audio
  it hears and sends it ahead of live audio once your transcription
  subscription is active, so a question spoken straight after the phrase is not
  cut off. The phrase itself is not in the transcript unless you were already
  transcribing. This covers cloud transcription; on-device and bring-your-own
  Soniox transcription start from the moment you subscribe.
- **False triggers happen.** The model is tuned for about one false detection
  every two hours of speech. Let an empty or unrelated transcript end the
  interaction quietly.
- **Microphone gates still apply.** When the wearer's voice activity detection
  or loudness Barrier drops quiet audio, the detector never hears it.

## In the simulator

```bash
glassly sim wake                                # a detection
glassly sim say "Hey Glassly, what time is it?" # a detection, then the rest as a transcription
```
