Skip to content

Commit b4f0144

Browse files
committed
fixed major error in _voicesInUse scheduling
1 parent e6daa64 commit b4f0144

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

examples/polyphonicSynth-Keyboard/sketch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function setup() {
2626
description = createP('p5.PolySynth is a handler class for monophonic extensions '+
2727
'of the p5.AudioVoice class. Use the computer keyboard to play notes on '+
2828
'the piano roll. Use UP_ARROW and DOWN_ARROW to change octave');
29+
2930
}
3031

3132
function draw() {

src/polysynth.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ define(function (require) {
1616
* @constructor
1717
*
1818
* @param {Number} [synthVoice] A monophonic synth voice inheriting
19-
* the AudioVoice class. Defaults ot p5.MonoSynth
19+
* the AudioVoice class. Defaults to p5.MonoSynth
2020
*
2121
* @param {Number} [polyValue] Number of voices, defaults to 8;
2222
*
@@ -158,10 +158,10 @@ define(function (require) {
158158
* @param {Number} [releaseTime] Time in seconds from now (defaults to 0)
159159
**/
160160
p5.PolySynth.prototype.setADSR = function(a,d,s,r) {
161-
this.audiovoices.forEach(function(voice){
161+
this.audiovoices.forEach(function(voice) {
162162
voice.setADSR(a,d,s,r);
163-
})
164-
}
163+
});
164+
};
165165

166166
/**
167167
* Trigger the Attack, and Decay portion of a MonoSynth.
@@ -205,16 +205,33 @@ define(function (require) {
205205

206206
this.notes[note] = currentVoice;
207207

208-
this._voicesInUse.setValueAtTime(this._voicesInUse.value + 1, t);
208+
//this._voicesInUse.setValueAtTime(this._voicesInUse.getValueAtTime(t) + 1, t);
209+
210+
var previousVal = this._voicesInUse._searchBefore(t) === null ? 0 : this._voicesInUse._searchBefore(t).value;
211+
this._voicesInUse.setValueAtTime(previousVal + 1, t);
212+
this._updateAfter(t, 1);
209213

210214
this._newest = currentVoice;
211215

212216
this.audiovoices[currentVoice].triggerAttack(note, velocity, tFromNow);
213217

214218
};
215219

220+
p5.PolySynth.prototype._updateAfter = function(time, value) {
221+
222+
if(this._voicesInUse._searchAfter(time) === null) {
223+
return;
224+
} else{
225+
this._voicesInUse._searchAfter(time).value += value;
226+
var nextTime = this._voicesInUse._searchAfter(time).time;
227+
this._updateAfter(nextTime, value);
228+
}
229+
}
230+
231+
232+
216233
/**
217-
* Trigger the Release of a MonoSynth note. This is similar to releasing
234+
* Trigger the Release of an AudioVoice note. This is similar to releasing
218235
* the key on a piano and letting the sound fade according to the
219236
* release level and release time.
220237
*
@@ -236,8 +253,14 @@ define(function (require) {
236253
var tFromNow = secondsFromNow || 0;
237254
var t = now + tFromNow;
238255

239-
this._voicesInUse.setValueAtTime(this._voicesInUse.value - 1, t);
240-
this.audiovoices[ this.notes[note] ].triggerRelease(secondsFromNow);
256+
// this._voicesInUse.setValueAtTime(this._voicesInUse.getValueAtTime(t)-1, t);
257+
// console.log('value at time: '+ t +' value '+this._voicesInUse.getValueAtTime(t));
258+
var previousVal = this._voicesInUse._searchBefore(t) === null ? 0 : this._voicesInUse._searchBefore(t).value;
259+
this._voicesInUse.setValueAtTime(previousVal - 1, t);
260+
this._updateAfter(t, -1);
261+
262+
263+
this.audiovoices[ this.notes[note] ].triggerRelease(tFromNow);
241264
this.notes[note] = undefined;
242265

243266
this._newest = this._newest === 0 ? 0 : (this._newest - 1) % (this.polyValue - 1);

0 commit comments

Comments
 (0)