open-audiovisual
Search GitHub

oav-p5 reference · Values over time (CHOP)

count()#

functionoav-p5
count(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

NameTypeDescription
onBoolean | Number | StringA 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.
limitNumberLoop at this number: 4 gives 0 1 2 3 0 1 … Left out, or 0 or less, it keeps counting up. optional default 0
nameString | NumberOnly 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 first lag() is the first lag, the second is the second. Put one inside an if, 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. Outside draw() 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 count keeps it. OAVp5.chop.count(…) is this function under another name.

Examples

jsOpenProcessing ↗
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
}
A counter that goes up by one on every click. Holding the button down counts once.
jsOpenProcessing ↗
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);
  }
}
Four dots light up one after another, one step per kick of the drum machine. Click the canvas once if you hear nothing.
jsOpenProcessing ↗
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);
}
Each click cycles the background through three colours, and the text counts the clicks without end.

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, for env() and count() to react to.
  • beat True 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.chop The CHOP functions under one roof, plus the state of the module behind them: OAVp5.chop.lag(…), .state, .slots(), .reset().

p5/oav-p5.js, line 3086

↑ Back to top · All names