open-audiovisual
Search GitHub

oav-p5 reference · Microphone and audio analysis

beatDetected()#

eventoav-p5
function beatDetected(level) {}

You define this function; oav-p5 calls it each time the microphone (or the drum machine) hears a beat.

Declare it the way you declare keyPressed():

js
function beatDetected(level) {
  // flash, spawn, jump to the next scene
}

It is called once per beat, right when the beat lands, not once per frame. Use beat instead if you would rather ask each frame.

Before it has a value

A sketch that defines beatDetected() and reads no microphone value starts the microphone on the first click or key. When the drum machine is on, the same function is called for its kicks and snares and the microphone is not opened for it. OAVp5.mic.on('beat', fn) hears the same beats without defining a global and returns a function that stops listening, but a listener alone never starts the microphone: read micLevel or call startMic() as well.

Parameters

NameTypeDescription
levelNumberHow loud it was at that moment, 0..1 (the value of micLevel).

Notes

  • If your function throws, the sketch keeps running: the error appears in the console and as a card beside the 🎹 with the file and line.

Examples

jsOpenProcessing ↗
let flash = 0;

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

function beatDetected(level) {
  flash = 0.4 + level * 0.6;   // louder beat, brighter flash
}

function draw() {
  background(20 + flash * 200);
  flash *= 0.9;
}
A flash per beat, brighter for a louder one. Asks for the microphone

Related

  • 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.
  • micLevel How loud the microphone hears things, from 0 (a quiet room) to 1 (shouting).
  • 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 893

↑ Back to top · All names