summaryrefslogtreecommitdiff
path: root/hr/michi/whitacre/haukify
blob: 857ed0442228cb9048872d82a95fc93c1ca174ae (plain)
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
#!/usr/bin/perl

# preamble
use strict;
use warnings;
# elbmaerp
#
# program logic goes here

  my %lines = ();
  my %defs = ();
  my %feds = ();
  my @skeleton = ();
  my @rhf = ();
  my @current;
{ # sub def scope
  my $DEN = 256;
  my $m = 0;
  #my @voices = map { $_ . "Melodie" } ("sopran", "alt", "tenor", "bass");
  my $n = 0;
  my $o = 0;
  my @chunks;
  my @measures;
  { # scope for reading in <dynArt.ly>
    my $lrd = 0;
    my $inda = 0;
    my $num;
    my $den;
    open(FH, "<dynArt.ly");
    while (<FH>) {
      next if (/^\s*%/ or /override/ or /partial/ or /alternative/);
      /{/; #vim bug
      s/{[^}]*}//g;
      if (/{/) {
        $lrd += 1;
        if ($lrd == 1) {
          @measures = ();
          $inda = 1 - $inda;
        }
        next;
      }
      next unless $inda;
      if (/}/) {
        $lrd -= 1;
        if ($lrd == 0) {
          if ($#measures == -1) {
          } else {
            my @bla = map $_, @measures;
            push @chunks, \@bla;
          }
          $inda = 1 - $inda;
        }
      }
      #push @measures, $DEN/$1 if /partial \D*(\d+)/;
      if (/time (\d+)\/(\d+)/) {
        $num = $1;
        $den = $2;
        while ($den < $DEN) {
          $num *= 2; $den *= 2;
        }
      }
      if (/^\s*s/) {
        if (/s(\d+\.*)\*(\d+)\s*$/) {
          my $times = ($2 and ($2 ne "")) ? $2 : 1;
          my $dur = &duration($1);
          push @measures, $dur for (1 .. $times);
        } else {
          my @code = split /\s+/;
          my $num = 0;
          for (@code) {
            $num += &duration($_);
          }
          push @measures, $num;
        }
      }
    }
  }
  my @fm = @{$chunks[0]};

  my $idx = 0;
  my $intimes = 0;
  my $conc = 0;
  
  { # scope for &conc
    my $cdur;
    my $crud;
    my $clev;
    sub conc() {
      return $_ if (/%/);
      if (/^\s*{\s*$/) {
        $clev += 1;
        return $_;
      }
      if (/^\s*}\s*$/) {
        $clev -= 1;
        if ($cdur and $crud) {
          $cdur = 0; $crud = 0;
        }
        return $_;
      }
      if ($clev == 1) {
        my @code = split /\s*/;
        my @tms = map { &duration($_) } @code;
        if ($cdur) {
          $crud += $_ for @code;
        } else {
          $cdur += $_ for @code;
        }
        if ($crud and $crud != $cdur) {
          print STDERR "parallel parts don’t match: $.; $n\n";
        }
        print STDERR "parallel parts don’t match dynArt: $.; $n\n"
        if $cdur % $measures[$idx];
        return $_;
        # recursive call of haukified w/ local counter and state vars!
      }
    }
  }
  sub min() {
    my $a = shift;
    my $b = shift;
    return $a < $b ? $a : $b;
  }
  my $par = "";
  sub par() {
    /{{/; # vim bug
    $par =~ /{([^}]*)}\s*\\\\\s*{([^}]*)}/;
    my (@c, @d);
    @c = split(/\s+/, $1);
    @d = split(/\s+/, $2);
    my ($c, $d);
    $c += &duration($_) for @c;
    $d += &duration($_) for @d;
    print STDERR "parallel mismatch in\n>>>$par\n" . ($c/$DEN) . " " . ($d/$DEN) . "\n" if ($c != $d);
    return &min($c, $d);
  }
  { # scope for &times
    my $tnum;
    my $tden;
    my $tstr;
    my $tdur = 0;
    sub times() {
      my $task = shift;
      if ($task eq "start") {
        $tnum = shift;
        $tden = shift;
        $tdur = 0;
        $tstr = "\\times $tnum\/$tden ";
        $intimes = 1 - $intimes;
      }
      if ($task eq "cont") {
        my $code = shift;
        $tstr .= $code . " ";
        $tdur += &duration($code);
      }
      if ($task eq "stop") {
        $intimes = 1 - $intimes;
        return ($tstr, $tdur*$tnum/$tden);
        #return ($tstr . "} ", $tdur*$tnum/$tden);
      }
    }
  }
  sub duration() {
    return 0 unless(/\D*(\d+)(\.*).*/);
    my ($den, $dots, $num) = ($1, $2, 1);
    $num = 0 if (/</);
    while ($dots) {
      $num *= 2;
      $den *= 2;
      $num += 1;
      chop($dots);
    }
    while ($den < $DEN) {
      $num *= 2; $den *= 2;
    }
    print STDERR "bad duration: $1\n" unless ($den == $DEN);
    return $num;
  }
  my $NUM = 0;
  # one func to rule them all
  sub haukified() {
    if (/^\s*<<\s*$/) {
      $conc += 1;
      return $_;
    }
    if (/^\s*>>\s*$/) {
      $conc -= 1;
      return $_;
    }
    return &conc($_) if ($conc > 1);
    local $, = "|";
    return $_ if (/%/ or /repeat/ or /time / or /clef/ or /partial/ or /version/);
    s/times\s*/times/g;
    #if (/times/) {
    #  $idx += 1 unless (length(@measures)<2);
    #  return $_;
    #}
    # time key -> dynArt
    chomp $_;
    #times, repeat & bla = { … }; partial!
    # -> tokenstream vorbereiten (trb -> hooks)
    # -> repeat ought to occur in dynArt as well
    s/ ~/~/g;
    /^(\s*)(.*)/;
    my $indent = $1;
    my @code = split(/\s+/, $2);
    my @parts;
    my $part = "";
    my $inpar = 0;
    for (@code) {
      if ($inpar) {
        if (/>>/) {
          $part .= "<< $par ";
          $NUM += &par($par);
          while ($NUM > $measures[$idx]) {
            $NUM -= $measures[$idx];
            $idx += 1 if($measures[$idx + 1]);
            #$measures[$idx] //= $measures[$idx-1];
          }
          $par = "";
          $inpar = 0;
        } else {
          $par .= "$_ ";
          next;
        }
      }
      if (/<</) {
        $inpar = 1;
        next;
      }
      if ($intimes) {
        /{/; #vim bug
        if (/}/) {
          my ($text, $dur) = &times("stop");
          $part .= $text;
          $NUM += $dur;
          my $tmp = $dur/$DEN;
        } else {
          &times("cont", $_);
          next;
        }
      }
      if (/times(\d+)\/(\d+)/) {
        &times("start", $1, $2);
        next;
      }
      $part .= "$_ ";
      if (/^\\(\S+)$/) {
        &align($defs{$1}, $1);
        next;
      }
      /{/; #vim bug
      $NUM += &duration($_) unless /}/;
      if ($NUM > $measures[$idx]) {
        print STDERR "error at measure " . ($idx+1) . " (local $m) in (orig) $o, (result) $n\n";
        print STDERR "$NUM out of $measures[$idx]\n";
        print STDERR "(occurred after reading $part)\n";
      }
      unless ($NUM < $measures[$idx]) {
        push @parts, $part;
        $n += 1;
        $idx += 1;
        $m += 1;
        $measures[$idx] //= $measures[$idx-1];
        $NUM = 0;
        $part = $indent;
      }
    }
    if ($part =~ /\S/) {
      push @parts, $part;
      print STDERR "underfull measure at (orig) $o, measure " . ($idx+1) . " (local $m)  ";
      print STDERR "$part?\n";
      print STDERR "$NUM out of $measures[$idx]\n";
      $idx += 1 unless ($#measures < 1);
      $measures[$idx] //= $measures[$idx-1];
    } else {
      $n -= 1;
      #$idx -= 1 unless (length(@measures)<2);
    }
    my $res = $indent;
    for (@parts) {
      s/~/ ~/g;
      $res .= $_ . "\n";
    }
    return $res;
    #return $indent . join("\n", map s/~/ ~/g, @parts);
  }
  #my $m;
  my %aligned = ();
  sub align {
    my $bla = shift;
    my @out = ();
    my $name = shift;
    my $oldm = $m;
    $m = 1; # measure innerhalb dieser def
    my $oldo = $o;
    $o = $lines{$name};
    for (@$bla) {
      if (/^\s*\\(\S+)\s*$/) {
        unless (/voice/) {
          print STDERR "$_";
          &align($defs{$1}, $1);
        }
      }
      $n += 1;
      push @out, $_ unless (/\d/);
    #print &haukified($_), "\n" if /\d/;
      $o += 1;
      push @out, &haukified($_) if /\d/;
    }
    $m = $oldm;
    $o = $oldo;
    if ($aligned{$name}) {
      my $match = 1;
      for (0 .. &min(@out, @{$feds{$name}})) {
        $match = 0 if $feds{$name}->[$_] != $out[$_];
      }
      print STDERR "inconsistent use of $name …" unless $match;
    } else {
      @{$feds{$name}} = map $_, @out;
    }
  }
  my $i = 0;
  sub align_voice() {
    #print STDERR "dynart exhausted before stimmen done!\n" unless ($#chunks > -1);
    @measures = ($#chunks == -1) ? @fm : @{pop @chunks};
    $idx = 0;
    $NUM = 0;
    &align(@_);
    $i += 1;
  }
  sub check() {
    $o += @{$skeleton[$i]};
    for (keys %defs) {
      &align_voice($defs{$_}, $_) if /Melodie/;
    }
    for (keys %defs) {
      $feds{$_} //= $defs{$_};
    }
  }
  sub flush() {
    for (0 .. $#rhf) {
      print for @{$skeleton[$_]};
      print for @{$feds{$rhf[$_]}};
    }
    print for @{$skeleton[$#rhf+1]};
  }
}

my $inddef = "";
my $def = 0;
while (<>) {
  if ($def) {
    /{/; #vim bug
    if (/^$inddef}\s*$/) {
      $def = 0;
      my @bla = map $_, @current;
      $defs{$rhf[$#rhf]} = \@bla;
      @current = ();
    }
  }
  if (/^(\s*)(\S*) = \\relative/) {
    push @current, $_;
    my @bla = map $_, @current;
    push @skeleton, \@bla;
    @current = ();
    $inddef = $1;
    push @rhf, $2;
    $lines{$2} = $.;
    $def = 1; #TODO: def zoix -> %def, später einbinden
    # ds chunk -> begin orig, begin hauked
    # TODO: conc innerhalb des taktes -> zwei gobble-ups, vgl, hinzufügen
    next;
  }
  push @current, $_;
}

{ # using the defs
  my @bla = map $_, @current;
  push @skeleton, \@bla;
  &check();
  &flush();
}

# some settings # vim: et