open-audiovisual
Search GitHub

oav-p5 reference · Drum machine and rhythm

startDrums()#

functionoav-p5
startDrums([pattern], [bpm])

Also: OAVp5.drums.start

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.

Call it once in setup(): startDrums('breakbeat', 120). A grid of 16 steps by 4 lanes (kick, snare, hat, clap) appears at the top of the page, with the tempo and the bar it is in, and plays a synthesized drum kit (no sample files). The grid is part of the page, not of the canvas: a recording never shows it, so use drawRhythm() to draw the rhythm on the canvas. A click on a cell changes the beat, and 🥁 in the 🎹 panel starts and stops the machine with no code at all.

The machine is also a clock and a source of events. Every hit calls drumHit() and the microphone's own functions (kickDetected(), snareDetected(), hatDetected(), beatDetected()), so a sketch written for the microphone moves over unchanged. drumStep, drumBar, drumPhase and beatPhase say where the machine is, and cue() lets you plan what happens at which bar.

Calling it again while it plays changes the pattern or the tempo without restarting: startDrums(90) only changes the tempo, startDrums('latin') only the pattern. A line of rhythm is any text that is not a preset name. A word is a drum (bd kick, sd snare, hh hat, oh open hat, cp clap, rim, lt mt ht toms, cr crash, rd ride, cb cowbell), ~ is a rest, words separated by spaces share the bar evenly, [ ] groups, *4 repeats, , puts another line on top, < > takes one per bar and bd(3,8) spreads 3 hits over 8 steps. A mistake shows a card that points at the character and says how to fix it, and 'four on floor' plays meanwhile.

A line copied from strudel.cc works too: startDrums('s("bd*4, [~ sd]*2").fast(2)'). s("…") and sound("…") with .fast(n) and .slow(n) are read, setcpm(110/4) sets the tempo if you give none, .bank(…) is left out (oav-p5 plays its own kit), and any other call in the chain gets a card that says it is not supported yet. The rest plays.

Before you call it

Nothing before the call. The counting, drumHit() and the other callbacks start at once, but a browser lets the page make sound only after a click or key press on it, so the drums are silent until the first one. A card at the top says so after a moment.

Parameters

NameTypeDescription
patternString | Object | NumberWhat to play. A preset name ('four on floor', 'breakbeat', 'half time', 'latin', 'sparse'). A line of rhythm such as 'bd*4, [~ sd]*2, hh*8', or one copied from strudel.cc. Your own grid { kick: 'x...x...x...x...', snare, hat, clap } (x or 1 is a hit, a lane you leave out is silent, {} is a silent clock). A number alone is the tempo. Leave it out to keep what plays now ('four on floor' at the start). optional
bpmNumberThe tempo in beats per minute (a beat is four steps). Kept between 30 and 300. Leave it out to keep the current tempo: 112 at the start, or the one a copied Strudel line sets with setcpm(…). optional

Returns

Promise<Boolean> Resolves to true once the machine plays (or when a later startDrums() took over). Resolves to false when stopDrums() came first, when there is no page to put the grid on, or when @openav/drums could not be loaded.

Notes

  • Drum sound does not need startSound(): the machine has its own kit. Pads on MIDI channel 10 play the same kit once startSound() is on.
  • The card has – to fold the grid away and × to stop the machine. A tab in the background keeps counting and playing. The loading options drums (lookahead or frame) and drumlatency tune the clock.

Examples

jsOpenProcessing ↗
let swell = 0;

function setup() {
  createCanvas(400, 300);
  startDrums('breakbeat', 120);   // the grid appears at the top of the page
}

function kickDetected(level) {
  swell = 1;
}

function draw() {
  background(20);
  swell *= 0.9;
  circle(width / 2, height / 2, 60 + swell * 120);
}
A grid appears at the top of the page. Click the canvas once to hear it: the circle jumps on every kick.
jsOpenProcessing ↗
const LINES = ['bd*4, [~ sd]*2, hh*8', 'bd(3,8), [~ sd]*2, hh*16', 'bd ~ ~ bd, ~ sd ~ ~, [hh hh]*4'];
let n = 0;

function setup() {
  createCanvas(400, 300);
  startDrums(LINES[0], 110);
  textAlign(CENTER, CENTER);
}

function mousePressed() {
  n = (n + 1) % LINES.length;
  startDrums(LINES[n]);   // changes the rhythm while it plays
}

function draw() {
  background(20);
  fill(255);
  text('click to change the rhythm', width / 2, height / 2 - 20);
  text(OAVp5.drums.src, width / 2, height / 2 + 20);
}
Click the canvas to step through three rhythms: the line of text under the caption is the one that plays now, and the grid at the top follows it.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startDrums({}, 90);   // no lane has a hit: nothing to hear, the clock still counts
  noStroke();
}

function draw() {
  background(20);
  for (let i = 0; i < 16; i++) {
    fill(i === drumStep ? 255 : 90);
    circle(30 + i * 22.5, height / 2, i === drumStep ? 18 : 8);
  }
}
A silent clock: sixteen dots with the current step enlarged, running at 90 bpm with no sound at all.

Related

  • stopDrums() Stops the drum machine and hides its card.
  • drumsAreOn Whether the drum machine is playing right now.
  • drumHit() An event you define: it is called for every hit of the drum machine, with the drum that sounded and how hard.
  • drumStep Which of the 16 steps of the bar the drum machine just played, from 0 to 15.
  • drumPhase How far through the current bar the drum machine is, smoothly from 0 up to just below 1, as it is heard.
  • 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.
  • cue() Plans something for a bar: at bar bar the function cueFired(name, bar) is called and cueName becomes name; with every, again every that many bars.
  • rhythmChanged() An event you define: it is called with the new line of rhythm every time the rhythm changes.
  • beat True for one frame each time the microphone (or the drum machine) hears a beat.

p5/oav-p5.js, line 1760

↑ Back to top · All names