open-audiovisual
Search GitHub

oav-p5 reference · Drum machine and rhythm

rhythmChanged()#

eventoav-p5
function rhythmChanged(src, how) {}

An event you define: it is called with the new line of rhythm every time the rhythm changes.

The line over the grid and the cells edit each other, so the rhythm can change without your code: a click on a cell, a line typed into the card, ↶. rhythmChanged(src, how) tells the sketch, so it can show the line on the canvas (where a recording has it) or store it. It is also called once after startDrums(), a moment after the rhythm module has loaded, with how equal to 'code'.

Parameters

NameTypeDescription
srcStringThe line of rhythm that plays now, for example 'bd*4, [~ sd]*2, hh*8'. A preset or a grid you drew comes as the line it is written as.
howStringWhat changed it: 'code' (startDrums()), 'cell' (a click on a cell of the grid), 'type' (typed into the line on the card, or OAVp5.drums.edit()), 'undo' (↶ or OAVp5.drums.undo()) or 'menu' (a preset picked on the card).

Examples

jsOpenProcessing ↗
let current = '';
let how = '';

function setup() {
  createCanvas(400, 300);
  startDrums('bd*4, [~ sd]*2, hh*8', 110);
  textAlign(CENTER, CENTER);
}

function rhythmChanged(src, mode) {
  current = src;
  how = mode;
}

function mousePressed() {
  OAVp5.drums.edit('bd(3,8), [~ sd]*2, hh*16');   // as if typed on the card
}

function draw() {
  background(20);
  fill(255);
  text(current, width / 2, height / 2 - 10);
  text('changed by: ' + how, width / 2, height / 2 + 20);
  text('click the canvas, or click a cell of the grid', width / 2, height - 30);
}
The line that plays and what changed it: "code" at the start, "type" after a click on the canvas, "cell" when you click a cell of the grid.

Related

  • startDrums() Starts a drum machine: a 16-step grid on the page that plays a preset, your own grid or one line of rhythm, and counts the bars for your sketch.
  • OAVp5.drums.src The line of rhythm that plays now, such as 'bd*4, [~ sd]*2, hh*8'; an empty string until the machine has loaded.
  • OAVp5.drums.edit() Plays a line of rhythm as if it was typed on the card, and returns whether it worked and, if not, what is wrong with it.
  • OAVp5.drums.undo() Goes back one line of rhythm, like ↶ on the card; returns true if there was an earlier line.
  • OAVp5.drums.history The earlier lines of rhythm, oldest first: every click on a cell, typed line or menu pick leaves the line before it here.
  • OAVp5.drums.on() Listens to the drum machine from anywhere: OAVp5.drums.on('hit', fn) calls fn like drumHit does, and returns a function that stops listening.
  • drawRhythm() Draws the rhythm on the canvas as a grid, a circle or a spiral, with a playhead and the hits lighting up, so a recording has it too.

p5/oav-p5.js, line 1666

↑ Back to top · All names