1
-
2
-
3
-
4
1
var psynth ;
5
2
6
3
var ptable_real , ptable_imag ;
7
4
var real_wave = [ ] ;
8
5
var imag_wave = [ ]
9
6
10
7
function preload ( ) {
11
- ptable_real = loadStrings ( 'Wurlitzer_2_real .txt' ) ;
12
- ptable_imag = loadStrings ( 'Wurlitzer_2_imag .txt' ) ;
8
+ ptable_real = loadStrings ( '../polyphonic_synth/TwelveStringGuitar_real .txt' ) ;
9
+ ptable_imag = loadStrings ( '../polyphonic_synth/TwelveStringGuitar_imag .txt' ) ;
13
10
}
14
11
15
12
@@ -22,16 +19,16 @@ function setup() {
22
19
frameRate ( 25 ) ;
23
20
24
21
25
- psynth = new PolySynth ( 15 , PeriodicWave ) ;
22
+ // psynth = new PolySynth(15,PeriodicWave);
23
+ psynth = new p5 . PolySynth ( PeriodicWave , 8 ) ;
26
24
27
25
for ( var i = 0 ; i < 2048 ; i ++ ) {
28
26
real_wave [ i ] = float ( ptable_real [ i ] ) ;
29
-
30
27
}
31
28
for ( var i = 0 ; i < 2048 ; i ++ ) {
32
29
imag_wave [ i ] = float ( ptable_imag [ i ] ) ;
33
-
34
30
}
31
+
35
32
36
33
}
37
34
@@ -44,81 +41,77 @@ function draw() {
44
41
45
42
function mousePressed ( ) {
46
43
47
- var note = int ( map ( mouseX , 0 , width , 60 , 84 ) ) ;
44
+ var note = int ( map ( mouseX , 0 , width , 200 , 440 ) ) ;
48
45
var length = map ( mouseY , 0 , 300 , 0 , 5 ) ;
49
-
50
- psynth . setAdsr ( 0.021 , 0.025 , length , 0.025 ) ;
46
+
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 } ) ;
51
54
52
55
// uncomment for SquareVoice detune parameters
53
56
//var d = int(random(1,12));
54
57
//psynth.setParams({detune: d });
55
58
56
59
// uncomment for PeriodicWave
57
- psynth . setParams ( { real : real_wave , imag : imag_wave } ) ;
58
-
59
- psynth . setNote ( note ) ;
60
- psynth . play ( ) ;
60
+ //psynth.setParams({real: real_wave , imag: imag_wave});
61
+
61
62
62
63
}
63
64
64
65
65
66
//////////////////////////////////////////////////////////////////////////////
66
67
function PeriodicWave ( params ) {
67
- AudioVoice . call ( this ) ;
68
+ p5 . MonoSynth . call ( this ) ;
68
69
69
70
this . real = new Float32Array ( [ 0 , 0 ] ) ;
70
71
this . imag = new Float32Array ( [ 0 , 1 ] ) ;
71
-
72
- this . context = getAudioContext ( ) ;
73
72
74
- this . wt = this . context . createPeriodicWave ( this . real , this . imag ) ;
73
+ this . wt = this . ac . createPeriodicWave ( this . real , this . imag ) ;
75
74
76
- this . oscillator = this . context . createOscillator ( ) ;
77
- this . oscillator . setPeriodicWave ( this . wt ) ;
75
+ // this.oscillator = this.context.createOscillator();
76
+ this . oscillator . oscillator . setPeriodicWave ( this . wt ) ;
77
+ this . filter . setType ( 'lowpass' ) ;
78
+ this . filter . set ( 22050 , 5 ) ;
78
79
79
- this . oscillator . disconnect ( ) ;
80
- this . oscillator . start ( ) ;
81
-
82
- this . oscillator . connect ( this . filter ) ;
83
-
84
- this . setNote = function ( note ) {
85
- this . oscillator . frequency . value = midiToFreq ( note ) ;
86
- }
80
+ this . env . connect ( this . filter ) ;
87
81
88
82
this . setParams = function ( params ) {
89
- // console.log(params.real);
90
83
this . real = new Float32Array ( params . real ) ;
91
84
this . imag = new Float32Array ( params . imag ) ;
92
- this . wt = this . context . createPeriodicWave ( this . real , this . imag ) ;
93
- this . oscillator . setPeriodicWave ( this . wt ) ;
85
+ this . wt = this . ac . createPeriodicWave ( this . real , this . imag ) ;
86
+ this . oscillator . oscillator . setPeriodicWave ( this . wt ) ;
94
87
}
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 ) ;
95
97
98
+ }
96
99
97
100
}
98
101
99
- PeriodicWave . prototype = Object . create ( AudioVoice . prototype ) ;
102
+ PeriodicWave . prototype = Object . create ( p5 . MonoSynth . prototype ) ;
100
103
PeriodicWave . prototype . constructor = PeriodicWave ;
101
104
102
105
103
106
//////////////////////////////////////////////////////////////////////////////////////////////
104
107
// A typical synth class which inherits from AudioVoice class
105
108
function SquareVoice ( ) {
106
109
107
- AudioVoice . call ( this ) ;
108
-
109
- this . osctype = 'square' ;
110
- this . oscillator = new p5 . Oscillator ( this . note , this . osctype ) ;
111
- this . oscillator . disconnect ( ) ;
112
- this . oscillator . start ( ) ;
110
+ p5 . MonoSynth . call ( this ) ;
113
111
114
- this . oscillator . connect ( this . filter ) ;
115
-
116
- this . setNote = function ( note ) {
117
- this . note = note ;
118
- this . oscillator . freq ( midiToFreq ( note ) ) ;
119
- }
112
+ this . oscillator . setType ( 'square' ) ;
120
113
}
121
- SquareVoice . prototype = Object . create ( AudioVoice . prototype ) ; // browsers support ECMAScript 5 ! warning for compatibility with older browsers
114
+ SquareVoice . prototype = Object . create ( p5 . MonoSynth . prototype ) ; // browsers support ECMAScript 5 ! warning for compatibility with older browsers
122
115
SquareVoice . prototype . constructor = SquareVoice ;
123
116
124
117
//////////////////////////////////////////////////////////////////////////////////////////////
@@ -150,101 +143,5 @@ function DetunedOsc(){
150
143
}
151
144
}
152
145
153
- DetunedOsc . prototype = Object . create ( AudioVoice . prototype ) ;
146
+ DetunedOsc . prototype = Object . create ( p5 . AudioVoice . prototype ) ;
154
147
DetunedOsc . prototype . constructor = DetunedOsc ;
155
-
156
-
157
-
158
-
159
-
160
-
161
- ////////////////////////////////////////////////////////////////////////////////////////////
162
- // A super AudioVoice class to talk to the PolySynth class
163
- function AudioVoice ( ) {
164
-
165
- this . osctype = 'sine' ;
166
- this . volume = 0.33 ;
167
- this . note = 60 ;
168
-
169
- this . attack = 0.25 ;
170
- this . decay = 0.25 ;
171
- this . sustain = 0.95 ;
172
- this . release = 0.25 ;
173
- this . env = new p5 . Env ( this . attack , this . volume , this . decay , this . volume , this . sustain , this . volume , this . release ) ;
174
-
175
- this . filter = new p5 . LowPass ( ) ;
176
- this . filter . set ( 22050 , 5 ) ;
177
-
178
- this . env . connect ( this . filter ) ;
179
-
180
- }
181
-
182
- AudioVoice . prototype . voicePlay = function ( ) {
183
- this . env . play ( this . filter ) ;
184
- }
185
-
186
- AudioVoice . prototype . attackPlay = function ( ) {
187
- this . env . triggerAttack ( this . oscillator ) ;
188
- }
189
-
190
- AudioVoice . prototype . releasePlay = function ( ) {
191
- this . env . triggerRelease ( this . oscillator ) ;
192
- }
193
-
194
- AudioVoice . prototype . setNote = function ( ) {
195
-
196
- }
197
-
198
- AudioVoice . prototype . setParams = function ( params ) {
199
-
200
- }
201
-
202
-
203
- AudioVoice . prototype . setAdsr = function ( a , d , s , r ) {
204
- this . attack = a ;
205
- this . decay = d ;
206
- this . sustain = s ;
207
- this . release = r ;
208
- this . env = new p5 . Env ( this . attack , this . decay , this . sustain , this . release ) ;
209
- this . env . play ( this . filter ) ;
210
- }
211
-
212
-
213
-
214
- /////////////////////////////////////////////////////////////////////////////
215
- // a class to deal with voices allocations, of notes, parameters etc.
216
- // should be abastracted from the user
217
- function PolySynth ( num , synthVoice ) {
218
- this . voices = [ ] ;
219
- this . num_voices = num ;
220
- this . poly_counter = 0 ;
221
-
222
- this . allocateVoices ( synthVoice ) ;
223
- }
224
-
225
- PolySynth . prototype . allocateVoices = function ( synthVoice ) {
226
- for ( var i = 0 ; i < this . num_voices ; i ++ ) {
227
- this . voices . push ( new synthVoice ( ) ) ;
228
- }
229
- }
230
-
231
- PolySynth . prototype . play = function ( ) {
232
- this . voices [ this . poly_counter ] . voicePlay ( ) ;
233
- this . poly_counter += 1 ;
234
- this . poly_counter = this . poly_counter % this . num_voices ;
235
- }
236
-
237
- PolySynth . prototype . setAdsr = function ( a , d , s , r ) {
238
- this . voices [ this . poly_counter ] . setAdsr ( a , d , s , r ) ;
239
- }
240
-
241
- PolySynth . prototype . setNote = function ( note ) {
242
- this . voices [ this . poly_counter ] . setNote ( note ) ;
243
- }
244
-
245
- PolySynth . prototype . setParams = function ( params ) {
246
- this . voices [ this . poly_counter ] . setParams ( params ) ;
247
- }
248
-
249
-
250
-
0 commit comments