env()#
functionoav-p5env([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
| Name | Type | Description |
|---|---|---|
event | String | Boolean | Number | What 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' |
attack | Number | Seconds the rise from 0 to 1 takes. optional default 0.01 |
decay | Number | Seconds the fall from 1 back to 0 takes. optional default 0.3 |
name | String | Number | Only 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 insetup()ormousePressed()too. Only the switch form remembers anything between frames, and an unnamed switch outsidedraw()returns 0. Calls indraw()are matched by order, likelag(). OAVp5.chop.env(…)is this function under another name.
Examples
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);
}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);
}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);
}Related
trigger()Fires an event of your own, by name, forenv()andcount()to react to.count()Counts how oftenonhas turned on, or how many'kick'(or any other event) have happened; with a limit it loops 0, 1, … limit − 1.lag()Followsvaluesmoothly instead of jumping: up inupseconds, down indownseconds (TouchDesigner's Lag CHOP).beatTrue 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.chopThe CHOP functions under one roof, plus the state of the module behind them:OAVp5.chop.lag(…),.state,.slots(),.reset().