open-audiovisual
Search GitHub

oav-p5 reference · Drum machine and rhythm

drawRhythm()#

functionoav-p5
drawRhythm([x], [y], [w], [h], [style], [o])

Also: OAVp5.drums.draw

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.

Call it in draw() after background(): drawRhythm(20, 100, 360, 100). Each row is one drum that plays in the pattern, a hit lights up the moment it is heard (the clock drumPhase reads), and a hit between two steps, such as a triplet, sits where it falls. The grid card is part of the page and a recording never shows it, but the canvas is what a recording takes, so this is how the rhythm gets into the video.

With no box it draws a strip along the bottom for 'grid' and a square in the middle for the other two. It draws with p5's own shapes between push() and pop(), so it leaves your styles alone and works in 2D and in WEBGL, in p5 1.x and 2.x. In WEBGL the origin is the middle of the canvas: drawRhythm(-width / 2, -height / 2, width, height, 'circle') fills it.

Before you call it

Start the machine with startDrums() first. Before it plays you get an empty four-lane grid.

Parameters

NameTypeDescription
xNumberLeft edge of the box to draw in. In WEBGL the middle of the canvas is (0, 0). If any of x, y, w, h is left out, the default box is used. optional
yNumberTop edge of the box. optional
wNumberWidth of the box. optional
hNumberHeight of the box. optional
styleString | Object'grid' (the 16 steps with a playhead), 'circle' (a ring of 16 per drum with a hand turned by drumPhase) or 'spiral' (the last few bars wound outwards, now at the tip). An object here is taken as o, with style inside it. optional default 'grid'
oObjectOptions: colors ({ kick: '#f66', … }, any p5 colour per drum), color (one colour for all), off (the empty steps), playhead (a colour, or false for none), bg (a background for the box) and bars (spiral only, 1 to 16, 4 if left out). optional

Returns

Boolean true if it drew something, false when the box is empty (w or h is not above 0) or there is no p5 global mode to draw with.

Notes

  • OAVp5.drums.draw(x, y, w, h, style, o) is the same function as a method.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startDrums('breakbeat', 120);
}

function draw() {
  background(14, 16, 26);
  drawRhythm(20, 100, 360, 100);   // the 16 steps with a playhead
}
The pattern as a grid on the canvas itself: a white playhead crosses it, and every hit lights up as it sounds.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startDrums('bd(3,8), [~ sd]*2, hh*8, ~ ~ ~ cp', 104);
}

function draw() {
  background(14, 16, 26);
  drawRhythm(10, 10, 180, 180, 'circle');
  drawRhythm(210, 10, 180, 180, 'spiral', { bars: 4, colors: { kick: '#ff7b54' } });
  drawRhythm(20, 220, 360, 60, 'grid', { bg: 'rgba(255, 255, 255, 0.05)' });
}
The same rhythm three ways: a ring per drum with a hand, the last four bars as a spiral, and the grid along the bottom.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300, WEBGL);
  startDrums('four on floor', 120);
}

function draw() {
  background(14, 16, 26);
  drawRhythm(-width / 2, -height / 2, width, height, 'circle');   // (0, 0) is the middle in WEBGL
}
In a WEBGL canvas the box starts at (-width / 2, -height / 2), because (0, 0) is the middle.

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.
  • drumPhase How far through the current bar the drum machine is, smoothly from 0 up to just below 1, as it is heard.
  • drumStep Which of the 16 steps of the bar the drum machine just played, from 0 to 15.
  • startRecording() Records the canvas and the sound your sketch makes into one video file, and returns a promise that says whether the recording began.
  • rhythmChanged() An event you define: it is called with the new line of rhythm every time the rhythm changes.

p5/oav-p5.js, line 1900

↑ Back to top · All names