startSound()#
functionoav-p5startSound([id])Also: OAVp5.sound.start
Makes the keys you play heard through an instrument you name, and returns a promise that says whether sound is on.
Call it once in setup(): startSound('organ'). From then on every note you play is heard: a MIDI keyboard, the 🎹 on screen, a phone, midi.play(60, 0.8). Your own notePressed() still gets the note, and the sound is made before the sketch is told, so a slow callback never delays it. Pads on MIDI channel 10 (the General MIDI drum channel) play the drum kit instead of the instrument, and the sustain pedal holds notes.
The instrument ids are piano, epiano, organ, strings, choir, harp, marimba, vibraphone, xylophone, glockenspiel, music-box and the synth presets synth/pad, synth/saw-lead, synth/square-lead, synth/pluck, synth/supersaw, synth/bass, synth/acid, synth/brass, synth/bell, synth/sub and synth/chip. Calling startSound(id) while sound plays switches the instrument. An id that does not exist is listed in the console together with the real ones: an instrument that is already playing stays, and at the first call the warm pad synth/pad plays.
Nothing is loaded before the first call (or before 🔊 in the 🎹 panel): @openav/sound comes from next to oav-p5.js and Tone.js 15 from jsDelivr (the tone option points it somewhere else). Only the piano plays recorded samples, about 2 MB the first time; the electric piano sounds meanwhile (or, when you switch from another instrument, that one keeps sounding), and OAVp5.sound.instrument changes to piano when the samples are in. Every other instrument is made by Tone.js and has no files to load.
Before you call it
A browser lets a page make sound only after a click or a key press on the page, and a key on a MIDI keyboard does not count. Calling startSound() in setup() is fine: OAVp5.sound.state is 'waiting' and a card beside the 🎹 says "Sound is ready but silent"; the first click or key press turns sound on. soundIsOn tells when it has.
Parameters
| Name | Type | Description |
|---|---|---|
id | String | An instrument id, for example 'organ' or 'synth/pluck' (the list is in the description). Leave it out to keep the instrument chosen before; at the very first call that is the piano. A value that is not a non-empty string counts as left out. optional default 'piano' |
Returns
Promise<Boolean> Resolves to true once sound is on. When the browser wants a click first, that is after the first click or key press on the page. Resolves to false when the loading option sound is off, when stopSound() came first, or when Tone.js could not be loaded.
Notes
- No code at all:
?oav_sound=pianoon the page address (any instrument id; on OpenProcessing?sketch_oav_sound=piano) makes any sketch heard from the first click, and?oav_sound=offkeeps it silent:startSound()then does nothing and resolves tofalse. In the 🎹 panel, 🔊 turns sound on and off and shows a picker with every instrument. - Sound is separate from the drum machine:
startDrums()has its own kit and is heard from the first click withoutstartSound().
Examples
let playing = 60;
function setup() {
createCanvas(400, 300);
startSound('synth/pluck'); // every key you play is heard
}
function mousePressed() {
playing = 48 + floor(mouseX / 25); // low on the left, high on the right
midi.play(playing, 0.8);
}
function mouseReleased() {
midi.play(playing, 0);
}
function draw() {
background(soundIsOn ? 30 : 90);
fill(255);
textAlign(CENTER, CENTER);
text(soundIsOn ? 'click to play a note' : 'click once to turn sound on', width / 2, height / 2);
}const KEYS = { a: 60, s: 62, d: 64, f: 65, g: 67, h: 69, j: 71, k: 72 };
let dots = [];
function setup() {
createCanvas(400, 300);
noStroke();
startSound('epiano');
}
function keyPressed() {
if (KEYS[key]) midi.play(KEYS[key], 0.8);
}
function keyReleased() {
if (KEYS[key]) midi.play(KEYS[key], 0);
}
function notePressed(n, v) {
dots.push({ x: map(n, 60, 72, 30, width - 30), y: height, v });
}
function draw() {
background(20);
for (const d of dots) {
fill(255, 160, 80);
circle(d.x, d.y, 20 + d.v * 30);
d.y -= 2;
}
dots = dots.filter((d) => d.y > -40);
fill(255);
text(soundIsOn ? 'play with a s d f g h j k' : 'press a key once to turn sound on', 20, 24);
}const IDS = ['synth/pluck', 'organ', 'marimba', 'piano'];
function setup() {
createCanvas(400, 300);
startSound(IDS[0]);
}
function keyPressed() {
const i = Number(key) - 1;
if (IDS[i]) startSound(IDS[i]); // switches while it plays
}
function draw() {
background(20);
fill(255);
const s = OAVp5.sound.status; // the instrument being loaded
text('keys 1-4 pick an instrument', 20, 30);
text('playing: ' + OAVp5.sound.instrument, 20, 60);
text('loading: ' + s.id + ' (' + s.state + ', ' + nf(s.progress * 100, 1, 0) + '%)', 20, 90);
}Related
stopSound()Turns sound off and lets every ringing note go; the instrument stays loaded, sostartSound()brings it back at once.soundIsOnWhether notes are being heard right now: true oncestartSound()has finished, false before and after.OAVp5.sound.stateWhere sound is:'off','waiting'(for a click),'loading','on'or'error'.OAVp5.sound.instrumentThe id of the instrument you hear right now, ornullbefore sound has loaded.notePressed()You define this function; oav-p5 calls it each time a key (or a pad) goes down.startDrums()Starts a drum machine: a 16-step grid on the page that plays a preset, your own grid or one line of rhythm, and counts the bars for your sketch.