drumBar#
variableoav-p5drumBarAlso: 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
drumBarkeeps it: oav-p5 leaves the name alone and a card says to readOAVp5.drums.barinstead.
Examples
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);
}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);
}Related
barStarted()An event you define: it is called at the first step of every bar of the drum machine, with the bar number.drumStepWhich of the 16 steps of the bar the drum machine just played, from 0 to 15.cue()Plans something for a bar: at barbarthe functioncueFired(name, bar)is called andcueNamebecomesname; withevery, again every that many bars.OAVp5.drums.countHow 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.