open-audiovisual
Search GitHub

oav-p5 reference · Microphone and audio analysis

beat#

variableoav-p5
beat

True for one frame each time the microphone (or the drum machine) hears a beat.

It behaves like mouseIsPressed while a button is down, except the "press" lasts one frame: if (beat) flash = 1; is all it takes to react to the music. A beat is a jump in loudness over what the room has been doing, with at least 100 ms between two beats; two beats between two frames count as one.

When the drum machine is playing, beat is also true for a frame on every kick and snare, so a sketch written for the microphone moves over to the machine unchanged.

Type
Boolean

True in the first draw() after a beat, and false until the next one. Reading it twice in the same frame gives the same answer.

Before it has a value

Reading beat asks for the microphone the first time, like micLevel. If you would rather be called, define beatDetected().

Examples

jsOpenProcessing ↗
let flash = 0;

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

function draw() {
  if (beat) flash = 1;        // true for exactly one frame per beat
  background(20 + flash * 200);
  flash *= 0.9;               // fade back down
}
The screen flashes on every beat and fades out. Clap or tap the desk. Asks for the microphone
jsOpenProcessing ↗
let count = 0;

function setup() {
  createCanvas(400, 300);
  textAlign(CENTER, CENTER);
  textSize(96);
}

function draw() {
  if (beat) count++;
  background(20);
  fill(255);
  text(count, width / 2, height / 2);
}
A counter that goes up by one per beat, never twice for the same one. Asks for the microphone

Related

  • beatDetected() You define this function; oav-p5 calls it each time the microphone (or the drum machine) hears a beat.
  • micLevel How loud the microphone hears things, from 0 (a quiet room) to 1 (shouting).
  • 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.

p5/oav-p5.js, line 1048

↑ Back to top · All names