open-audiovisual
Search GitHub

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

midiMessage()#

eventoav-p5
function midiMessage(bytes, source) {}

You define this function; oav-p5 calls it for every MIDI message it receives, with the raw bytes and where they came from.

Declare it the way you declare keyPressed():

js
function midiMessage(bytes, source) {
  // anything that came in
}

It is the widest net: it is called first for every note, control change and pitch bend, and it is the only function that hears the messages oav-p5 has no friendlier name for, such as channel pressure (0xD0), polyphonic aftertouch (0xA0) and program change (0xC0). The OAVp5.last object keeps the latest note, control change or pitch bend, but not those.

It does not hear system messages: the MIDI clock, start, stop and song position go to OAVp5.clock, and system-exclusive messages are dropped. A note-on with velocity 0 arrives here as it was sent (a note-on with 0), while noteOff() is the one that treats it as a release.

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

Parameters

NameTypeDescription
bytesArray<Number>The message as three numbers [status, data1, data2]. The status byte holds the kind in its top half and the channel in its bottom half (0x90 is a note-on on channel 1, 0x93 on channel 4). The data bytes are 0 to 127, and a byte the message does not have is 0.
sourceStringWhere it came from: 'hardware' (a real controller), 'screen' (the 🎹), 'phone', 'virtual-out' (a program playing into OAV Virtual MIDI), 'link' (another page in the same room), or whatever label you gave OAVp5.input().

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 = 'play something';

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

function midiMessage(bytes, source) {
  shown = bytes.map((b) => '0x' + b.toString(16).toUpperCase()).join(' ') + '  from ' + source;
}

function draw() {
  background(20);
  fill(255);
  text(shown, width / 2, height / 2);
}
Prints the raw bytes of the last message and where it came from. Plug in a controller, or press 🎹 (bottom right) and use the on-screen one.
jsOpenProcessing ↗
let pressure = 0;

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

function midiMessage(bytes) {
  if ((bytes[0] & 0xF0) === 0xD0) pressure = bytes[1] / 127;   // channel pressure (aftertouch)
}

function mouseMoved() {
  OAVp5.input([0xD0, constrain(round(mouseX / width * 127), 0, 127)], 'mouse');   // no aftertouch? move the mouse
}

function draw() {
  background(20);
  circle(width / 2, height / 2, 40 + pressure * 260);
}
The circle follows aftertouch, which no friendlier function reports. No keyboard with aftertouch? Move the mouse across the canvas: it sends the same message.

Related

  • noteOn() You define this function; oav-p5 calls it for every MIDI note-on, with the key, how hard and the channel.
  • 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.
  • pitchBend() You define this function; oav-p5 calls it whenever the pitch wheel moves, with its position from −1 to 1.
  • OAVp5.last The latest note, control change or pitch bend that arrived: its type, number, value, channel, where it came from and which control it is. null before any.
  • 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