open-audiovisual
Search GitHub

oav-p5 reference · Timeline, cues and score

drumBar#

variableoav-p5
drumBar

Also: OAVp5.drums.bar

Which bar the drum machine is in, counted from 1 when it started.

A bar is 16 steps, four beats. Use it to give a piece a shape: floor((drumBar - 1) / 4) % 3 is the section number when you change every four bars. To make something happen at an exact bar, cue() is neater than testing drumBar in draw(). barStarted() is the function that is called at the start of each bar.

Type
Number
Range
0, 1, 2, 3 …

0 before the machine has played its first step, then 1, 2, 3 … with no upper limit. It keeps its last value when the machine stops.

Before it has a value

Start the machine with startDrums(). Stopping and starting again counts from bar 1 again.

Notes

  • A sketch that declares its own variable called drumBar keeps it: oav-p5 leaves the name alone and a card says to read OAVp5.drums.bar instead.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startDrums('four on floor', 150);
  textAlign(CENTER, CENTER);
  textSize(64);
}

function draw() {
  background(20);
  fill(255);
  text(drumBar, width / 2, height / 2);
}
A big number that goes up by one at the start of every bar.
jsOpenProcessing ↗
const SECTIONS = ['#3b5bdb', '#e8590c', '#2f9e44'];

function setup() {
  createCanvas(400, 300);
  startDrums('breakbeat', 160);
  noStroke();
}

function draw() {
  const section = floor((max(drumBar, 1) - 1) / 2) % SECTIONS.length;   // a new section every 2 bars
  background(SECTIONS[section]);
  fill(255);
  text('bar ' + drumBar + ', section ' + (section + 1), 20, 30);
}
The background changes colour every two bars and the text names the bar and the section.

Related

  • barStarted() An event you define: it is called at the first step of every bar of the drum machine, with the bar number.
  • drumStep Which of the 16 steps of the bar the drum machine just played, from 0 to 15.
  • 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.
  • OAVp5.drums.count How many steps the drum machine has played since it started, from 0; -1 before the first one.
  • 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