Skip to content

Commit 76974d1

Browse files
authored
Merge branch 'synths' into jvntf-synths
2 parents 812df5d + b4f0144 commit 76974d1

File tree

3 files changed

+32
-94
lines changed

3 files changed

+32
-94
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/monosynth.js

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -138,92 +138,6 @@ define(function (require) {
138138
this._isOn = false;
139139
};
140140

141-
142-
/**
143-
* Set cutoms parameters to a specific synth implementation.
144-
* This method does nothing by default unless you implement
145-
* something for it.
146-
* For instance if you want to build a complex synthesizer
147-
* with one or more filters, effects etc. this is where you
148-
* will want to set their values.
149-
*
150-
* @method setParams
151-
* @param
152-
*
153-
*/
154-
155-
p5.MonoSynth.prototype.setParams = function(params) {
156-
};
157-
158-
/**
159-
* loads preset values
160-
* @param {String} preset A preset that has been written for MonoSynth
161-
* @return {Object} Return the MonoSynth
162-
*/
163-
p5.MonoSynth.prototype.loadPreset = function(preset) {
164-
var options = this[preset];
165-
this.oscillator.setType(options.oscillator.type);
166-
167-
this.env.setADSR(options.env.attack, options.env.decay,
168-
options.env.sustain, options.env.release);
169-
170-
this.filter.setType(options.filter.type);
171-
this.filter.set(options.filter.freq, options.filter.res);
172-
return this;
173-
};
174-
175-
//PRESETS
176-
p5.MonoSynth.prototype.default = {
177-
'oscillator' : {
178-
'type' : 'sine'
179-
},
180-
'env' : {
181-
'attack' : 0.02,
182-
'decay' : 0.25,
183-
'sustain': 0.05,
184-
'release': 0.35
185-
},
186-
'filter': {
187-
'type' : 'highpass',
188-
'freq' : 5,
189-
'res' : 1
190-
}
191-
};
192-
193-
p5.MonoSynth.prototype.simpleBass = {
194-
'oscillator' : {
195-
'type' : 'square'
196-
},
197-
'env' : {
198-
'attack': 0,
199-
'decay': .60,
200-
'sustain': 0.1,
201-
'release': .28
202-
},
203-
'filter' : {
204-
'type' : 'lowpass',
205-
'freq' : 15000,
206-
'res' : 1
207-
}
208-
};
209-
210-
p5.MonoSynth.prototype.electricPiano = {
211-
'oscillator' : {
212-
'type' : 'sine'
213-
},
214-
'env' : {
215-
'attack': 0.029,
216-
'decay': .16,
217-
'sustain': 0.1,
218-
'release': .1
219-
},
220-
'filter': {
221-
'type' : 'lowpass',
222-
'freq' : 15000,
223-
'res' : 1
224-
}
225-
};
226-
227141
/**
228142
* Set values like a traditional
229143
* <a href="https://en.wikipedia.org/wiki/Synthesizer#/media/File:ADSR_parameter.svg">

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.getValueAtTime(t) + 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.getValueAtTime(t) - 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)