-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstrumentBoard.java
624 lines (477 loc) · 21.2 KB
/
InstrumentBoard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package com.jbatista.vsynth.components.modules;
import com.jbatista.bricks.Instrument;
import com.jbatista.bricks.KeyboardNote;
import com.jbatista.bricks.components.OutputConnector;
import com.jbatista.bricks.components.Patch;
import com.jbatista.bricks.components.builtin.*;
public class InstrumentBoard {
private static final double[] OCTAVE_RATIOS = new double[]{0.25, 0.5, 1, 1.5, 2.0};
private final Instrument instrument = new Instrument();
private final EnvelopeGenerator oscillatorEnvelope1 = new EnvelopeGenerator(instrument);
private final EnvelopeGenerator oscillatorEnvelope2 = new EnvelopeGenerator(instrument);
private final EnvelopeGenerator pitchEnvelope1 = new EnvelopeGenerator(instrument);
private final EnvelopeGenerator pitchEnvelope2 = new EnvelopeGenerator(instrument);
private final Oscillator oscillator1 = new Oscillator(instrument);
private final Oscillator oscillator2 = new Oscillator(instrument);
private final Oscillator oscillator3 = new Oscillator(instrument);
private final Oscillator noise = new Oscillator(instrument);
private final Lfo lfo = new Lfo(instrument);
private final Mixer oscillatorMixer1 = new Mixer(instrument);
private final Mixer oscillatorMixer2 = new Mixer(instrument);
private final Mixer mainMixer = new Mixer(instrument);
private final EnvelopeGenerator filterEnvelope1 = new EnvelopeGenerator(instrument);
private final EnvelopeGenerator filterEnvelope2 = new EnvelopeGenerator(instrument);
private final LowPassFilter lowPassFilter1 = new LowPassFilter(instrument);
private final LowPassFilter lowPassFilter2 = new LowPassFilter(instrument);
private final Keyboard keyboard = new Keyboard(instrument);
private final Patch[] patches = new Patch[19];
private int octaveOffset1 = 2;
private int octaveOffset2 = 2;
private double oscillator1fineTune = 1;
private double oscillator2fineTune = 1;
private boolean onlyModulateOscillator2 = false;
public InstrumentBoard() {
for (int i = 0; i < patches.length; i++) patches[i] = new Patch();
// oscillators interaction
oscillator1.getOutput(0).connectPatch(patches[0]);
oscillator3.getInput(0).connectPatch(patches[0]);
oscillator3.getController(0).setValue(1);
oscillator3.getController(1).setValue(0.5);
// lfo interaction
lfo.getOutput(0).connectPatch(patches[1]);
lfo.getOutput(1).connectPatch(patches[2]);
oscillator1.getInput(1).connectPatch(patches[1]);
oscillator2.getInput(1).connectPatch(patches[2]);
oscillator3.getInput(1).connectPatch(patches[1]);
// noise setup: always active, starts muted
noise.getInput(0).write(1);
noise.getController(0).setValue(5);
noise.getController(3).setValue(0);
noise.getOutput(1).connectPatch(patches[3]);
// mixer setup: oscillator 1, 3, noise
oscillator1.getOutput(1).connectPatch(patches[4]);
oscillator3.getOutput(1).connectPatch(patches[5]);
oscillatorMixer1.getController(0).setValue(0.05);
oscillatorMixer1.getController(1).setValue(0.05);
oscillatorMixer1.getController(2).setValue(0.05);
oscillatorMixer1.getInput(0).connectPatch(patches[3]);
oscillatorMixer1.getInput(1).connectPatch(patches[4]);
oscillatorMixer1.getInput(2).connectPatch(patches[5]);
// mixer setup: oscillator 2, noise
oscillator2.getOutput(1).connectPatch(patches[6]);
oscillatorMixer2.getController(0).setValue(0.05);
oscillatorMixer2.getController(1).setValue(0.005);
oscillatorMixer2.getInput(0).connectPatch(patches[6]);
oscillatorMixer2.getInput(1).connectPatch(patches[3]);
// oscillator envelope setup
oscillatorMixer1.getOutput(0).connectPatch(patches[7]);
oscillatorMixer2.getOutput(0).connectPatch(patches[8]);
oscillatorEnvelope1.getInput(0).connectPatch(patches[7]);
oscillatorEnvelope2.getInput(0).connectPatch(patches[8]);
// filter setup
oscillatorEnvelope1.getOutput(0).connectPatch(patches[9]);
oscillatorEnvelope2.getOutput(0).connectPatch(patches[10]);
lowPassFilter1.getInput(0).connectPatch(patches[9]);
lowPassFilter2.getInput(0).connectPatch(patches[10]);
// filter envelope setup
filterEnvelope1.getInput(0).write(1);
filterEnvelope2.getInput(0).write(1);
filterEnvelope1.getOutput(0).connectPatch(patches[11]);
filterEnvelope2.getOutput(0).connectPatch(patches[12]);
lowPassFilter1.getInput(1).connectPatch(patches[11]);
lowPassFilter2.getInput(1).connectPatch(patches[12]);
// mixer setup: main
lowPassFilter1.getOutput(0).connectPatch(patches[13]);
lowPassFilter2.getOutput(0).connectPatch(patches[14]);
mainMixer.getInput(0).connectPatch(patches[13]);
mainMixer.getInput(1).connectPatch(patches[14]);
// oscillator pitch envelopes
pitchEnvelope1.getOutput(0).connectPatch(patches[15]);
pitchEnvelope2.getOutput(0).connectPatch(patches[16]);
oscillator1.getInput(0).connectPatch(patches[15]);
oscillator2.getInput(0).connectPatch(patches[16]);
// polyphony
keyboard.getOutput(0).connectPatch(patches[17]);
pitchEnvelope1.getInput(0).connectPatch(patches[17]);
pitchEnvelope1.getInput(1).connectPatch(patches[17]);
oscillatorEnvelope1.getInput(1).connectPatch(patches[17]);
filterEnvelope1.getInput(1).connectPatch(patches[17]);
pitchEnvelope1.getController(8).setValue(1);
pitchEnvelope2.getController(8).setValue(1);
setMono();
}
public synchronized void pressKey(KeyboardNote note) {
keyboard.pressKey(note);
}
public synchronized void releaseKey(KeyboardNote note) {
keyboard.releaseKey(note);
}
public void setMono() {
keyboard.getController(0).setValue(1);
// 2nd key
pitchEnvelope2.getInput(0).disconnectAllPatches();
pitchEnvelope2.getInput(1).disconnectAllPatches();
oscillatorEnvelope2.getInput(1).disconnectAllPatches();
filterEnvelope2.getInput(1).disconnectAllPatches();
pitchEnvelope2.getInput(0).connectPatch(patches[17]);
pitchEnvelope2.getInput(1).connectPatch(patches[17]);
oscillatorEnvelope2.getInput(1).connectPatch(patches[17]);
filterEnvelope2.getInput(1).connectPatch(patches[17]);
}
public void setPoly() {
keyboard.getController(0).setValue(2);
// 2nd key
pitchEnvelope2.getInput(0).disconnectAllPatches();
pitchEnvelope2.getInput(1).disconnectAllPatches();
oscillatorEnvelope2.getInput(1).disconnectAllPatches();
filterEnvelope2.getInput(1).disconnectAllPatches();
keyboard.getOutput(1).connectPatch(patches[18]);
pitchEnvelope2.getInput(0).connectPatch(patches[18]);
pitchEnvelope2.getInput(1).connectPatch(patches[18]);
oscillatorEnvelope2.getInput(1).connectPatch(patches[18]);
filterEnvelope2.getInput(1).connectPatch(patches[18]);
}
public Instrument getInstrument() {
return instrument;
}
public void runInstrumentProcess() {
instrument.runProcess();
}
public OutputConnector getSoundOutput() {
return mainMixer.getOutput(0);
}
//<editor-fold desc="modulation settings" defaultstate="collapsed">
// 0, 1, 2, 3, 4
public void setLfoShape(int shape) {
lfo.getController(0).setValue(shape);
}
public int getLfoShape() {
return (int) lfo.getController(0).getValue();
}
public void onlyModulateOscillator2(boolean value) {
this.onlyModulateOscillator2 = value;
if (this.onlyModulateOscillator2) {
oscillator1.getInput(1).disconnectPatch(patches[1]);
oscillator3.getInput(1).disconnectPatch(patches[1]);
} else {
oscillator1.getInput(1).connectPatch(patches[1]);
oscillator3.getInput(1).connectPatch(patches[1]);
}
}
public boolean isOnlyModulateOscillator2() {
return onlyModulateOscillator2;
}
// 0 ~ 1
public void setLfoValue(double value) {
lfo.getController(1).setValue(value);
}
public double getLfoValue() {
return lfo.getController(1).getValue();
}
public double getLfoFrequency() {
return lfo.getController(1).getDisplayValue();
}
public double getOscillatorModStr() {
return oscillator1.getController(3).getValue();
}
public void setOscillatorModStr(double value) {
oscillator1.getController(2).setValue(value);
oscillator2.getController(2).setValue(value);
oscillator3.getController(2).setValue(value);
}
public boolean isFilterClosing() {
return lowPassFilter1.getController(2).getValue() == 1;
}
public void setFilterCloses(boolean value) {
final double result = value ? 1 : 0;
lowPassFilter1.getController(2).setValue(result);
lowPassFilter2.getController(2).setValue(result);
}
//</editor-fold>
//<editor-fold desc="oscillator settings" defaultstate="collapsed">
// 0, 1, 2, 3, 4
public void setOscillator1Shape(int shape) {
oscillator1.getController(0).setValue(shape);
}
public int getOscillator1Shape() {
return (int) oscillator1.getController(0).getValue();
}
// 0, 1, 2, 3, 4
public void setOscillator2Shape(int shape) {
oscillator2.getController(0).setValue(shape);
}
public int getOscillator2Shape() {
return (int) oscillator2.getController(0).getValue();
}
// 0, 1, 2, 3, 4
public void setOctaveOffset1(int octaveOffset1) {
this.octaveOffset1 = Math.max(0, Math.min(octaveOffset1, 4));
pitchEnvelope1
.getInput(0)
.setRatio(OCTAVE_RATIOS[this.octaveOffset1] * this.oscillator1fineTune);
}
public int getOctaveOffset1() {
return octaveOffset1;
}
// 0, 1, 2, 3, 4
public void setOctaveOffset2(int octaveOffset2) {
this.octaveOffset2 = Math.max(0, Math.min(octaveOffset2, 4));
pitchEnvelope2
.getInput(0)
.setRatio(OCTAVE_RATIOS[this.octaveOffset2] * this.oscillator2fineTune);
}
public int getOctaveOffset2() {
return octaveOffset2;
}
public double getOscillator1fineTune() {
return oscillator1fineTune;
}
public void setOscillator1fineTune(double oscillator1fineTune) {
this.oscillator1fineTune = Math.max(0.5, Math.min(oscillator1fineTune, 1.5));
pitchEnvelope1
.getInput(0)
.setRatio(OCTAVE_RATIOS[octaveOffset1] * this.oscillator1fineTune);
}
public double getOscillator2fineTune() {
return oscillator2fineTune;
}
public void setOscillator2fineTune(double oscillator2fineTune) {
this.oscillator2fineTune = Math.max(0.5, Math.min(oscillator2fineTune, 1.5));
pitchEnvelope2
.getInput(0)
.setRatio(OCTAVE_RATIOS[octaveOffset2] * this.oscillator2fineTune);
}
public void setOscillatorAttackLevel(double oscillatorAttackLevel) {
oscillatorEnvelope1.getController(0).setValue(oscillatorAttackLevel);
oscillatorEnvelope2.getController(0).setValue(oscillatorAttackLevel);
}
public double getOscillatorAttackLevel() {
return oscillatorEnvelope1.getController(0).getValue();
}
public void setOscillatorDecayLevel(double oscillatorDecayLevel) {
oscillatorEnvelope1.getController(1).setValue(oscillatorDecayLevel);
oscillatorEnvelope2.getController(1).setValue(oscillatorDecayLevel);
}
public double getOscillatorDecayLevel() {
return oscillatorEnvelope1.getController(1).getValue();
}
public void setOscillatorSustainLevel(double oscillatorSustainLevel) {
oscillatorEnvelope1.getController(2).setValue(oscillatorSustainLevel);
oscillatorEnvelope2.getController(2).setValue(oscillatorSustainLevel);
}
public double getOscillatorSustainLevel() {
return oscillatorEnvelope1.getController(2).getValue();
}
public void setOscillatorReleaseLevel(double oscillatorReleaseLevel) {
oscillatorEnvelope1.getController(3).setValue(oscillatorReleaseLevel);
oscillatorEnvelope2.getController(3).setValue(oscillatorReleaseLevel);
}
public double getOscillatorReleaseLevel() {
return oscillatorEnvelope1.getController(3).getValue();
}
public void setOscillatorAttackSpeed(double oscillatorAttackSpeed) {
oscillatorEnvelope1.getController(4).setValue(oscillatorAttackSpeed);
oscillatorEnvelope2.getController(4).setValue(oscillatorAttackSpeed);
}
public double getOscillatorAttackSpeed() {
return oscillatorEnvelope1.getController(4).getValue();
}
public void setOscillatorDecaySpeed(double oscillatorDecaySpeed) {
oscillatorEnvelope1.getController(5).setValue(oscillatorDecaySpeed);
oscillatorEnvelope2.getController(5).setValue(oscillatorDecaySpeed);
}
public double getOscillatorDecaySpeed() {
return oscillatorEnvelope1.getController(5).getValue();
}
public void setOscillatorSustainSpeed(double oscillatorSustainSpeed) {
oscillatorEnvelope1.getController(6).setValue(oscillatorSustainSpeed);
oscillatorEnvelope2.getController(6).setValue(oscillatorSustainSpeed);
}
public double getOscillatorSustainSpeed() {
return oscillatorEnvelope1.getController(6).getValue();
}
public void setOscillatorReleaseSpeed(double oscillatorReleaseSpeed) {
oscillatorEnvelope1.getController(7).setValue(oscillatorReleaseSpeed);
oscillatorEnvelope2.getController(7).setValue(oscillatorReleaseSpeed);
}
public double getOscillatorReleaseSpeed() {
return oscillatorEnvelope1.getController(7).getValue();
}
//</editor-fold>
//<editor-fold desc="pitch envelope" defaultstate="collapsed">
public void setPitchAttackLevel(double pitchAttackLevel) {
pitchEnvelope1.getController(0).setValue(pitchAttackLevel);
pitchEnvelope2.getController(0).setValue(pitchAttackLevel);
}
public double getPitchAttackLevel() {
return pitchEnvelope1.getController(0).getValue();
}
public void setPitchDecayLevel(double pitchDecayLevel) {
pitchEnvelope1.getController(1).setValue(pitchDecayLevel);
pitchEnvelope2.getController(1).setValue(pitchDecayLevel);
}
public double getPitchDecayLevel() {
return pitchEnvelope1.getController(1).getValue();
}
public void setPitchSustainLevel(double pitchSustainLevel) {
pitchEnvelope1.getController(2).setValue(pitchSustainLevel);
pitchEnvelope2.getController(2).setValue(pitchSustainLevel);
}
public double getPitchSustainLevel() {
return pitchEnvelope1.getController(2).getValue();
}
public void setPitchReleaseLevel(double pitchReleaseLevel) {
pitchEnvelope1.getController(3).setValue(pitchReleaseLevel);
pitchEnvelope2.getController(3).setValue(pitchReleaseLevel);
}
public double getPitchReleaseLevel() {
return pitchEnvelope1.getController(3).getValue();
}
public void setPitchAttackSpeed(double pitchAttackSpeed) {
pitchEnvelope1.getController(4).setValue(pitchAttackSpeed);
pitchEnvelope2.getController(4).setValue(pitchAttackSpeed);
}
public double getPitchAttackSpeed() {
return pitchEnvelope1.getController(4).getValue();
}
public void setPitchDecaySpeed(double pitchDecaySpeed) {
pitchEnvelope1.getController(5).setValue(pitchDecaySpeed);
pitchEnvelope2.getController(5).setValue(pitchDecaySpeed);
}
public double getPitchDecaySpeed() {
return pitchEnvelope1.getController(5).getValue();
}
public void setPitchSustainSpeed(double pitchSustainSpeed) {
pitchEnvelope1.getController(6).setValue(pitchSustainSpeed);
pitchEnvelope2.getController(6).setValue(pitchSustainSpeed);
}
public double getPitchSustainSpeed() {
return pitchEnvelope1.getController(6).getValue();
}
public void setPitchReleaseSpeed(double pitchReleaseSpeed) {
pitchEnvelope1.getController(7).setValue(pitchReleaseSpeed);
pitchEnvelope2.getController(7).setValue(pitchReleaseSpeed);
}
public double getPitchReleaseSpeed() {
return pitchEnvelope1.getController(7).getValue();
}
//</editor-fold>
//<editor-fold desc="volume settings" defaultstate="collapsed">
public double getOscillator1Volume() {
return oscillator1.getController(3).getValue();
}
// 0 ~ 1
public void setOscillator1Volume(double oscillator1Volume) {
oscillator1.getController(3).setValue(oscillator1Volume);
}
public double getOscillator2Volume() {
return oscillator2.getController(3).getValue();
}
// 0 ~ 1
public void setOscillator2Volume(double oscillator2Volume) {
oscillator2.getController(3).setValue(oscillator2Volume);
}
public double getOscillator3Volume() {
return oscillator3.getController(3).getValue();
}
// 0 ~ 1
public void setOscillator3Volume(double oscillator3Volume) {
oscillator3.getController(3).setValue(oscillator3Volume);
}
public double getNoiseVolume() {
return noise.getController(3).getValue();
}
// 0 ~ 1
public void setNoiseVolume(double noiseVolume) {
noise.getController(3).setValue(noiseVolume);
}
public double getGlobalVolume() {
return mainMixer.getController(0).getValue();
}
// 0 ~ 1
public void setGlobalVolume(double globalVolume) {
mainMixer.getController(0).setValue(globalVolume);
mainMixer.getController(1).setValue(globalVolume);
}
//</editor-fold>
//<editor-fold desc="filter settings" defaultstate="collapsed">
public double getFilterValue() {
return lowPassFilter1.getController(0).getValue();
}
// 0 ~ 1
public void setFilterCutoff(double cutoff) {
lowPassFilter1.getController(0).setValue(cutoff);
lowPassFilter2.getController(0).setValue(cutoff);
}
public double getFilterCutoff() {
return lowPassFilter1.getController(0).getValue();
}
public double getFilterCutoffFrequency() {
return lowPassFilter1.getController(0).getDisplayValue();
}
// 0 ~ 1
public void setFilterResonance(double resonance) {
lowPassFilter1.getController(1).setValue(resonance);
lowPassFilter2.getController(1).setValue(resonance);
}
public double getFilterRessonance() {
return lowPassFilter1.getController(1).getValue();
}
public void setFilterAttackLevel(double filterAttackLevel) {
filterEnvelope1.getController(0).setValue(filterAttackLevel);
filterEnvelope2.getController(0).setValue(filterAttackLevel);
}
public double getFilterAttackLevel() {
return filterEnvelope1.getController(0).getValue();
}
public void setFilterDecayLevel(double filterDecayLevel) {
filterEnvelope1.getController(1).setValue(filterDecayLevel);
filterEnvelope2.getController(1).setValue(filterDecayLevel);
}
public double getFilterDecayLevel() {
return filterEnvelope1.getController(1).getValue();
}
public void setFilterSustainLevel(double filterSustainLevel) {
filterEnvelope1.getController(2).setValue(filterSustainLevel);
filterEnvelope2.getController(2).setValue(filterSustainLevel);
}
public double getFilterSustainLevel() {
return filterEnvelope1.getController(2).getValue();
}
public void setFilterReleaseLevel(double filterReleaseLevel) {
filterEnvelope1.getController(3).setValue(filterReleaseLevel);
filterEnvelope2.getController(3).setValue(filterReleaseLevel);
}
public double getFilterReleaseLevel() {
return filterEnvelope1.getController(3).getValue();
}
public void setFilterAttackSpeed(double filterAttackSpeed) {
filterEnvelope1.getController(4).setValue(filterAttackSpeed);
filterEnvelope2.getController(4).setValue(filterAttackSpeed);
}
public double getFilterAttackSpeed() {
return filterEnvelope1.getController(4).getValue();
}
public void setFilterDecaySpeed(double filterDecaySpeed) {
filterEnvelope1.getController(5).setValue(filterDecaySpeed);
filterEnvelope2.getController(5).setValue(filterDecaySpeed);
}
public double getFilterDecaySpeed() {
return filterEnvelope1.getController(5).getValue();
}
public void setFilterSustainSpeed(double filterSustainSpeed) {
filterEnvelope1.getController(6).setValue(filterSustainSpeed);
filterEnvelope2.getController(6).setValue(filterSustainSpeed);
}
public double getFilterSustainSpeed() {
return filterEnvelope1.getController(6).getValue();
}
public void setFilterReleaseSpeed(double filterReleaseSpeed) {
filterEnvelope1.getController(7).setValue(filterReleaseSpeed);
filterEnvelope2.getController(7).setValue(filterReleaseSpeed);
}
public double getFilterReleaseSpeed() {
return filterEnvelope1.getController(7).getValue();
}
//</editor-fold>
}