diff --git a/lib/addons/p5.sound.js b/lib/addons/p5.sound.js index a8a7e3f450..25681ab89d 100644 --- a/lib/addons/p5.sound.js +++ b/lib/addons/p5.sound.js @@ -1,4 +1,4 @@ -/** [p5.sound] Version: 1.0.1 - 2021-05-25 */ +/** [p5.sound] Version: 1.0.1 - 2022-11-22 */ /** *
p5.sound extends p5 with Web Audio functionality including audio input, @@ -218,7 +218,7 @@ function getAudioContext() { * example below. This method utilizes * StartAudioContext * , a library by Yotam Mann (MIT Licence, 2016).
- * @param {Element|Array} [elements] This argument can be an Element, + * @param {Element|Array} [element(s)] This argument can be an Element, * Selector String, NodeList, p5.Element, * jQuery Element, or an Array of any of those. * @param {Function} [callback] Callback to invoke when the AudioContext @@ -895,8 +895,8 @@ function noteToFreq(note) { } var wholeNotes = { - A: 21, - B: 23, + A: 33, + B: 35, C: 24, D: 26, E: 28, @@ -968,7 +968,7 @@ function soundFormats() { } function disposeSound() { - for (var i = 0; i < main.soundArray.length; i++) { + for (var i = main.soundArray.length - 1; i >= 0; i--) { main.soundArray[i].dispose(); } } @@ -1828,10 +1828,10 @@ function () { }, { key: "playMode", value: function playMode(str) { - var s = str.toLowerCase(); + var s = str.toLowerCase().trim(); if (s === 'restart' && this.buffer && this.bufferSourceNode) { - for (var i = 0; i < this.bufferSourceNodes.length - 1; i++) { + for (var i = 0; i < this.bufferSourceNodes.length; i++) { var now = main.audiocontext.currentTime; this.bufferSourceNodes[i].stop(now); } @@ -2043,11 +2043,10 @@ function () { this._paused = false; } else if (this.buffer && this.bufferSourceNode) { var now = main.audiocontext.currentTime; - var t = time || 0; this.pauseTime = 0; - this.bufferSourceNode.stop(now + t); + this.bufferSourceNode.stop(now + time); - this._counterNode.stop(now + t); + this._counterNode.stop(now + time); this._playing = false; this._paused = false; @@ -2349,7 +2348,7 @@ function () { }, { key: "channels", value: function channels() { - return this.buffer.numberOfChannels; + if (this.buffer) return this.buffer.numberOfChannels; } /** * Return the sample rate of the sound file. @@ -2362,7 +2361,7 @@ function () { }, { key: "sampleRate", value: function sampleRate() { - return this.buffer.sampleRate; + if (this.buffer) return this.buffer.sampleRate; } /** * Return the number of samples in a sound file. @@ -2376,7 +2375,7 @@ function () { }, { key: "frames", value: function frames() { - return this.buffer.length; + if (this.buffer) return this.buffer.length; } /** * Returns an array of amplitude peaks in a p5.SoundFile that can be @@ -2400,7 +2399,7 @@ function () { value: function getPeaks(length) { if (this.buffer) { if (!length) { - length = window.width * 5; + length = window.innerWidth * 5; } if (this.buffer) { @@ -3195,10 +3194,48 @@ function () { } else { this.output.connect(unit); } - } else { - this.output.connect(this.panner.connect(main.input)); - } + } + } + /** + * Disconnects the output of this p5.Amplitude object. + * + * @method disconnect + * @for p5.Amplitude + * @example + *
+ * let sound, amplitude;
+ * function preload(){
+ * sound = loadSound('assets/beat.mp3');
+ * }
+ *
+ * function setup() {
+ * let cnv = createCanvas(100, 100);
+ * cnv.mouseClicked(togglePlay);
+ * amplitude = new p5.Amplitude();
+ * sound.loop();
+ * }
+ *
+ * function draw() {
+ * background(220);
+ * text('Disconnect:', 20, 20);
+ * let level = amplitude.getLevel();
+ * let size = map(level, 0, 1, 0, 200);
+ * ellipse(width/2, height/2, size, size);
+ * }
+ *
+ * function togglePlay() {
+ * if (sound.isPlaying()){
+ * sound.pause();
+ * amplitude.disconnect();
+ * }
+ * else{
+ * sound.play();
+ * }
+ * }
+ *
* let mic, recorder, soundFile;
- * let state = 0;
+ * // keeps record if recording is started
+ * let isRecordingStarted = false;
+ * // keeps record if the recorded result is played
+ * let isResultPlayed = false;
*
* function setup() {
* let cnv = createCanvas(100, 100);
@@ -10581,9 +10606,6 @@ var soundRecorder_ac = main.audiocontext;
* // create an audio in
* mic = new p5.AudioIn();
*
- * // prompts user to enable their browser mic
- * mic.start();
- *
* // create a sound recorder
* recorder = new p5.SoundRecorder();
*
@@ -10601,31 +10623,34 @@ var soundRecorder_ac = main.audiocontext;
* // ensure audio is enabled
* userStartAudio();
*
- * // make sure user enabled the mic
- * if (state === 0 && mic.enabled) {
- *
- * // record to our p5.SoundFile
- * recorder.record(soundFile);
- *
- * background(255,0,0);
- * text('Recording!', width/2, height/2);
- * state++;
+ * if (!isRecordingStarted && !isResultPlayed) {
+ * // make sure user enabled the mic by prompting to enable their browser mic
+ * // start recording after the mic is enabled
+ * mic.start(function() {
+ * // record to our p5.SoundFile
+ * recorder.record(soundFile);
+ *
+ * background(255,0,0);
+ * text('Recording!', width/2, height/2);
+ * isRecordingStarted = true;
+ * });
* }
- * else if (state === 1) {
+ * else if (isRecordingStarted && !isResultPlayed) {
* background(0,255,0);
*
* // stop recorder and
* // send result to soundFile
* recorder.stop();
+ * // stop browser from accessing the mic
+ * mic.dispose();
*
* text('Done! Tap to play and download', width/2, height/2, width - 20);
- * state++;
+ * isResultPlayed = true;
* }
*
- * else if (state === 2) {
+ * else if (isRecordingStarted && isResultPlayed) {
* soundFile.play(); // play the result!
* save(soundFile, 'mySound.wav');
- * state++;
* }
* }
*