@@ -5,8 +5,8 @@ var real_wave = [];
5
5
var imag_wave = [ ]
6
6
7
7
function preload ( ) {
8
- ptable_real = loadStrings ( '../polyphonic_synth/TwelveStringGuitar_real .txt' ) ;
9
- ptable_imag = loadStrings ( '../polyphonic_synth/TwelveStringGuitar_imag .txt' ) ;
8
+ ptable_real = loadStrings ( 'Wurlitzer_2_imag .txt' ) ;
9
+ ptable_imag = loadStrings ( 'Wurlitzer_2_real .txt' ) ;
10
10
}
11
11
12
12
@@ -29,6 +29,10 @@ function setup() {
29
29
imag_wave [ i ] = float ( ptable_imag [ i ] ) ;
30
30
}
31
31
32
+ //Uncomment for PeriodicWave
33
+ // psynth.audiovoices.forEach(function(voice){
34
+ // voice.setParams({real: real_wave , imag: imag_wave});
35
+ // });
32
36
33
37
}
34
38
@@ -40,66 +44,79 @@ function draw() {
40
44
}
41
45
42
46
function mousePressed ( ) {
43
-
44
47
var note = int ( map ( mouseX , 0 , width , 200 , 440 ) ) ;
45
48
var length = map ( mouseY , 0 , 300 , 0 , 5 ) ;
46
49
47
-
48
-
49
- psynth . play ( note , 1 , 0 , length ) ;
50
- psynth . noteADSR ( note , 0.021 , 0.025 , length , 0.025 ) ;
51
-
52
- var index = psynth . notes [ note ] . getValueAtTime ( ) ;
53
- psynth . audiovoices [ index ] . setParams ( { real : real_wave , imag : imag_wave } ) ;
54
-
55
- // uncomment for SquareVoice detune parameters
56
- //var d = int(random(1,12));
57
- //psynth.setParams({detune: d });
58
-
59
- // uncomment for PeriodicWave
60
- //psynth.setParams({real: real_wave , imag: imag_wave});
61
-
62
-
50
+ psynth . setADSR ( 0.021 , 0.025 , length , 0.025 ) ;
51
+ psynth . play ( note , 1 , 0 , 1 ) ;
63
52
}
64
53
65
54
66
- //////////////////////////////////////////////////////////////////////////////
67
55
function PeriodicWave ( params ) {
68
- p5 . MonoSynth . call ( this ) ;
69
-
56
+ p5 . AudioVoice . call ( this ) ;
57
+ this . osc = new p5 . Oscillator ( 'sine' ) ;
58
+
70
59
this . real = new Float32Array ( [ 0 , 0 ] ) ;
71
60
this . imag = new Float32Array ( [ 0 , 1 ] ) ;
72
-
73
61
this . wt = this . ac . createPeriodicWave ( this . real , this . imag ) ;
74
62
75
- // this.oscillator = this.context.createOscillator();
76
- this . oscillator . oscillator . setPeriodicWave ( this . wt ) ;
77
- this . filter . setType ( 'lowpass' ) ;
63
+ this . osc . disconnect ( ) ;
64
+ this . osc . start ( ) ;
65
+
66
+ this . env = new p5 . Env ( 0.021 , 0.025 , 0.025 , 0.025 , 0.95 , 0.33 , 0.25 ) ;
67
+ this . env . disconnect ( ) ;
68
+
69
+ this . filter = new p5 . LowPass ( ) ;
78
70
this . filter . set ( 22050 , 5 ) ;
79
71
72
+
80
73
this . env . connect ( this . filter ) ;
74
+ this . osc . connect ( this . filter ) ;
75
+
76
+ this . connect ( ) ;
77
+
78
+ this . filter . set ( 22050 , 5 ) ;
79
+
81
80
82
81
this . setParams = function ( params ) {
83
82
this . real = new Float32Array ( params . real ) ;
84
83
this . imag = new Float32Array ( params . imag ) ;
85
84
this . wt = this . ac . createPeriodicWave ( this . real , this . imag ) ;
86
- this . oscillator . oscillator . setPeriodicWave ( this . wt ) ;
85
+ this . osc . oscillator . setPeriodicWave ( this . wt ) ;
86
+ }
87
+
88
+ this . setADSR = function ( aTime , aLevel , dTime , dLevel ) {
89
+ this . env . set ( aTime , aLevel , dTime , dLevel )
90
+ }
91
+
92
+ this . play = function ( ) {
93
+ this . env . play ( this . filter ) ;
94
+
87
95
}
88
- this . setADSR = function ( a , d , s , r ) {
89
- this . attack = a ;
90
- this . decay = d ;
91
- this . sustain = s ;
92
- this . release = r ;
93
- this . volume = 1
94
- this . env . set ( this . attack , this . volume , this . decay , this . volume , this . release , this . volume ) ;
95
- // this.env.set(this.attack, this.decay, this.sustain, this.release);
96
- this . env . play ( this . filter ) ;
97
96
97
+ this . triggerAttack = function ( note , velocity , secondsFromNow ) {
98
+ var secondsFromNow = secondsFromNow || 0 ;
99
+
100
+ //triggerAttack uses ._setNote to convert a midi string to a frequency if necessary
101
+ var freq = typeof note === 'string' ? this . _setNote ( note )
102
+ : typeof note === 'number' ? note : 440 ;
103
+ var vel = velocity || 1 ;
104
+ // this.env.setRange(this.env.aLevel / velocity,0);
105
+ this . _isOn = true ;
106
+ this . osc . freq ( freq , 0 , secondsFromNow ) ;
107
+ this . env . triggerAttack ( this . filter , secondsFromNow ) ;
108
+
109
+ }
110
+
111
+ this . triggerRelease = function ( secondsFromNow ) {
112
+ var secondsFromNow = secondsFromNow || 0 ;
113
+ this . env . triggerRelease ( this . filter , secondsFromNow ) ;
114
+ this . _isOn = false ;
98
115
}
99
116
100
117
}
101
118
102
- PeriodicWave . prototype = Object . create ( p5 . MonoSynth . prototype ) ;
119
+ PeriodicWave . prototype = Object . create ( p5 . AudioVoice . prototype ) ;
103
120
PeriodicWave . prototype . constructor = PeriodicWave ;
104
121
105
122
@@ -115,33 +132,91 @@ SquareVoice.prototype = Object.create(p5.MonoSynth.prototype); // browsers supp
115
132
SquareVoice . prototype . constructor = SquareVoice ;
116
133
117
134
//////////////////////////////////////////////////////////////////////////////////////////////
118
- // A second one
135
+ // A Detuned synth
119
136
function DetunedOsc ( ) {
120
137
121
- AudioVoice . call ( this ) ;
138
+ p5 . MonoSynth . call ( this ) ;
122
139
123
140
this . osctype = 'sine' ;
124
- this . detune = 5 ;
141
+ this . detune = - 5 ;
142
+
143
+ this . oscOne = this . oscillator ;
144
+ this . oscTwo = new p5 . Oscillator ( ) ;
145
+
146
+ this . filter . setType ( 'lowpass' ) ;
147
+ this . filter . set ( 22050 , 5 ) ;
125
148
126
- this . oscOne = new p5 . Oscillator ( midiToFreq ( this . note ) , this . osctype ) ;
127
- this . oscTwo = new p5 . Oscillator ( midiToFreq ( this . note ) - this . detune , this . osctype ) ;
128
149
this . oscOne . disconnect ( ) ;
129
150
this . oscTwo . disconnect ( ) ;
130
- this . oscOne . start ( ) ;
131
- this . oscTwo . start ( ) ;
151
+
132
152
133
153
this . oscOne . connect ( this . filter ) ;
134
154
this . oscTwo . connect ( this . filter ) ;
135
155
136
- this . setNote = function ( note ) {
137
- this . oscOne . freq ( midiToFreq ( note ) ) ;
138
- this . oscTwo . freq ( midiToFreq ( note ) - this . detune ) ;
139
- }
140
156
141
- this . setParams = function ( params ) {
142
- this . detune = params . detune ;
157
+ this . env . setInput ( this . oscOne , this . oscTwo ) ;
158
+
159
+ this . oscOne . start ( ) ;
160
+ this . oscTwo . start ( ) ;
161
+
162
+
163
+ this . triggerAttack = function ( note , velocity , secondsFromNow ) {
164
+ this . oscTwo . oscillator . detune . value
165
+ var secondsFromNow = secondsFromNow || 0 ;
166
+ var freq = typeof note === 'string' ? this . _setNote ( note )
167
+ : typeof note === 'number' ? note : 440 ;
168
+ var vel = velocity || 1 ;
169
+
170
+ this . _isOn = true ;
171
+
172
+ this . oscOne . freq ( freq , 0 , secondsFromNow ) ;
173
+ this . oscTwo . freq ( freq + this . detune , 0 , secondsFromNow ) ;
174
+ this . env . ramp ( this . output , secondsFromNow , this . env . aLevel ) ;
143
175
}
176
+
144
177
}
145
178
146
- DetunedOsc . prototype = Object . create ( p5 . AudioVoice . prototype ) ;
179
+ DetunedOsc . prototype = Object . create ( p5 . MonoSynth . prototype ) ;
147
180
DetunedOsc . prototype . constructor = DetunedOsc ;
181
+
182
+
183
+
184
+ function AdditiveSynth ( ) {
185
+ p5 . MonoSynth . call ( this ) ;
186
+
187
+ this . osctype = 'triangle' ;
188
+ this . harmonics = [ 1 , 2 , 4 , 6 , 8 ] ;
189
+ this . note = 60 ;
190
+
191
+ this . oscbank = [ ] ;
192
+ this . oscillator . dispose ( ) ;
193
+ delete this . oscillator ;
194
+ this . env . disconnect ( ) ;
195
+
196
+ for ( var i = 0 ; i < this . harmonics . length ; i ++ ) {
197
+ this . oscbank . push ( new p5 . Oscillator ( ) ) ;
198
+ this . oscbank [ i ] . setType ( this . osctype ) ;
199
+ this . oscbank [ i ] . disconnect ( ) ;
200
+ this . oscbank [ i ] . connect ( this . filter ) ;
201
+ this . env . connect ( this . oscbank [ i ] ) ;
202
+ this . oscbank [ i ] . start ( ) ;
203
+ }
204
+
205
+ this . triggerAttack = function ( note , velocity , secondsFromNow ) {
206
+ var secondsFromNow = secondsFromNow || 0 ;
207
+ var freq = typeof note === 'string' ? this . _setNote ( note )
208
+ : typeof note === 'number' ? note : 440 ;
209
+ var vel = velocity || 1 ;
210
+
211
+ this . _isOn = true ;
212
+
213
+ for ( var i = 0 ; i < this . harmonics . length ; i ++ ) {
214
+ this . oscbank [ i ] . freq ( freq + midiToFreq ( this . harmonics [ i ] * 12 ) , 0 , secondsFromNow ) ;
215
+ }
216
+
217
+ this . env . ramp ( this . output , secondsFromNow , this . env . aLevel ) ;
218
+ }
219
+
220
+ }
221
+ AdditiveSynth . prototype = Object . create ( p5 . MonoSynth . prototype ) ;
222
+ AdditiveSynth . prototype . constructor = AdditiveSynth ;
0 commit comments