count()#
functionoav-p5count(on, [limit], [name])Also: OAVp5.chop.count
Counts how often on has turned on, or how many 'kick' (or any other event) have happened; with a limit it loops 0, 1, … limit − 1.
A counter you do not have to keep yourself: count(mouseIsPressed) is the number of clicks so far, count(beat, 4) walks 0 1 2 3 0 1 … on the beat, and count('kick') counts the kicks of the drum machine. A switch has to go off and on again to be counted twice.
Event counts start when count('kick') is first called, so earlier kicks are not counted. An event of your own fired with trigger() is counted from its first firing, whenever count() is first called.
Before you call it
The event names work like env(): 'kick', 'snare', 'hat' and 'beat' come from the microphone, or from the drum machine once startDrums() has been called, and the first count('kick') asks for the microphone if the drum machine is not on. The first call loads @openav/chop, which takes a few frames.
Parameters
| Name | Type | Description |
|---|---|---|
on | Boolean | Number | String | A switch: a boolean, or a number above 0.5, and each time it goes from off to on counts one. Or an event name ('kick', 'beat', 'note', or one of your own fired with trigger()): how many of those events have happened. |
limit | Number | Loop at this number: 4 gives 0 1 2 3 0 1 … Left out, or 0 or less, it keeps counting up. optional default 0 |
name | String | Number | Only for the switch form (count(beat, 4, 'a')): a name for its memory, as the last argument. The event-name form needs none. optional read from arguments |
Returns
Number The count, a whole number from 0. It is 0 until @openav/chop has loaded (a few frames), and for an unnamed switch outside draw().
Notes
- Unnamed calls find their memory by their order in
draw(): the firstlag()is the first lag, the second is the second. Put one inside anif, or in a loop whose length changes, and the order slips: a card says so, and from then on oav-p5 matches each call by the line it is written on. A name at the end is the sure way. Outsidedraw()an unnamed call has nothing to remember by: it returns what Returns says and a card says so. - A sketch with its own variable or function called
countkeeps it.OAVp5.chop.count(…)is this function under another name.
Examples
function setup() {
createCanvas(400, 300);
textAlign(CENTER, CENTER);
textSize(96);
}
function draw() {
background(20);
fill(255);
text(count(mouseIsPressed), width / 2, height / 2); // clicks so far
}function setup() {
createCanvas(400, 300);
noStroke();
startDrums('bd*4, [~ sd]*2, hh*8', 108);
}
function draw() {
background(20);
const nth = count('kick', 4); // 0 1 2 3 0 1 ...
for (let i = 0; i < 4; i++) {
fill(i === nth ? color(255, 90, 31) : 70);
circle(100 + i * 67, 150, i === nth ? 50 : 30);
}
}const colors = ['#ff5a1f', '#78beff', '#ffd36b'];
function setup() {
createCanvas(400, 300);
}
function mousePressed() { trigger('boom'); }
function draw() {
background(colors[count('boom', 3)]); // 0 1 2 0 1 2 ...
fill(20);
text('clicks: ' + count('boom'), 20, 30);
}Related
env()A 0..1 pulse that jumps up after each event (a kick, a beat, a key, a press) and then fades out: TouchDesigner's Trigger CHOP.trigger()Fires an event of your own, by name, forenv()andcount()to react to.beatTrue for one frame each time the microphone (or the drum machine) hears a beat.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.OAVp5.chopThe CHOP functions under one roof, plus the state of the module behind them:OAVp5.chop.lag(…),.state,.slots(),.reset().