open-audiovisual
Search GitHub

oav-p5 reference · Drum machine and rhythm

drumPhase#

variableoav-p5
drumPhase

Also: OAVp5.drums.phase

How far through the current bar the drum machine is, smoothly from 0 up to just below 1, as it is heard.

Use it for anything that should move continuously with the music: rotate(drumPhase * TWO_PI) turns once per bar. drumStep says the same thing in 16 jumps; drumPhase is the smooth version. The value is what the listener hears, which is the machine's position corrected for the latency of the speakers, so a drawing driven by it lands on the beat you hear.

A bar has four beats; beatPhase is the same idea for one beat.

Type
Number
Range
0..1

Read fresh each time you look at it, so it is never a frame old. 0 at the start of a bar, close to 1 at its end, then 0 again. It is 0 before the machine has played and holds still while it is stopped.

Before it has a value

Start the machine with startDrums(); until it plays, drumPhase is 0.

Notes

  • A sketch that declares its own drumPhase keeps it, and oav-p5 does not write that name. OAVp5.drums.phase is the same number under a name nothing else can take.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startDrums('four on floor', 120);
  strokeWeight(4);
}

function draw() {
  background(20);
  stroke(255, 211, 107);
  translate(width / 2, height / 2);
  rotate(drumPhase * TWO_PI - HALF_PI);
  line(0, 0, 110, 0);   // a hand that turns once per bar
}
A clock hand that turns exactly once every bar, smoothly.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startDrums('breakbeat', 110);
  noStroke();
}

function draw() {
  background(20);
  const a = drumStep / 16 * TWO_PI - HALF_PI;   // hops from step to step
  const b = drumPhase * TWO_PI - HALF_PI;       // moves all the time
  fill(255, 120, 90);
  circle(200 + cos(a) * 100, 150 + sin(a) * 100, 20);
  fill(120, 200, 255);
  circle(200 + cos(b) * 70, 150 + sin(b) * 70, 20);
}
Two dots on two circles: the orange one hops from step to step, the blue one runs smoothly.

Related

  • beatPhase How far through the current beat the drum machine is, smoothly from 0 up to just below 1, four times per bar.
  • drumStep Which of the 16 steps of the bar the drum machine just played, from 0 to 15.
  • drumBar Which bar the drum machine is in, counted from 1 when it started.
  • 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.
  • 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 756

↑ Back to top · All names