open-audiovisual
Search GitHub

oav-p5 reference · Values over time (CHOP)

env()#

functionoav-p5
env([event], [attack], [decay], [name])

Also: OAVp5.chop.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.

Call it every frame and use the number like a size or a brightness: circle(width / 2, height / 2, 60 + env('kick', 0.01, 0.3) * 200) swells on every kick. The number rises to 1 in attack seconds after an event and falls back to 0 in decay seconds, quick at first and landing softly, the way a bass drum's swell looks right.

An event that arrives while the last pulse is still sounding restarts from where the pulse is, never from zero, so fast hits pile up into a plateau instead of flickering. The attack is measured from 0: a retrigger from halfway up takes half the attack.

The events: 'kick', 'snare' and 'hat' are the hits kickDetected() hears (from the microphone, or from the drum machine when it plays), 'beat' is beat, 'note' is any key going down, 'bar' and 'step' come from the drum machine, a cue's name fires when cue() does, and any other name is yours to fire with trigger(). The switch form, env(mouseIsPressed, 0.02, 0.5), fires on every press.

Before you call it

'kick', 'snare', 'hat' and 'beat' need something that makes them. If the sketch has not started the drum machine (startDrums()), the first env('kick') asks for the microphone, like reading micLevel, and ?oav_mic=off means nothing ever fires. Once the drum machine is started, the microphone is not opened for it. 'note' needs a key: play one, press 🎹, or call OAVp5.play(). A name of your own needs trigger(): if nothing fires it within 4 seconds, a card says so.

Parameters

NameTypeDescription
eventString | Boolean | NumberWhat to react to. A name: 'kick', 'snare', 'hat', 'beat' (from the microphone or the drum machine), 'note' (any key), 'bar', 'step' (the drum machine), a cue's name, or an event of your own fired with trigger(). Or a switch: a boolean, or a number above 0.5, and each time it turns on counts as an event. Left out: 'beat'. optional default 'beat'
attackNumberSeconds the rise from 0 to 1 takes. optional default 0.01
decayNumberSeconds the fall from 1 back to 0 takes. optional default 0.3
nameString | NumberOnly for the switch form (env(mouseIsPressed, …, 'a')): a name for its memory, as the last argument. A named event such as 'kick' needs none. optional read from arguments

Returns

Number 0..1: 0 when nothing has happened, 1 at the top of the pulse. Until @openav/chop has loaded (a few frames) it returns 0.

Notes

  • A named event needs no place in draw(): env('kick') works in setup() or mousePressed() too. Only the switch form remembers anything between frames, and an unnamed switch outside draw() returns 0. Calls in draw() are matched by order, like lag().
  • OAVp5.chop.env(…) is this function under another name.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
}

function draw() {
  background(20);
  const k = env(mouseIsPressed, 0.02, 0.5);   // 1 right after each press, then it fades
  circle(width / 2, height / 2, 60 + k * 200);
}
Click the canvas: the circle jumps open and fades back each time.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  noStroke();
  startDrums('bd*4, [~ sd]*2, hh*8', 108);   // a drum machine, so no microphone is needed
}

function draw() {
  background(20);
  const kick = env('kick', 0.01, 0.3);       // 1 on every kick, 0 again 0.3 s later
  const snare = env('snare', 0.005, 0.15);
  fill(255, 90, 31);
  circle(120, 150, 40 + kick * 160);
  fill(120, 190, 255);
  circle(280, 150, 40 + snare * 160);
}
Two circles swell on the drum machine's kick and snare. Click the canvas once if you hear nothing: browsers wait for a click before they play sound.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
}

function mousePressed() {
  OAVp5.play(48 + floor(random(24)), 0.8);   // play a key, as a controller would
}

function draw() {
  background(20);
  const k = env('note', 0.005, 0.8);         // every key makes it flash
  fill(255, 211, 107);
  circle(width / 2, height / 2, 40 + k * 240);
}
Click the canvas, or play a key on a controller or the 🎹: every key pulses the circle.

Related

  • trigger() Fires an event of your own, by name, for env() and count() to react to.
  • 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.
  • lag() Follows value smoothly instead of jumping: up in up seconds, down in down seconds (TouchDesigner's Lag CHOP).
  • beat True for one frame each time the microphone (or the drum machine) hears a beat.
  • kickDetected() You define these; oav-p5 calls them when the microphone (or the drum machine) hears a kick, a snare or a hat.
  • 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 3045

↑ Back to top · All names