Fix rhythmic model enclosed by beaming (pull beaming after the rhythmic model)
[pae2xml.git] / pae2xml.pl
blob1e2d2800d979081bc178477beab9d213491c66ad
1 #!/usr/bin/perl
4 # Copyright (C) 2003 Rainer Typke
5 # Copyright (C) 2010 Reinhold Kainhofer <reinhold@kainhofer.com>
6 #pae2xml is licensed under the terms of the GNU General Public License Version
7 #2 as published by the <a href="http://www.fsf.org/" target="_top">Free Software Foundation</a>.
8 #This gives you legal permission to copy, distribute and/or modify <em>pae2xml</em> under
9 #certain conditions. Read
10 #the <a href="http://www.gnu.org/copyleft/gpl.html" target="_top">online version of the license</a>
11 #for more details. pae2xml is provided AS IS with NO WARRANTY OF ANY KIND,
12 #INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
14 our $script = "pae2xml.pl";
15 our $version = "1.0";
17 ### Handle command-line options
19 # import module
20 use Getopt::Long;
21 use File::Basename;
23 our $format = 'rism';
25 sub print_help {
26 print 'pae2xml.pl [OPTIONS...] FILE
28 Converts the Plaine & Easie file FILE to MusicXML. If FILE is -, STDIN is used.
30 Possible options:
31 -h, --help Prints out this help message
32 --format=FORMAT, -f FORMAT
33 Interprets input file as FORMAT. Possible values are
34 rism, pae (default: rism)
35 -v, -- version Prints out version information
38 sub print_version {
39 print "$script $version
40 Copyright (C) 2003 Rainer Typke
41 Copyright (C) 2010 Reinhold Kainhofer <reinhold\@kainhofer.com>
44 sub handle_options {
45 my $print_help = 0;
46 my $print_version = 0;
47 my $result = GetOptions ("f|format=s" => \$format, "help|h" => \$print_help, "version|v" => \$print_version);
48 if ($print_version) {
49 print_version ();
50 exit 0;
52 if ($print_help || (@ARGV == 0)) {
53 print_help ();
54 exit 0;
57 handle_options ();
61 $divisions = 960;
62 $old_duration = $divisions;
63 $old_type = "";
64 $old_octave = 4;
65 $BEAM = 0;
66 @rhythmic_model = ();
67 $rhythmic_model_index = 0;
69 # Store all alterations already used in the current measure, so that e.g.
70 # xCC also detects the second note as a Cis! Needs to be reset at the
71 # beginning of a new bar.
72 %active_alterations = {};
74 ($mday, $mon, $year) = (localtime()) [3..5];
75 $encoding_date = sprintf("%4d-%02d-%02d", $year + 1900, $mon+1, $mday);
77 $TIE = 0;
79 foreach $a (@ARGV) {
80 $p = read_file($a);
81 $toprint = "";
82 if ($format eq "rism") {
83 $p =~ s/\s*\=\=+\s*(.*?)\s*\=\=+\s*/$1/sg;
84 $p =~ s/\s*included.*?-------------*\s*(.*?)\s*/$1/s;
86 ($q, $r) = ($p, $p);
87 if ($q !~ /^.*1\.1\.1.*$/gsx && $r =~ /^.*plain.*$/gsx) {
88 print_error("$a contains 'plain', but not 1.1.1!\n");
89 } else {
90 if ($p =~ /^\s*([^\n]+)\n(.*?)\n((\d+\.\d+\.\d.*?plain.*?\n)+)(.*?)\n?([^\n]+)\n([^\n]+)\s*$/gs) {
91 my ($comp, $title, $incipits, $sonst, $libsig, $rismsig) = ($1, $2, $3, $5, $6, $7);
93 $toprint .= "
94 COMPOSER: $comp
95 TITLE: $title
96 INCIPIT(S): $incipits
97 OTHER INFO: $sonst
98 LIB. SIGN.: $libsig
99 RISM SIGN.: $rismsig\n\n";
100 parse_rism_incipits($incipits, $comp, $title, $sonst, $libsig, $rismsig);
102 else {
103 if (index($p,"plain&easy") > -1) {
104 print_error("Ignoring the following text:\n\n\n$p\n\n\n");
108 } else {
109 # Just a plaine & easie snippet, without any further RISM fields
110 if ($a eq "-") {
111 $filename = "out.xml";
112 } else {
113 $filename = basename ($a, ".pae") . ".xml";
115 parse_pe ($filename, $p, "", "", "", "", "", "", "");
120 ##############################################################################
121 ### RISM file parsing
122 ##############################################################################
124 sub parse_rism_incipits {
125 my ($incipits, $comp, $title, $sonst, $libsig, $rismsig) = @_;
127 $toprint .= "parsing: $incipits\n";
129 while ($incipits =~ /^(\d+\.\d+\..+?)(\d+\.\d+\..*)$/gs) {
130 my ($inc1) = $1;
131 $incipits = $2;
132 parse_rism_incipit($inc1, $comp, $title, $sonst, $libsig, $rismsig);
134 parse_rism_incipit($incipits, $comp, $title, $sonst, $libsig, $rismsig);
137 sub parse_rism_incipit {
138 my ($pe, $comp, $title, $sonst, $libsig, $rismsig) = @_;
140 if ($pe =~ /^\s*(\d+\.\d+\.\d)(\.|:)\s*(.*?)\nplain&easy:\s*(.*)$/gs) {
141 my ($inr, $instr, $pecode) = ($1, $3, $4);
143 my $filename="$rismsig-$inr.xml";
144 $filename =~ s/RISM\s*A\/II\s*:?\s*//gs;
146 foreach $_ ($rismsig,$title,$inr,$instr,$comp,$libsig,$sonst)
151 $toprint .= "
152 INCIPIT NO.: $inr
153 INSTR.: $instr\n";
154 parse_pe ($filename, $pecode, $inr, $instr, $comp, $title, $sonst, $libsig, $rismsig);
156 } else {
157 print_error("could not parse $pe\n");
162 ##############################################################################
163 ### pure Plaine & Easie data parsing
164 ##############################################################################
166 sub parse_pe {
167 my ($filename, $pe, $inr, $instr, $comp, $title, $sonst, $libsig, $rismsig) = @_;
169 $pe =~ s/@ü/@0ü/gs; # make missing time signature explicit
170 while ($pe =~ s/([^\-])(\d+)(\'|\,)(A|B|C|D|E|F|G)/$1$3$2$4/gs) {}; # octave first, then duration. Truly global.
172 if ($pe =~ /^\s*(%([\w\-\+\d]+))?(@([\d\w\/ ]+))?\s*&?\s*(\$([^ü]*))(.*)$/gs) {
173 my ($clef, $timesig, $keysig, $rest) = ($2, $4, $6, $7);
175 print "Writing $filename...\n";
176 open(OUT, ">$filename");
179 print OUT '<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
180 <!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
181 <score-partwise>
183 print OUT " <work>\n" if ($rismsig || $title);
184 print OUT " <work-number>$rismsig</work-number>\n" if ($rismsig);
185 print OUT " <work-title>$title</work-title>\n" if ($title);
186 print OUT " </work>\n" if ($rismsig || $title);
187 print OUT " <movement-number>$inr</movement-number>\n" if ($inr);
188 print OUT " <movement-title>$instr</movement-title>\n" if ($instr);
189 print OUT " <identification>\n";
190 print OUT " <creator type=\"composer\">$comp</creator>\n" if ($comp);
191 print OUT ' <encoding>
192 <software>pae2xml by R. Typke</software>
193 <encoding-date>'.$encoding_date.'</encoding-date>
194 </encoding>
196 print OUT " <source>$libsig</source>\n" if ($libsig);
197 print OUT ' </identification>
198 <part-list>
199 <score-part id="P1">
200 <part-name>'.$sonst.'</part-name>
201 </score-part>
202 </part-list>
203 <part id="P1">
204 <measure number="1">
205 <attributes>
206 <divisions>'.$divisions.'</divisions>
207 '.keysignature ($keysig)
208 .timesignature ($timesig)
209 .clef ($clef)
210 .' </attributes>
214 $toprint .= "
215 CLEF: $clef
216 KEY SIG.: $keysig
217 TIME SIG.: $timesig
218 REST: $rest\n";
219 parse_notes($rest, $keysig);
221 else { print_error("could not parse $pe\n"); }
222 print OUT " </part>
223 </score-partwise>\n";
224 close OUT;
227 # Repeat $1 by a count of $2
228 sub repeat {
229 (my $e, my $count) = @_;
230 my $res = "";
231 for (my $i=1; $i <= $count; ++$i)
233 $res .= $e;
235 return $res;
238 sub parse_notes {
239 my ($notes, $keysig) = @_;
240 my $qq = 0; # in group of cue notes
242 my $meas = 2; # measure number
243 my $mopen = 1; # measure tag still open
245 if ($notes =~ /^\s*(.*?)\s*$/) {
246 $notes = $1;
249 $notes =~ s/!([^!]*)!(f*)/repeat($1, length($2)+1)/gse; # write out repetitions
250 while ( $notes =~ s/(:?\/+:?|^)([^\/:]*)(:?\/+:?)i(:?\/+:?)/$1$2$3$2$4/gs) {}; # replace whole-measure repeats (i notation)
252 $notes =~ s/(\d+)\(([^;]+\))/\($1$2/gs; # pull note lengths into fermatas or triplets
253 $notes =~ s/(xx|x|bb|b|n)\(/\($1/gs; # pull accidentals into tuplets or fermatas:
254 $notes =~ s/(\d+)(xx|x|bb|b|n)(A|B|C|D|E|F|G)/$2$1$3/gs; # accidentals first, then duration
256 $notes =~ s/(\.|\d|\,|\')qq/qq$1/gs; # pull beginning mark of group of grace notes in front of corresponding notes
257 $notes =~ s/(xx|x|bb|b|n)qq/qq$1/gs; # qq first, then parts of notes
258 $notes =~ s/([\.\d,'xbn])+(q|g)/$2$1/gs; # q and g first, then parts of notes
259 $notes =~ s/(\{)(\d\.*(\d\.*)+)/$2$1/gs; # pull beaming inside a rhythmic model
261 # Beam starts/endings are handled by the first/last note of the beam, since
262 # we need to know for the first note the <beam>begin</beam> and for the
263 # last note that the <beam>end</beam> tag should be used!
264 # Thus, the RegExp for a note contains the beam start/end tags { and }
265 while ($notes ne "") {
266 if ($notes =~ /^(\'+|\,+)(.*)$/) { # Octave marks
267 ($oct, $notes) = ($1, $2);
268 octave($oct);
269 } elsif ($notes =~ /^qq(.*)$/) { # Begin grace
270 $notes = $1;
271 $qq = 1;
272 } elsif ($notes =~ /^r(.*)$/) { # End grace
273 $notes = $1;
274 $qq = 0;
275 } elsif ($notes =~ /^([0-9]\.?(?:\s?[0-9]+\.?)+)\s*(.*)$/) { # Rhythmic model
276 ($model, $notes) = ($1, $2);
277 @rhythmic_model = parse_rhythmic_model ($1);
278 $rhythmic_model_index = -1;
279 } elsif ($notes =~ /^\=(\d*)(\/.*)$/) { # multi-measure rests
280 $measrest = $1;
281 $notes = $2;
282 if ($measrest eq '') {
283 $measrest = 1;
285 $toprint .= "$measrest measures of rest.\n";
286 if ($measrest > 0) {
287 # Create a real multi-bar rest
288 print OUT ' <attributes>
289 <measure-style>
290 <multiple-rest>'.$measrest.'</multiple-rest>
291 </measure-style>
292 </attributes>
295 # Now create the measures
296 for $n (1..$measrest) {
297 print OUT ' <note>
298 <rest />
299 <duration>'.($beats*$divisions*4/$beattype).'</duration>
300 </note>
302 if ($n < $measrest) {
303 print OUT " </measure>\n";
304 reset_measure_attributes ();
305 if ($notes ne "") {
306 print OUT ' <measure number="'.$meas.'">
308 $meas++;
309 } else {
310 $mopen = 0;
314 } elsif ($notes =~ /^({?(\^)?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)(.*)$/) { # a note
315 ($note, $notes) = ($1,$7);
316 parse_note($note, $keysig, "", "", $qq);
317 } elsif ($notes =~ /^({?\((g|q)?{?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)[t\+]*\)[t\+}]*)(.*)$/) { # one note with a fermata
318 ($note, $notes) = ($1,$6);
319 parse_note($note, $keysig, "", "", $qq);
320 } elsif ($notes =~ /^(\(({?(\^)?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?){3}\))(.*)$/) { # a triplet
321 ($triplet, $notes) = ($1,$8);
322 $triplet =~ /^\(({?(\^)?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)(.*)\)$/gs;
323 ($note, $triplet) = ($1,$7);
324 my $time_mod = " <time-modification>
325 <actual-notes>3</actual-notes>
326 <normal-notes>2</normal-notes>
327 </time-modification>\n";
328 parse_note($note, $keysig, '<tuplet type="start"/>', $time_mod, $qq);
329 $triplet =~ /^({?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)(.*)$/gs;
330 ($note, $triplet) = ($1,$6);
331 parse_note($note, $keysig, '', $time_mod, $qq);
332 parse_note($triplet, $keysig, '<tuplet type="stop"/>', $time_mod, $qq);
333 } elsif ($notes =~ /^((\d+)\(({?(\^)?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)+\;(\d+)\))(.*)$/) { # an n-tuplet
334 ($tuplet, $notes) = ($1,$10);
335 $tuplet =~ /^(\d+)\(({?(\,|\')*(x|xx|b|bb|n)?(\d*\.*)(g|q)?(\-|A|B|C|D|E|F|G)t?\+?}?)(.*);(\d)\)$/gs;
336 ($combdur, $note, $notedur, $tuplet, $numval) = ($1,$2,$5, $8,$9);
337 my $act_notes = $numval;
338 my $norm_notes = duration($combdur)/duration($notedur);
339 my $time_mod = " <time-modification>
340 <actual-notes>$act_notes</actual-notes>
341 <normal-notes>$norm_notes</normal-notes>
342 </time-modification>\n";
343 parse_note($note, $keysig, '<tuplet type="start"/>', $time_mod, $qq);
344 while ($tuplet =~ /^({?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?\}?)([^}]+}?)$/gs) {
345 ($note, $tuplet) = ($1,$6);
346 parse_note($note, $keysig, '', $time_mod, $qq);
348 parse_note($tuplet, $keysig, '<tuplet type="stop"/>', $time_mod, $qq);
349 } elsif ($notes =~ /^(%(\w(-|\+)\d))(.*)$/) { # Clef change
350 ($clef,$notes) = ($2,$4);
351 print OUT " <attributes>\n";
352 print OUT clef ($clef);
353 print OUT " </attributes>\n";
354 } elsif ($notes =~ /^@(c?\d+(\/\d+)?( \d+\/\d+)*|c\/?|o\.?)\s*(.*)$/) { # time signatue change
355 ($timesig,$notes) = ($1,$4);
356 print OUT " <attributes>\n";
357 print OUT timesignature($timesig);
358 print OUT " </attributes>\n";
359 } elsif ($notes =~ /^\$((b|x)[ABCDEFG]*)\s*(.*)$/) { # key signature change
360 ($keysig, $notes) = ($1, $3);
361 print OUT " <attributes>\n";
362 print OUT keysignature ($keysig);
363 print OUT " </attributes>\n";
364 } elsif ($notes =~ /^(:?\/+:?)(.*)$/) { # Barline (and repeats)
365 $barline = $1;
366 $notes = $2;
367 if ($barline =~ /^:\/\/:/) {
368 print OUT ' <barline location="right">
369 <bar-style>light-light</bar-style>
370 <repeat direction="backward"/>
371 </barline>
373 } elsif ($barline =~ /^:\/\/$/ ) {
374 print OUT ' <barline location="right">
375 <bar-style>light-heavy</bar-style>
376 <repeat direction="backward"/>
377 </barline>
379 } elsif ($barline =~ /^\/\/$/) {
380 $type = "light-light";
381 # At the end of a piece use a light-heavy barline
382 if ($notes eq "") {
383 $type = "light-heavy";
385 print OUT " <barline location=\"right\">
386 <bar-style>$type</bar-style>
387 </barline>
390 print OUT " </measure>\n";
391 reset_measure_attributes ();
392 if ($notes ne "") {
393 print OUT ' <measure number="'.$meas.'">
395 if ($barline =~ /^\/\/:$/) {
396 print OUT ' <barline location="left">
397 <bar-style>heavy-light</bar-style>
398 <repeat direction="forward"/>
399 </barline>
401 } elsif ($barline =~ /^:\/\/:$/) {
402 print OUT ' <barline location="left">
403 <repeat direction="forward"/>
404 </barline>
407 print OUT $clefattr;
408 $meas++;
409 } else {
410 $mopen = 0;
412 $toprint .= "bar line\n";
414 } elsif ($notes =~ /^\((\=)\)(.*)$/) { # a bar of rest with a fermata
415 ($rst, $notes) = ($1, $2);
416 $toprint .= "rest: $rst\n";
417 print OUT ' <note>
418 <rest />
419 <duration>'.($beats*$divisions*4/$beattype).'</duration>
420 <notations>
421 <fermata type="upright"/>
422 </notations>
423 </note>
426 elsif ($notes =~ s/(\d+\.*)\(({?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)\)/\($1$2\)/gs) { # pull duration into fermata parentheses
427 # print "after replacement: $notes\n"; exit;
428 } elsif ($notes =~ /^ +(.*)$/) {
429 $notes = $1;
430 print("Invalid space encountered in notes before $notes\n");
432 else {
433 print_error("got stuck with $notes\n");
434 $notes = "";
437 if ($mopen) {
438 print OUT " </measure>\n";
439 reset_measure_attributes ();
444 sub parse_note {
445 my($note, $keysig, $notation, $addition, $in_qq_group) = @_;
447 my ($fermata) = (0);
448 my ($actualnotes, $normalnotes) = (1,1);
450 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*$/) {
451 ($actualnotes, $normalnotes) = ($1, $2);
454 if ($note =~ /^\((.*)\)(.*)$/) {
455 $note = "$1$2";
456 $fermata = 1;
459 $note =~ /^({)?(\^)?(g|q)?((\,|\')*)(x|xx|b|bb|n)?(\d*)(\.*)(\-|A|B|C|D|E|F|G)(t?)(\+?)(}?)$/;
460 my ($beamstart, $chord, $gracecue, $oct, $acc, $dur, $dot, $pitch, $trill, $tie, $beamend) = ($1, $2, $3, $4, $6, $7, $8, $9, $10, $11, $12);
462 print OUT ' <note>
464 if ($gracecue eq "g") {
465 print OUT ' <grace slash="yes" steal-time-following="33"/>
468 if ($gracecue eq "q" || $in_qq_group) {
469 print OUT ' <grace/>
472 if ($pitch eq "-") {
473 print OUT " <rest />\n";
474 } else {
475 if ($chord eq "^") {
476 print OUT " <chord/>\n";
478 print OUT ' <pitch>
479 <step>'.$pitch.'</step>
480 '.alter($pitch, $acc, $keysig)
481 .' <octave>'.octave($oct).'</octave>
482 </pitch>
485 # We are using a rhythmic model, extract the correct duration
486 $this_duration = "";
487 $this_head = "";
488 if (($dur.$dot eq "") && scalar(@rhythmic_model)) {
489 if ($chord ne "^") {
490 $rhythmic_model_index = ($rhythmic_model_index +1 ) % scalar(@rhythmic_model);
492 $this_duration = $rhythmic_model[$rhythmic_model_index][0];
493 $this_head = $rhythmic_model[$rhythmic_model_index][1];
494 } elsif ($dur.$dot ne "" && scalar(@rhythmic_model)) {
495 # The rhythmic model ends when a new new rhythmic value appears!
496 @rhythmic_model = ();
498 if ($gracecue ne "g") {
499 if (!$this_duration) {
500 $this_duration = duration ($dur, $dot);
502 print OUT ' <duration>'.($this_duration*$normalnotes/$actualnotes).'</duration>
506 $tienotation = "";
507 if ($tie eq "+") {
508 if ($TIE) {
509 $tienotation = " <tied type=\"stop\"/>\n";
511 $tienotation .= " <tied type=\"start\"/>\n";
512 if (!$TIE) {
513 $TIE = 1;
514 print OUT ' <tie type="start"/>
517 } else {
518 if ($TIE) {
519 print OUT ' <tie type="stop"/>
521 $tienotation = " <tied type=\"stop\"/>\n";
522 $TIE = 0;
526 # Determine graphic notehead: acciaccaturas are always 8th, otherwise use duration
527 if ($gracecue eq "g") {
528 print OUT " <type>eighth</type>\n";
529 } else {
530 if (!$this_head) {
531 $this_head = notehead ($dur, $dot);
533 print OUT $this_head;
535 # If we have an explicit accidental on the note, print the <accidental> tag
536 print OUT accidental_explicit ($acc);
538 # addition is typically empty or a time-modification tag
539 print OUT $addition;
541 # print out beaming information if needed:
542 if (($beamstart eq "{") && ($beamend eq "}")) {
543 # Single-note beam means a hook
544 print OUT " <beam>forward hook</beam>\n";
545 } elsif ($beamstart eq "{") {
546 ++$BEAM;
547 print OUT " <beam number=\"$BEAM\">begin</beam>\n";
548 } elsif (($BEAM > 0) && ($beamend eq "}")) {
549 print OUT " <beam number=\"$BEAM\">end</beam>\n";
550 --$BEAM;
551 } elsif ($BEAM > 0) {
552 print OUT " <beam number=\"$BEAM\">continue</beam>\n";
555 my $notationbracket = $fermata || $tienotation || ($trill eq "t") || ($notation ne "");
556 if ($notationbracket) {
557 print OUT " <notations>\n";
559 if ($tienotation) {
560 print OUT $tienotation;
562 if ($fermata) {
563 print OUT " <fermata type=\"upright\"/>\n";
565 if ($trill eq "t") {
566 print OUT ' <ornaments>
567 <trill-mark/>
568 </ornaments>
571 if ($notation ne "") {
572 print OUT " $notation\n";
574 if ($notationbracket) {
575 print OUT " </notations>\n";
578 print OUT ' </note>
581 $toprint .= "note: oct. $oct/acc. $acc/dur. $dur/dots $dot/grace,cue $gracecue/pitch $pitch\n";
584 sub reset_measure_attributes {
585 %active_alterations = {};
586 # TODO: reset all measure-only attributes, like manual accidentals
589 sub alter {
590 my ($pitch, $acc, $keysig) = @_;
592 my $alt = 0;
593 # If we had the same pitch with explicit alteration already in the current
594 # measure, that alteration goes on, unless the current note has an explicit
595 # alteration
596 if ($acc eq "") {
597 $acc = $active_alterations{$pitch};
598 } else {
599 # Store the explicit alteration of the current pitch!
600 $active_alterations{$pitch} = $acc;
603 if (index ($keysig,$pitch) > -1) {
604 $keysig =~ /^(.).*$/gs;
605 if ($1 eq 'x') {
606 $alt = 1;
607 } else {$alt = -1;}
610 my %acc_alt = ("n", 0, "b", -1, "bb", -2, "x", 1, "xx", 2);
611 if ($acc_alt{$acc} ne "") {
612 $alt = $acc_alt{$acc};
615 if ($alt != 0) {
616 return "\t\t\t\t\t<alter>$alt</alter>\n";
618 return "";
621 sub accidental_explicit {
622 my ($acc) = @_;
623 my %accidentals = ("xx", "double-sharp", "x", "sharp", "n", "natural", "b", "flat", "bb", "flat-flat");
624 my $this_acc = $accidentals{$acc};
625 if ($this_acc) {
626 return "\t\t\t\t<accidental>$this_acc</accidental>\n";
627 } else {
628 return "";
632 sub raw_notehead {
633 my ($duration, $dots) = @_;
634 my %du=("0", "long", "9", "breve", "1", "whole", "2", "half", "4", "quarter",
635 "8", "eighth", "6", "16th", "3", "32nd", "5", "64th", "7", "128th");
636 if (($duration ne "") && $du{$duration}) {
637 my $res = " <type>$du{$duration}</type>\n";
638 $res .= repeat (" <dot/>\n", length ($dots));
639 return $res;
642 sub notehead {
643 my ($duration, $dots) = @_;
644 if ($duration.$dots ne "") {
645 my $head = raw_notehead ($duration, $dots);
646 $old_type = $head if $head;
648 return $old_type;
651 sub parse_rhythmic_model {
652 (my $model) = @_;
653 my @m = ();
654 while ($model =~ s/^([0-9])\s*(\.?)\s*([0-9\.\s]*)$/$3/) {
655 my ($this_dur, $this_dots) = ($1, $2);
656 my $dur = raw_duration ($this_dur, $this_dots);
657 my $notehead = raw_notehead ($this_dur, $this_dots);
658 push @m, [$dur, $notehead];
660 return @m;
664 sub raw_duration {
665 my ($duration, $dots) = @_;
666 my %du=("1",4*$divisions,"2",2*$divisions,"4",$divisions,
667 "8",$divisions/2,"6",$divisions/4,"3",$divisions/8,
668 "5",$divisions/16,"7",$divisions/32,
669 "9",$divisions*8,"0",$divisions*16); # breve/long
670 my $res = $du{$duration};
671 if ($res) {
672 my $add = $res;
673 while ($dots ne "") {
674 $add /= 2;
675 $res += $add;
676 $dots =~ /^.(.*)$/gs;
677 $dots = $1;
680 return $res;
682 sub duration {
683 my ($duration, $dots) = @_;
684 if ($duration.$dots eq "7.") {
685 print_error ("Neumic notation is not supported by MusicXML!");
688 if ($duration.$dots ne "") {
689 $old_duration = raw_duration ($duration, $dots);
690 if ($old_duration eq "") {
691 print_error("strange duration '$duration'\n");
694 return $old_duration;
697 sub octave {
698 my ($octave) = @_;
700 if ($octave ne "") {
701 $octave =~ /^(.)(.*)$/gs;
702 if ($1 eq ",") {
703 $old_octave = 4 - length $octave;
704 } else {
705 $old_octave = 3 + length $octave;
708 return $old_octave;
711 sub clef {
712 my ($clef) = @_;
713 my $clefoctave = '';
714 if ($clef =~ /^(\w)(\-|\+)(\d)$/) {
715 ($clefsign, $clefline) = ($1, $3);
716 if ($2 =~ /^\+$/) {
717 print "Warning: Mensural clefs are not supported by MusicXML, using modern clef (input: $clef)\n";
719 if ($clefsign eq 'g') {
720 $clefsign = "G";
721 $clefoctave = " <clef-octave-change>-1</clef-octave-change>\n";
723 } else {
724 ($clefsign, $clefline) = ("G", 2);
726 return ' <clef>
727 <sign>'.$clefsign.'</sign>
728 <line>'.$clefline.'</line>
729 '.$clefoctave.' </clef>
733 sub keysignature {
734 my ($keysig) = @_;
736 # TODO: How is a change to C major represented? by "$ " or "$x " or "$b "?
737 # At the beginning, the $ part is left out, but mid-piece key changes
738 # need to way to clear all accidentals! We accept all three cases above!
739 my %fif=("", 0, "x", 0, "b", 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);
740 $keysig =~ s/(\s+)|&//gs; # it is unclear what the & means, so we'll ignore it for now.
741 $keysig =~ s/\[|\]//gs; # IGNORING brackets around a key sig.
742 $fifths = $fif{$keysig};
743 if ($fifths eq "") {
744 $fifths = "0";
745 print_error("Strange key signature '$keysig'.\n");
747 return ' <key>
748 <fifths>'.$fifths.'</fifths>
749 </key>
754 sub timesignature {
755 my ($timesig) = @_;
756 my $symbol = "";
758 if ($timesig =~ /^(o(\.)?)$/) {
759 if ($2 eq ".") {
760 $timesig = "9/8";
761 } else {
762 $timesig = "3/4";
764 print "Mensural time signature \"$1\" not supported, using $timesig.\n";
766 if ($timesig =~ /^(\d+\/\d+)( \d+\/\d+)+$/ ) {
767 print "Alternating time signature \"$timesig\" not supported by MusicXML, falling back to $1.\n";
768 $timesig = $1;
770 if ($timesig =~ /^c((\d+)(\/(\d+))?)$/gs) {
771 print "Time signature \"$timesig\" not supported by MusicXML, falling back to $1.\n";
772 $timesig = "$1"; # TODO: it would be better to show the "C". Example: 451.023.814
775 # For missing timesignature, fall back to "c"
776 if ($timesig eq "0" || $timesig eq "" || $timesig eq "c" ) {
777 $symbol = "common";
778 ($beats, $beattype) = (4,4);
779 } elsif ($timesig =~ /^c\/$/gi) {
780 $symbol = "cut";
781 ($beats, $beattype) = (2,2);
782 } elsif ($timesig =~ /^(\d+)\/(\d+)$/gs) {
783 ($beats, $beattype) = ($1, $2);
784 } elsif ($timesig =~/^(\d+)$/gs) {
785 $symbol = "single-number";
786 ($beats, $beattype) = ($1,2);
787 } else {
788 print_error("Time signature '$timesig' looks strange, falling back to 4/4.\n");
789 ($beats, $beattype) = (4,4);
791 if ($symbol) {
792 $symbol = " symbol=\"$symbol\"";
794 $timesig = " <time$symbol>
795 <beats>$beats</beats>
796 <beat-type>$beattype</beat-type>
797 </time>
799 return $timesig;
802 sub print_error {
803 my ($msg) = @_;
805 print "\nAn error occurred; context:\n\n$toprint\n
806 Error: $msg\n";
809 sub read_file {
810 my ($fn) = @_;
811 my $res = "";
812 if ($fn eq "-") {
813 while (<STDIN>) { $res .= $_; } # read all lines
814 } else {
815 if (!(open FH, $fn)) {
816 return "";
818 while (<FH>) { $res .= $_; } # read all lines
819 close (FH);
821 return $res;