Handle i notation (whole measure repeated)
[pae2xml.git] / pae2xml.pl
blob7b549968471a7826c229c3f817928cdda840664d
1 #!/usr/bin/perl
4 # Copyright (C) 2003 Rainer Typke
5 #pae2xml is licensed under the terms of the GNU General Public License Version
6 #2 as published by the <a href="http://www.fsf.org/" target="_top">Free Software Foundation</a>.
7 #This gives you legal permission to copy, distribute and/or modify <em>pae2xml</em> under
8 #certain conditions. Read
9 #the <a href="http://www.gnu.org/copyleft/gpl.html" target="_top">online version of the license</a>
10 #for more details. pae2xml is provided AS IS with NO WARRANTY OF ANY KIND,
11 #INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
15 $divisions = 960;
16 $old_duration = $divisions;
17 $old_octave = 4;
19 ($mday, $mon, $year) = (localtime()) [3..5];
20 $encoding_date = sprintf("%4d-%02d-%02d", $year + 1900, $mon+1, $mday);
22 $TIE = 0;
24 foreach $a (@ARGV) {
25 $p = read_file($a);
26 $toprint = "";
27 $p =~ s/\s*\=\=+\s*(.*?)\s*\=\=+\s*/$1/sg;
28 $p =~ s/\s*included.*?-------------*\s*(.*?)\s*/$1/s;
30 ($q, $r) = ($p, $p);
31 if ($q !~ /^.*1\.1\.1.*$/gsx && $r =~ /^.*plain.*$/gsx) {
32 print_error("$a contains 'plain', but not 1.1.1!\n");
33 } else {
34 if ($p =~ /^\s*([^\n]+)\n(.*?)\n((\d+\.\d+\.\d.*?plain.*?\n)+)(.*?)\n?([^\n]+)\n([^\n]+)\s*$/gs) {
35 my ($comp, $title, $incipits, $sonst, $libsig, $rismsig) = ($1, $2, $3, $5, $6, $7);
37 $toprint .= "
38 COMPOSER: $comp
39 TITLE: $title
40 INCIPIT(S): $incipits
41 OTHER INFO: $sonst
42 LIB. SIGN.: $libsig
43 RISM SIGN.: $rismsig\n\n";
44 parse_incipits($incipits, $comp, $title, $sonst, $libsig, $rismsig);
46 else {
47 if (index($p,"plain&easy") > -1) {
48 print_error("Ignoring the following text:\n\n\n$p\n\n\n");
56 sub parse_incipits {
57 my ($incipits, $comp, $title, $sonst, $libsig, $rismsig) = @_;
59 $toprint .= "parsing: $incipits\n";
61 while ($incipits =~ /^(\d+\.\d+\..+?)(\d+\.\d+\..*)$/gs) {
62 my ($inc1) = $1;
63 $incipits = $2;
64 parse_pe($inc1, $comp, $title, $sonst, $libsig, $rismsig);
66 parse_pe($incipits, $comp, $title, $sonst, $libsig, $rismsig);
69 sub parse_pe {
70 my ($pe, $comp, $title, $sonst, $libsig, $rismsig) = @_;
72 $pe =~ s/@ü/@0ü/gs; # make missing time signature explicit
73 while ($pe =~ s/([^\-])(\d+)(\'|\,)(A|B|C|D|E|F|G)/$1$3$2$4/gs) {}; # octave first, then duration. Truly global.
75 if ($pe =~ /^\s*(\d+\.\d+\.\d)(\.|:)\s*(.*?)\nplain&easy:\s*(%([\w\-\d]+))?(@([\d\w\/]+))?\s*&?\s*(\$([^ü]+))(.*)$/gs) {
76 my ($inr, $instr, $clef, $timesig, $keysig, $rest) = ($1, $3, $5, $7, $9, $10);
78 my $filename="$rismsig-$inr.xml";
79 $filename =~ s/RISM\s*A\/II\s*:?\s*//gs;
80 print "Writing $filename...\n";
82 open(OUT, ">$filename");
84 if ($clef =~ /^(\w)\-(\d)$/) {
85 ($clefsign, $clefline) = ($1, $2);
86 } else {
87 ($clefsign, $clefline) = ("G", 2);
90 $timesig = timesignature($timesig);
92 my %fif=("", 0, "xF", 1, "xFC", 2, "xFCG",3, "xFCGD",4, "xFCGDA",5, "xFCGDAE",6, "xFCGDAEB",7, "bB",-1, "bBE",-2, "bBEA",-3, "bBEAD",-4, "bBEADG",-5, "bBEADGC",-6, "bBEADGCF",-7);
93 $keysig =~ s/(\s+)|&//gs; # it is unclear what the & means, so we'll ignore it for now.
94 $keysig =~ s/\[|\]//gs; # IGNORING brackets around a key sig.
95 $fifths = $fif{$keysig};
96 if ($fifths eq "") { $fifths = "0";
97 print_error("Strange key signature '$keysig'.\n");}
99 foreach $_ ($rismsig,$title,$inr,$instr,$comp,$encoding_date,$libsig,$sonst)
104 print OUT '<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
105 <!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 0.6 Partwise//EN" "file:/c:/Program Files/MusicXML/partwise.dtd">
106 <score-partwise>
107 <work>
108 <work-number>'.$rismsig.'</work-number>
109 <work-title>'.$title.'</work-title>
110 </work>
111 <movement-number>'.$inr.'</movement-number>
112 <movement-title>'.$instr.'</movement-title>
113 <identification>
114 <creator type="composer">'.$comp.'</creator>
115 <encoding>
116 <software>pae2xml by R. Typke</software>
117 <encoding-date>'.$encoding_date.'</encoding-date>
118 </encoding>
119 <source>'.$libsig.'</source>
120 </identification>
121 <part-list>
122 <score-part id="P1">
123 <part-name>'.$sonst.'</part-name>
124 </score-part>
125 </part-list>
126 <part id="P1">
127 <measure number="1">
128 <attributes>
129 <divisions>'.$divisions.'</divisions>
130 <key>
131 <fifths>'.$fifths.'</fifths>
132 </key>
133 '.$timesig.'
134 <clef>
135 <sign>'.$clefsign.'</sign>
136 <line>'.$clefline.'</line>
137 </clef>
138 </attributes>
142 $toprint .= "
143 INCIPIT NO.: $inr
144 INSTR.: $instr
145 CLEF: $clef
146 KEY SIG.: $keysig
147 TIME SIG.: $timesig
148 REST: $rest\n";
149 parse_notes($rest, $keysig);
151 else { print_error("could not parse $pe\n"); }
152 print OUT " </part>
153 </score-partwise>\n";
154 close OUT;
158 sub parse_notes {
159 my ($notes, $keysig) = @_;
160 my $qq = 0; # in group of cue notes
162 my $meas = 2; # measure number
163 my $mopen = 1; # measure tag still open
165 if ($notes =~ /^\s*(.*?)\s*$/) {
166 $notes = $1;
169 $notes =~ s/!([^!]*)!/$1$1/gs; # write out repetitions
170 $notes =~ s/\{([^\}]*)\}/$1/gs; # ignore beamings
171 while ( $notes =~ s/(:?\/+:?|^)([^\/:]*)(:?\/+:?)i(:?\/+:?)/$1$2$3$2$4/gs) {}; # replace whole-measure repeats (i notation)
173 $notes =~ s/(\d+)\(([^;]+\))/\($1$2/gs; # pull note lengths into fermatas or triplets
174 $notes =~ s/(xx|x|bb|b|n)\(/\($1/gs; # pull accidentals into tuplets or fermatas:
175 $notes =~ s/(\d+)(xx|x|bb|b|n)(A|B|C|D|E|F|G)/$2$1$3/gs; # accidentals first, then duration
177 # $notes =~ s/x\(/\(x/gs; # pull accidentals into tuplets or fermatas
178 # $notes =~ s/bb\(/\(bb/gs; # pull accidentals into tuplets or fermatas
179 # $notes =~ s/b\(/\(b/gs; # pull accidentals into tuplets or fermatas
180 # $notes =~ s/n\(/\(n/gs; # pull accidentals into tuplets or fermatas
181 # $notes =~ s/(\'+|\,+)\(/\($1/g; # pull octave marks into tuplets or fermatas
183 $notes =~ s/(\.|\d|\,|\')qq/qq$1/gs; # pull beginning mark of group of grace notes in front of corresponding notes
184 $notes =~ s/(xx|x|bb|b|n)qq/qq$1/gs; # qq first, then parts of notes
186 $notes =~ s/\=(\d)/$1/gs; # replace multibar rests #n with just n
188 while ($notes ne "") {
189 if ($notes =~ /^(\'+|\,+)(.*)$/) {
190 ($oct, $notes) = ($1, $2);
191 octave($oct);
192 } elsif ($notes =~ /^qq(.*)$/) {
193 $notes = $1;
194 $qq = 1;
195 } elsif ($notes =~ /^r(.*)$/) {
196 $notes = $1;
197 $qq = 0;
198 } elsif ($notes =~ /^(\d+|\=)(\/.*)$/) {
199 $measrest = $1;
200 $notes = $2;
201 if ($measrest eq '=') {
202 $measrest = 1;
204 $toprint .= "$measrest measures of rest.\n";
205 for $n (1..$measrest) {
206 print OUT ' <note>
207 <rest />
208 <duration>'.($beats*$divisions*4/$beattype).'</duration>
209 '.# <type>quarter</type>
211 </note>
213 if ($n < $measrest) {
214 print OUT " </measure>\n";
215 if ($notes ne "") {
216 print OUT ' <measure number="'.$meas.'">
218 $meas++;
222 } elsif ($notes =~ /^((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?)(.*)$/) { # a note
223 ($note, $notes) = ($1,$6);
224 parse_note($note, $keysig, "", "", $qq);
225 } elsif ($notes =~ /^(\((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?\))(.*)$/) { # one note with a fermata
226 ($note, $notes) = ($1,$6);
227 parse_note($note, $keysig, "", "", $qq);
228 } elsif ($notes =~ /^(\(((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?){3}\))(.*)$/) { # a triplet
229 ($triplet, $notes) = ($1,$7);
230 # print "TRIPLET: ".$triplet." -> ";
231 $triplet =~ /^\(((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?)(.*)\)$/gs;
232 ($note, $triplet) = ($1,$6);
233 #print "$note $triplet\n";
234 parse_note($note, $keysig, '<tuplet type="start"/>', ' <time-modification>
235 <actual-notes>3</actual-notes>
236 <normal-notes>2</normal-notes>
237 </time-modification>', $qq);
238 $triplet =~ /^((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?)(.*)$/gs;
239 ($note, $triplet) = ($1,$6);
240 #print "$note $triplet\n";
241 parse_note($note, $keysig, '', ' <time-modification>
242 <actual-notes>3</actual-notes>
243 <normal-notes>2</normal-notes>
244 </time-modification>', $qq);
245 parse_note($triplet, $keysig, '<tuplet type="stop"/>', ' <time-modification>
246 <actual-notes>3</actual-notes>
247 <normal-notes>2</normal-notes>
248 </time-modification>', $qq);
249 } elsif ($notes =~ /^((\d+)\(((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?)+\;(\d+)\))(.*)$/) { # an n-tuplet
250 ($tuplet, $notes) = ($1,$9);
251 # print "N-TUPLET: ".$tuplet." -> ";
252 $tuplet =~ /^(\d+)\(((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?)(.*);(\d)\)$/gs;
253 ($combdur, $note, $tuplet, $numval) = ($1,$2,$7,$8);
254 #print "i=$combdur, n=$numval; $note / $tuplet\n";
255 my $ind_dur = duration($combdur)/$numval;
256 # my $norm_notes =
257 my $act_notes = $numval;
258 parse_note($note, $keysig, '<tuplet type="start"/>', ' <time-modification>
259 <actual-notes>'.$act_notes.'</actual-notes>
260 <normal-notes>1</normal-notes>
261 </time-modification>', $qq);
262 while ($tuplet =~ /^((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?)(.+)$/gs) {
263 ($note, $tuplet) = ($1,$6);
264 #print "$note / $tuplet\n";
265 parse_note($note, $keysig, '', ' <time-modification>
266 <actual-notes>'.$act_notes.'</actual-notes>
267 <normal-notes>1</normal-notes>
268 </time-modification>', $qq);
270 parse_note($tuplet, $keysig, '<tuplet type="stop"/>', ' <time-modification>
271 <actual-notes>'.$act_notes.'</actual-notes>
272 <normal-notes>1</normal-notes>
273 </time-modification>', $qq);
274 } elsif ($notes =~ /^(%\w-\d)(.*)$/) {
275 ($clef,$notes) = ($1,$2);
276 $clef =~ /^%(\w)\-(\d)$/;
277 ($clefsign, $clefline) = ($1, $2);
278 print OUT ' <attributes>
279 <clef>
280 <sign>'.$clefsign.'</sign>
281 <line>'.$clefline.'</line>
282 </clef>
283 </attributes>
285 } elsif ($notes =~ /^@(\d\/\d|c\/?)\s*(.*)$/) {
286 # print "$notes\n";
287 ($timesig,$notes) = ($1,$2);
288 #print "-> $timesig / $notes\n"; exit;
289 $timesig = timesignature($timesig);
290 print OUT " <attributes>\n$timesig
291 </attributes>\n";
292 } elsif ($notes =~ /^(:?\/+:?)(.*)$/) {
293 $barline = $1;
294 $notes = $2;
295 if ($barline =~ /^:\/\/:/) {
296 print OUT ' <barline location="right">
297 <bar-style>light-light</bar-style>
298 <repeat direction="backward"/>
299 </barline>
301 } elsif ($barline =~ /^:\/\/$/ ) {
302 print OUT ' <barline location="right">
303 <bar-style>light-heavy</bar-style>
304 <repeat direction="backward"/>
305 </barline>
307 } elsif ($barline =~ /^\/\/$/) {
308 print OUT ' <barline location="right">
309 <bar-style>light-light</bar-style>
310 <repeat direction="backward"/>
311 </barline>
314 print OUT " </measure>\n";
315 if ($notes ne "") {
316 print OUT ' <measure number="'.$meas.'">
318 if ($barline =~ /^\/\/:$/) {
319 print OUT ' <barline location="left">
320 <bar-style>heavy-light</bar-style>
321 <repeat direction="forward"/>
322 </barline>
324 } elsif ($barline =~ /^:\/\/:$/) {
325 print OUT ' <barline location="left">
326 <repeat direction="forward"/>
327 </barline>
330 print OUT $clefattr;
331 $meas++;
332 } else {
333 $mopen = 0;
335 $toprint .= "bar line\n";
336 } #elsif ($notes =~ /^(\d*\.*\-)(.*)$/) {
337 #($rst, $notes) = ($1, $2);
338 #$toprint .= "rest: $rst\n";
339 #$rst =~ /^(\d*)(\.*)\-$/;
340 #($rst, $dots) =($1,$2);
341 #print OUT ' <note>
342 # <rest />
343 # <duration>'.duration($rst, $dots).'</duration>
344 #'.# <type>quarter</type>
346 # </note>
348 elsif ($notes =~ /^\((\=)\)(.*)$/) { # a bar of rest with a fermata
349 ($rst, $notes) = ($1, $2);
350 $toprint .= "rest: $rst\n";
351 print OUT ' <note>
352 <rest />
353 <duration>'.($beats*$divisions*4/$beattype).'</duration>
354 '.# <type>quarter</type>
355 ' <notations>
356 <fermata type="upright"/>
357 </notations>
358 </note>
361 elsif ($notes =~ s/(\d+\.*)\(((\,|\')*(x|xx|b|bb|n)?\d*\.*(g|q)?(\-|A|B|C|D|E|F|G)t?\+?)\)/\($1$2\)/gs) { # pull duration into fermata parentheses
362 # print "after replacement: $notes\n"; exit;
364 else {
365 print_error("got stuck with $notes\n");
366 $notes = "";
369 if ($mopen) {
370 print OUT " </measure>\n";
375 sub parse_note {
376 my($note, $keysig, $notation, $addition, $in_qq_group) = @_;
378 my ($fermata) = (0);
379 my ($actualnotes, $normalnotes) = (1,1);
381 if ($addition =~ /^\s*<time-modification>\s*<actual-notes>\s*(\d+)\s*<\/actual-notes>\s*<normal-notes>\s*(\d+)\s*<\/normal-notes>\s*<\/time-modification>\s*$/) {
382 ($actualnotes, $normalnotes) = ($1, $2);
385 if ($note =~ /^\((.*)\)$/) {
386 $note = $1;
387 $fermata = 1;
390 $note =~ /^((\,|\')*)(x|xx|b|bb|n)?(\d*)(\.*)(g|q)?(\-|A|B|C|D|E|F|G)(t?)(\+?)$/;
391 my ($oct, $acc, $dur, $dot, $gracecue, $pitch, $trill, $tie) = ($1, $3, $4, $5, $6, $7, $8, $9);
393 print OUT ' <note>
395 if ($gracecue eq "g") {
396 print OUT ' <grace steal-time-following="33"/>
399 if ($gracecue eq "q" || $in_qq_group) {
400 print OUT ' <cue/>
403 if ($pitch eq "-") {
404 print OUT " <rest />\n";
405 } else {
406 print OUT ' <pitch>
407 <step>'.$pitch.'</step>
409 alter($pitch, $acc, $keysig).'
410 <octave>'.octave($oct).'</octave>
411 </pitch>
414 if ($gracecue ne "g") {
415 print OUT ' <duration>'.(duration($dur, $dot)*$normalnotes/$actualnotes).'</duration>
418 # <type>quarter</type>
420 if ($tie eq "+") {
421 if (!$TIE) {
422 $TIE = 1;
423 print OUT '<tie type="start"/>
426 } else {
427 if ($TIE) {
428 print OUT '<tie type="stop"/>
430 $TIE = 0;
434 print OUT $addition;
436 my $notationbracket = $fermata || ($trill eq "t") || ($notation ne "");
437 if ($notationbracket) {
438 print OUT " <notations>\n";
440 if ($fermata) { print OUT '
441 <fermata type="upright"/>'."\n"; }
442 if ($trill eq "t") { print OUT ' <ornaments>
443 <trill-mark/>
444 </ornaments>
447 if ($notation ne "") {
448 print OUT " $notation\n";
450 if ($notationbracket) {
451 print OUT " </notations>\n";
454 print OUT ' </note>
457 $toprint .= "note: oct. $oct/acc. $acc/dur. $dur/dots $dot/grace,cue $gracecue/pitch $pitch\n";
460 sub alter {
461 my ($pitch, $acc, $keysig) = @_;
463 my $alt = 0;
465 if (index ($keysig,$pitch) > -1) {
466 $keysig =~ /^(.).*$/gs;
467 if ($1 eq 'x') {
468 $alt = 1;
469 } else {$alt = -1;}
472 my %acc_alt = ("n", 0, "b", -1, "bb", -2, "x", 1, "xx", 2);
473 if ($acc_alt{$acc} ne "") {
474 $alt = $acc_alt{$acc};
477 if ($alt != 0) {
478 return "<alter>$alt</alter>\n";
480 return "";
483 sub duration {
484 my ($duration, $dots) = @_;
486 if ($duration.$dots ne "") {
487 my %du=("1",4*$divisions,"2",2*$divisions,"4",$divisions,
488 "8",$divisions/2,"6",$divisions/4,"3",$divisions/8,
489 "5",$divisions/16,"7",$divisions/32,
490 "9",$divisions*8,"0",$divisions*16); # breve/long
491 $old_duration = $du{$duration};
492 if ($old_duration eq "") {
493 print_error("strange duration '$duration'\n");
495 my $add = $old_duration;
496 while ($dots ne "") {
497 $add /= 2;
498 $old_duration += $add;
499 $dots =~ /^.(.*)$/gs;
500 $dots = $1;
503 return $old_duration;
506 sub octave {
507 my ($octave) = @_;
509 if ($octave ne "") {
510 $octave =~ /^(.)(.*)$/gs;
511 if ($1 eq ",") {
512 $old_octave = 4 - length $octave;
513 } else {
514 $old_octave = 3 + length $octave;
517 return $old_octave;
520 sub timesignature {
521 my ($timesig) = @_;
523 if ($timesig eq "c3") {
524 $timesig = "3/2"; # it would be better to display it as "C". Example: 451.023.814
526 if ($timesig =~ /^c(\d+)\/(\d+)$/gs) {
527 $timesig = "$1/$2"; # it would be better to show the "C"
530 if ($timesig eq "0" || $timesig eq "") { # unclear how to handle absence of time signature.
531 $timesig ='<time symbol="common">
532 <beats>4</beats>
533 <beat-type>4</beat-type>
534 </time>
535 '; # using 4/4 for now.
536 ($beats, $beattype) = (4,4);
537 } elsif ($timesig =~ /^c(\/?)$/gi) {
538 if ($1 eq "/") {
539 $timesig = '<time symbol="cut">
540 <beats>2</beats>
541 <beat-type>2</beat-type>
542 </time>
544 ($beats, $beattype) = (2,2);
545 } else {
546 $timesig = '<time symbol="common">
547 <beats>4</beats>
548 <beat-type>4</beat-type>
549 </time>
551 ($beats, $beattype) = (4,4);
553 } elsif ($timesig =~ /^(\d+)\/(\d+)$/gs) {
554 ($beats, $beattype) = ($1, $2);
555 $timesig = '<time>
556 <beats>'.$beats.'</beats>
557 <beat-type>'.$beattype.'</beat-type>
558 </time>
560 } else {
561 print_error("Time signature '$timesig' looks strange.\n");
562 # $timesig = ""; we assume 4/4 just to get something legible:
563 ($beats, $beattype) = (4,4);
564 $timesig = '<time>
565 <beats>'.$beats.'</beats>
566 <beat-type>'.$beattype.'</beat-type>
567 </time>
570 return $timesig;
573 sub print_error {
574 my ($msg) = @_;
576 print "\nAn error occurred; context:\n\n$toprint\n
577 Error: $msg\n";
580 sub read_file {
581 my ($fn) = @_;
582 my $res = "";
583 if ($fn eq "-") {
584 while (<STDIN>) { $res .= $_; } # read all lines
585 } else {
586 if (!(open FH, $fn)) {
587 return "";
589 while (<FH>) { $res .= $_; } # read all lines
590 close (FH);
592 return $res;