Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PolySynth.setType() #758

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/polysynth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/tests/p5.PolySynth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down