OAVp5.on()#
functionoav-p5OAVp5.on(name, fn)
Listens to a MIDI event by name without defining a global function, and returns a function that stops listening.
The event functions (notePressed(), knobMoved(), noteOn() …) are defined once, by name. OAVp5.on('notePressed', fn) is the other way: any number of listeners, added and removed while the sketch runs, with no global names taken. It is how a library, a class or a Hydra sketch listens, and the way to listen for a while and stop.
A listener counts as a defined function for the pad rule: once anything listens to 'padPressed', a pad stops calling notePressed(). An error inside fn shows in the console and on a card, and the other listeners still run. For the microphone's events use OAVp5.mic.on().
Parameters
| Name | Type | Description |
|---|---|---|
name | String | The event: 'notePressed', 'noteReleased', 'knobMoved', 'faderMoved', 'padPressed', 'padReleased', 'noteOn', 'noteOff', 'controlChange', 'pitchBend' or 'midiMessage'. 'noteon' and 'noteoff' work in any case. Anything else throws an error that lists the names. |
fn | Function | Called with the same arguments the function of that name would get (notePressed(note, velocity), knobMoved(n, value) …). |
Returns
Function A function that takes fn off again: const off = OAVp5.on(…); off();.
Notes
midi.on(…)is the same function.
Examples
let heard = 0;
function setup() {
createCanvas(400, 300);
OAVp5.on('noteOn', (note, velocity) => { heard++; });
}
function mousePressed() { OAVp5.play(60 + floor(random(12)), 0.8); } // no keys? click
function draw() {
background(20);
fill(255);
text('notes heard: ' + heard, 20, 30);
}let heard = 0;
let off;
function setup() {
createCanvas(400, 300);
off = OAVp5.on('noteOn', () => {
heard++;
if (heard === 5) off(); // stop listening after five notes
});
}
function mousePressed() { OAVp5.play(60 + floor(random(12)), 0.8); }
function draw() {
background(20);
fill(255);
text('notes heard: ' + heard + (heard >= 5 ? ' (stopped listening)' : ''), 20, 30);
}Related
OAVp5.mic.on()Listens to a microphone event ('beat','kick','snare'or'hat') without defining a global function, and returns a function that stops listening.notePressed()You define this function; oav-p5 calls it each time a key (or a pad) goes down.noteOn()You define this function; oav-p5 calls it for every MIDI note-on, with the key, how hard and the channel.knobMoved()You define this function; oav-p5 calls it whenever a knob moves.OAVp5.input()Sends one MIDI message into oav-p5 by hand, as bytes: the whole sketch reacts as if a controller had sent it.