open-audiovisual
Search GitHub

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

notePressed()#

eventoav-p5
function notePressed(note, velocity) {}

You define this function; oav-p5 calls it each time a key (or a pad) goes down.

Declare it the way you declare keyPressed():

js
function notePressed(note, velocity) {
  // a key went down
}

It is called once per key, with the key and how hard. A pad also calls it, unless the sketch defines padPressed().

Before it has a value

Defining it is enough: oav-p5 calls it from every source, and if Web MIDI is missing or refused it says so in a card beside the 🎹 (Web MIDI itself is asked for at load, see knob1). Keys from 🎹 and from a phone call it too.

Parameters

NameTypeDescription
noteNumberThe MIDI number of the key (60 is middle C). noteName(note) turns it into a name.
velocityNumberHow hard, 0..1.

Notes

  • An error inside it does not stop the music: the next key still plays, and the message shows in the console and on a card beside the 🎹, with the file and line.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  background(20);
  noStroke();
}

function notePressed(note, velocity) {
  fill(random(255), random(255), random(255), 200);
  circle(map(note, 36, 96, 20, width - 20), height / 2, 10 + velocity * 120);
}
Every key leaves a circle: low keys on the left, high on the right, harder keys bigger.

Related

  • noteReleased() You define this function; oav-p5 calls it each time a key (or a pad) is let go.
  • padPressed() You define this function; oav-p5 calls it each time a drum pad is hit.
  • noteOn() You define this function; oav-p5 calls it for every MIDI note-on, with the key, how hard and the channel.
  • note The MIDI number (0 to 127) of the last key pressed.
  • noteName() Turns a MIDI number into a note name: noteName(61) is 'C#4'.

p5/oav-p5.js, line 254

↑ Back to top · All names