# Audio and microphone

> Play tones and samples routed to PC speakers or the DualSense, and detect blowing into a microphone.

Source: https://gamepadly.com/sdk/guides/audio-and-mic

## Tones and noise

Synthesized tones play through the hub's `audio` engine. `createLinkAudio()` from `@gamepadly/sdk/link` provides
one, and its PC output works without Gamepadly Link or a DualSense. Without an `audio` engine these calls do
nothing.

```ts
import { createGamepadly } from '@gamepadly/sdk';
import { createLinkAudio } from '@gamepadly/sdk/link';

const audio = createLinkAudio();
const hub = createGamepadly({ audio });
const p1 = hub.player(0);

soundButton.onclick = () => audio.initPcOutput(); // browsers only start audio after a click

p1.audio.playPcTone({ freqHz: 880, durationMs: 80, intensity: 0.4 });
p1.audio.playPcNoise({ durationMs: 200, freqHz: 1200, filterType: 'bandpass', pan: -0.3 });
p1.audio.playControllerSpeakerTone({ freqHz: 660, durationMs: 120 }); // DualSense speaker with 4-channel audio
```

## Samples

To play audio files without the Link engine, give the hub a sample bank that wraps a plain `AudioContext`
(`createLinkAudio()` already includes one):

```ts
import { createGamepadly } from '@gamepadly/sdk';
import { createPcAudioRouting, createSampleBank } from '@gamepadly/sdk/audio';

const ctx = new AudioContext();
const hub = createGamepadly({ samples: createSampleBank(createPcAudioRouting(ctx)) });
const p1 = hub.player(0);

await p1.audio.loadSample('door', '/sfx/door.ogg');
const handle = p1.audio.playSample('door', { route: 'pc', pan: 0.5, gain: 0.8 });
// handle.stop() for looping sounds
```

Browsers start an `AudioContext` suspended, so call `ctx.resume()` from a click before the first sound.

`route` picks the output:

| Route | Output |
| :--- | :--- |
| `'pc'` | PC speakers or headphones (default) |
| `'speaker'` | The DualSense speaker, falling back to the PC |
| `'vcm'` | The DualSense voice coils, falling back to the haptic router |
| `'quad'` | All four DualSense channels in sync (speaker plus voice coils) |

The controller routes need [4-channel audio (L2)](https://gamepadly.com/sdk/guides/link). Without it they fall back as listed, so
one call works for every player. `loadSample` accepts a URL, an encoded `ArrayBuffer`, or raw PCM. To generate
voice-coil textures procedurally, see [`createTactileBuffer()`](https://gamepadly.com/sdk/api/sdk#createtactilebuffer) in the API reference.

## Microphone blow detection

```ts
import { createMicBlowSensor } from '@gamepadly/sdk/mic';

const mic = createMicBlowSensor(); // prefers the DualSense microphone, falls back to the default mic
startButton.onclick = () => mic.start(); // asks for microphone permission, so it needs a click

// each frame
const { blowing, intensity } = mic.sample();
windmill.speed = intensity;

// on exit
mic.stop();
```

The sensor separates breath noise from voice and humming by its spectrum, so talking does not count as blowing.
`createMicLevelMeter()` returns a plain input level if you only need volume.
