open-audiovisual
Search GitHub

oav-p5 reference · MIDI: knobs, faders, pads, keys

pitchBend()#

eventoav-p5
function pitchBend(value, channel) {}

You define this function; oav-p5 calls it whenever the pitch wheel moves, with its position from −1 to 1.

Declare it the way you declare keyPressed():

js
function pitchBend(value, channel) {
  // the wheel moved
}

A pitch wheel usually springs back to the middle when you let go, so you will hear a stream of values ending at 0. MIDI sends the position as 14 bits (0 to 16383, with 8192 in the middle). oav-p5 hands it to you as −1 to 1. The latest position is also in OAVp5.bend.

For one incoming message the order is: midiMessage(), then the specific one (noteOn(), controlChange(), pitchBend()), then the friendly one (padPressed(), knobMoved(), notePressed() and their partners).

Before it has a value

Defining the function is enough. oav-p5 asks the browser for Web MIDI when it loads (the default, ?oav_hardware=auto), and a sketch that defines it also gets a card if Web MIDI is missing or blocked. Keys from 🎹, a phone, OAVp5.input() and other programs reach it too. OAVp5.on('pitchBend', fn) does the same without defining a global function, and returns a function that stops it.

Parameters

NameTypeDescription
valueNumberThe wheel's position: −1 all the way down, 0 in the middle, 1 all the way up.
channelNumberThe MIDI channel, 1 to 16.

Notes

  • Pitch bend is not mapped to anything else: there is no bend variable, only this function and OAVp5.bend.
  • An error inside it does not stop the music: the next message is still handled, and the message shows in the console and on a card beside the 🎹, with the file and line.

Examples

jsOpenProcessing ↗
let bend = 0;

function setup() {
  createCanvas(400, 300);
}

function pitchBend(value) {
  bend = value;
}

function draw() {
  background(20);
  circle(width / 2 + bend * 150, height / 2, 50);   // wheel up: right, wheel down: left
}
The circle slides left and right with the pitch wheel and returns to the middle when it springs back. Plug in a controller, or press 🎹 (bottom right) and use the on-screen one.
jsOpenProcessing ↗
let bend = 0;

function setup() {
  createCanvas(400, 300);
}

function pitchBend(value) { bend = value; }

function mouseDragged() {
  OAVp5.input([0xE0, 0, constrain(round(mouseX / width * 127), 0, 127)]);   // no wheel? drag the mouse
}

function draw() {
  background(20);
  const x = spring(width / 2 + bend * 150, 120, 8);   // a spring smooths the steps
  circle(x, height / 2, 50);
}
The same wheel through spring(). No wheel? Drag the mouse across the canvas: it sends the same message.

Related

  • OAVp5.bend Where the pitch wheel is, from −1 (all the way down) through 0 (middle) to 1 (all the way up).
  • controlChange() You define this function; oav-p5 calls it for every MIDI control change (a knob, a fader, the mod wheel, the sustain pedal), whether or not the controller is known.
  • midiMessage() You define this function; oav-p5 calls it for every MIDI message it receives, with the raw bytes and where they came from.
  • OAVp5.on() Listens to a MIDI event by name without defining a global function, and returns a function that stops listening.
  • OAVp5.input() Sends one MIDI message into oav-p5 by hand, as bytes: the whole sketch reacts as if a controller had sent it.

p5/oav-p5.js, line 255

↑ Back to top · All names