open-audiovisual
Search GitHub

oav-p5 reference · Microphone and audio analysis

OAVp5.mic.analyzer#

variableoav-p5
OAVp5.mic.analyzer

The AudioAnalyzer from @openav/audio that listens to the microphone, while the microphone is on; null otherwise.

A way into the audio itself when the 0 to 1 numbers are not enough: read the spectrum of the microphone with the browser's own OAVp5.mic.analyzer.analyser.getByteFrequencyData(array), or connect your own Web Audio nodes to it. oav-p5 updates the analyzer once per frame, so do not call its update() yourself.

Type
AudioAnalyzer | null

Made when the microphone starts and dropped when it stops. It has its own AudioContext (OAVp5.mic.analyzer.ctx) and the Web Audio AnalyserNode it reads from (OAVp5.mic.analyzer.analyser).

Notes

  • This is advanced. Check it is not null first, and read it again after stopMic() and startMic(): it is a new object each time.

Examples

jsOpenProcessing ↗
let bins;

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

function draw() {
  background(20);
  const a = OAVp5.mic.analyzer;
  if (!a) return;   // the microphone is not on yet
  bins = bins || new Uint8Array(a.analyser.frequencyBinCount);
  a.analyser.getByteFrequencyData(bins);   // the spectrum, 0..255 per frequency bin
  fill(255, 211, 107);
  for (let i = 0; i < 64; i++) {
    const h = bins[i] / 255 * (height - 20);
    rect(20 + i * 5.8, height - 10 - h, 4, h);
  }
}
The lowest 64 bins of the microphone's spectrum as bars, read straight from the analyser. Whistle and watch the bars move. Asks for the microphone

Related

  • OAVp5.mic.start() Turns the microphone on and returns a promise that says whether it worked: the same as startMic().
  • OAVp5.mic.state Where the microphone is: 'off', 'asking', 'on', 'denied' or 'unavailable'.
  • bass How much of the microphone's sound is low (bass), middle (mid) or high (treble), each from 0 to 1.
  • micLevel How loud the microphone hears things, from 0 (a quiet room) to 1 (shouting).

p5/oav-p5.js, line 899

↑ Back to top · All names