open-audiovisual
Search GitHub

oav-p5 reference · Microphone and audio analysis

OAVp5.mic.on()#

functionoav-p5
OAVp5.mic.on(name, fn)

Listens to a microphone event ('beat', 'kick', 'snare' or 'hat') without defining a global function, and returns a function that stops listening.

The listener version of beatDetected(), kickDetected(), snareDetected() and hatDetected(): any number of them, added and removed while the sketch runs, with no global names taken. An error inside fn shows in the console and on a card.

Before you call it

Listening does not turn the microphone on: call startMic(), press 🎤, or read micLevel too, or nothing will ever fire. (Defining the global beatDetected() is different: oav-p5 sees that function and starts the microphone at the first click.) These events come from the microphone only, not from the drum machine.

Parameters

NameTypeDescription
nameString'beat', 'kick', 'snare' or 'hat'. Anything else throws an error that lists them.
fnFunctionCalled as fn(level) with how strong the hit was, 0 to 1: the same number beatDetected(level) and kickDetected(level) get.

Returns

Function A function that takes fn off again: const off = OAVp5.mic.on(…); off();.

Notes

  • When the drum machine plays, the global beatDetected(), kickDetected() and the others are also called for its hits. A listener added with OAVp5.mic.on() hears the microphone only.

Examples

jsOpenProcessing ↗
let flash = 0;

function setup() {
  createCanvas(400, 300);
  startMic();
  OAVp5.mic.on('beat', (level) => { flash = 0.4 + level * 0.6; });   // louder beat, brighter flash
}

function draw() {
  background(20 + flash * 200);
  flash *= 0.9;
}
A flash on every beat the microphone hears, brighter for a louder one. Clap or tap the desk. 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.
  • kickDetected() You define these; oav-p5 calls them when the microphone (or the drum machine) hears a kick, a snare or a hat.
  • OAVp5.on() Listens to a MIDI event by name without defining a global function, and returns a function that stops listening.
  • beat True for one frame each time the microphone (or the drum machine) hears a beat.
  • startMic() Turns the microphone on and returns a promise that says whether it worked.

p5/oav-p5.js, line 903

↑ Back to top · All names