Fix triplets and duplets (the normal-notes value was wrongly set to 1 rather than...
[pae2xml.git] / pae2xml.pl
blobbc79762317a913b9eb4a64c52718c5b7f7f3968c
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
260 # Beam starts/endings are handled by the first/last note of the beam, since
261 # we need to know for the first note the <beam>begin</beam> and for the
262 # last note that the <beam>end</beam> tag should be used!
263 # Thus, the RegExp for a note contains the beam start/end tags { and }
264 while ($notes ne "") {
265 if ($notes =~ /^(\'+|\,+)(.*)$/) { # Octave marks
266 ($oct, $notes) = ($1, $2);
267 octave($oct);
268 } elsif ($notes =~ /^qq(.*)$/) { # Begin grace
269 $notes = $1;
270 $qq = 1;
271 } elsif ($notes =~ /^r(.*)$/) { # End grace
272 $notes = $1;
273 $qq = 0;
274 } elsif ($notes =~ /^([0-9]\.?(?:\s?[0-9]+\.?)+)\s*(.*)$/) { # Rhythmic model
275 ($model, $notes) = ($1, $2);
276 @rhythmic_model = parse_rhythmic_model ($1);
277 $rhythmic_model_index = -1;
278 } elsif ($notes =~ /^\=(\d*)(\/.*)$/) { # multi-measure rests
279 $measrest = $1;
280 $notes = $2;
281 if ($measrest eq '') {
282 $measrest = 1;
284 $toprint .= "$measrest measures of rest.\n";
285 if ($measrest > 0) {
286 # Create a real multi-bar rest
287 print OUT ' <attributes>
288 <measure-style>
289 <multiple-rest>'.$measrest.'</multiple-rest>
290 </measure-style>
291 </attributes>
294 # Now create the measures
295 for $n (1..$measrest) {
296 print OUT ' <note>
297 <rest />
298 <duration>'.($beats*$divisions*4/$beattype).'</duration>
299 </note>
301 if ($n < $measrest) {
302 print OUT " </measure>\n";
303 reset_measure_attributes ();
304 if ($notes ne "") {
305 print OUT ' <measure number="'.$meas.'">
307 $meas++;
308 } else {
309 $mopen = 0;
313 } elsif ($notes =~ /^({?(\^)?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)(.*)$/) { # a note
314 ($note, $notes) = ($1,$7);
315 parse_note($note, $keysig, "", "", $qq);
316 } elsif ($notes =~ /^({?\((g|q)?{?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)[t\+]*\)[t\+}]*)(.*)$/) { # one note with a fermata
317 ($note, $notes) = ($1,$6);
318 parse_note($note, $keysig, "", "", $qq);
319 } elsif ($notes =~ /^(\(({?(\^)?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?){3}\))(.*)$/) { # a triplet
320 ($triplet, $notes) = ($1,$8);
321 $triplet =~ /^\(({?(\^)?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)(.*)\)$/gs;
322 ($note, $triplet) = ($1,$7);
323 my $time_mod = " <time-modification>
324 <actual-notes>3</actual-notes>
325 <normal-notes>2</normal-notes>
326 </time-modification>\n";
327 parse_note($note, $keysig, '<tuplet type="start"/>', $time_mod, $qq);
328 $triplet =~ /^({?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)(.*)$/gs;
329 ($note, $triplet) = ($1,$6);
330 parse_note($note, $keysig, '', $time_mod, $qq);
331 parse_note($triplet, $keysig, '<tuplet type="stop"/>', $time_mod, $qq);
332 } elsif ($notes =~ /^((\d+)\(({?(\^)?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?}?)+\;(\d+)\))(.*)$/) { # an n-tuplet
333 ($tuplet, $notes) = ($1,$10);
334 $tuplet =~ /^(\d+)\(({?(\,|\')*(x|xx|b|bb|n)?(\d*\.*)(g|q)?(\-|A|B|C|D|E|F|G)t?\+?}?)(.*);(\d)\)$/gs;
335 ($combdur, $note, $notedur, $tuplet, $numval) = ($1,$2,$5, $8,$9);
336 my $act_notes = $numval;
337 my $norm_notes = duration($combdur)/duration($notedur);
338 my $time_mod = " <time-modification>
339 <actual-notes>$act_notes</actual-notes>
340 <normal-notes>$norm_notes</normal-notes>
341 </time-modification>\n";
342 parse_note($note, $keysig, '<tuplet type="start"/>', $time_mod, $qq);
343 while ($tuplet =~ /^({?(g|q)?(\,|\')*(x|xx|b|bb|n)?\d*\.*(\-|A|B|C|D|E|F|G)t?\+?\}?)([^}]+}?)$/gs) {
344 ($note, $tuplet) = ($1,$6);
345 parse_note($note, $keysig, '', $time_mod, $qq);
347 parse_note($tuplet, $keysig, '<tuplet type="stop"/>', $time_mod, $qq);
348 } elsif ($notes =~ /^(%(\w(-|\+)\d))(.*)$/) { # Clef change
349 ($clef,$notes) = ($2,$4);
350 print OUT " <attributes>\n";
351 print OUT clef ($clef);
352 print OUT " </attributes>\n";
353 } elsif ($notes =~ /^@(c?\d+(\/\d+)?( \d+\/\d+)*|c\/?|o\.?)\s*(.*)$/) { # time signatue change
354 ($timesig,$notes) = ($1,$4);
355 print OUT " <attributes>\n";
356 print OUT timesignature($timesig);
357 print OUT " </attributes>\n";
358 } elsif ($notes =~ /^\$((b|x)[ABCDEFG]*)\s*(.*)$/) { # key signature change
359 ($keysig, $notes) = ($1, $3);
360 print OUT " <attributes>\n";
361 print OUT keysignature ($keysig);
362 print OUT " </attributes>\n";
363 } elsif ($notes =~ /^(:?\/+:?)(.*)$/) { # Barline (and repeats)
364 $barline = $1;
365 $notes = $2;
366 if ($barline =~ /^:\/\/:/) {
367 print OUT ' <barline location="right">
368 <bar-style>light-light</bar-style>
369 <repeat direction="backward"/>
370 </barline>
372 } elsif ($barline =~ /^:\/\/$/ ) {
373 print OUT ' <barline location="right">
374 <bar-style>light-heavy</bar-style>
375 <repeat direction="backward"/>
376 </barline>
378 } elsif ($barline =~ /^\/\/$/) {
379 $type = "light-light";
380 # At the end of a piece use a light-heavy barline
381 if ($notes eq "") {
382 $type = "light-heavy";
384 print OUT " <barline location=\"right\">
385 <bar-style>$type</bar-style>
386 </barline>
389 print OUT " </measure>\n";
390 reset_measure_attributes ();
391 if ($notes ne "") {
392 print OUT ' <measure number="'.$meas.'">
394 if ($barline =~ /^\/\/:$/) {
395 print OUT ' <barline location="left">
396 <bar-style>heavy-light</bar-style>
397 <repeat direction="forward"/>
398 </barline>
400 } elsif ($barline =~ /^:\/\/:$/) {
401 print OUT ' <barline location="left">
402 <repeat direction="forward"/>
403 </barline>
406 print OUT $clefattr;
407 $meas++;
408 } else {
409 $mopen = 0;
411 $toprint .= "bar line\n";
412 } #elsif ($notes =~ /^(\d*\.*\-)(.*)$/) {
413 #($rst, $notes) = ($1, $2);
414 #$toprint .= "rest: $rst\n";
415 #$rst =~ /^(\d*)(\.*)\-$/;
416 #($rst, $dots) =($1,$2);
417 #print OUT ' <note>
418 # <rest />
419 # <duration>'.duration($rst, $dots).'</duration>
420 #'.# <type>quarter</type>
422 # </note>
424 elsif ($notes =~ /^\((\=)\)(.*)$/) { # a bar of rest with a fermata
425 ($rst, $notes) = ($1, $2);
426 $toprint .= "rest: $rst\n";
427 print OUT ' <note>
428 <rest />
429 <duration>'.($beats*$divisions*4/$beattype).'</duration>
430 <notations>
431 <fermata type="upright"/>
432 </notations>
433 </note>
436 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
437 # print "after replacement: $notes\n"; exit;
439 elsif ($notes =~ /^ +(.*)$/) {
440 $notes = $1;
441 print("Invalid space encountered in notes before $notes\n");
443 else {
444 print_error("got stuck with $notes\n");
445 $notes = "";
448 if ($mopen) {
449 print OUT " </measure>\n";
450 reset_measure_attributes ();
455 sub parse_note {
456 my($note, $keysig, $notation, $addition, $in_qq_group) = @_;
458 my ($fermata) = (0);
459 my ($actualnotes, $normalnotes) = (1,1);
461 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*$/) {
462 ($actualnotes, $normalnotes) = ($1, $2);
465 if ($note =~ /^\((.*)\)(.*)$/) {
466 $note = "$1$2";
467 $fermata = 1;
470 $note =~ /^({)?(\^)?(g|q)?((\,|\')*)(x|xx|b|bb|n)?(\d*)(\.*)(\-|A|B|C|D|E|F|G)(t?)(\+?)(}?)$/;
471 my ($beamstart, $chord, $gracecue, $oct, $acc, $dur, $dot, $pitch, $trill, $tie, $beamend) = ($1, $2, $3, $4, $6, $7, $8, $9, $10, $11, $12);
473 print OUT ' <note>
475 if ($gracecue eq "g") {
476 print OUT ' <grace slash="yes" steal-time-following="33"/>
479 if ($gracecue eq "q" || $in_qq_group) {
480 print OUT ' <grace/>
483 if ($pitch eq "-") {
484 print OUT " <rest />\n";
485 } else {
486 if ($chord eq "^") {
487 print OUT " <chord/>\n";
489 print OUT ' <pitch>
490 <step>'.$pitch.'</step>
491 '.alter($pitch, $acc, $keysig)
492 .' <octave>'.octave($oct).'</octave>
493 </pitch>
496 # We are using a rhythmic model, extract the correct duration
497 $this_duration = "";
498 $this_head = "";
499 if (($dur.$dot eq "") && scalar(@rhythmic_model)) {
500 if ($chord ne "^") {
501 $rhythmic_model_index = ($rhythmic_model_index +1 ) % scalar(@rhythmic_model);
503 $this_duration = $rhythmic_model[$rhythmic_model_index][0];
504 $this_head = $rhythmic_model[$rhythmic_model_index][1];
505 } elsif ($dur.$dot ne "" && scalar(@rhythmic_model)) {
506 # The rhythmic model ends when a new new rhythmic value appears!
507 @rhythmic_model = ();
509 if ($gracecue ne "g") {
510 if (!$this_duration) {
511 $this_duration = duration ($dur, $dot);
513 print OUT ' <duration>'.($this_duration*$normalnotes/$actualnotes).'</duration>
517 $tienotation = "";
518 if ($tie eq "+") {
519 if ($TIE) {
520 $tienotation = " <tied type=\"stop\"/>\n";
522 $tienotation .= " <tied type=\"start\"/>\n";
523 if (!$TIE) {
524 $TIE = 1;
525 print OUT ' <tie type="start"/>
528 } else {
529 if ($TIE) {
530 print OUT ' <tie type="stop"/>
532 $tienotation = " <tied type=\"stop\"/>\n";
533 $TIE = 0;
537 # Determine graphic notehead: acciaccaturas are always 8th, otherwise use duration
538 if ($gracecue eq "g") {
539 print OUT " <type>eighth</type>\n";
540 } else {
541 if (!$this_head) {
542 $this_head = notehead ($dur, $dot);
544 print OUT $this_head;
546 # If we have an explicit accidental on the note, print the <accidental> tag
547 print OUT accidental_explicit ($acc);
549 # addition is typically empty or a time-modification tag
550 print OUT $addition;
552 # print out beaming information if needed:
553 if (($beamstart eq "{") && ($beamend eq "}")) {
554 # Single-note beam means a hook
555 print OUT " <beam>forward hook</beam>\n";
556 } elsif ($beamstart eq "{") {
557 ++$BEAM;
558 print OUT " <beam number=\"$BEAM\">begin</beam>\n";
559 } elsif (($BEAM > 0) && ($beamend eq "}")) {
560 print OUT " <beam number=\"$BEAM\">end</beam>\n";
561 --$BEAM;
562 } elsif ($BEAM > 0) {
563 print OUT " <beam number=\"$BEAM\">continue</beam>\n";
566 my $notationbracket = $fermata || $tienotation || ($trill eq "t") || ($notation ne "");
567 if ($notationbracket) {
568 print OUT " <notations>\n";
570 if ($tienotation) {
571 print OUT $tienotation;
573 if ($fermata) {
574 print OUT " <fermata type=\"upright\"/>\n";
576 if ($trill eq "t") {
577 print OUT ' <ornaments>
578 <trill-mark/>
579 </ornaments>
582 if ($notation ne "") {
583 print OUT " $notation\n";
585 if ($notationbracket) {
586 print OUT " </notations>\n";
589 print OUT ' </note>
592 $toprint .= "note: oct. $oct/acc. $acc/dur. $dur/dots $dot/grace,cue $gracecue/pitch $pitch\n";
595 sub reset_measure_attributes {
596 %active_alterations = {};
597 # TODO: reset all measure-only attributes, like manual accidentals
600 sub alter {
601 my ($pitch, $acc, $keysig) = @_;
603 my $alt = 0;
604 # If we had the same pitch with explicit alteration already in the current
605 # measure, that alteration goes on, unless the current note has an explicit
606 # alteration
607 if ($acc eq "") {
608 $acc = $active_alterations{$pitch};
609 } else {
610 # Store the explicit alteration of the current pitch!
611 $active_alterations{$pitch} = $acc;
614 if (index ($keysig,$pitch) > -1) {
615 $keysig =~ /^(.).*$/gs;
616 if ($1 eq 'x') {
617 $alt = 1;
618 } else {$alt = -1;}
621 my %acc_alt = ("n", 0, "b", -1, "bb", -2, "x", 1, "xx", 2);
622 if ($acc_alt{$acc} ne "") {
623 $alt = $acc_alt{$acc};
626 if ($alt != 0) {
627 return "\t\t\t\t\t<alter>$alt</alter>\n";
629 return "";
632 sub accidental_explicit {
633 my ($acc) = @_;
634 my %accidentals = ("xx", "double-sharp", "x", "sharp", "n", "natural", "b", "flat", "bb", "flat-flat");
635 my $this_acc = $accidentals{$acc};
636 if ($this_acc) {
637 return "\t\t\t\t<accidental>$this_acc</accidental>\n";
638 } else {
639 return "";
643 sub raw_notehead {
644 my ($duration, $dots) = @_;
645 my %du=("0", "long", "9", "breve", "1", "whole", "2", "half", "4", "quarter",
646 "8", "eighth", "6", "16th", "3", "32nd", "5", "64th", "7", "128th");
647 if ($duration && $du{$duration}) {
648 my $res = " <type>$du{$duration}</type>\n";
649 $res .= repeat (" <dot/>\n", length ($dots));
650 return $res;
653 sub notehead {
654 my ($duration, $dots) = @_;
655 if ($duration.$dots ne "") {
656 my $head = raw_notehead ($duration, $dots);
657 $old_type = $head if $head;
659 return $old_type;
662 sub parse_rhythmic_model {
663 (my $model) = @_;
664 my @m = ();
665 while ($model =~ s/^([0-9])\s*(\.?)\s*([0-9\.\s]*)$/$3/) {
666 my ($this_dur, $this_dots) = ($1, $2);
667 my $dur = raw_duration ($this_dur, $this_dots);
668 my $notehead = raw_notehead ($this_dur, $this_dots);
669 push @m, [$dur, $notehead];
671 return @m;
675 sub raw_duration {
676 my ($duration, $dots) = @_;
677 my %du=("1",4*$divisions,"2",2*$divisions,"4",$divisions,
678 "8",$divisions/2,"6",$divisions/4,"3",$divisions/8,
679 "5",$divisions/16,"7",$divisions/32,
680 "9",$divisions*8,"0",$divisions*16); # breve/long
681 my $res = $du{$duration};
682 if ($res) {
683 my $add = $res;
684 while ($dots ne "") {
685 $add /= 2;
686 $res += $add;
687 $dots =~ /^.(.*)$/gs;
688 $dots = $1;
691 return $res;
693 sub duration {
694 my ($duration, $dots) = @_;
695 if ($duration.$dots eq "7.") {
696 print_error ("Neumic notation is not supported by MusicXML!");
699 if ($duration.$dots ne "") {
700 $old_duration = raw_duration ($duration, $dots);
701 if ($old_duration eq "") {
702 print_error("strange duration '$duration'\n");
705 return $old_duration;
708 sub octave {
709 my ($octave) = @_;
711 if ($octave ne "") {
712 $octave =~ /^(.)(.*)$/gs;
713 if ($1 eq ",") {
714 $old_octave = 4 - length $octave;
715 } else {
716 $old_octave = 3 + length $octave;
719 return $old_octave;
722 sub clef {
723 my ($clef) = @_;
724 my $clefoctave = '';
725 if ($clef =~ /^(\w)(\-|\+)(\d)$/) {
726 ($clefsign, $clefline) = ($1, $3);
727 if ($2 =~ /^\+$/) {
728 print "Warning: Mensural clefs are not supported by MusicXML, using modern clef (input: $clef)\n";
730 if ($clefsign eq 'g') {
731 $clefsign = "G";
732 $clefoctave = " <clef-octave-change>-1</clef-octave-change>\n";
734 } else {
735 ($clefsign, $clefline) = ("G", 2);
737 return ' <clef>
738 <sign>'.$clefsign.'</sign>
739 <line>'.$clefline.'</line>
740 '.$clefoctave.' </clef>
744 sub keysignature {
745 my ($keysig) = @_;
747 # TODO: How is a change to C major represented? by "$ " or "$x " or "$b "?
748 # At the beginning, the $ part is left out, but mid-piece key changes
749 # need to way to clear all accidentals! We accept all three cases above!
750 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);
751 $keysig =~ s/(\s+)|&//gs; # it is unclear what the & means, so we'll ignore it for now.
752 $keysig =~ s/\[|\]//gs; # IGNORING brackets around a key sig.
753 $fifths = $fif{$keysig};
754 if ($fifths eq "") {
755 $fifths = "0";
756 print_error("Strange key signature '$keysig'.\n");
758 return ' <key>
759 <fifths>'.$fifths.'</fifths>
760 </key>
765 sub timesignature {
766 my ($timesig) = @_;
767 my $symbol = "";
769 if ($timesig =~ /^(o(\.)?)$/) {
770 if ($2 eq ".") {
771 $timesig = "9/8";
772 } else {
773 $timesig = "3/4";
775 print "Mensural time signature \"$1\" not supported, using $timesig.\n";
777 if ($timesig =~ /^(\d+\/\d+)( \d+\/\d+)+$/ ) {
778 print "Alternating time signature \"$timesig\" not supported by MusicXML, falling back to $1.\n";
779 $timesig = $1;
781 if ($timesig =~ /^c((\d+)(\/(\d+))?)$/gs) {
782 print "Time signature \"$timesig\" not supported by MusicXML, falling back to $1.\n";
783 $timesig = "$1"; # TODO: it would be better to show the "C". Example: 451.023.814
786 # For missing timesignature, fall back to "c"
787 if ($timesig eq "0" || $timesig eq "" || $timesig eq "c" ) {
788 $symbol = "common";
789 ($beats, $beattype) = (4,4);
790 } elsif ($timesig =~ /^c\/$/gi) {
791 $symbol = "cut";
792 ($beats, $beattype) = (2,2);
793 } elsif ($timesig =~ /^(\d+)\/(\d+)$/gs) {
794 ($beats, $beattype) = ($1, $2);
795 } elsif ($timesig =~/^(\d+)$/gs) {
796 $symbol = "single-number";
797 ($beats, $beattype) = ($1,2);
798 } else {
799 print_error("Time signature '$timesig' looks strange, falling back to 4/4.\n");
800 ($beats, $beattype) = (4,4);
802 if ($symbol) {
803 $symbol = " symbol=\"$symbol\"";
805 $timesig = " <time$symbol>
806 <beats>$beats</beats>
807 <beat-type>$beattype</beat-type>
808 </time>
810 return $timesig;
813 sub print_error {
814 my ($msg) = @_;
816 print "\nAn error occurred; context:\n\n$toprint\n
817 Error: $msg\n";
820 sub read_file {
821 my ($fn) = @_;
822 my $res = "";
823 if ($fn eq "-") {
824 while (<STDIN>) { $res .= $_; } # read all lines
825 } else {
826 if (!(open FH, $fn)) {
827 return "";
829 while (<FH>) { $res .= $_; } # read all lines
830 close (FH);
832 return $res;