@@ -29,19 +29,19 @@ define(function (require) {
29
29
this . audiovoices = [ ] ;
30
30
this . _voicesInUse = [ ] ;
31
31
32
+ this . notes = { } ;
33
+
32
34
this . _newest = 0 ;
33
35
this . _oldest = 0 ;
34
36
35
37
36
38
this . polyValue = polyValue || 8 ;
37
39
38
- this . AudioVoice = audioVoice ;
40
+ this . AudioVoice = audioVoice === undefined ? p5 . MonoSynth : audioVoice ;
39
41
40
42
this . _voicesInUse = new TimelineSignal ( 0 ) ;
41
43
42
-
43
44
this . allocateVoices ( ) ;
44
-
45
45
p5sound . soundArray . push ( this ) ;
46
46
} ;
47
47
@@ -69,23 +69,30 @@ p5.PolySynth.prototype.play = function (note,velocity, secondsFromNow, susTime){
69
69
// this.voices[note].play(note,velocity, secondsFromNow, susTime);
70
70
71
71
72
- if ( this . _voicesInUse . value < this . polyValue ) {
72
+ // if(this._voicesInUse.value < this.polyValue) {
73
73
74
- var currentVoice = this . _voicesInUse . value ;
75
- this . noteAttack ( currentVoice , note , velocity , secondsFromNow ) ;
76
- this . noteRelease ( currentVoice , secondsFromNow + susTime ) ;
77
- } else {
78
- if ( this . audiovoices [ this . _oldest ] . _isOn ) {
79
- this . noteRelease ( this . _oldest ) ;
80
- }
81
- this . noteAttack ( this . _oldest , note , velocity , secondsFromNow ) ;
82
- this . noteRelease ( this . _oldest , secondsFromNow + susTime ) ;
74
+ // var currentVoice = this._voicesInUse.value;
83
75
84
- this . _newest = this . _oldest ;
85
- this . _oldest = ( this . _oldest + 1 ) % ( this . polyValue - 1 ) ;
86
- }
76
+ // this.noteAttack(currentVoice, note, velocity, secondsFromNow);
77
+ // this.noteRelease(currentVoice, secondsFromNow + susTime);
78
+
79
+
80
+
81
+ // } else {
82
+ // if(this.audiovoices[this._oldest]._isOn) {
83
+ // this.noteRelease(this._oldest);
84
+ // }
87
85
88
86
87
+ // this.noteAttack(this._oldest, note, velocity, secondsFromNow);
88
+ // this.noteRelease(this._oldest, secondsFromNow + susTime);
89
+
90
+ // this._newest = this._oldest;
91
+ // this._oldest = ( this._oldest + 1 ) % (this.polyValue - 1);
92
+ // }
93
+
94
+ this . noteAttack ( note , velocity , secondsFromNow ) ;
95
+ this . noteRelease ( note , secondsFromNow + susTime ) ;
89
96
90
97
// this.noteAttack(this._newest, note, velocity,secondsFromNow);
91
98
// this.noteRelease(this._newest, secondsFromNow+susTime);
@@ -120,11 +127,11 @@ p5.PolySynth.prototype.play = function (note,velocity, secondsFromNow, susTime){
120
127
**/
121
128
122
129
p5 . PolySynth . prototype . noteADSR = function ( note , a , d , s , r ) {
123
- if ( this . voices [ note ] == null ) {
124
- this . voices [ note ] = new this . AudioVoice ( ) ;
125
- }
126
- this . voices [ note ] . _setNote ( note ) ;
127
- this . voices [ note ] . setADSR ( a , d , s , r ) ;
130
+ if ( this . voices [ note ] == null ) {
131
+ this . voices [ note ] = new this . AudioVoice ( ) ;
132
+ }
133
+ this . voices [ note ] . _setNote ( note ) ;
134
+ this . voices [ note ] . setADSR ( a , d , s , r ) ;
128
135
}
129
136
130
137
/**
@@ -138,23 +145,49 @@ p5.PolySynth.prototype.noteADSR = function (note,a,d,s,r){
138
145
* @param {Number } [secondsFromNow] time from now (in seconds)
139
146
*
140
147
*/
141
- p5 . PolySynth . prototype . noteAttack = function ( voice , note , velocity , secondsFromNow ) {
148
+ p5 . PolySynth . prototype . noteAttack = function ( _note , _velocity , secondsFromNow ) {
142
149
var now = p5sound . audiocontext . currentTime ;
143
150
var tFromNow = secondsFromNow || 0 ;
144
151
var t = now + tFromNow ;
152
+
153
+ var note = _note === undefined ? 60 : _note ;
154
+ var velocity = _velocity === undefined ? 1 : _velocity ;
155
+
156
+ var currentVoice ;
157
+
158
+ if ( this . notes [ note ] !== undefined ) {
159
+ // this.audiovoices[this.notes[note]].triggerRelease();
160
+ this . noteRelease ( note , 0 ) ;
161
+ }
162
+
163
+
164
+ if ( this . _voicesInUse . value < this . polyValue ) {
165
+ currentVoice = this . _voicesInUse . value ;
166
+ } else {
167
+ currentVoice = this . _oldest ;
168
+
169
+ // this._newest = this._oldest;
170
+ //
171
+ var oldestNote = p5 . prototype . freqToMidi ( this . audiovoices [ this . _oldest ] . oscillator . freq ( ) . value ) ;
172
+ this . noteRelease ( oldestNote ) ;
173
+ this . _oldest = ( this . _oldest + 1 ) % ( this . polyValue - 1 ) ;
174
+ }
175
+
176
+ this . notes [ note ] = currentVoice ;
177
+
145
178
this . _voicesInUse . setValueAtTime ( this . _voicesInUse . value + 1 , t ) ;
146
179
147
- this . _newest = voice ;
148
- this . audiovoices [ voice ] . triggerAttack ( note , velocity , secondsFromNow ) ;
149
- this . audiovoices [ voice ] . _isOn = true ;
180
+ this . _newest = currentVoice ;
181
+
182
+ this . audiovoices [ currentVoice ] . triggerAttack ( note , velocity , secondsFromNow ) ;
150
183
151
184
// if(this.voices[note] == null){
152
185
// this.voices[note] = new this.AudioVoice();
153
186
// }
154
187
// this.voices[note]._setNote(note);
155
188
// this.voices[note].env.setRange(velocity,0);
156
189
// this.voices[note].triggerAttack(secondsFromNow);
157
- }
190
+ } ;
158
191
159
192
/**
160
193
* Trigger the Release of a MonoSynth. This is similar to releasing
@@ -167,16 +200,27 @@ p5.PolySynth.prototype.noteAttack = function (voice, note, velocity, secondsFrom
167
200
*
168
201
*/
169
202
170
- p5 . PolySynth . prototype . noteRelease = function ( voice , secondsFromNow ) {
171
- var now = p5sound . audiocontext . currentTime ;
172
- var tFromNow = secondsFromNow || 0 ;
173
- var t = now + tFromNow ;
174
- this . _voicesInUse . setValueAtTime ( this . _voicesInUse . value - 1 , t ) ;
175
-
176
-
177
- this . audiovoices [ voice ] . triggerRelease ( secondsFromNow ) ;
203
+ p5 . PolySynth . prototype . noteRelease = function ( _note , secondsFromNow ) {
204
+ var note = _note === undefined ?
205
+ p5 . prototype . freqToMidi ( this . audiovoices [ this . _newest ] . oscillator . freq ( ) . value )
206
+ : _note ;
207
+
208
+ if ( this . notes [ _note ] === undefined ) {
209
+ console . warn ( 'Cannot release a note that is not already playing' ) ;
210
+ } else {
211
+ var now = p5sound . audiocontext . currentTime ;
212
+ var tFromNow = secondsFromNow || 0 ;
213
+ var t = now + tFromNow ;
214
+
215
+
216
+
217
+ this . _voicesInUse . setValueAtTime ( this . _voicesInUse . value - 1 , t ) ;
218
+ this . audiovoices [ this . notes [ note ] ] . triggerRelease ( secondsFromNow ) ;
219
+ this . notes [ note ] = undefined ;
220
+
221
+ this . _newest = this . _newest === 0 ? 0 : ( this . _newest - 1 ) % ( this . polyValue - 1 ) ;
222
+ }
178
223
179
- this . _newest = this . _newest === 0 ? 0 : ( this . _newest - 1 ) % ( this . polyValue - 1 ) ;
180
224
// if (this.voices[note] != null) {
181
225
// this.voices[note]._setNote(note);
182
226
// this.voices[note].triggerRelease(secondsFromNow);
0 commit comments