open-audiovisual
Search GitHub

oav-p5 reference · Loading options

sound#

optionoav-p5
?oav_sound=click | off | an instrument id

Whether notes are heard: click (only after startSound() or 🔊), off (never), or an instrument id that starts at the first click.

With click, nothing makes a sound until the sketch calls startSound() or someone presses 🔊 in the 🎹 panel. With an instrument id, every key, pad, the on-screen controller and a phone are heard from the first click on the page, with no code at all, so any MIDI sketch becomes playable by adding the option to the page address: ?oav_sound=piano. off is a promise to the page: nothing sounds, startSound() resolves false and the 🔊 button is hidden.

Browsers let a page play sound only after a click, and a MIDI key is not a click, so the first sound waits for one: a card says "sound is ready but silent" until then.

Type
String
Values
click | off | an instrument id
default
click

An instrument id such as piano, organ, strings, harp or music-box. on and auto start the default instrument (the piano). An id that does not exist plays the default synth (synth/pad) and the console lists the ids that exist.

Notes

  • Set it before oav-p5 loads. window.OAV_P5 = { sound: … }; above the loader line wins, then ?oav_sound=… on the page address, then ?sound=… on the script's own URL. Values in window.OAV_P5 are turned into text, so write 'off', not false. On OpenProcessing the page address takes ?sketch_oav_sound=… (the project's README says OpenProcessing forwards it to the sketch).

Examples

jsOpenProcessing ↗
window.OAV_P5 = { sound: 'organ' };

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

function mousePressed() {
  OAVp5.play(60 + floor(random(12)), 0.8);   // a key, as a controller would play it
}

function draw() {
  background(20);
  fill(255);
  text('sound: ' + OAVp5.sound.state + ' (' + OAVp5.sound.instrument + ')', 20, 30);
  text('click the canvas to play a note', 20, 60);
}
No startSound() call: the organ is heard from your first click, which also plays a note. The state goes from "waiting" to "on".
jsOpenProcessing ↗
window.OAV_P5 = { sound: 'off' };

let answer = 'click the canvas to try';

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

function mousePressed() {
  startSound().then((ok) => { answer = 'startSound() resolved ' + ok; });
}

function draw() {
  background(20);
  fill(255);
  text('sound: ' + OAVp5.sound.state, 20, 30);
  text(answer, 20, 60);   // startSound() resolved false
}
Nothing can make sound: the state stays "off", startSound() resolves false, and the 🔊 button is not in the panel.

Related

  • 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.
  • soundIsOn Whether notes are being heard right now: true once startSound() has finished, false before and after.
  • tone Where Tone.js, the sound engine behind startSound(), is loaded from: an ES module address. Empty means jsDelivr.
  • OAVp5.play() Plays a key as if a controller did: midi.play(60, 0.8) presses middle C, midi.play(60, 0) lets it go.

p5/oav-p5.js, line 130

↑ Back to top · All names