Skip to content

Commit a9b7658

Browse files
committed
fixed error caused by many layers of scheduling
1 parent 14edf09 commit a9b7658

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/monosynth.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ define(function (require) {
3737
AudioVoice.call(this);
3838

3939
this.oscillator = new p5.Oscillator();
40-
this.oscillator.disconnect();
40+
// this.oscillator.disconnect();
4141

4242
this.env = new p5.Env();
4343
this.env.setRange(1, 0);
@@ -52,12 +52,13 @@ define(function (require) {
5252

5353
// oscillator --> env --> filter --> this.output (gain) --> p5.soundOut
5454

55-
this.oscillator.connect(this.filter);
55+
// this.oscillator.connect(this.filter);
5656
this.env.setInput(this.oscillator);
5757
this.env.connect(this.filter);
5858
this.filter.connect(this.output);
5959

6060
this.oscillator.start();
61+
this.connect();
6162

6263
//Audiovoices are connected to soundout by default
6364

@@ -102,7 +103,7 @@ define(function (require) {
102103
// set range of env (TO DO: allow this to be scheduled in advance)
103104
var vel = velocity || 1;
104105

105-
this.triggerAttack(note,velocity,secondsFromNow);
106+
this.triggerAttack(note,vel,secondsFromNow);
106107
this.triggerRelease(secondsFromNow + susTime);
107108
};
108109

@@ -117,15 +118,13 @@ define(function (require) {
117118
* @method triggerAttack
118119
*/
119120
p5.MonoSynth.prototype.triggerAttack = function (note, velocity, secondsFromNow) {
120-
121-
var now = p5sound.audiocontext.currentTime;
121+
//scheduling in relation to audioContext.currentTime will be handeled by oscillator.frew() and env.ramp()
122122
var tFromNow = secondsFromNow || 0;
123-
var t = now + tFromNow;
124123
var n = p5.prototype.midiToFreq(note);
125-
126124
this._isOn = true;
127-
this.oscillator.freq(n, 0, t);
128-
this.env.ramp(this.output, t, velocity);
125+
126+
this.oscillator.freq(n, 0, tFromNow);
127+
this.env.ramp(this.output, tFromNow, velocity);
129128
};
130129

131130
/**
@@ -169,7 +168,7 @@ define(function (require) {
169168
this.oscillator.setType(options.oscillator.type);
170169

171170
this.env.setADSR(options.env.attack, options.env.decay,
172-
options.env.sustain, options.env.release);
171+
options.env.sustain, options.env.release);
173172

174173
this.filter.setType(options.filter.type);
175174
this.filter.set(options.filter.freq, options.filter.res);

src/polysynth.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ define(function (require) {
7575
this._voicesInUse = new TimelineSignal(0);
7676

7777
this.output = p5sound.audiocontext.createGain();
78+
this.connect();
7879

7980
//Construct the appropriate number of audiovoices
8081
this._allocateVoices();
@@ -175,7 +176,11 @@ define(function (require) {
175176
*/
176177
p5.PolySynth.prototype.noteAttack = function (_note, _velocity, secondsFromNow) {
177178
var now = p5sound.audiocontext.currentTime;
179+
180+
//this value goes to the audiovoices which handle their own scheduling
178181
var tFromNow = secondsFromNow || 0;
182+
183+
//this value is used by this._voicesInUse
179184
var t = now + tFromNow;
180185

181186
var note = _note === undefined ? 60 : _note;
@@ -204,7 +209,7 @@ define(function (require) {
204209

205210
this._newest = currentVoice;
206211

207-
this.audiovoices[currentVoice].triggerAttack(note, velocity, secondsFromNow);
212+
this.audiovoices[currentVoice].triggerAttack(note, velocity, tFromNow);
208213

209214
};
210215

@@ -231,7 +236,6 @@ define(function (require) {
231236
var tFromNow = secondsFromNow || 0;
232237
var t = now + tFromNow;
233238

234-
235239
this._voicesInUse.setValueAtTime(this._voicesInUse.value - 1, t);
236240
this.audiovoices[ this.notes[note] ].triggerRelease(secondsFromNow);
237241
this.notes[note] = undefined;

0 commit comments

Comments
 (0)