Files
strudel/packages/gamepad
2025-10-20 20:42:10 +02:00
..
2024-11-27 01:03:09 +08:00
2025-09-10 23:08:16 +02:00
2024-11-27 01:03:09 +08:00

@strudel/gamepad

This package adds gamepad input functionality to strudel Patterns.

Install

npm i @strudel/gamepad --save

Usage

import { gamepad } from '@strudel/gamepad';

// Initialize gamepad (optional index parameter, defaults to 0)
const pad = gamepad(0); // Default mapping is XBOX {a: 0, b: 1, x: 2, y: 3}

//const pad = gamepad(0, 'XBOX'); // XBOX button mapping {a: 0, b: 1, x: 2, y: 3}
//const pad = gamepad(0, 'NES'); // Nintendo button mapping {a: 1, b: 0, x: 3, y: 2}
//const pad = gamepad(0, {a: 0, b: 1, x: 2, y: 3}); // Custom mapping


// Use gamepad inputs in patterns
const pattern = sequence([
  // Button inputs
  pad.a,        // A button value (0-1)
  pad.tglA,     // A button toggle (0 or 1)
  
  // Analog stick inputs
  pad.x1,       // Left stick X (0-1)
  pad.x1_2,     // Left stick X (-1 to 1)
]);

Available Controls

Buttons

  • 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

  • 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)

Examples

// 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
]);

// 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

// Use toggle buttons to switch patterns on/off

// Define button sequences
const HADOKEN = [
  'd',               // Down
  'r',               // Right
  'a',               // A
];

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

// Add these lines to enable buttons(but why?)
$:pad.D.segment(16).gain(0)
$:pad.R.segment(16).gain(0)
$:pad.A.segment(16).gain(0)

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

Multiple Gamepads

You can connect multiple gamepads by specifying the gamepad index: Make sure to press buttons on all connected gamepads before hitting play, so the browser can properly detect them.

const pad1 = gamepad(0);  // First gamepad
const pad2 = gamepad(1);  // Second gamepad

Vibration

You can use the vibrate control to provide haptic feedback to the gamepad. The vibrate control is a patternable control, so you can use it to control the vibration of the gamepad.

The vibrate control has several parameters:

  • vibGamepadIndex - Index of the gamepad to vibrate (0-3)
  • vibStrong - Intensity of the strong/left motor (0-1)
  • vibWeak - Intensity of the weak/right motor (0-1)
  • vibDuration - Duration of vibration in milliseconds
  • vibEnable - Enable/disable vibration (0 or 1)
// Vibration with gamepad 
// vibrate(gamepadId, { strong: 1, weak: 0.5, duration: 100 })
// vibrate("gamepadId")
// vibrate(gamepadId)

$: note("c4*4").vibrate(0, { strong: 1, weak: 0.5, duration: 100 }) // gamepad index 0
$: note("c4*4").vibrate("0:1:0.5:200") // Array format
$: note("c4*4").vibrate("<0 1 0 1>") // select gamepad index

$: note("c4*4").vibStrong("<0.1 0.2 0.5 1>").vibWeak("<0.1 0.2 0.5 1>").vibDuration("<10 20 50 100>").vibrate(0) // gamepad index 0