# ctx.mic

Their microphone, live, while your app is open — a voice over a beat, a voice memo, a tuner. Fave1 asks them first and never keeps the sound.

mic · card mode · app mode

## Calls

`src/index.ts`

```ts
const mic = await ctx.mic.open();          // speakers on: echo cancelling, noise and gain control
const rec = new MediaRecorder(mic.stream);  // or feed it to Web Audio
rec.start();
// …
rec.stop();
mic.stop();                                 // or ctx.mic.close() — every mic your app opened
```

| option | What it does | default |
| --- | --- | --- |
| `echo` | The browser removes what the speakers are playing from what the mic hears. Leave it on unless they wear earphones | true |
| `noise` | Noise suppression. Good for speech, rough on singing | true |
| `gain` | Automatic level. Turn it off when you set the level yourself | true |

| field | What it is | type |
| --- | --- | --- |
| `stream` | One audio track, no video — for `MediaRecorder` or `createMediaStreamSource` | MediaStream |
| `track` | That audio track | MediaStreamTrack |
| `echo · noise · gain` | What the browser actually applied — it may not honor everything you asked for | boolean |
| `latency` | Input delay the device reports, in seconds — `0` when it doesn’t say | number |
| `stop()` | Turns this mic off. `ctx.mic.active` tells you if any is still on | function |

Sound only. Need `mic` in your manifest — it is a separate permission from `camera`, with its own switch. Without it, or with the switch off in Fave1 settings, `open()` rejects with `denied`.

## What the person sees

The first `open()` each time your app opens shows Fave1’s line in your panel: **Let \<your app> use your microphone?** When they tap Allow, their browser asks for that site. While a mic is on, a **red dot** sits in your panel’s header. Close your app, close Fave1, or call `stop()`, and the mic goes off.

Call `open()` from a button they pressed — “Record” — not at launch, and turn it off as soon as the take is done. Nothing is played back through the speakers unless you do it yourself.

## Recording over your own sound

To record a voice over music you play with Web Audio, send both to one `MediaStreamDestination` and record that. Keep the mic out of the speakers, or it feeds back.

`src/index.ts`

```ts
const ac = new AudioContext();
const out = ac.createMediaStreamDestination();    // what gets recorded
const music = ac.createGain();                     // your beat goes in here
music.connect(ac.destination);                     // …to the speakers

const earphones = true;                            // ask them — off means the mic hears your speakers too
const mic = await ctx.mic.open({ echo: !earphones, noise: !earphones, gain: !earphones });
const voice = ac.createMediaStreamSource(mic.stream);
voice.connect(out);                                // voice → the recording only

// The person hears the beat late (output delay) and the mic hears them late (input delay),
// so the voice lands behind the beat. Delay the music by the same amount — in the recording only.
const late = ac.createDelay(1);
late.delayTime.value = (ac.outputLatency || 0) + (ac.baseLatency || 0) + mic.latency;
music.connect(late).connect(out);

const rec = new MediaRecorder(out.stream);
```

Wired earphones and built-in speakers are a few hundredths of a second. Bluetooth earphones can be a quarter of a second and browsers don’t always report it — give people a slider to nudge it.

## When it doesn’t work

| code | What happened · what to do | retry |
| --- | --- | --- |
| `denied` | Not declared, the switch is off, they tapped Not now, or their browser said no (`reason` `not-now` · `browser`). `closed` if `open()` lands after your app closed | no |
| `blocked` | This site doesn’t let any microphone run on it (`reason` `site`). Offer the same thing in app mode, at `<your-app>.fave1.app` | no |
| `unavailable` | No microphone (`reason` `none`) or the page isn’t https (`insecure`) | no |
| `busy` | Another app is holding it | later |

## The rule

> **Only through ctx.mic.** The sound stays on their page — save it only when they ask (a clip they keep with `ctx.files`, a download), say so in your listing if it ever leaves, and never identify people by their voice. An app that calls `navigator.mediaDevices.getUserMedia` itself, listens without a tap, or keeps the mic on after the feature is done gets sent back in review.
