OAVp5.mic.on()#
functionoav-p5OAVp5.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
| Name | Type | Description |
|---|---|---|
name | String | 'beat', 'kick', 'snare' or 'hat'. Anything else throws an error that lists them. |
fn | Function | Called 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 withOAVp5.mic.on()hears the microphone only.
Examples
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;
}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.beatTrue 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.