open-audiovisual
Search GitHub

oav-p5 reference · Drum machine and rhythm

OAVp5.drums.on()#

functionoav-p5
OAVp5.drums.on(name, fn)

Listens to the drum machine from anywhere: OAVp5.drums.on('hit', fn) calls fn like drumHit does, and returns a function that stops listening.

The functions you define (drumHit, barStarted …) are one listener each. on() is for the cases where that does not fit: several listeners for one event, a library or a Hydra sketch that has no drumHit() of its own, a listener you want to remove again. It is the same idea as midi.on(). An error thrown inside fn is printed and the other listeners still run.

Parameters

NameTypeDescription
nameStringWhat to listen to: 'hit' (like drumHit()), 'step' (like stepStarted()), 'bar' (like barStarted()), 'cue' (like cueFired()) or 'rhythm' (like rhythmChanged()). Any other name throws an error that lists them.
fnFunctionCalled with the same arguments as the function it matches: (lane, velocity), (step, bar), (bar), (name, bar) or (src, how).

Returns

Function A function: call it to stop listening.

Examples

jsOpenProcessing ↗
let bars = 0;
let off = null;

function setup() {
  createCanvas(400, 300);
  startDrums('four on floor', 160);
  off = OAVp5.drums.on('bar', (bar) => { bars++; });   // returns a function that stops it
  textAlign(CENTER, CENTER);
}

function mousePressed() {
  if (off) { off(); off = null; }   // stop counting
}

function draw() {
  background(20);
  fill(255);
  text('bars counted: ' + bars, width / 2, height / 2);
  text(off ? 'click to stop counting' : 'stopped', width / 2, height / 2 + 30);
}
A counter that goes up once per bar; a click removes the listener and the number stops.

Related

  • drumHit() An event you define: it is called for every hit of the drum machine, with the drum that sounded and how hard.
  • stepStarted() An event you define: it is called at every one of the 16 steps of the drum machine, with the step and the bar.
  • barStarted() An event you define: it is called at the first step of every bar of the drum machine, with the bar number.
  • cueFired() An event you define: it is called when the bar of a cue declared with cue() has come, with the cue's name and the bar.
  • rhythmChanged() An event you define: it is called with the new line of rhythm every time the rhythm changes.
  • OAVp5.drums The object behind the drum names: the state of the machine, its counters, the line of rhythm, its history and the loaded machine.

p5/oav-p5.js, line 1470

↑ Back to top · All names