open-audiovisual
Search GitHub

oav-p5 reference · Timeline, cues and score

drumStep#

variableoav-p5
drumStep

Also: OAVp5.drums.step

Which of the 16 steps of the bar the drum machine just played, from 0 to 15.

A bar has 16 steps (sixteenth notes) and a beat has 4, so drumStep % 4 === 0 is a beat and drumStep === 0 the start of a bar. At 120 bpm a step lasts 125 ms. Use it to light a cell or pick a frame: fill(i === drumStep ? 255 : 80). If you would rather be called on each step, define stepStarted(). For something that moves smoothly instead of in 16 jumps, use drumPhase.

Type
Number
Range
0..15

Changes in the frame in which a step is heard, right before draw(). 0 at the start, and it keeps its last value when the machine stops.

Before it has a value

Start the machine with startDrums(). Before that, and when the machine is stopped, the value is 0 or the last one, so check drumsAreOn if the difference matters.

Notes

  • A sketch that declares its own variable called drumStep keeps it: oav-p5 leaves the name alone and a card says to read OAVp5.drums.step instead. While the tab is hidden the clock keeps counting but nothing else is told.

Examples

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

function draw() {
  background(20);
  for (let i = 0; i < 16; i++) {
    fill(drumsAreOn && i === drumStep ? 255 : 70);
    rect(20 + i * 22, 130, 18, 40);   // sixteen cells, the current one lights up
  }
}
Sixteen cells in a row with the current step lit, one cell at a time, from left to right.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startDrums('breakbeat', 110);
  textAlign(CENTER, CENTER);
  textSize(18);
}

function draw() {
  background(drumStep % 4 === 0 ? 70 : 20);   // a flash on every beat
  fill(255);
  text('step ' + drumStep + ' of 16' + (drumStep === 0 ? '  (bar starts)' : ''), width / 2, height / 2);
}
The background lights up on every beat (steps 0, 4, 8, 12) and the text counts the steps.

Related

  • stepStarted() An event you define: it is called at every one of the 16 steps of the drum machine, with the step and the bar.
  • drumBar Which bar the drum machine is in, counted from 1 when it started.
  • drumPhase How far through the current bar the drum machine is, smoothly from 0 up to just below 1, as it is heard.
  • beatPhase How far through the current beat the drum machine is, smoothly from 0 up to just below 1, four times per bar.
  • drumsAreOn Whether the drum machine is playing right now.
  • 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.

p5/oav-p5.js, line 316

↑ Back to top · All names