open-audiovisual
Search GitHub

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

noteOn()#

eventoav-p5
function noteOn(note, velocity, channel) {}

You define this function; oav-p5 calls it for every MIDI note-on, with the key, how hard and the channel.

Declare it the way you declare keyPressed():

js
function noteOn(note, velocity, channel) {
  // a key or pad went down
}

It is called once per note-on message, as it came in. It is the level below notePressed(): the same moment, but with the channel, and for pads too, even when the sketch defines padPressed(). A note-on with velocity 0, which some keyboards send instead of a note-off, is not a note-on here: it calls noteOff().

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

Parameters

NameTypeDescription
noteNumberThe MIDI number of the key, 0 to 127 (60 is middle C). noteName(note) turns it into a name.
velocityNumberHow hard, above 0 and up to 1 (MIDI's 1 to 127 divided by 127).
channelNumberThe MIDI channel, 1 to 16. Pads on the MiniLab 3 and the LPD8 send on channel 10.

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 a key';

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

function noteOn(note, velocity, channel) {
  shown = noteName(note) + '  velocity ' + nf(velocity, 1, 2) + '  channel ' + channel;
}

function draw() {
  background(20);
  fill(255);
  text(shown, width / 2, height / 2);
}
Shows the key you played, how hard and on which channel. Plug in a controller, or press 🎹 (bottom right) and use the on-screen one.
jsOpenProcessing ↗
let hits = [];

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

function noteOn(note, velocity, channel) {
  hits.push({ note, velocity, pad: channel === 10, born: millis() });   // channel 10: the pads
}

function draw() {
  background(20);
  hits = hits.filter((h) => millis() - h.born < 1500);
  for (const h of hits) {
    const fade = 1 - (millis() - h.born) / 1500;
    const x = map(h.note, 24, 96, 20, width - 20);
    fill(h.pad ? color(255, 90, 31, fade * 255) : color(120, 190, 255, fade * 255));
    if (h.pad) square(x - 25, 125, 50 + h.velocity * 40);
    else circle(x, 150, 20 + h.velocity * 80);
  }
}
Keys leave blue circles and pads leave orange squares, placed by pitch, bigger for harder hits, fading after a moment. Plug in a controller, or press 🎹 (bottom right) and use the on-screen one.

Related

  • noteOff() You define this function; oav-p5 calls it for every MIDI note-off, with the key and the channel.
  • notePressed() You define this function; oav-p5 calls it each time a key (or a pad) goes down.
  • padPressed() You define this function; oav-p5 calls it each time a drum pad is hit.
  • 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