open-audiovisual
Search GitHub

oav-p5 reference · Drum machine and rhythm

OAVp5.drums.edit()#

functionoav-p5
OAVp5.drums.edit(text)

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.

Unlike startDrums(text), which plays 'four on floor' when the line has a mistake, edit() keeps the line that was playing and tells you why the new one failed, so a live-coding sketch can show the message. The line goes into OAVp5.drums.history and rhythmChanged() is called with how equal to 'type'. A mistake also shows the card that points at the character.

Before you call it

The machine and the rhythm module must have loaded: call startDrums() first and give it a moment.

Parameters

NameTypeDescription
textStringThe line of rhythm, for example 'bd*4, [~ sd]*2, hh*8', or a line copied from strudel.cc.

Returns

Object { ok: true, error: null, text } when the line plays. { ok: false, error, text } when it has a mistake: error has code, index and length (where the bad word is in text), a message and a hint, and the last line that worked keeps playing. Before the machine has loaded: { ok: false, error: null }.

Examples

jsOpenProcessing ↗
const TRIES = ['bd*4, hh*8', 'bd(3,8), [~ sd]*2', 'bd*4, xyz*8'];
let n = 0;
let result = '';

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

function mousePressed() {
  const r = OAVp5.drums.edit(TRIES[n % TRIES.length]);
  result = r.ok ? 'ok: ' + r.text : 'wrong: ' + r.error.message;
  n++;
}

function draw() {
  background(20);
  fill(255);
  text('click to try the next line', width / 2, 40);
  text(result, width / 2, height / 2 - 10);
  text('playing: ' + OAVp5.drums.src, width / 2, height / 2 + 30);
}
Click three times: the first two lines play; the third has a mistake, so the message is shown and the line before it keeps playing.

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.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.
  • rhythmChanged() An event you define: it is called with the new line of rhythm every time the rhythm changes.
  • 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.

p5/oav-p5.js, line 1494

↑ Back to top · All names