# Microphone

> Raw audio frames and voice-activity detection from the glasses mic.

Source: https://docs.glassly.ai/app-devs/core-concepts/microphone/audio-chunks/

`session.mic` is the low-level audio input: raw frames and a voice-activity
signal. If you want text, use [`session.transcription`](https://docs.glassly.ai/app-devs/core-concepts/microphone/speech-to-text.md)
instead. Reach for `mic` when you need the audio itself, like recording a clip or
running your own audio processing.

Both subscriptions need the `MICROPHONE` permission in your
[manifest](https://docs.glassly.ai/app-devs/core-concepts/miniapp-manifest.md). Without it the phone drops
them, no events arrive, and [`onPermissionError`](https://docs.glassly.ai/app-devs/core-concepts/permissions.md#permission-errors) fires with `PERMISSION_NOT_DECLARED`.

## Raw audio

```typescript
session.mic.onAudioChunk((chunk) => {
  // chunk.data is base64-encoded PCM or LC3.
  buffer.push(chunk.data);
});
```

Each `AudioChunkData` has:

| Field | Type | Notes |
|---|---|---|
| `data` | `string` | Base64-encoded audio. PCM or LC3 depending on the phone's mic mode. |
| `sampleRate` | `number?` | Sample rate, when reported. |
| `format` | `string?` | The encoding, when reported. |

## Voice activity

`onVoiceActivity` fires when the user starts and stops speaking, without giving
you the audio:

```typescript
session.mic.onVoiceActivity(({ status }) => {
  // status: true while speaking, false when silent.
  if (status) session.display.render([{type: "text", id: "msg", box: {x: 0, y: 0, w: 576, h: 288}, text: "Listening…"}]);
});
```

## Overriding gating

Two methods temporarily override how the glasses gate audio, for the lifetime
of your miniapp. Both need `MICROPHONE` and revert to the Glassly App's
configured value when your miniapp disconnects.

`setVoiceActivityDetectionEnabled(enabled)` overrides the glasses-side voice
activity detection. Disabled, mic gating falls back to the loudness gate alone
(if that's enabled).

`setLoudnessGateEnabled(enabled)` overrides the center-mic loudness gate
("Barrier"), which blocks quiet or self-talk audio independent of VAD.

```typescript
await session.mic.setVoiceActivityDetectionEnabled(false);
await session.mic.setLoudnessGateEnabled(true);
```

## Cleaning up

Both subscription methods return an unsubscribe function, and `stop()` tears
down everything this module owns:

```typescript
const off = session.mic.onAudioChunk(handler);
off();              // drop this one

session.mic.stop(); // drop all of them
```

`session.mic.hasPermission` reports whether `MICROPHONE` is declared in your
manifest, not whether the user granted the OS prompt. If they denied it, the
subscription succeeds but no chunks arrive.

`mic`, `transcription`, and `translation` are independent. Subscribing to raw
audio does not give you transcripts, and vice versa. Audio *output* (playback and
text-to-speech) lives on [`session.speaker`](https://docs.glassly.ai/app-devs/core-concepts/speakers/text-to-speech.md).
