open-audiovisual
Search GitHub

oav-p5 reference · Microphone and audio analysis

startMic()#

functionoav-p5
startMic()

Turns the microphone on and returns a promise that says whether it worked.

You rarely need it: reading micLevel and its friends already starts the microphone. Call it when you want to choose the moment (a button, the end of an intro) or know the answer: startMic().then((ok) => { … }). Calling it again while it is on, or while the browser is still asking, returns the same result.

Before you call it

Browsers want a click before audio plays: call it from mousePressed() or a button handler if setup() is too early. It works whatever the mic option is, except ?oav_mic=off, where nothing can start the microphone.

Returns

Promise<Boolean> Resolves to true once the microphone is on, false if it was blocked, is missing, or the mic option is off.

Notes

  • After stopMic(), only startMic() (or 🎤) turns it back on; reading micLevel does not.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  textAlign(CENTER, CENTER);
}

function mousePressed() {
  startMic().then((ok) => console.log(ok ? 'microphone on' : 'no microphone'));
}

function draw() {
  background(20);
  fill(255);
  text(micIsOn ? 'level ' + nf(micLevel, 1, 2) : 'click to start the microphone', width / 2, height / 2);
}
Nothing happens until you click; then the browser asks, and the number starts moving. Asks for the microphone

Related

  • stopMic() Turns the microphone off and sets every microphone value back to 0.
  • micIsOn Whether the microphone is running right now.
  • micLevel How loud the microphone hears things, from 0 (a quiet room) to 1 (shouting).

p5/oav-p5.js, line 1073

↑ Back to top · All names