forked from eberdahl/SaM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Synth-A-Modeler
executable file
·830 lines (617 loc) · 26.1 KB
/
Synth-A-Modeler
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
#!/usr/bin/perl
#
# Edgar Berdahl, Jan 21 2012
# Updated February 2012
#
# First argument is input filename for MDX file
# Second argument is output filename for DSP file
#
# For example: perl5.10.0 Synth-A-Modeler examplelinks.mdx examplelinks.dsp
#
# ALL NUMBERS IN MDX FILES SHOULD BE FLOATING-POINT NUMBERS (NOT INTEGERS)
# (c) Edgar Berdahl, 2012
# Audio Communication Group
# Technical University of Berlin
#
# -------------------------------------------------------------------
#
# The above copyright notice including the copyright holders and this
# permission notice shall be included in all copies, substantial
# portions, or derivatives of the Software and Software portions.
#
# The end-user documentation included with the redistribution, if any,
# must include the following acknowledgment: "This software was
# developed at least in part using Synth-A-Modeler, which is a modular
# and open-source environment for designing physical models (see
# https://github.com/eberdahl/SaM)", in the same place and form as other
# third-party acknowledgments. In addition, this acknowledgment must
# appear in the software itself in the same form and location as other
# such third-party acknowledgments.
#
# Alternatively, if software is generated using Synth-A-Modeler, then
# the end-user documentation included with the generated software, if
# any, must include the following statement: "This software was generated
# using Synth-A-Modeler, which is a modular and open-source environment
# for designing physical models (see https://github.com/eberdahl/SaM)",
# in the same place and form as other third-party acknowledgments. In
# addition, this acknowledgment must appear prominently in any software
# generated by Synth-A-Modeler in the same form and location as other
# such third-party acknowledgments, and in such a manner that users
# will be aware that the software was generated by Synth-A-Modeler.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation except subject to the additional
# attribution requirements described beneath the copyright notice above.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the modified GNU General Public
# License along with this program.
# Here we include the regular expression definitions necessary for parsing model specification files.
require 'SAM-reg-ex';
use v5.10;
use warnings;
no warnings 'once'; # since some variables are used only once here but are e.g. defined in SAM-reg-ex
$filename = $ARGV[0]; # e.g. "examplelinks.mdl";
$filenameout = $ARGV[1]; # e.g. "examplelinks.dsp";
my $indx = 0; # index variable used over and over again
# Open file for storing verbose information on compilation result
open my $fh_compilation, '>', "SAM-debug-compilation.txt"
or die "Error: Cannot open SAM-debug-compilation.txt for storing comilation result!";
# Insert timestamp and input file name into SAM-debug-compilation.txt
my ($sec,$min,$hour,$day,$month,$yr19,@rest) = localtime(time);
my $date = ($yr19+1900)."-".sprintf("%02d",++$month)."-".sprintf("%02d",$day);
my $time = sprintf("%02d",$hour).":".sprintf("%02d",$min).":".sprintf("%02d",$sec);
say $fh_compilation $date." ".$time;
say $fh_compilation "Inputfile: ".$filename;
say $fh_compilation "----------";
# Handle when the compiler fails
sub diegracefully($) {
my $error_line = "The Synth-A-Modeler Perl script returned an ERROR while working on " . $filenameout . "! Verbose compilation details follow:\n";
say $error_line;
say $_[0];
unlink($filenameout); # Delete output DSP file to avoid confusing the make build system
close $fh_compilation;
open my $fh_die, '<', "SAM-debug-compilation.txt" or die "FUCK";
#open $filehandle, '<', $filename
while (<$fh_die>) {
chomp;
print "$_\n";
}
close $fh_die;
say "\n";
die;
}
# Open DSP file for output and start writing into it
open my $fh_out, '>', $filenameout
or diegracefully "Cannot write '$filenameout': $!\n";
say $fh_out "// This DSP file has been generated by the Synth-A-Modeler compiler.";
say $fh_out "import(\"physicalmodeling.lib\");\n";
# Copy over all verbatim lines of Faust code, in the same order
open $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
while (<$filehandle>)
{
chomp;
$theline = $_;
if ($theline =~ qr/$faustCodeLine/) {
say $fh_compilation "Copying Faust line: " . $1;
say $fh_out $1;
}
}
# Usage: $elementParams = replace_param( $n, $newParam, $elementParams );
#
# replace_param receives a comma-separated list of parameters (which can be either labels
# or numerical values), and it replaces the $nth item in the list with $newParam, which
# can either be a label or a numerical value.
my $myLabelOrValue = qr/([-\+\.a-zA-Z\d]+)/;
my $myFullLabelOrValue = qr/(\A\s*[-\+\.a-zA-Z\d]+\s*$)/;
my $myOptionalListOfLabelsOrValues = qr/([-\+\.,a-zA-Z\d]*)/;
my $separateParams = qr/${myLabelOrValue}\s*,\s*${myOptionalListOfLabelsOrValues}\s*/;
sub replace_param
{
$indexToReplace = $_[0];
$insertionValue = $_[1];
$listOfParams = $_[2];
#say $fh_compilation "BOO" . $listOfParams . " : " . $indexToReplace . " : " . $insertionValue;
my $inFrontOf = "";
my $current = "";
my $behind = $listOfParams;
do {
if ($inFrontOf eq "") { $inFrontOf = $current;
} else { $inFrontOf = $inFrontOf . "," . $current; }
if ($behind =~ qr/$myFullLabelOrValue/) { # only one element left
#say $fh_compilation "FULLLABELORVALUE: " . $1;
$current = $1;
$behind = "";
} else {
if ($behind =~ qr/$separateParams/) {
#say $fh_compilation "SEPARATING: " . $1 . ", 2222: " . $2;
$current = $1;
$behind = $2;
} else {
diegracefully "";
}
}
$indexToReplace--;
#say $fh_compilation "INFRONTOF: " . $inFrontOf . " BEHIND: " . $behind;
} while ($indexToReplace > 0);
if (($inFrontOf eq "") && ($behind eq "")) { return $insertionValue; }
if ($inFrontOf eq "") { return $insertionValue . "," . $behind; }
if ($behind eq "") { return $inFrontOf . "," . $insertionValue; }
return $inFrontOf . "," . $insertionValue . "," . $behind;
}
# Then read through the MDL file to get the number of elements
# while constructing the vector of inputs and outputs to the main function.
#
# Separate iterations are carried out for ports and audioouts in order to ensure they are included last ...
my $vertexNum = 0; # number of mass-like objects (including ports)
my $linkNum = 0; # number of link-like objects
my $portNum = 0; # number of ports
my $waveguideNum = 0;
my $junctionNum = 0;
my $inputs = "";
my $outputs = "";
my $audioOutputs = "";
my $audioOutNum = 0;
my $adjustInputs = "";
my $adjustNum = 0;
# First we handle the objects that are not ports or audioouts (i.e. links, masses that aren't ports, waveguides, junctions)
open $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
while (<$filehandle>)
{
chomp;
$theline = $_;
if ($theline =~ qr/$linkline/) {
#say $fh_compilation "Read this link: " . $1 . " " . $2 . " " . $3 . " " . $4 . " " . $5 . " " . $6;
$linkNum++;
}
if ($theline =~ qr/$vertexline/) {
#say $fh_compilation "Read this vertex: " . $1 . " " . $2 . " " . $3 . " " . $4;
#$vertexNum++;
if ($1 ne "port") { # if it is NOT a port
$vertexNum++;
if (($vertexNum+$waveguideNum+$junctionNum) > 1) {
$inputs = $inputs . ",";
$outputs = $outputs . ",";
}
$inputs = $inputs . $3 . "p";
$outputs = $outputs . $3;
}
}
if ($theline =~ qr/$waveguideline/) {
$waveguideNum++;
if (($vertexNum+$waveguideNum+$junctionNum) > 1) {
$inputs = $inputs . ",";
$outputs = $outputs . ",";
}
$inputs = $inputs . $3 . "Lp,";
$inputs = $inputs . $3 . "Rp";
$outputs = $outputs . $3 . "L,";
$outputs = $outputs . $3 . "R";
}
if ($theline =~ qr/$junctionline/) {
$junctionNum++;
if (($vertexNum+$waveguideNum+$junctionNum) > 1) {
$inputs = $inputs . ",";
$outputs = $outputs . ",";
}
$inputs = $inputs . $2 . "p";
$outputs = $outputs . $2;
}
}
close $filehandle;
# Then we handle only the ports
open $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
while (<$filehandle>)
{
chomp;
$theline = $_;
if ($theline =~ qr/$vertexline/) {
#say $fh_compilation "Read this vertex: " . $1 . " " . $2 . " " . $3 . " " . $4;
if ($1 eq "port") { # if it is a port in particular
$portNum++; $vertexNum++;
if (($vertexNum+$waveguideNum) > 1) {
$inputs = $inputs . ",";
$outputs = $outputs . ",";
}
$inputs = $inputs . $3 . "p";
$outputs = $outputs . $3;
}
}
}
close $filehandle;
# Finally we handle the audioouts
open $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
while (<$filehandle>)
{
chomp;
$theline = $_;
if ($theline =~ qr/$audiooutline/) {
$audioOutNum++;
$audioOutputs = $audioOutputs . "," . $1;
# say $fh_compilation "Read AUDIOOUT: " . $theline;
}
#if ($theline =~ qr/$adjustline/) {
#say $fh_compilation "ADJUST 1: " . $1 . ", 2: " . $2 . ", 3: " . $3 . ". 4: " . $4;
#my $guiLine = $1 . " = " . $4 . ";";
#say $fh_out $guiLine;
#}
}
close $filehandle;
$outputs = $outputs . $audioOutputs;
my $feedbackNum = $vertexNum - $portNum + 2*$waveguideNum + $junctionNum;
my $theline = "There are " . $vertexNum . " vertices and " . $linkNum . " link(s) and " . $waveguideNum . " waveguides and " . $portNum . " port(s) and " . ($vertexNum - $portNum) . " non-port mass-like objects.";
say $fh_compilation $theline;
#$theline = "So feedbackNum=" . $feedbackNum . "\n";
#say $fh_compilation $theline;
# Now write the beginning of the big function block:
my $functionDef = "\n\nbigBlock(" . $inputs . ") = (" . $outputs . ") with {";
say $fh_out $functionDef;
say $fh_out "\t// For waveguide terminations:";
open $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
my $termEqn;
while (<$filehandle>)
{
chomp;
$theline = $_;
if ($theline =~ qr/$terminationline/) {
say $fh_compilation "Read TERMINATION: " . $theline;
say $fh_compilation "Read for: " . $1 . " and " . $2;
my $faustTermCode = $1; my $termLabel = $2;
# Find the associated waveguide to determine whether the output from the termination goes into
# the leftward-going waveguide segment or the rightward-going waveguide segment
open $fh_inner, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
$waveguideMatches = 0;
while (<$fh_inner>) {
chomp;
$theline_inner = $_;
if ($theline_inner =~ qr/$waveguideline/) {
say $fh_compilation "Read WAVEGUIDE: " . $theline_inner;
say $fh_compilation "Read for: " . $1 . " and " . $2 . " and3=" . $3 . " and4=" . $4 . " and5=" . $5;
my $faustWaveguideCode = $2; my $waveguideName = $3; my $i4 = $4; my $i5 = $5; # Save regex
say $fh_compilation "termlabel=" . $termLabel . ", i4=" . $i4 . ", $i5=" . $i5;
if ($termLabel =~ m/(^\s?$i4\s?$)/) {
say $fh_compilation "TERM is on the left!";
$termEqn1 = "\t" . $waveguideName . "R = " . $termLabel . " : " . $faustWaveguideCode . ";";
$termEqn2 = "\t" . $termLabel . " = " . $waveguideName . "Lp : " . $faustTermCode . ";";
$waveguideMatches = $waveguideMatches + 1;
}
if ($termLabel =~ m/(^\s?$i5\s?$)/) {
say $fh_compilation "TERM is on the right!";
$termEqn1 = "\t" . $waveguideName . "L = " . $termLabel . " : " . $faustWaveguideCode . ";";
$termEqn2 = "\t" . $termLabel . " = " . $waveguideName . "Rp : " . $faustTermCode . ";";
$waveguideMatches = $waveguideMatches + 1;
}
}
}
close $fh_inner;
if ($waveguideMatches == 1) {
say $fh_out $termEqn1;
say $fh_out $termEqn2;
say $fh_out "";
} else {
say $fh_compilation "ERROR: Wrong number " . $waveguideMatches . " of waveguides connected to termination " . $termLabel; diegracefully "";
}
}
}
close $filehandle;
# Handle junctions
say $fh_out "\t// For junctions:";
open $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
my $junctionEqn;
my %linkstoremove;
my %junctionmasslikeobjects;
while (<$filehandle>)
{
chomp;
$theline = $_;
#if ($theline =~ qr/$junctioninteractionline/) {
if ($theline =~ qr/$junctionline/) {
say $fh_compilation "Read JUNCTION: " . $theline;
say $fh_compilation "Read for: " . $1 . " and " . $2;
my $faustJunctionName = "junction";
my $faustJunctionParams = $1;
my $junctionIdentifier = $2;
# First check if there is an associated link-like object
my $linkIdentifier = ""; # The junction is NOT connected to a link-like object unless we find one
my $linkedMassLikeObject = ""; # Same for the mass that could be connected if a link is found
$numConnectingLinks = 0;
open my $fh_inner, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
while (<$fh_inner>) {
chomp;
$theline_inner = $_;
if ($theline_inner =~ qr/$linkline/) {
my $i1 = $1; my $i2 = $2; my $i3 = $3;
say $fh_compilation "Found link 1=" . $1 . ", 2=" . $2 . ", 3=" . $3 . ", 4=" . $4 . ", 5=" . $5;
if ($junctionIdentifier eq $4) {
$faustJunctionName = $faustJunctionName . $1;
$faustJunctionParams = $faustJunctionParams . ", " . $2;
#$outline = "junction" . $1 . "(" . $junctionHeight . ", " . $2 . "), " . $junctionIdentifier . ", " . $5 . ",();";
$numLinks--;
$linkstoremove{$3} = $junctionIdentifier; # add to list of link-like objects not to be introduced into DSP file
$junctionmasslikeobjects{$junctionIdentifier} = $5; # add to hash of junction names to masses they're connected to
$linkIdentifier = $3;
$linkedMassLikeObject = $5;
#say $fh_out $outline;
$numConnectingLinks++;
} elsif ($junctionIdentifier eq $5) {
$faustJunctionName = $faustJunctionName . $1;
$faustJunctionName = $faustJunctionName . "Underneath";
$faustJunctionParams = $faustJunctionParams . ", " . $2;
#$outline = "junction" . $1 . "Underneath" . "(" . $junctionHeight . ", " . $2 . "), " . $junctionIdentifier . ", " . $4 . ",();";
$numLinks--;
$linkstoremove{$3} = $junctionIdentifier; # add to list of link-like objects not to be introduced into DSP file
$junctionmasslikeobjects{$junctionIdentifier} = $4; # add to hash of junction names to masses they're connected to
#say $fh_compilation "3=" . $3 . ", 4=" . $4 . ", 5=" . $5;
$linkIdentifier = $3;
$linkedMassLikeObject = $4;
#say $fh_out $outline;
$numConnectingLinks++;
}
}
}
close $fh_inner;
if ($numConnectingLinks > 1) {
say $fh_compilation "ERROR: Currently only one link-like object can be connected to a junction.";
say $fh_compilation "ERROR: However, " . $numConnectingLinks . ">1 link-like objects were connected to the junction " . $junctionIdentifier . ".";
diegracefully "";
} elsif ($numConnectingLinks == 0) { # Or copy the junction line directly if there are no links associated with it
#say $fh_compilation "numConnectingLinks=" . $numConnectingLinks;
say $fh_compilation "NO LINK-LIKE OBJECTS WERE CONNECTED TO THIS JUNTION!";
#say $fh_out $theline;
}
# Find the associated waveguides to form the surrounding equations
open $fh_inner, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
$waveguideMatches = 0;
$sumImpedances = "0.0";
$sumWaveForcesIn = "0.0";
while (<$fh_inner>) {
chomp;
$theline_inner = $_;
if ($theline_inner =~ qr/$waveguideline/) {
say $fh_compilation "Read WAVEGUIDE: " . $theline_inner;
say $fh_compilation "Read for: " . $1 . " and " . $2 . " and3=" . $3 . " and4=" . $4 . " and5=" . $5;
$waveguideImpedance = $1; my $faustWaveguideCode = $2; my $waveguideName = $3; my $i4 = $4; my $i5 = $5; # Save regex
say $fh_compilation "junctionIdentifier=" . $junctionIdentifier . ", i4=" . $i4 . ", $i5=" . $i5;
if ($junctionIdentifier =~ m/(^\s?$i4\s?$)/) {
say $fh_compilation "JUNCTION is on the left!";
$sumImpedances = $sumImpedances . "+" . $waveguideImpedance;
$sumWaveForcesIn = $sumWaveForcesIn . "+" . $waveguideName . "Lp*" . $waveguideImpedance;
$junctionEqn1 = "\t" . $waveguideName . "R = " . $junctionIdentifier . "TO" . $waveguideName . " : " . $faustWaveguideCode . ";";
$junctionEqn2 = "\t" . $junctionIdentifier . "TO" . $waveguideName . " = " . $junctionIdentifier . "Outputs:(_,!)-" . $waveguideName . "Lp;";
say $fh_out $junctionEqn1;
say $fh_out $junctionEqn2;
$waveguideMatches = $waveguideMatches + 1;
}
if ($junctionIdentifier =~ m/(^\s?$i5\s?$)/) {
say $fh_compilation "JUNCTION is on the right!";
$sumImpedances = $sumImpedances . "+" . $waveguideImpedance;
$sumWaveForcesIn = $sumWaveForcesIn . "+" . $waveguideName . "Rp*" . $waveguideImpedance;
$junctionEqn1 = "\t" . $waveguideName . "L = " . $junctionIdentifier . "TO" . $waveguideName . " : " . $faustWaveguideCode . ";";
$junctionEqn2 = "\t" . $junctionIdentifier . "TO" . $waveguideName . " = " . $junctionIdentifier . "Outputs:(_,!)-" . $waveguideName . "Rp;";
say $fh_out $junctionEqn1;
say $fh_out $junctionEqn2;
$waveguideMatches = $waveguideMatches + 1;
}
}
}
close $fh_inner;
if ($linkIdentifier eq "") {
# If there are no link-like objects, just use a linear link for now with zero stiffness, damping, and offset.
$junctionEqn1 = "\t" . $junctionIdentifier . "Outputs = (0.0, " . $sumWaveForcesIn . ", " . $sumImpedances . ") : " . "junctionlink" . "(0.0, 0.0, 0.0, 0.0);";
} else {
$junctionEqn1 = "\t" . $junctionIdentifier . "Outputs = (" . $linkedMassLikeObject . "p, " . $sumWaveForcesIn . ", " . $sumImpedances . ") : " . $faustJunctionName . "(" . $faustJunctionParams . ");";
}
$junctionEqn2 = "\t" . $junctionIdentifier . "=" . $junctionIdentifier . "Outputs:(_,!);"; # for listening
say $fh_out $junctionEqn1;
say $fh_out $junctionEqn2;
say $fh_out "\n";
# Actually I think it's ok to have zero waveguides attached to a junction -- it's just silly!
if ($waveguideMatches == 0) {
say $fh_compilation "ERROR: Wrong number " . $waveguideMatches . " of waveguides connected to junction " . $junctionIdentifier; diegracefully "";
}
}
}
close $filehandle;
#To display the keys stored in the hash representing links that should not be copied over into the MDX file:
say $fh_compilation "Printing the list of links of remove:";
foreach (sort keys %linkstoremove) {
say $fh_compilation "$_ : $linkstoremove{$_}";
}
say $fh_compilation "Printing the list of links of remove:";
foreach (sort keys %junctionmasslikeobjects) {
say $fh_compilation "$_ : $junctionmasslikeobjects{$_}";
}
# Then for each mass-like object
# - provide a link between its index to xN, xNo (where N is an integer) and its label
say $fh_out "\t// For mass-like objects:";
open $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
my $vertexLine;
my $vertexInd = 0;
my $portInd = 0;
my $massLabel;
while (<$filehandle>)
{
chomp;
$theline = $_;
if ($theline =~ qr/$vertexline/) {
# say $fh_compilation "Read a vertex: $theline\n";
$typeOfMass = $1;
$massName = $3;
if ($1 eq "port") { # if it is an external mass
$vertexLine = "\t" . $3 . " = (0.0";
} else { # if it is a mass to be simulated
$vertexLine = "\t" . $3 . " = (0.0";
}
open $fh_inner, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
while (<$fh_inner>) {
chomp;
$theline_inner = $_;
# Add forces from links only if they should not be removed by junctions
if ($theline_inner =~ qr/$linkline/) {
#say $fh_compilation "Read LINK for: " . $1 . " and " . $2 . "and3=" . $3;
if (!(exists $linkstoremove{ $3 })) {
if ($4 eq $massName) {
$vertexLine = $vertexLine . "-" . $3;
}
if ($5 eq $massName) {
$vertexLine = $vertexLine . "+" . $3;
}
}
}
# Add any forces coming out of junctions
if ($theline_inner =~ qr/$junctionline/) {
say $fh_compilation "Read JUNCTION: " . $theline;
say $fh_compilation "Read for: " . $1 . " and " . $2;
if ($junctionmasslikeobjects{$2} eq $massName) {
$vertexLine = $vertexLine . "+(" . $2 . "Outputs:(!,_))";
}
}
}
close $fh_inner;
my $elementParams = $2; # save captured Faust object parameters for element
my $listOfLabels = $4; # save list of labels for element
# Change output line only if we find an adjust specifying to do so
# Pass through the adjust's to see if any of the Faust object's parameters should be real-time adjustable
open $fh_inner, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
while (<$fh_inner>) {
chomp;
$theline_inner = $_;
if ($theline_inner =~ qr/$adjustline/) {
my $i1 = $1; my $i2 = $2; my $i3 = $3; # Save adjust regex
say $fh_compilation "Does " . $i2 . " matchz " . $listOfLabels . "?";
say $fh_compilation "MASS Adjust 1: " . $1 . ", 2: " . $2 . ", 3: " . $3 . " for " . $elementParams;
# Check if the adjust's label is included among the comma-separated list of labels
if ($listOfLabels =~ m/(^\s?$i2\s?$)|(^\s?$i2\s?,.*$)|(^.*,\s?$i2\s?$)|(^.*,\s?$i2\s?,.*$)/) {
say $fh_compilation "YES!";
say $fh_compilation "CALLING REPLACEPARAMS " . $elementParams . "," . $i3 . "->" . $i1;
$elementParams = replace_param( $i3, $i1, $elementParams );
say $fh_compilation "RETURN VALUE: " . $elementParams;
}
}
}
close $fh_inner;
if ($1 eq "port") { # if it is an external port
$vertexLine = $vertexLine . ");";
} elsif ($1 eq "resonators") { # if it describes some resonators
$vertexLine = $vertexLine . ") : " . $typeOfMass . "((" . $elementParams . "));"; # need extra parentheses to represent variable number of parameters using parallel composition
} else { # if it is any other mass-like object to be simulated
$vertexLine = $vertexLine . ") : " . $typeOfMass . "(" . $elementParams . ");";
}
say $fh_out $vertexLine;
}
}
close $filehandle;
say $fh_out "";
# We parse the link-like objects and format them such that they can be inserted into the output .DSP file
# LINKS HAVE TO BE HANDLED AFTER JUNCTIONS BECAUSE OF LINKS THAT CAN BE ABSORBED INTO JUNCTIONS
say $fh_out "\t// For link-like objects:";
open $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
my $linkLine;
while (<$filehandle>)
{
chomp;
$theline = $_;
#say $fh_compilation $theline;
if ($theline =~ qr/$linkline/) { # if we find a line specifying a link
#say $fh_compilation "FOUNDALINK!!";
my $o1 = $1; my $o2 = $2; my $o3 = $3; my $o4 = $4; my $o5 = $5; my $o6 = $6; # save captures
# Only continue processing the link if it has NOT been removed by being absorbed into a junction
if (!(exists $linkstoremove{ $3 })) {
# Change output line only if we find an adjust specifying to do so
# Pass through the adjust's to see if any of the Faust object's parameters should be real-time adjustable
open my $fh_inner, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
while (<$fh_inner>) {
chomp;
$theline_inner = $_;
if ($theline_inner =~ qr/$adjustline/) {
my $i1 = $1; my $i2 = $2; my $i3 = $3;
say $fh_compilation "Does " . $i2 . " match " . $o6 . "?";
#say $fh_compilation "LINK Adjust 1: " . $1 . ", 2: " . $2 . ", 3: " . $3;
# Check if the adjust's label is included in the current list of comma-separated labels
if ($o6 =~ m/(^\s?$i2\s?$)|(^\s?$i2\s?,.*$)|(^.*,\s?$i2\s?$)|(^.*,\s?$i2\s?,.*$)/) {
say $fh_compilation "YES!";
#$o2 = $o2 . "," . $i3 . "->" . $i1;
say $fh_compilation "CALLING REPLACE_PARAM " . $o2 . "," . $i3 . "->" . $i1;
$o2 = replace_param( $i3, $i1, $o2 );
say $fh_compilation "RETURN VALUE: " . $o2;
}
}
}
close $fh_inner;
# Set default output line
$linkLine = "\t" . $o3 . " = (" . $o4 . "p - " . $o5 . "p) : " . $o1 . "(" . $o2 . ");";
say $fh_out $linkLine;
}
}
}
say $fh_out "";
close $filehandle;
# Handle any additional specified audio outputs
say $fh_out "\t// For audio outputs:";
open my $filehandle, '<', $filename
or diegracefully "Cannot read '$filename': $!\n";
my $audiooutEqn;
#my $audioParams;
while (<$filehandle>)
{
chomp;
$theline = $_;
if ($theline =~ qr/$audiooutline/) {
say $fh_compilation "Read AUDIOOUT: " . $theline;
say $fh_compilation "Read for: " . $1 . " and " . $2;
$audiooutEqn = "\t" . $1 . " = " . $2 . ";";
#$audioParams = $2;
#say $fh_compilation "AUDIOPARAMS: " . $audioParams;
#while ($audioParams =~ qr/$audiooutParseSum/) {
# say $fh_compilation "We have 1: " . $1 . ", 2: " . $2 . ", 3: " . $3;
# $audiooutEqn = $audiooutEqn . "+" . $1 . "*(" . $2 . ")";
# $audioParams = $3;
# #say $fh_compilation "Now parsing: \"" . $audioParams . "\"";
#}
#$audiooutEqn = $audiooutEqn . ";";
say $fh_out $audiooutEqn;
}
}
close $filehandle;
say $fh_out "};\n";
# Finally we generate text for selecting the appropriate feedbacks
# and selecting the outputs (opposite of the feedbacks)
my $feedbackSelector = "";
my $outputsSelector = "";
for $indx (1 .. ($feedbackNum)) {
$feedbackSelector = $feedbackSelector . "_";
$outputsSelector = $outputsSelector . "!";
if ($indx < $feedbackNum) {
$feedbackSelector = $feedbackSelector . ",";
$outputsSelector = $outputsSelector . ",";
}
}
for $indx (1 .. ($portNum+$audioOutNum)) {
if (($indx > 1) || ($feedbackNum > 0)) {
$feedbackSelector = $feedbackSelector . ",";
$outputsSelector = $outputsSelector . ",";
}
$feedbackSelector = $feedbackSelector . "!";
$outputsSelector = $outputsSelector . "_";
}
say $fh_out "process = (bigBlock)~(" . $feedbackSelector . "):(" . $outputsSelector . ");";
close $fh_out;