From 84bc43dbfc19497839bb0cee0bbace2f4eb8cb2f Mon Sep 17 00:00:00 2001 From: KevinGrajeda Date: Sat, 18 Mar 2023 16:51:41 -0600 Subject: [PATCH 1/2] add setType() to PolySynth --- src/polysynth.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/polysynth.js b/src/polysynth.js index 806ad8ed..18bf504c 100644 --- a/src/polysynth.js +++ b/src/polysynth.js @@ -210,6 +210,29 @@ class PolySynth { }); } + /** + * Set type to 'sine', 'triangle', 'sawtooth' or 'square'. + * If the current synthVoice is not MonoSynth, it + * replaces the current voice with MonoSynth. + * @method setType + * @for p5.PolySynth + * @param {String} type 'sine', 'triangle', 'sawtooth' or 'square'. + */ + setType(type) { + if (!(this.audiovoices[0] instanceof p5.MonoSynth)) { + console.warn( + '`PolySynth.setType()` replaces the synthVoice with `MonoSynth`' + ); + this.AudioVoice = p5.MonoSynth; + this.audiovoices = []; + this._allocateVoices(); + } + + this.audiovoices.forEach((voice) => { + voice.setType(type); + }); + } + /** * Trigger the Attack, and Decay portion of a MonoSynth. * Similar to holding down a key on a piano, but it will From 0130db75e40d745670f541d571d9ca6f0b1c4199 Mon Sep 17 00:00:00 2001 From: KevinGrajeda Date: Sat, 18 Mar 2023 17:01:47 -0600 Subject: [PATCH 2/2] add test for PolySynth.setType() --- test/tests/p5.PolySynth.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/tests/p5.PolySynth.js b/test/tests/p5.PolySynth.js index b763f4a2..f9f62ef8 100644 --- a/test/tests/p5.PolySynth.js +++ b/test/tests/p5.PolySynth.js @@ -57,6 +57,14 @@ describe('p5.PolySynth', function () { expect(monoSynth.env).to.have.property('control'); } }); + it('can change synthType', function () { + let polySynth = new p5.PolySynth(p5.MonoSynth, 6); + polySynth.setType('square'); + for (let i = 0; i < 6; i++) { + const monoSynth = polySynth.audiovoices[i]; + expect(monoSynth.getType()).to.equal('square'); + } + }); it('can trigger a note attack at the present moment with only one argument', function (done) { let polySynth = new p5.PolySynth(p5.MonoSynth, 3);