1import { MiniRepl } from '../../../website/src/docs/MiniRepl'; 2 3# Gamepad 4 5The 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. 6 7## Getting Started 8 9Initialize a gamepad by calling the gamepad() function with an optional index parameter. 10 11<MiniRepl 12 client:idle 13 tune={`// Initialize gamepad (optional index parameter, defaults to 0) 14const gp = gamepad(0) 15note("c a f e").mask(gp.a)`} 16/> 17 18## Available Controls 19 20The gamepad module provides access to buttons and analog sticks as normalized signals (0-1) that can modulate your patterns. 21 22### Buttons 23 24| Type | Controls | 25| ---------------- | ---------------------------------------------------------------------------------------------- | 26| Face Buttons | `a`, `b`, `x`, `y` (or uppercase `A`, `B`, `X`, `Y`) | 27| | Toggle versions: `tglA`, `tglB`, `tglX`, `tglY` | 28| Shoulder Buttons | `lb`, `rb`, `lt`, `rt` (or uppercase `LB`, `RB`, `LT`, `RT`) | 29| | Toggle versions: `tglLB`, `tglRB`, `tglLT`, `tglRT` | 30| D-Pad | `up`, `down`, `left`, `right` (or `u`, `d`, `l`, `r` or uppercase) | 31| | Toggle versions: `tglUp`, `tglDown`, `tglLeft`, `tglRight` (or `tglU`, `tglD`, `tglL`, `tglR`) | 32| Stick Buttons | `l3`, 'r3' (or `ls`, `rs`) | 33| | Toggle versions: `tglL3`, 'tglR3' (or `tglLs`, `tglRs`) | 34| System Buttons | `start`, `back` (or uppercase `START`, `BACK`) | 35| | Toggle versions: `tglStart`, `tglBack` (or `tglSTART`, `tglBACK`) | 36 37### Analog Sticks 38 39| Stick | Controls | 40| ----------- | ------------------------------ | 41| Left Stick | `x1`, `y1` (0 to 1 range) | 42| | `x1_2`, `y1_2` (-1 to 1 range) | 43| Right Stick | `x2`, `y2` (0 to 1 range) | 44| | `x2_2`, `y2_2` (-1 to 1 range) | 45 46### Button Sequence 47 48| Stick | Controls | 49| --------------- | --------------------------------------- | 50| Button Sequence | `btnSequence()`, `btnSeq()`, `btnseq()` | 51 52## Using Gamepad Inputs 53 54Once initialized, you can use various gamepad inputs in your patterns. Here are some examples: 55 56### Button Inputs 57 58You can use button inputs to control different aspects of your music, such as gain or triggering events. 59 60<MiniRepl 61 client:idle 62 tune={`const gp = gamepad(0) 63setcpm(120) 64// Use button values to control amplitude 65$: stack( 66 s("[[hh hh] oh hh oh]/2").mask(gp.tglX).bank("RolandTR909"), // X btn for HH 67 s("cr*1").mask(gp.Y).bank("RolandTR909"), // LB btn for CR 68 s("bd").mask(gp.tglA).bank("RolandTR909"), // A btn for BD 69 s("[ht - - mt - - lt - ]/2").mask(gp.tglB).bank("RolandTR909"), // B btn for Toms 70 s("sd*4").mask(gp.RB).bank("RolandTR909"), // RB btn for SD 71) 72`} 73/> 74 75### Analog Stick Inputs 76 77Analog sticks can be used for continuous control, such as pitch shifting or panning. 78 79<MiniRepl 80 client:idle 81 tune={`const gp = gamepad(0) 82setcpm(120) 83// Use analog stick for continuous control 84$: note("c4 d3 a3 e3").sound("sawtooth") 85 .lpf(gp.x1.range(100,4000)) 86 .lpq(gp.y1.range(5,30)) 87 .decay(gp.y2.range(0.1,2)) 88 .lpenv(gp.x2.range(-5,5))`} 89/> 90 91### Button Sequences 92 93You can define button sequences to trigger specific actions, like playing a sound when a sequence is detected. 94 95<MiniRepl client:idle tune={`const gp = gamepad(0) 96setcpm(120) 97// Define button sequences 98const HADOUKEN = [ 99 'd', // Down 100 'r', // Right 101 'a', // A 102] 103const KONAMI = 'uuddlrlrba' //Konami Code ↑↑↓↓←→←→BA 104 105// Check butto-n sequence (returns 1 while detected, 0 when not within last 1 second) 106$: s("free_hadouken -").slow(2) 107.mask(gp.btnSequence(HADOUKEN)).room(1) 108 109// hadouken.wav by Syna-Max 110//https://freesound.org/people/Syna-Max/sounds/67674/ 111samples({free_hadouken: 'https://cdn.freesound.org/previews/67/67674_111920-lq.mp3'}) 112`} /> 113 114## Multiple Gamepads 115 116Strudel supports multiple gamepads. You can specify the gamepad index to connect to different devices. 117 118<MiniRepl 119 client:idle 120 tune={`const pad1 = gamepad(0); // First gamepad 121const pad2 = gamepad(1); // Second gamepad`} 122/>