import { MiniRepl } from '../../../website/src/docs/MiniRepl';

# Gamepad

The Gamepad module allows you to integrate gamepad input functionality into your musical patterns. This can be particularly useful for live performances or interactive installations where you want to manipulate sounds using a game controller.

## Getting Started

Initialize a gamepad by calling the gamepad() function with an optional index parameter.

<MiniRepl
  client:idle
  tune={`// Initialize gamepad (optional index parameter, defaults to 0)
const pad = gamepad(0)`}
/>

## Available Controls

The gamepad module provides access to buttons and analog sticks as normalized signals (0-1) that can modulate your patterns.

### Buttons

| Type             | Controls                                                                                       |
| ---------------- | ---------------------------------------------------------------------------------------------- |
| Face Buttons     | `a`, `b`, `x`, `y` (or uppercase `A`, `B`, `X`, `Y`)                                           |
|                  | Toggle versions: `tglA`, `tglB`, `tglX`, `tglY`                                                |
| Shoulder Buttons | `lb`, `rb`, `lt`, `rt` (or uppercase `LB`, `RB`, `LT`, `RT`)                                   |
|                  | Toggle versions: `tglLB`, `tglRB`, `tglLT`, `tglRT`                                            |
| D-Pad            | `up`, `down`, `left`, `right` (or `u`, `d`, `l`, `r` or uppercase)                             |
|                  | Toggle versions: `tglUp`, `tglDown`, `tglLeft`, `tglRight` (or `tglU`, `tglD`, `tglL`, `tglR`) |

### Analog Sticks

| Stick       | Controls                       |
| ----------- | ------------------------------ |
| Left Stick  | `x1`, `y1` (0 to 1 range)      |
|             | `x1_2`, `y1_2` (-1 to 1 range) |
| Right Stick | `x2`, `y2` (0 to 1 range)      |
|             | `x2_2`, `y2_2` (-1 to 1 range) |

## Using Gamepad Inputs

Once initialized, you can use various gamepad inputs in your patterns. Here are some examples:

### Button Inputs

You can use button inputs to control different aspects of your music, such as gain or triggering events.

<MiniRepl
  client:idle
  tune={`const pad = gamepad(0)
  // Use button values to control amplitude
$: sequence([
  s("bd").gain(pad.X),    // X button controls gain
  s("[hh oh]").gain(pad.tglY),    // Y button toggles gain
])`}
/>

### Analog Stick Inputs

Analog sticks can be used for continuous control, such as pitch shifting or panning.

<MiniRepl
  client:idle
  tune={`const pad = gamepad(0)
  // Use analog stick for continuous control
$: note("c4*4".add(pad.y1_2.range(-24,24))) // Left stick Y controls pitch shift
  .pan(pad.x1_2)          // Left stick X controls panning`}
/>

### Button Sequences

You can define button sequences to trigger specific actions, like playing a sound when a sequence is detected.

<MiniRepl client:idle tune={`const pad = gamepad(0)
// Define button sequences
const HADOKEN = [
  'd',               // Down
  'r',               // Right
  'a',               // A
]

const KONAMI = 'uuddlrlrba' //Konami Code ↑↑↓↓←→←→BA

// Check button sequence (returns 1 when detected, 0 when not within last 1 second)
$: sound("hadoken").gain(pad.checkSequence(HADOKEN))`} />

## Multiple Gamepads

Strudel supports multiple gamepads. You can specify the gamepad index to connect to different devices.

<MiniRepl
  client:idle
  tune={`const pad1 = gamepad(0);  // First gamepad
const pad2 = gamepad(1);  // Second gamepad`}
/>
