# Optional Link (L2)

> Drive the DualSense speaker and voice-coil haptics as 4-channel audio on Windows, through the browser or the open-source Gamepadly Link companion.

Source: https://gamepadly.com/sdk/guides/link

> **Note**
> Everything on this page is optional. Games work fully at L0 and L1 without it. L2 needs **Windows and a
> DualSense connected by USB**. Bluetooth, macOS, and Linux fall back to standard rumble and triggers.

A USB DualSense exposes a 4-channel audio device: channels 0 and 1 drive its speaker, channels 2 and 3 its left
and right voice coils, the actuators that make DualSense haptics feel detailed. `@gamepadly/sdk/link` streams
audio to that device so haptic presets play on the voice coils (`'4ch_vcm'`) and samples can use the controller
speaker.

```ts
import { createGamepadly } from '@gamepadly/sdk';
import {
  LINK_RELEASES_URL,
  connectDualSenseViaBrowser,
  connectDualSenseViaCompanion,
  createLinkAudio,
} from '@gamepadly/sdk/link';

const audio = createLinkAudio();
const hub = createGamepadly({ audio, teardownAudioOnExit: true });
```

There are two ways to reach the device. Both must start from a click.

## Through the browser

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

browserButton.onclick = async () => {
  const res = await connectDualSenseViaBrowser(audio);
  if (res.ok) hub.syncCapabilities();
};
```

Chrome and Edge can route audio to a specific device with `setSinkId()`. This needs no install, but Windows must
be set to a **Quadraphonic** speaker configuration for the DualSense, and the browser asks once for permission to
read audio device names.

## Through Gamepadly Link

[Gamepadly Link](https://github.com/gamepadly/gamepadly-link) is an open-source (MIT) Windows tray app that streams
4-channel audio over a local connection. On sites other than gamepadly.com it asks the user to approve the site
in the tray first.

```ts
import { LINK_RELEASES_URL, connectDualSenseViaCompanion } from '@gamepadly/sdk/link';

linkButton.onclick = async () => {
  const res = await connectDualSenseViaCompanion(audio, { minProtocolVersion: 1.1 });
  if (!res.ok) {
    // 'link_not_running' | 'link_outdated' | 'user_denied' | 'consent_timeout' | 'consent_busy'
    // | 'origin_not_allowed' | 'dualsense_audio_not_found' | 'failed'
    showError(res.error, res.retryAfterSec, LINK_RELEASES_URL);
    return;
  }
  hub.syncCapabilities();
};
```

Never connect to Link on page load: the browser's local network access rules require a user gesture, and
players who do not run Link should never see an error for it.

## Your own companion

The [`protocol/v1.1`](https://github.com/gamepadly/gamepadly-link/blob/main/protocol/v1.1.md) specification is open.
Fork Link or build your own companion, and point the SDK at it with
`createLinkAudio({ httpBase, wsBase })`. If your Content Security Policy blocks `blob:` worklets, host the audio
worklet yourself and pass `workletUrl`.
