open-audiovisual
Search GitHub

oav-p5 reference · Sound and instruments

OAVp5.sound.state#

variableoav-p5
OAVp5.sound.state

Where sound is: 'off', 'waiting' (for a click), 'loading', 'on' or 'error'.

'off' is where it starts and where stopSound() leaves it. startSound() moves it to 'loading' while @openav/sound and Tone.js arrive, to 'waiting' when everything is ready but the browser has not yet had its click, and to 'on' when notes are heard. 'error' means the sound parts could not be loaded (a network, a school firewall or an ad blocker); the message is in OAVp5.sound.error, and calling startSound() again tries once more.

soundIsOn is the same thing reduced to state === 'on'.

Type
String
Range
off | waiting | loading | on | error

Starts as 'off' and changes whenever the sound moves to another stage.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startSound('marimba');
  textAlign(CENTER, CENTER);
  textSize(28);
}

function draw() {
  background(20);
  const hints = { off: 'off', loading: 'loading…', waiting: 'click to hear', on: 'listening', error: 'failed' };
  fill(255);
  text(hints[OAVp5.sound.state], width / 2, height / 2);
}
One word for each stage; "click to hear" is what the sketch shows between a ready sound and the first click.

Related

  • soundIsOn Whether notes are being heard right now: true once startSound() has finished, false before and after.
  • startSound() Makes the keys you play heard through an instrument you name, and returns a promise that says whether sound is on.
  • stopSound() Turns sound off and lets every ringing note go; the instrument stays loaded, so startSound() brings it back at once.
  • OAVp5.sound.error The message of the last thing that went wrong while loading sound, or null when nothing has.
  • OAVp5.sound.status How the instrument is loading: { id, state, progress, error }, with progress from 0 to 1.

p5/oav-p5.js, line 1346

↑ Back to top · All names