-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericPatch.java
More file actions
1637 lines (1495 loc) · 47.5 KB
/
GenericPatch.java
File metadata and controls
1637 lines (1495 loc) · 47.5 KB
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Synth Patch Conversion
* Copyright (C) 2003-4, Kenneth L. Martinez (kmartin@users.sourceforge.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package PatchConversion;
/**
* Describes a synth patch (a.k.a. preset, program, etc). Implementor
* must be able to store all patch parameters in its attributes, read and
* write patch sysex, and read and write synth-specific representation
* of patch as XML.
*
* Version history:
* 1.01 New module type mod_vca
* 1.00 First release
*
* @author Kenneth L. Martinez
*/
import java.util.*;
public class GenericPatch {
private String patchName = "No Name";
private String patchNumber;
private String patchBank;
private String patchComment;
private String version = "1.01";
private String synthGenericVersion;
private ArrayList modules; // oscillators, filters, etc which make up a patch
private ArrayList connections; // all connections between modules
private ArrayList matrixMods; // allowable matrix modulations (if applicable)
private GenericPatch inputGP;
GenericPatch(String s) {
synthGenericVersion = s;
modules = new ArrayList();
connections = new ArrayList();
matrixMods = new ArrayList();
}
/**
* Set all parameter values to initial settings - mixer inputs at
* zero, modulation amounts to zero, etc
*/
// public void initializeModules() {
// for (int i = 0; i < modules.size(); i++) {
// ((Module)modules.get(i)).initialize();
// }
// }
public String getPatchName() {
return patchName;
}
public void setPatchName(String s) {
patchName = s;
}
public String getPatchNumber() {
return patchNumber;
}
public void setPatchNumber(String s) {
patchNumber = s;
}
public String getPatchBank() {
return patchBank;
}
public void setPatchBank(String s) {
patchBank = s;
}
public String getPatchComment() {
return patchComment;
}
public void setPatchComment(String s) {
patchComment = s;
}
/**
* see if all modules have values within their valid ranges
* @return valid
*/
public boolean isValid() {
int i;
try {
for (i = 0; i < modules.size(); i++) {
if (((SynthParm)modules.get(i)).isValid() == false) {
return false;
}
}
} catch (NullPointerException e) {
return false;
}
return true;
}
public void readInfoXML(String xml) {
String s = XMLReader.getTagValue(xml, "patch_name");
if (s != null) {
setPatchName(s);
}
setPatchNumber(XMLReader.getTagValue(xml, "patch_number"));
setPatchBank(XMLReader.getTagValue(xml, "patch_bank"));
setPatchComment(XMLReader.getTagValue(xml, "patch_comment"));
}
public ArrayList getModules() {
return modules;
}
public ArrayList getConnections() {
return connections;
}
public ArrayList getMatrixMods() {
return matrixMods;
}
public GenericPatch getInputGP() {
return inputGP;
}
public void addModule(Module mod) {
modules.add(mod);
mod.setGp(this);
}
public void addModule(int i, Module mod) {
modules.add(i, mod);
mod.setGp(this);
}
public void removeModule(Module mod) {
modules.remove(mod);
mod.setGp(null);
}
public void addConnection(Connection conn) {
// This is now done in ModuleInputJack.addConn()
// if (findConnectionToTarget(conn.getTargetJack()) != null) {
// System.out.println("Error: can't add connection from " +
// conn.getSourceJack().getMod().getName() + " " +
// conn.getSourceJack().getName() + " to " +
// conn.getTargetJack().getMod().getName() + " " +
// conn.getTargetJack().getName() + " - target already connected");
// int i = 1 / 0; // abort
// }
connections.add(conn);
conn.setGp(this);
}
public void removeConnection(String sourceModName, String sourceMjName,
String targetModName, String targetMjName) {
Connection conn = findConnection(sourceModName, sourceMjName,
targetModName, targetMjName);
removeConnection(conn);
}
public void removeConnection(Connection conn) {
conn.getSourceJack().removeConn(conn);
conn.getTargetJack().removeConn();
connections.remove(conn);
conn.setGp(null);
}
public void addMatrixMod(MatrixMod mm) {
matrixMods.add(mm);
mm.setGp(this);
}
public boolean addConnectionIfNotFound(String sourceModName, String sourceMjName,
String targetModName, String targetMjName) throws PatchDefinitionException {
Connection conn;
ModuleOutputJack mjSource;
ModuleInputJack mjTarget;
if (sourceModName.equalsIgnoreCase(targetModName) &&
sourceMjName.equalsIgnoreCase(targetMjName)) {
return true;
}
conn = findConnection(sourceModName, sourceMjName,
targetModName, targetMjName);
if (conn == null) {
// System.out.println("addConnection " + sourceModName + ", " + sourceMjName + ", " +
// targetModName + ", " + targetMjName);
mjSource = findModuleOutputJack(sourceModName, sourceMjName);
if (mjSource == null) {
System.out.println("Error: Can't find source jack - can't duplicate connection " +
sourceModName + "," + sourceMjName + " to " + targetModName + "," + targetMjName);
return false;
}
mjTarget = findModuleInputJack(targetModName, targetMjName);
if (mjTarget == null) {
System.out.println("Error: Can't find target jack - can't duplicate connection " +
sourceModName + "," + sourceMjName + " to " + targetModName + "," + targetMjName);
return false;
}
conn = new Connection(mjSource, mjTarget);
addConnection(conn);
conn.setGp(this);
}
return true;
}
/**
* see if input XML contains this type of patch
*/
public boolean matchXML(String xml) {
if (xml.indexOf("generic_patch") != -1) {
return true;
} else {
return false;
}
}
/**
* convert patch internal variables to XML
*/
public String writeXML() throws PatchDefinitionException {
int i;
StringBuffer sb = new StringBuffer();
reduceModNumbers();
sb.append("<generic_patch>");
sb.append("<version>" + version + "</version>");
sb.append("<synth_generic_version>" + synthGenericVersion + "</synth_generic_version>");
sb.append("<patch_name>" + patchName + "</patch_name>");
if (patchNumber != null) {
sb.append("<patch_number>" + patchNumber + "</patch_number>");
}
if (patchBank != null) {
sb.append("<patch_bank>" + patchBank + "</patch_bank>");
}
if (patchComment != null) {
sb.append("<patch_comment>" + patchComment + "</patch_comment>");
}
for (i = 0; i < modules.size(); i++) {
sb.append(((Module)modules.get(i)).writeXML());
}
sb.append("</generic_patch>");
return sb.toString();
}
/**
* Reduce numbering for all module types in the generic patch. First of a
* type should be 1, next is 2, etc.
*
*/
public void reduceModNumbers() {
int i, j, num;
String s;
Module mod, mod2;
HashMap modTypesChecked = new HashMap();
for (i = 0; i < modules.size(); i++) {
mod = (Module)modules.get(i);
if (mod.getUsed() < 3) {
continue;
}
// Have we processed this module type yet?
if (modTypesChecked.containsKey(mod.getType())) {
continue;
}
// No - add it to the list
modTypesChecked.put(mod.getType(), null);
// Find all modules of this type and reduce their numbers.
// This works for the first module found also (although the
// type compare is not necessary)
for (j = i, num = 0; j < modules.size(); j++) {
mod2 = (Module)modules.get(j);
if (mod2.getUsed() == 3 && mod2.getType().equalsIgnoreCase(mod.getType())) {
num++;
if (mod2.getNumber() != num) {
mod2.setNumber(num);
}
s = mod2.getName();
if (Character.isDigit(s.charAt(s.length() - 1))) {
mod2.setName(s.substring(0, s.length() - 1) + num);
}
reduceJackNumbers(mod2);
}
}
}
}
/**
* Reduce numbering for jacks (first of a type should be 1, next is 2, etc)
* and also reduce numbers of corresponding parms when they exist.
*
* @param mod Module to process
*/
public void reduceJackNumbers(Module mod) {
int i, j, num = 0, oldNum;
String prefix;
Module mod2;
ModuleInputJack mij, mij2;
ModuleParm mp;
HashMap jackTypesChecked = new HashMap();
for (i = 0; i < mod.getInputJacks().size(); i++) {
mij = (ModuleInputJack)mod.getInputJacks().get(i);
if (mij.getNumber() > 0) {
prefix = mij.getPrefix();
// Have we processed this jack type yet?
if (jackTypesChecked.containsKey(prefix)) {
continue;
}
// No - add it to the list
jackTypesChecked.put(prefix, null);
// Find all jacks of this type and reduce their numbers
// This works for the first jack found also (although the
// prefix compare is not necessary)
for (j = i, num = 0; j < mod.getInputJacks().size(); j++) {
mij2 = (ModuleInputJack)mod.getInputJacks().get(j);
if (mij2.isUsed() && mij2.getNumber() > 0 &&
mij2.getPrefix().equalsIgnoreCase(prefix)) {
num++;
oldNum = mij2.getNumber();
mij2.setNumber(num);
mp = mij2.getAttenuator();
if (mp != null) {
mp.setNumber(num);
}
}
}
}
}
}
/**
* read XML into internal variables
*/
public boolean readXML(String xml) throws PatchDefinitionException {
if (matchXML(xml) == false) {
System.out.println("Error: input file has no generic patch");
return false;
}
// Read XML into temp patch structure of modules/jacks/connections
inputGP = new GenericPatch(synthGenericVersion);
String tag[], sourceModule, sourceJack, targetModule, targetJack;
Module mod;
Connection conn;
XMLReader xr = new XMLReader(XMLReader.getTagValue(xml, "generic_patch"));
while ((tag = xr.getNextTag()) != null) {
if (tag[0].equalsIgnoreCase("version")) {
if (tag[1].equalsIgnoreCase(version) == false) {
System.out.println("Warning: Expected generic patch version " +
version + ", input is " + tag[1]);
}
} else if (tag[0].equalsIgnoreCase("synth_generic_version")) {
// FIXME can't test for this, can we? differs by synth
// if (tag[1].equalsIgnoreCase(synthGenericVersion) == false) {
// System.out.println("Warning: Expected synth generic version " +
// synthGenericVersion + ", input is " + tag[1]);
// }
} else if (tag[0].equalsIgnoreCase("patch_name")) {
patchName = tag[1];
} else if (tag[0].equalsIgnoreCase("patch_number")) {
patchNumber = tag[1];
} else if (tag[0].equalsIgnoreCase("patch_bank")) {
patchBank = tag[1];
} else if (tag[0].equalsIgnoreCase("patch_comment")) {
patchComment = tag[1];
} else if (tag[0].equalsIgnoreCase("module")) {
mod = new Module(tag[1]);
mod.setUsed(3);
inputGP.addModule(mod);
} else {
System.out.println("Error: unknown tag " + tag[0]);
return false;
}
}
for (int i = 0; i < inputGP.getModules().size(); i++) {
mod = (Module)inputGP.getModules().get(i);
for (int j = 0; j < mod.getInputJacks().size(); j++) {
ModuleInputJack mij = (ModuleInputJack)mod.getInputJacks().get(j);
inputGP.addConnection(mij.buildConnection());
}
}
return true;
}
/**
* convert input XML into our generic patch
*/
public boolean convertXML() throws PatchDefinitionException {
// Compare module by module, to see if all input modules can
// be duplicated by this synth
HashMap modMatchLists = initialModuleMatch();
if (modMatchLists.size() == 0) {
return false;
}
ModMatchData md;
Module mod, ourMod;
ModuleParm mp, ourMp;
ModuleInputJack mij, ourMij;
ModuleOutputJack moj, ourMoj;
ArrayList modMatches, a;
Iterator e;
int i, j, k, num;
int debug = 0;
if (debug == 1) {
System.out.println("=================================================");
e = inputGP.moduleIterator();
while (e.hasNext()) {
mod = (Module)e.next();
modMatches = (ArrayList)modMatchLists.get(mod);
for (k = 0; k < modMatches.size(); k++) {
md = (ModMatchData)modMatches.get(k);
ourMod = md.getMod();
System.out.println(mod.getName() + " possible match " + ourMod.getName());
for (i = 0; i < mod.getParms().size(); i++) {
mp = (ModuleParm)mod.getParms().get(i);
a = (ArrayList)md.getParmMatches().get(i);
for (j = 0; j < a.size(); j++) {
ourMp = (ModuleParm)a.get(j);
System.out.println(" " + mp.getName() + " possible match " + ourMp.getName());
}
}
for (i = 0; i < mod.getInputJacks().size(); i++) {
mij = (ModuleInputJack)mod.getInputJacks().get(i);
a = (ArrayList)md.getInputJackMatches().get(i);
for (j = 0; j < a.size(); j++) {
ourMij = (ModuleInputJack)a.get(j);
System.out.println(" " + mij.getName() + " possible match " + ourMij.getName());
}
}
for (i = 0; i < mod.getOutputJacks().size(); i++) {
moj = (ModuleOutputJack)mod.getOutputJacks().get(i);
ourMoj = (ModuleOutputJack)md.getOutputJackMatches().get(i);
System.out.println(" " + moj.getName() + " possible match " + ourMoj.getName());
}
}
}
}
// Make final module matches, selecting a single module to match each
// input module if it has the same connections. Also select matches
// for all parms and jacks of matched modules.
// FIXME should probably add way to override: maybe allow to try
// favored combination first (which may not be in numeric order),
// or allow combination to be specified which is known to work,
// if automatic routine fails or gives undesired result
HashMap finalMatch = finalModuleMatch(modMatchLists);
if (finalMatch.size() == 0) {
System.out.println("Error: cannot implement the input generic patch");
return false;
}
// Definite matches have been selected, so copy parm values from input
// to our generic patch and mark our matching jacks and parms used.
boolean found;
e = inputGP.moduleIterator();
while (e.hasNext()) {
mod = (Module)e.next();
md = (ModMatchData)finalMatch.get(mod);
ourMod = md.getMod();
// FIXME temporary kludge copy process
ourMod.setUsed(3);
for (i = 0; i < mod.getParms().size(); i++) {
mp = (ModuleParm)mod.getParms().get(i);
ourMp = (ModuleParm)md.getParmMatches().get(i);
if (ourMp.getPv().validateParm(mp.getValue()) == false) {
System.out.println("warning: Module " + ourMod.getName() + " parm " +
ourMp.getName() + " cannot accept value " + mp.getValue());
} else {
ourMp.setValue(mp.getValue());
}
ourMp.setUsed(true);
if (mp.getMorph() != null && mp.getMorph().isUsed()) {
if (ourMp.getMorph() == null) {
System.out.println("warning: Can't create morph for module " +
ourMod.getName() + " parm " + ourMp.getName());
} else {
ourMp.getMorph().setValue(mp.getMorph().getValue());
ourMp.getMorph().setUsed(true);
}
}
}
for (i = 0; i < mod.getInputJacks().size(); i++) {
mij = (ModuleInputJack)mod.getInputJacks().get(i);
ourMij = (ModuleInputJack)md.getInputJackMatches().get(i);
ourMij.setUsed(true);
}
for (i = 0; i < mod.getOutputJacks().size(); i++) {
moj = (ModuleOutputJack)mod.getOutputJacks().get(i);
ourMoj = (ModuleOutputJack)md.getOutputJackMatches().get(i);
ourMoj.setUsed(true);
}
}
setPatchName(inputGP.getPatchName());
setPatchNumber(inputGP.getPatchNumber());
setPatchBank(inputGP.getPatchBank());
setPatchComment(inputGP.getPatchComment());
return true;
}
/**
* Prepare lists of our potential matching modules for each of the
* modules in the input generic patch
*/
HashMap initialModuleMatch() {
Module mod;
HashMap modMatchLists = new HashMap();
ArrayList modMatches;
int i, j, num;
boolean failed = false;
// Process all modules of each type in a group
ModuleTypeIterator mte = inputGP.moduleTypeIterator();
String typ;
while (mte.hasNext()) {
typ = mte.nextType();
// Find all input modules of this type
mte.setToFirstMod();
num = 0;
while ((mod = mte.nextMod()) != null) {
num++;
modMatches = initialMatchToOurModules(mod);
if (modMatches.size() == 0) {
System.out.println("Error: unable to match input module " + mod.getName());
failed = true;
}
modMatchLists.put(mod, modMatches);
}
}
if (failed) {
System.out.println("Error: cannot implement the input generic patch");
return new HashMap();
}
return modMatchLists;
}
/**
* Returns a list of modules in our generic patch which are potential
* matches for the given module from the input generic patch. "Match"
* means it has at least the same number of each type of jack and parm.
* Also makes lists of potential matches for each jack and parm.
*
* @param mod
* @return
*/
ArrayList initialMatchToOurModules(Module mod) {
ArrayList modMatches = new ArrayList(), errorList, modErrorList = new ArrayList();
Module ourMod;
ModuleParm mp, ourMp;
ModuleOutputJack moj, ourMoj, matchJack;
ModuleInputJack mij, ourMij;
Connection conn;
MatrixMod mm;
ArrayList parmMatches, inputJackMatches, outputJackMatches;
ArrayList parms, inputJacks, outputJacks;
boolean found;
int i, j;
Iterator e, e2;
GenericPatch inputGP = mod.getGp();
e = moduleIterator();
while (e.hasNext()) {
ourMod = (Module)e.next();
if (ourMod.getType().equalsIgnoreCase(mod.getType()) == false) {
continue;
}
// FIXME added to prevent counter-intuitive match; is there a better way?
if (mod.getName().equals("Amp Envelope") &&
ourMod.getName().equals("Amp Envelope") == false) {
continue;
}
errorList = new ArrayList();
// compare each output jack
outputJackMatches = new ArrayList();
for (i = 0; i < mod.getOutputJacks().size(); i++) {
moj = (ModuleOutputJack)mod.getOutputJacks().get(i);
matchJack = null;
ourMoj = ourMod.findOutputJack(moj.getName());
if (ourMoj == null) {
errorList.add("Module " + mod.getName() + " unable to match output jack " +
moj.getName());
} else {
// only match if our module's output is connected to
// same type of module and input jack with same prefix
found = true;
e2 = inputGP.connectionTargetIterator(moj);
while (e2.hasNext()) {
conn = (Connection)e2.next();
if (findConnectionToSameType(ourMoj.getMod().getName(),
ourMoj.getName(),
conn.getTargetJack().getMod().getType(),
conn.getTargetJack().getPrefix()) == false) {
found = false;
errorList.add("Module " + mod.getName() + " unable to match connection of output jack " +
moj.getName() + " to " + conn.getTargetJack().getMod().getType() +
" " + conn.getTargetJack().getPrefix());
}
}
if (found) {
matchJack = ourMoj;
}
}
outputJackMatches.add(matchJack);
}
// compare each input jack
inputJackMatches = new ArrayList();
for (i = 0; i < mod.getInputJacks().size(); i++) {
mij = (ModuleInputJack)mod.getInputJacks().get(i);
conn = inputGP.findConnectionToTarget(mij);
inputJacks = new ArrayList();
for (j = 0; j < ourMod.getInputJacks().size(); j++) {
ourMij = (ModuleInputJack)ourMod.getInputJacks().get(j);
if (ourMij.getPrefix().equalsIgnoreCase(mij.getPrefix())) {
if ((mij.getAttenuator() != null && ourMij.getAttenuator() != null) ||
(mij.getAttenuator() == null && ourMij.getAttenuator() == null)) {
if (findConnectionFromSameType(conn.getSourceJack().getMod().getType(),
conn.getSourceJack().getPrefix(),
ourMij.getMod().getName(),
ourMij.getName())) {
inputJacks.add(ourMij);
}
}
}
}
if (inputJacks.size() == 0) {
for (j = 0; j < matrixMods.size(); j++) {
mm = (MatrixMod)matrixMods.get(j);
if (mm.seeIfJackCanBeAdded(conn.getSourceJack().getMod().getType(),
conn.getSourceJack().getPrefix(),
ourMod.getName(),
mij.getPrefix())) {
break;
}
}
if (j >= matrixMods.size()) {
errorList.add("Module " + mod.getName() + " unable to match input jack " +
mij.getName());
}
}
inputJackMatches.add(inputJacks);
}
// compare each input parm
parmMatches = new ArrayList();
for (i = 0; i < mod.getParms().size(); i++) {
mp = (ModuleParm)mod.getParms().get(i);
parms = new ArrayList();
for (j = 0; j < ourMod.getParms().size(); j++) {
ourMp = (ModuleParm)ourMod.getParms().get(j);
// FIXME compare parm range here too? If so, range match
// would be required. As it is, match can occur even if
// value is out of range.
if (ourMp.getPrefix().equalsIgnoreCase(mp.getPrefix())) {
parms.add(ourMp);
}
}
if (parms.size() == 0) {
for (j = 0; j < matrixMods.size(); j++) {
mm = (MatrixMod)matrixMods.get(j);
if (mm.seeIfParmCanBeAdded(mp.getPrefix())) {
break;
}
}
if (j >= matrixMods.size()) {
errorList.add("Module " + mod.getName() + " unable to match parm " +
mp.getName());
}
}
parmMatches.add(parms);
}
if (errorList.size() == 0) {
modMatches.add(new ModMatchData(ourMod, parmMatches, inputJackMatches,
outputJackMatches));
} else {
modErrorList.add("Could not match input module " + mod.getName() +
" to our module " + ourMod.getName());
modErrorList.addAll(errorList);
}
}
if (modMatches.size() == 0) {
System.out.println(modErrorList);
}
return modMatches;
}
/**
* Iterates through groupings of potential matches, stopping at the
* first one which can implement the generic patch.
*
* @param modMatchLists
* @return
*/
HashMap finalModuleMatch(HashMap modMatchLists) throws PatchDefinitionException {
Module mod, mod2, ourMod;
ModuleParm mp, ourMp;
ModuleOutputJack moj, ourMoj = null;
ModuleInputJack mij, ourMij;
MatrixMod mm;
ArrayList modMatches, trialMatch, a, errorList = new ArrayList(), unmatchedConns;
ModMatchData md, md2, mdNew;
Connection conn;
Object o;
Iterator e;
int i, j, k, indx, cnt = 0;
boolean found;
StringBuffer sb;
// For each permutation of matching modules, see if all connections
// are possible. Return the first complete match.
e = new ModulePermutationIterator(inputGP, modMatchLists);
while (e.hasNext()) {
trialMatch = (ArrayList)e.next();
cnt++;
sb = new StringBuffer("Trial match " + cnt + " ");
for (i = 0; i < trialMatch.size(); i++) {
md = (ModMatchData)trialMatch.get(i);
sb.append("/" + md.getMod().getName());
}
sb.append("/");
errorList.add(sb.toString());
for (i = 0; i < matrixMods.size(); i++) {
((MatrixMod)matrixMods.get(i)).removeCurrentMod();
}
HashMap finalMatch = new HashMap();
HashMap alreadyMatchedList = new HashMap();
// Try to use all hard-wired connections before resorting to using
// the limited number of matrix mods
unmatchedConns = finalMatchHardWired(trialMatch, finalMatch, alreadyMatchedList, errorList);
// FIXME not ready yet
// finalMatchCheckMMSourceGroups(finalMatch, alreadyMatchedList, unmatchedConns);
finalMatchMultiDestMods(trialMatch, finalMatch, alreadyMatchedList, errorList);
found = finalMatchSingleDestModsAndParms(trialMatch, finalMatch, alreadyMatchedList, errorList);
// FIXME - may still use multi-dest mod route as single route
// if its extra connection is to unused jack (e.g. PWM when
// osc waveform is saw)
if (found) {
return finalMatch;
}
}
if (cnt == 0) {
System.out.println("Error: cannot match one or more input modules");
} else {
System.out.println(errorList);
}
return new HashMap(); // No match found, return empty match list
}
/**
* Match input jacks which have hard-wired connections
*/
public ArrayList finalMatchHardWired(ArrayList trialMatch, HashMap finalMatch,
HashMap alreadyMatchedList, ArrayList errorList) {
Module mod, mod2, ourMod;
ModuleParm mp, ourMp;
ModuleInputJack mij, ourMij;
ArrayList a;
ArrayList unmatchedConns = new ArrayList();
ModMatchData md, md2, mdNew;
Connection conn;
Object o;
int i, j, k, indx;
HashMap alreadyMatched;
for (i = 0; i < inputGP.getModules().size(); i++) {
mod = (Module)inputGP.getModules().get(i);
md = (ModMatchData)trialMatch.get(i);
alreadyMatched = new HashMap();
mdNew = new ModMatchData(md.getMod(),
(ArrayList)md.getParmMatches().clone(),
(ArrayList)md.getInputJackMatches().clone(),
(ArrayList)md.getOutputJackMatches().clone());
for (j = 0; j < mod.getInputJacks().size(); j++) {
mij = (ModuleInputJack)mod.getInputJacks().get(j);
o = mdNew.getInputJackMatches().get(j);
a = (ArrayList)o;
// Match by prefix and same source module
for (k = 0; k < a.size(); k++) {
ourMij = (ModuleInputJack)a.get(k);
if (alreadyMatched.containsKey(ourMij)) {
continue;
}
if (ourMij.getPrefix().equalsIgnoreCase(mij.getPrefix())) {
// Find source module which is connected to input patch's input jack
conn = inputGP.findConnectionToTarget(mij);
mod2 = conn.getSourceJack().getMod();
indx = inputGP.getModules().indexOf(mod2);
md2 = (ModMatchData)trialMatch.get(indx);
ourMod = md2.getMod(); // get our matching source module
conn = findConnectionToTarget(ourMij);
// If this jack is connected to our matching source module, it's a match
if (conn != null && ourMod == conn.getSourceJack().getMod()) {
mdNew.getInputJackMatches().set(j, ourMij);
alreadyMatched.put(ourMij, mij);
// If there's an attenuator, match it to our attenuator
mp = mij.getAttenuator();
if (mp != null) {
ourMp = ourMij.getAttenuator();
indx = mod.getParms().indexOf(mp);
mdNew.getParmMatches().set(indx, ourMp);
}
break;
}
}
}
if (j >= mod.getInputJacks().size()) {
unmatchedConns.add(mij.getConn());
}
}
finalMatch.put(mod, mdNew);
alreadyMatchedList.put(mod, alreadyMatched);
}
return unmatchedConns;
}
/**
* All matrix mods in a given source group must have the same source jack.
* Look for groups of matrix mods; if found, try to assign the largest
* groups first. E.g. if key velocity affects 2 dest jacks in a patch, first
* try to use a source group containing exactly 2 matrix mods; if key
* velocity is the source for a group with more than 2 matrix mods, the
* additional matrix mods can't be used in this patch.
*/
public void finalMatchCheckMMSourceGroups(HashMap finalMatch,
HashMap alreadyMatchedList, ArrayList unmatchedConns) {
MatrixMod mm;
ArrayList a, sourceGroups = new ArrayList();
HashMap h = new HashMap();
int i, grp;
// Look for matrix mods with group numbers
for (i = 0; i < matrixMods.size(); i++) {
mm = (MatrixMod)matrixMods.get(i);
grp = mm.getSourceGroup();
if (grp != 0) {
a = (ArrayList)h.get(new Integer(grp));
if (a == null) {
a = new ArrayList();
h.put(new Integer(grp), a);
}
a.add(mm);
}
}
// Create list of groups, ordered from largest to smallest
Iterator e = h.values().iterator();
while (e.hasNext()) {
a = (ArrayList)e.next();
if (a.size() == 1) { // Groups need 2 or more
continue;
}
for (i = 0; i < sourceGroups.size(); i++) {
if (a.size() > ((ArrayList)sourceGroups.get(i)).size()) {
sourceGroups.add(i, a);
break;
}
}
if (i >= sourceGroups.size()) {
sourceGroups.add(a);
}
}
Module mod;
ModuleInputJack mij;
Object o;
HashMap alreadyMatched;
ModMatchData mdNew;
int j;
// Group unmatched connections by source jack, largest group first
// Now try to match largest source groups with the source jacks
// having the most unmatched connections
}
/**
* Try to implement matrix mods which have multiple dest jacks (e.g. an
* LFO affects both Osc1's and Osc2's Pulse Width).
*/
public void finalMatchMultiDestMods(ArrayList trialMatch, HashMap finalMatch,
HashMap alreadyMatchedList, ArrayList errorList) throws PatchDefinitionException {
Module mod, mod2, ourMod;
ModuleParm mp, ourMp;
ModuleOutputJack moj, ourMoj = null;
MatrixMod mm;
ModMatchData md, md2, mdNew;
Connection conn;
int i, j, k, indx;
HashMap alreadyMatched;
// Loop thru each input module's output jacks
// Find jacks with connections to more than one jack w/same prefix,
// not yet matched, owned by same module type, whose attenuators
// have the same value.
// If not found, skip to next output jack & then next input module
// Feed output jack & input jacks to mm to match
for (i = 0; i < inputGP.getModules().size(); i++) {
mod = (Module)inputGP.getModules().get(i);
alreadyMatched = (HashMap)alreadyMatchedList.get(mod);
mdNew = (ModMatchData)finalMatch.get(mod);
// Try to implement matrix mods which have multiple dest jacks
outer: for (j = 0; j < mod.getOutputJacks().size(); j++) {
moj = (ModuleOutputJack)mod.getOutputJacks().get(j);
HashMap h, targetJacks = new HashMap();
ModuleInputJack mij2, inTargetMj[] = new ModuleInputJack[2];
for (k = 0; k < inputGP.getConnections().size(); k++) {
conn = (Connection)inputGP.getConnections().get(k);
// Find connections from this output jack
if (conn.getSourceJack() != moj) {
continue;
}
// See if target jack has already been matched
mij2 = conn.getTargetJack();
mod2 = mij2.getMod();
h = (HashMap)alreadyMatchedList.get(mod2);
if (h.containsValue(mij2)) {
continue;
}
// FIXME this only works for one dual on each output jack
if (targetJacks.containsKey(mij2.getPrefix())) {
inTargetMj[0] = (ModuleInputJack)targetJacks.get(mij2.getPrefix());
inTargetMj[1] = mij2;
// found - now try to get mm w/corresponding conns
String ourModNames[] = new String[2];
md2 = (ModMatchData)finalMatch.get(inTargetMj[0].getMod());
ourModNames[0] = md2.getMod().getName(); // get our matching source module
md2 = (ModMatchData)finalMatch.get(inTargetMj[1].getMod());
ourModNames[1] = md2.getMod().getName(); // get our matching source module
ourMod = mdNew.getMod();
// FIXME get our source mod & jack; get our dest mods
ModuleInputJack ourMijs[];
for (k = 0; k < matrixMods.size(); k++) {
mm = (MatrixMod)matrixMods.get(k);
ourMijs = mm.createMultiDestMod(ourMod.getName(), conn.getSourceJack().getName(),
ourModNames, mij2.getPrefix());
if (ourMijs != null) {
for (int k2 = 0; k2 < ourMijs.length; k2++) {
indx = inTargetMj[k2].getMod().getInputJacks().indexOf(inTargetMj[k2]);
md2 = (ModMatchData)finalMatch.get(inTargetMj[k2].getMod());
md2.getInputJackMatches().set(indx, ourMijs[k2]);
alreadyMatched.put(ourMijs[k2], inTargetMj[k2]);
// If there's an attenuator, match it to our attenuator
mp = inTargetMj[k2].getAttenuator();
if (mp != null) {
ourMp = ourMijs[k2].getAttenuator();
indx = inTargetMj[k2].getMod().getParms().indexOf(mp);
md2.getParmMatches().set(indx, ourMp);
}
}
break;
}
}
if (k >= matrixMods.size()) {
// FIXME is this really an error, or just a warning?
// Seems it's OK if two single dest mods can be used
errorList.add("finalModuleMatch could not final match dual mod route from " + moj.getMod().getName() + " " + moj.getName() + " to " + mij2.getMod().getName() + " " + mij2.getPrefix());
}
break outer;
} else {
targetJacks.put(mij2.getPrefix(), mij2);
}
}
}