Skip to content

Commit dd4420b

Browse files
therewasaguyjvntf
authored andcommitted
update monosynth chain and play/adsr scheduling
1 parent bf6e11c commit dd4420b

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/monosynth.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ define(function (require) {
2020
* <div><code>
2121
* var monosynth;
2222
* var x;
23-
*
23+
*
2424
* function setup() {
2525
* monosynth = new p5.MonoSynth();
2626
* monosynth.loadPreset('simpleBass');
@@ -54,7 +54,6 @@ define(function (require) {
5454

5555
// this.oscillator.connect(this.filter);
5656
this.env.setInput(this.oscillator);
57-
this.env.connect(this.filter);
5857
this.filter.connect(this.output);
5958

6059
this.oscillator.start();
@@ -67,6 +66,7 @@ define(function (require) {
6766
p5sound.soundArray.push(this);
6867
};
6968

69+
p5.MonoSynth.prototype = Object.create(p5.AudioVoice.prototype);
7070

7171
/**
7272
* Used internally by play() and triggerAttack()
@@ -75,8 +75,8 @@ define(function (require) {
7575
* Synth creators with more complex setups may have overridden
7676
* the oscillator chain with more than one oscillator,
7777
* and should override this method accordingly.
78-
*
79-
* @param {Number} note midi value of a note, where
78+
*
79+
* @param {Number} note midi value of a note, where
8080
* middle c is 60
8181
* @param {Number} [secondsFromNow] (optional) a time (in seconds
8282
* from now) at which
@@ -90,53 +90,53 @@ define(function (require) {
9090

9191
/**
9292
* Play tells the MonoSynth to start playing a note
93-
*
94-
* @method playNote
93+
*
94+
* @method play
9595
* @param {Number} [note] midi note to play (ranging from 0 to 127 - 60 being a middle C)
9696
* @param {Number} [velocity] velocity of the note to play (ranging from 0 to 1)
9797
* @param {Number} [secondsFromNow] time from now (in seconds) at which to play
9898
* @param {Number} [sustainTime] time to sustain before releasing the envelope
99-
*
100-
*/
99+
*
100+
*/
101101

102102
p5.MonoSynth.prototype.play = function (note, velocity, secondsFromNow, susTime) {
103103
// set range of env (TO DO: allow this to be scheduled in advance)
104-
var vel = velocity || 1;
105-
106-
this.triggerAttack(note,vel,secondsFromNow);
104+
var susTime = susTime || this.sustain;
105+
this.triggerAttack(note, veocity, secondsFromNow);
107106
this.triggerRelease(secondsFromNow + susTime);
108107
};
109108

110109
/**
111110
* Trigger the Attack, and Decay portion of the Envelope.
112111
* Similar to holding down a key on a piano, but it will
113-
* hold the sustain level until you let go.
112+
* hold the sustain level until you let go.
114113
*
115114
* @param {Number} secondsFromNow time from now (in seconds)
116115
* @param {Number} [velocity] velocity of the note to play (ranging from 0 to 1)
117116
* @param {Number} [secondsFromNow] time from now (in seconds) at which to play
118117
* @method triggerAttack
119-
*/
118+
*/
120119
p5.MonoSynth.prototype.triggerAttack = function (note, velocity, secondsFromNow) {
121-
//scheduling in relation to audioContext.currentTime will be handeled by oscillator.frew() and env.ramp()
122-
var tFromNow = secondsFromNow || 0;
123-
var n = p5.prototype.midiToFreq(note);
124-
this._isOn = true;
120+
var secondsFromNow = secondsFromNow || 0;
121+
var freq = p5.prototype.midiToFreq(note);
122+
var vel = velocity || 1;
125123

126-
this.oscillator.freq(n, 0, tFromNow);
127-
this.env.ramp(this.output, tFromNow, velocity);
124+
this._isOn = true;
125+
this.oscillator.freq(freq, 0, secondsFromNow);
126+
this.env.ramp(this.output, secondsFromNow, vel);
128127
};
129128

130129
/**
131130
* Trigger the Release of the Envelope. This is similar to releasing
132131
* the key on a piano and letting the sound fade according to the
133132
* release level and release time.
134-
*
133+
*
135134
* @param {Number} secondsFromNow time to trigger the release
136135
* @method triggerRelease
137-
*/
136+
*/
138137

139138
p5.MonoSynth.prototype.triggerRelease = function (secondsFromNow) {
139+
var secondsFromNow = secondsFromNow || 0;
140140
this.env.ramp(this.output, secondsFromNow, 0);
141141
this._isOn = false;
142142
};
@@ -149,11 +149,11 @@ define(function (require) {
149149
* For instance if you want to build a complex synthesizer
150150
* with one or more filters, effects etc. this is where you
151151
* will want to set their values.
152-
*
152+
*
153153
* @method setParams
154-
* @param
155-
*
156-
*/
154+
* @param
155+
*
156+
*/
157157

158158
p5.MonoSynth.prototype.setParams = function(params) {
159159
};
@@ -232,7 +232,7 @@ define(function (require) {
232232
* <a href="https://en.wikipedia.org/wiki/Synthesizer#/media/File:ADSR_parameter.svg">
233233
* ADSR envelope
234234
* </a>.
235-
*
235+
*
236236
* @method setADSR
237237
* @param {Number} attackTime Time (in seconds before envelope
238238
* reaches Attack Level
@@ -261,7 +261,7 @@ define(function (require) {
261261
* @param {Number} sustain
262262
* @param {Number} release
263263
*/
264-
Object.defineProperties(p5.MonoSynth, {
264+
Object.defineProperties(p5.MonoSynth.prototype, {
265265
'attack': {
266266
get : function() {
267267
return this.env.aTime;
@@ -339,7 +339,7 @@ define(function (require) {
339339

340340
/**
341341
* Get rid of the MonoSynth and free up its resources / memory.
342-
*
342+
*
343343
* @method dispose
344344
*/
345345
p5.MonoSynth.prototype.dispose = function() {

0 commit comments

Comments
 (0)