open-audiovisual
Search GitHub

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

controlChange()#

eventoav-p5
function controlChange(cc, value, channel) {}

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.

Declare it the way you declare keyPressed():

js
function controlChange(cc, value, channel) {
  // a control moved
}

It hears every control, including the ones that are not a knob or a fader, such as the mod wheel and the pedals, which knobMoved() never reports. The latest value of every controller number is also in OAVp5.cc, so a sketch that only wants to look can read OAVp5.cc[1] in draw().

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('controlChange', fn) does the same without defining a global function, and returns a function that stops it.

Parameters

NameTypeDescription
ccNumberThe controller number, 0 to 127 (1 is the mod wheel, 64 the sustain pedal, 74 the first knob on a MiniLab 3).
valueNumberWhere the control is now, 0 to 1 (MIDI's 0 to 127 divided by 127).
channelNumberThe MIDI channel, 1 to 16.

Notes

  • 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 shown = 'move a knob, the mod wheel or a pedal';

function setup() {
  createCanvas(400, 300);
  textAlign(CENTER, CENTER);
  textSize(24);
}

function controlChange(cc, value, channel) {
  shown = 'CC ' + cc + ' = ' + nf(value, 1, 2) + '  (channel ' + channel + ')';
}

function draw() {
  background(20);
  fill(255);
  text(shown, width / 2, height / 2);
}
Shows the controller number, its value and the channel of whatever you moved last. Plug in a controller, or press 🎹 (bottom right) and use the on-screen one.
jsOpenProcessing ↗
let sustain = false;

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

function controlChange(cc, value) {
  if (cc === 64) sustain = value >= 0.5;   // the sustain pedal: 64 and up counts as down
}

function mousePressed() {
  OAVp5.input([0xB0, 64, 127]);   // no pedal? press the mouse
}

function mouseReleased() {
  OAVp5.input([0xB0, 64, 0]);
}

function draw() {
  background(sustain ? 90 : 20);
}
The background lights up while the sustain pedal is down. No pedal? Hold the mouse button: it sends the same message.

Related

  • knobMoved() You define this function; oav-p5 calls it whenever a knob moves.
  • faderMoved() You define this function; oav-p5 calls it whenever a fader moves.
  • OAVp5.cc An array of 128 numbers, one per MIDI controller number: where each control last was, 0 to 1. OAVp5.cc[1] is the mod wheel, OAVp5.cc[64] the sustain pedal.
  • 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