diff --git a/examples/002-Amplitude-VisualizingLoudness/sketch.js b/examples/002-Amplitude-VisualizingLoudness/sketch.js index 319caca0..26eee3d2 100644 --- a/examples/002-Amplitude-VisualizingLoudness/sketch.js +++ b/examples/002-Amplitude-VisualizingLoudness/sketch.js @@ -1,17 +1,17 @@ async function setup() { - sound = loadSound('https://tonejs.github.io/audio/berklee/gong_1.mp3'); + soundy = await loadSound('https://tonejs.github.io/audio/berklee/gong_1.mp3'); createCanvas(400, 400); textAlign(CENTER); fill(255); amp = new p5.Amplitude(); - sound.connect(amp); + soundy.connect(amp); describe('The color of the background changes based on the amplitude of the sound.'); } function mousePressed() { - sound.play(); + soundy.play(); } function draw() { diff --git a/examples/006-EnvelopeAndfilter/sketch.js b/examples/006-EnvelopeAndfilter/sketch.js index b918fe6c..b69f45f2 100644 --- a/examples/006-EnvelopeAndfilter/sketch.js +++ b/examples/006-EnvelopeAndfilter/sketch.js @@ -1,4 +1,4 @@ -let osc, env; +let noise, env; function setup() { let cnv = createCanvas(100, 100); diff --git a/examples/008_b-FFT-WaveForm-VisualizeSoundFile/sketch.js b/examples/008_b-FFT-WaveForm-VisualizeSoundFile/sketch.js index 38142f16..9474bdd9 100644 --- a/examples/008_b-FFT-WaveForm-VisualizeSoundFile/sketch.js +++ b/examples/008_b-FFT-WaveForm-VisualizeSoundFile/sketch.js @@ -6,7 +6,7 @@ async function setup(){ soundFile = await loadSound('assets/chime.mp3') cnv.mouseClicked(togglePlay); filter = new p5.Biquad(800, 'lowpass'); - filter.res(18); + filter.res(30); verb = new p5.Reverb(0.850); fft = new p5.FFT(32); soundFile.disconnect(); @@ -21,15 +21,19 @@ function draw(){ background(220); let spectrum = fft.analyze(); noStroke(); + + push(); fill(255, 0, 0); - for (let i = 0; i < spectrum.length; i++) { let x = map(i, 0, spectrum.length, 0, width); let h = -height + map(spectrum[i], 0, 0.1, height, -1000); rect(x, height, width / spectrum.length, h ) } + pop(); let waveform = fft.waveform(); + + push(); noFill(); beginShape(); stroke(20); @@ -39,10 +43,11 @@ function draw(){ vertex(x,y); } endShape(); - + pop(); + textAlign(CENTER); - text('tap to play', width/2, 20); - filter.freq(map(mouseX, 0, width, 200, 18000)) + text('click to start playback', width/2, 20); + filter.freq(map(mouseX, 0, width, 40, 14000)) describe('The sketch displays the frequency spectrum and waveform of the sound that plays.'); } diff --git a/examples/019-p5-to-tone/sketch.js b/examples/019-p5-to-tone/sketch.js index 29f68e7c..5ba2812b 100644 --- a/examples/019-p5-to-tone/sketch.js +++ b/examples/019-p5-to-tone/sketch.js @@ -1,23 +1,36 @@ -let synth, ctx - function setup() { createCanvas(400, 400); - //get the p5.sound.js Audio Context - ctx = getAudioContext() - //set the Tone.js Audio Context to match the p5.sound.js context - Tone.setContext(ctx) - //create a new MembraneSynth from the Tone.js library - synth = new Tone.MembraneSynth(); - //create a new p5.sound.js Reverb effect - rev = new p5.Reverb(3) - //connect the MembraneSynth to the Reverb - rev.setInput(synth) -} - -function draw() { background(220); + textAlign(CENTER); + textWrap(WORD); + textSize(13); + text('click and drag the mouse', width/2, 150); + + //initiallize the oscillator + beep = new p5.Oscillator(); + + describe('A grey sketch that demonstrates how to use the Oscillator class in p5.sound.'); } function mousePressed() { - synth.triggerAttackRelease("C2", "8n"); + beep.start(); +} + +function mouseReleased() { + beep.stop(); +} + +function draw() { + background(220); + let frequency = map(mouseX, 0, width, 440, 880); + let amp = map(mouseY, 0, height, 1, 0); + beep.freq(frequency); + beep.amp(amp); + if (beep.started) { + text('Frequency: ' + frequency.toFixed(0) + 'Hz', 0, height/2, width); + text('Amplitude: ' + amp.toFixed(2), 0, height/2 + 20, width); + } + else { + text('click and drag the mouse to change the frequency and amplitude values', 0, height/2, width); + } } \ No newline at end of file diff --git a/examples/020-p5-to-tone-panning/sketch.js b/examples/020-p5-to-tone-panning/sketch.js index 1c43cc68..cb574340 100644 --- a/examples/020-p5-to-tone-panning/sketch.js +++ b/examples/020-p5-to-tone-panning/sketch.js @@ -1,5 +1,5 @@ let sound_location = 0 - +let panner function setup() { createCanvas(400, 400) // get and set the "Audio Context" @@ -7,10 +7,10 @@ function setup() { Tone.setContext(ctx) synthy = new Tone.MonoSynth() - panny = new p5.Panner() + panner = new p5.Panner() // connect a Tone.js audio node to a p5 sound effect - panny.setInput(synthy) + //panner.setInput(synthy) } function draw() { @@ -20,7 +20,7 @@ function draw() { function mousePressed() { sound_location = random(-1,1) - panny.pan(sound_location) + panner.pan(sound_location) synthy.triggerAttackRelease("D#5", (1.5)) } diff --git a/examples/027-3D-Sound-Source/index.html b/examples/027-3D-Sound-Source/index.html new file mode 100644 index 00000000..271244e4 --- /dev/null +++ b/examples/027-3D-Sound-Source/index.html @@ -0,0 +1,16 @@ + + + + + + + + + + +
+
+ + + + \ No newline at end of file diff --git a/examples/027-3D-Sound-Source/sketch.js b/examples/027-3D-Sound-Source/sketch.js new file mode 100644 index 00000000..1a827c17 --- /dev/null +++ b/examples/027-3D-Sound-Source/sketch.js @@ -0,0 +1,41 @@ +let sample, spatializer, x, y, z; + +let randomTime = 0; + +async function setup() { + sample = await loadSound("../../sounds/drums.mp3"); + sample.loop(true); + createCanvas(100, 100, WEBGL); + + camera(0, 0, 200); + + angleMode(DEGREES); + describe("a sketch that plays processes an audio file with a reverb effect."); + + spatializer = new p5.Panner3D(); + spatializer.maxDist(100); + sample.disconnect(); + sample.connect(spatializer); + +} + +function mousePressed() { + if (!sample.isPlaying()) { + sample.play(); + } + else { + x = random(-10, 10); + y = random(-10, 10); + z = random(0, 10); + + spatializer.set(x, y, z); + } +} + +function draw() { + background(220); + push() + translate(x, y, z); + box(5, 5, 5); + pop() +} diff --git a/examples/027-3D-Sound-Source/style.css b/examples/027-3D-Sound-Source/style.css new file mode 100644 index 00000000..9386f1c2 --- /dev/null +++ b/examples/027-3D-Sound-Source/style.css @@ -0,0 +1,7 @@ +html, body { + margin: 0; + padding: 0; +} +canvas { + display: block; +} diff --git a/examples/028-SoundFileRate/assets/chime.mp3 b/examples/028-SoundFileRate/assets/chime.mp3 new file mode 100644 index 00000000..74a26ba5 Binary files /dev/null and b/examples/028-SoundFileRate/assets/chime.mp3 differ diff --git a/examples/028-SoundFileRate/assets/drums.mp3 b/examples/028-SoundFileRate/assets/drums.mp3 new file mode 100644 index 00000000..54dc3b15 Binary files /dev/null and b/examples/028-SoundFileRate/assets/drums.mp3 differ diff --git a/examples/028-SoundFileRate/index.html b/examples/028-SoundFileRate/index.html new file mode 100644 index 00000000..271244e4 --- /dev/null +++ b/examples/028-SoundFileRate/index.html @@ -0,0 +1,16 @@ + + + + + + + + + + +
+
+ + + + \ No newline at end of file diff --git a/examples/028-SoundFileRate/sketch.js b/examples/028-SoundFileRate/sketch.js new file mode 100644 index 00000000..b5680445 --- /dev/null +++ b/examples/028-SoundFileRate/sketch.js @@ -0,0 +1,30 @@ +async function setup() { + sample = await loadSound("assets/drums.mp3"); + sample.loop(true); + createCanvas(100, 100); + textAlign(CENTER); + textWrap(WORD); + textSize(10); + describe("a sketch that changes the playback rate of a soundfile"); +} + +function draw() { + background(220); + rate = map(mouseX, 0, width, 0.5, 2); + sample.rate(rate); + if (!sample.isPlaying()) { + text("click to play the sound, move your mouse to change the playback rate", 0, 20, width); + } + else { + text("Playback Rate: " + rate.toFixed(2), 0, 20, width); + } +} + +function mousePressed() { + if(!sample.isPlaying()) { + sample.play(); + } + else { + sample.stop(); + } +} diff --git a/examples/028-SoundFileRate/style.css b/examples/028-SoundFileRate/style.css new file mode 100644 index 00000000..9386f1c2 --- /dev/null +++ b/examples/028-SoundFileRate/style.css @@ -0,0 +1,7 @@ +html, body { + margin: 0; + padding: 0; +} +canvas { + display: block; +} diff --git a/examples/029-AmplitudeAnalysis/assets/chime.mp3 b/examples/029-AmplitudeAnalysis/assets/chime.mp3 new file mode 100644 index 00000000..74a26ba5 Binary files /dev/null and b/examples/029-AmplitudeAnalysis/assets/chime.mp3 differ diff --git a/examples/029-AmplitudeAnalysis/assets/drums.mp3 b/examples/029-AmplitudeAnalysis/assets/drums.mp3 new file mode 100644 index 00000000..54dc3b15 Binary files /dev/null and b/examples/029-AmplitudeAnalysis/assets/drums.mp3 differ diff --git a/examples/029-AmplitudeAnalysis/index.html b/examples/029-AmplitudeAnalysis/index.html new file mode 100644 index 00000000..271244e4 --- /dev/null +++ b/examples/029-AmplitudeAnalysis/index.html @@ -0,0 +1,16 @@ + + + + + + + + + + +
+
+ + + + \ No newline at end of file diff --git a/examples/029-AmplitudeAnalysis/sketch.js b/examples/029-AmplitudeAnalysis/sketch.js new file mode 100644 index 00000000..13fa3322 --- /dev/null +++ b/examples/029-AmplitudeAnalysis/sketch.js @@ -0,0 +1,41 @@ +let amplitude = 0; + +async function setup() { + sample = await loadSound("assets/drums.mp3"); + sample.loop(true); + + createCanvas(100, 100); + textAlign(CENTER); + textWrap(WORD); + textSize(8); + describe("a sketch that visualizes the loudness of a soundfile with smoothing control"); + + analyze = new p5.Amplitude(); + sample.connect(analyze); +} + +function draw() { + background(amplitude * 512, 0, 0); + + amplitude = analyze.getLevel(); + + fill(255); + if (!sample.isPlaying()) { + text("click to play the sound, move your mouse to adjust smoothing", 0, 20, width); + } + else { + text("Amplitude: " + amplitude.toFixed(2), 0, 20, width); + } + smoothing = map(mouseX, 0, width, 0, 1); + analyze.smooth(smoothing); + text("Smoothing: " + smoothing.toFixed(2), 0, 60, width); +} + +function mousePressed() { + if(!sample.isPlaying()) { + sample.play(); + } + else { + sample.stop(); + } +} diff --git a/examples/029-AmplitudeAnalysis/style.css b/examples/029-AmplitudeAnalysis/style.css new file mode 100644 index 00000000..9386f1c2 --- /dev/null +++ b/examples/029-AmplitudeAnalysis/style.css @@ -0,0 +1,7 @@ +html, body { + margin: 0; + padding: 0; +} +canvas { + display: block; +} diff --git a/examples/030-AmplitudeSetInput/assets/chime.mp3 b/examples/030-AmplitudeSetInput/assets/chime.mp3 new file mode 100644 index 00000000..74a26ba5 Binary files /dev/null and b/examples/030-AmplitudeSetInput/assets/chime.mp3 differ diff --git a/examples/030-AmplitudeSetInput/assets/drums.mp3 b/examples/030-AmplitudeSetInput/assets/drums.mp3 new file mode 100644 index 00000000..54dc3b15 Binary files /dev/null and b/examples/030-AmplitudeSetInput/assets/drums.mp3 differ diff --git a/examples/030-AmplitudeSetInput/index.html b/examples/030-AmplitudeSetInput/index.html new file mode 100644 index 00000000..271244e4 --- /dev/null +++ b/examples/030-AmplitudeSetInput/index.html @@ -0,0 +1,16 @@ + + + + + + + + + + +
+
+ + + + \ No newline at end of file diff --git a/examples/030-AmplitudeSetInput/sketch.js b/examples/030-AmplitudeSetInput/sketch.js new file mode 100644 index 00000000..d1562dc6 --- /dev/null +++ b/examples/030-AmplitudeSetInput/sketch.js @@ -0,0 +1,39 @@ +let amplitude = 0; + +async function setup() { + sample = await loadSound("assets/drums.mp3"); + sample.loop(true); + + createCanvas(100, 100); + textAlign(CENTER); + textWrap(WORD); + textSize(8); + describe("a sketch that demonstrates setting the input for amplitude analysis"); + + analyze = new p5.Amplitude(); + //sample.connect(analyze); + analyze.setInput(sample); +} + +function draw() { + background(amplitude * 512, 0, 0); + + amplitude = analyze.getLevel(); + + fill(255); + if (!sample.isPlaying()) { + text("click to play the sound, move your mouse to adjust smoothing", 0, 20, width); + } + else { + text("Amplitude: " + amplitude.toFixed(2), 0, 20, width); + } +} + +function mousePressed() { + if(!sample.isPlaying()) { + sample.play(); + } + else { + sample.stop(); + } +} diff --git a/examples/030-AmplitudeSetInput/style.css b/examples/030-AmplitudeSetInput/style.css new file mode 100644 index 00000000..9386f1c2 --- /dev/null +++ b/examples/030-AmplitudeSetInput/style.css @@ -0,0 +1,7 @@ +html, body { + margin: 0; + padding: 0; +} +canvas { + display: block; +} diff --git a/src/app.js b/src/app.js index f8145ca6..8ab39904 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,4 @@ +import "./core/audioParamPolyfill.js"; import { getAudioContext, setAudioContext, userStartAudio, userStopAudio } from './core/Utils'; p5.prototype.getAudioContext = getAudioContext; p5.prototype.setAudioContext = setAudioContext; diff --git a/src/core/Utils.js b/src/core/Utils.js index 9768b4c6..167c050e 100644 --- a/src/core/Utils.js +++ b/src/core/Utils.js @@ -4,6 +4,7 @@ * @for p5.sound */ +import { polyfillAudioListener } from "./audioListenerPolyfill.js"; import { getContext as ToneGetContext, setContext as ToneSetContext } from "tone/build/esm/core/Global.js"; import { start as ToneStart } from "tone/build/esm/core/Global.js"; @@ -53,8 +54,9 @@ function clamp(value, min, max) { */ function getAudioContext() { if (!audioContext) { - audioContext = new window.AudioContext(); - ToneSetContext(audioContext); + audioContext = ToneGetContext().rawContext; + //polyfillAudioListener(audioContext); + //ToneSetContext(audioContext); } return audioContext; } @@ -94,6 +96,7 @@ function getAudioContext() { */ function setAudioContext(context) { audioContext = context; + polyfillAudioListener(audioContext); ToneSetContext(context); } diff --git a/src/core/audioListenerPolyfill.js b/src/core/audioListenerPolyfill.js new file mode 100644 index 00000000..bc9d9e0d --- /dev/null +++ b/src/core/audioListenerPolyfill.js @@ -0,0 +1,52 @@ +/** + * Firefox does not implement the AudioParam-based AudioListener + * properties (positionX/Y/Z, forwardX/Y/Z, upX/Y/Z) — only the + * deprecated setPosition()/setOrientation() methods. + * + * We fake each property with a real AudioParam borrowed from a + * ConstantSourceNode, then drive the deprecated setters from their + * values once per animation frame. + * + * Limitation: listener movement becomes k-rate (updated per frame, + * ~60Hz), not a-rate. Fine for perceptual listener movement; not + * sample-accurate automation. Per-source Panner3D positioning is + * unaffected (Firefox supports PannerNode params natively). + */ +const LISTENER_PARAMS = [ + ["positionX", 0], ["positionY", 0], ["positionZ", 0], + ["forwardX", 0], ["forwardY", 0], ["forwardZ", -1], + ["upX", 0], ["upY", 1], ["upZ", 0], +]; + +export function polyfillAudioListener(ctx) { + const listener = ctx.listener; + if (listener.positionX) return; // Chrome/Edge/Safari: native, no-op + + // Muted sink keeps the donor nodes in the processing graph — + // params on unpulled nodes may not advance their automation. + const sink = ctx.createGain(); + sink.gain.value = 0; + sink.connect(ctx.destination); + + const params = {}; + for (const [name, defaultValue] of LISTENER_PARAMS) { + const source = ctx.createConstantSource(); + source.offset.value = defaultValue; + source.connect(sink); + source.start(); + params[name] = source.offset; + Object.defineProperty(listener, name, { value: source.offset }); + } + + let last = []; + (function sync() { + if (ctx.state === "closed") return; + const v = LISTENER_PARAMS.map(([n]) => params[n].value); + if (v.some((x, i) => x !== last[i])) { + listener.setPosition(v[0], v[1], v[2]); + listener.setOrientation(v[3], v[4], v[5], v[6], v[7], v[8]); + last = v; + } + requestAnimationFrame(sync); + })(); +} \ No newline at end of file diff --git a/src/core/audioParamPolyfill.js b/src/core/audioParamPolyfill.js new file mode 100644 index 00000000..f1f3a2c3 --- /dev/null +++ b/src/core/audioParamPolyfill.js @@ -0,0 +1,31 @@ +/** + * Polyfill for AudioParam.cancelAndHoldAtTime(). + * + * Firefox does not implement it (Bugzilla #1308431, open since 2016). + * Chrome, Edge, and Safari have native implementations, so this is a + * no-op there — the typeof guard means we never clobber a native method. + * + * Approximation note: this holds the param at its value as of *call* + * time, not at cancelTime. That is exact for Tone.js's actual usage + * (Envelope.triggerRelease and Param.rampTo pass "now") but wrong for + * a cancel scheduled in the future during an active ramp. Documented + * limitation — do not "fix" by removing, fix by computing the ramp + * value at cancelTime if that case ever matters. + * + * Patching AudioParam.prototype covers params from AudioContext and + * OfflineAudioContext alike — they share the prototype. + * + * This module is side-effect-only. It must execute before any + * AudioNode is constructed. + */ +if ( + typeof AudioParam !== "undefined" && + typeof AudioParam.prototype.cancelAndHoldAtTime !== "function" +) { + AudioParam.prototype.cancelAndHoldAtTime = function (cancelTime) { + const heldValue = this.value; + this.cancelScheduledValues(cancelTime); + this.setValueAtTime(heldValue, cancelTime); + return this; // spec: returns the AudioParam, allows chaining + }; +} \ No newline at end of file diff --git a/src/effects/Delay.js b/src/effects/Delay.js index f0a485d7..94c45c09 100644 --- a/src/effects/Delay.js +++ b/src/effects/Delay.js @@ -128,6 +128,54 @@ class Delay extends p5soundMixEffect { * @method feedback * @for Delay * @param {number} feedbackAmount A number between 0 and 0.99. + * @example + *
+ * + * let osc, delay, feedback = 0.5; + * + * async function setup() { + * sound = await loadSound('assets/chime.mp3') + * sound.loop(true); + * + * let cnv = createCanvas(100, 100); + * background(220); + * textAlign(CENTER); + * textWrap(WORD); + * textSize(9); + * text('click to start audio', width/2, height/2); + * + * delay = new p5.Delay(0.250, feedback); + * + * sound.disconnect(); + * sound.connect(delay); + * + * describe('a sketch that demonstrates the feedback parameter.'); + * } + * + * function mousePressed() { + * if (!sound.isPlaying()) { + * sound.play(); + * } + * else { + * sound.stop(); + * } + * } + * + * function draw() { + * if(!sound.isPlaying()) { + * background(220); + * text('click to start audio', 0, height/2 - 20, width); + * } + * else { + * background(0, 255, 255); + * text('move mouse to change feedback', 0, height/2 - 20, width); + * } + * feedback = map(mouseX, 0, width, 0.1, 0.99); + * delay.feedback(feedback); + * text('feedback: ' + feedback.toFixed(2), width/2, height/2 + 20); + * } + * + *
*/ feedback(value) { this.node.feedback.rampTo(clamp(value, 0, 0.99), 0.1); diff --git a/src/effects/Reverb.js b/src/effects/Reverb.js index f5c6b745..3fb5e97c 100644 --- a/src/effects/Reverb.js +++ b/src/effects/Reverb.js @@ -10,7 +10,7 @@ import { p5soundMixEffect } from "../core/p5soundMixEffect.js"; /** * Add reverb to a sound. * - * Reverb is an effect that is used commonly in electronic sound production. It makes input sound like it is in an actual space. Large spaces have longer decay times + * Reverb is an effect that is used commonly in electronic sound production. It makes input sound like it is in an actual space. Large spaces have longer decay times, while smaller spaces produce less acoustic reflections and therefore have less distinct reverb characteristics. * @class Reverb * @constructor * @extends p5soundMixEffect diff --git a/src/sources/SoundFile.js b/src/sources/SoundFile.js index 1d09427a..28a56b73 100644 --- a/src/sources/SoundFile.js +++ b/src/sources/SoundFile.js @@ -10,13 +10,8 @@ import { p5soundSource } from "../core/p5soundSource"; /** * loadSound() returns a new SoundFile from a specified - * path. If called during preload(), the SoundFile will be ready - * to play in time for setup() and draw(). If called outside of - * preload, the SoundFile will not be ready immediately, so - * loadSound accepts a callback as the second parameter. Using a - * - * local server is recommended when loading external files. - * + * path. Call loadSound() from an async function with await + * to ensure the sound is loaded before use. * @method loadSound * @for p5.sound * @param {String|Array} path Path to the sound file, or an array with @@ -26,25 +21,24 @@ import { p5soundSource } from "../core/p5soundSource"; * from the HTML5 File API, or a p5.File. * @return {SoundFile} Returns a SoundFile * @example - *
+ *
+ * * let mySound; - * function preload() { - * mySound = loadSound('/assets/doorbell.mp3'); - * } * - * function setup() { - * let cnv = createCanvas(100, 100); - * cnv.mousePressed(canvasPressed); - * background(220); - * text('tap here to play', 10, 20); - * } + * async function setup() { + * describe("a sketch that loads and plays a sound file"); + * mySound = await loadSound('/assets/doorbell.mp3'); + * createCanvas(100, 100); + * background(220); + * text('tap here to play', 10, 20); + * } * - * function canvasPressed() { - * // playing a sound file on a user gesture - * // is equivalent to `userStartAudio()` - * mySound.play(); - * } - *
+ * function mousePressed() { + * // playing a sound file on a user gesture is equivalent to `userStartAudio()` + * mySound.play(); + * } + *
+ *
*/ function loadSound (path) { if (typeof path === 'string') path = encodeURI(path); @@ -80,16 +74,11 @@ function loadSound (path) { * * let sound, amp, delay, cnv; * - * function preload() { - * //replace this sound with something local with rights to distribute - * //need to fix local asset loading first though :) - * sound = loadSound('/assets/doorbell.mp3'); - * } - * - * function setup() { - * cnv = createCanvas(100, 100); + * async function setup() { + * describe("a sketch that plays a soundfile through a delay effect"); + * createCanvas(100, 100); + * sound = await loadSound('/assets/doorbell.mp3'); * textAlign(CENTER); - * cnv.mousePressed(playSound); * amp = new p5.Amplitude(); * delay = new p5.Delay(); * sound.disconnect(); @@ -97,7 +86,7 @@ function loadSound (path) { * delay.connect(amp); * } * - * function playSound() { + * function mousePressed() { * sound.play(); * } * @@ -198,6 +187,9 @@ class SoundFile extends p5soundSource { * @method stop * @for SoundFile * @example + *
+ * + * let sample; * async function setup() { * sample = await loadSound("assets/chime.mp3"); * createCanvas(100, 100); @@ -385,16 +377,12 @@ class SoundFile extends p5soundSource { *
* * let soundSource, cnv, btn; - * - * function preload() { - * soundSource = loadSound('/assets/Damscray_-_Dancing_Tiger_01.mp3'); - * } * - * function setup() { + * async function setup() { * describe( * 'a sketch that says click to play sound. there is a button that says load sound. when you click the button, the path of the sound file player changes and the new sound plays.'); - * cnv = createCanvas(100, 100); - * cnv.mousePressed(playSound); + * createCanvas(100, 100); + * soundSource = await loadSound('/assets/Damscray_-_Dancing_Tiger_01.mp3'); * background(220); * textAlign(CENTER); * textWrap(WORD); @@ -405,7 +393,7 @@ class SoundFile extends p5soundSource { * soundSource.loop(); * } * - * function playSound() { + * function mousePressed() { * soundSource.play(); * } * @@ -661,18 +649,14 @@ class SoundFile extends p5soundSource { *
* * let player; - * - * function preload() { - * player = loadSound('/assets/lucky_dragons_-_power_melody.mp3'); - * } * * function setup() { * let cnv = createCanvas(100, 100); * background(220); + * player = await loadSound('/assets/lucky_dragons_-_power_melody.mp3'); * textAlign(CENTER); * textSize(10); * text('click to play', width/2, height/2); - * cnv.mousePressed(playSound); * player.onended(coolFunction); * } * @@ -681,7 +665,7 @@ class SoundFile extends p5soundSource { * text('sound is done', width/2, height/2); * } * - * function playSound() { + * function mousePressed() { * background(0, 255, 255); * text('sound is playing', width/2, height/2); * if (!player.isPlaying()) { @@ -704,13 +688,10 @@ class SoundFile extends p5soundSource { *
* * let player; - * - * function preload() { - * player = loadSound('/assets/lucky_dragons_-_power_melody.mp3'); - * } * - * function setup() { + * async function setup() { * describe('A sketch that calculates and displays the length of a sound file using number of samples and sample rate.'); + * player = await loadSound('/assets/lucky_dragons_-_power_melody.mp3'); * createCanvas(100, 100); * background(220); * textAlign(CENTER);