Recode everything to UTF-8
[pae2xml.git] / pae2xml.pl
blob730db04c0712e2bb0fb6b9ab9705b8885b073651
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>
425 } 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
426 # print "after replacement: $notes\n"; exit;
427 } elsif ($notes =~ /^ +(.*)$/) {
428 $notes = $1;
429 print("Invalid space encountered in notes before $notes\n");
430 } else {
431 print_error("got stuck with $notes\n");
432 $notes = "";
435 if ($mopen) {
436 print OUT " </measure>\n";
437 reset_measure_attributes ();
442 sub parse_note {
443 my($note, $keysig, $notation, $addition, $in_qq_group) = @_;
445 my ($fermata) = (0);
446 my ($actualnotes, $normalnotes) = (1,1);
448 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*$/) {
449 ($actualnotes, $normalnotes) = ($1, $2);
452 if ($note =~ /^\((.*)\)(.*)$/) {
453 $note = "$1$2";
454 $fermata = 1;
457 $note =~ /^({)?(\^)?(g|q)?((\,|\')*)(x|xx|b|bb|n)?(\d*)(\.*)(\-|A|B|C|D|E|F|G)(t?)(\+?)(}?)$/;
458 my ($beamstart, $chord, $gracecue, $oct, $acc, $dur, $dot, $pitch, $trill, $tie, $beamend) = ($1, $2, $3, $4, $6, $7, $8, $9, $10, $11, $12);
460 print OUT ' <note>
462 if ($gracecue eq "g") {
463 print OUT ' <grace slash="yes" steal-time-following="33"/>
466 if ($gracecue eq "q" || $in_qq_group) {
467 print OUT ' <grace/>
470 if ($pitch eq "-") {
471 print OUT " <rest />\n";
472 } else {
473 if ($chord eq "^") {
474 print OUT " <chord/>\n";
476 print OUT ' <pitch>
477 <step>'.$pitch.'</step>
478 '.alter($pitch, $acc, $keysig)
479 .' <octave>'.octave($oct).'</octave>
480 </pitch>
483 # We are using a rhythmic model, extract the correct duration
484 $this_duration = "";
485 $this_head = "";
486 if (($dur.$dot eq "") && scalar(@rhythmic_model)) {
487 if ($chord ne "^") {
488 $rhythmic_model_index = ($rhythmic_model_index +1 ) % scalar(@rhythmic_model);
490 $this_duration = $rhythmic_model[$rhythmic_model_index][0];
491 $this_head = $rhythmic_model[$rhythmic_model_index][1];
492 } elsif ($dur.$dot ne "" && scalar(@rhythmic_model)) {
493 # The rhythmic model ends when a new new rhythmic value appears!
494 @rhythmic_model = ();
496 if ($gracecue ne "g") {
497 if (!$this_duration) {
498 $this_duration = duration ($dur, $dot);
500 print OUT ' <duration>'.($this_duration*$normalnotes/$actualnotes).'</duration>
504 $tienotation = "";
505 if ($tie eq "+") {
506 if ($TIE) {
507 $tienotation = " <tied type=\"stop\"/>\n";
509 $tienotation .= " <tied type=\"start\"/>\n";
510 if (!$TIE) {
511 $TIE = 1;
512 print OUT ' <tie type="start"/>
515 } else {
516 if ($TIE) {
517 print OUT ' <tie type="stop"/>
519 $tienotation = " <tied type=\"stop\"/>\n";
520 $TIE = 0;
524 # Determine graphic notehead: acciaccaturas are always 8th, otherwise use duration
525 if ($gracecue eq "g") {
526 print OUT " <type>eighth</type>\n";
527 } else {
528 if (!$this_head) {
529 $this_head = notehead ($dur, $dot);
531 print OUT $this_head;
533 # If we have an explicit accidental on the note, print the <accidental> tag
534 print OUT accidental_explicit ($acc);
536 # addition is typically empty or a time-modification tag
537 print OUT $addition;
539 # print out beaming information if needed:
540 if (($beamstart eq "{") && ($beamend eq "}")) {
541 # Single-note beam means a hook
542 print OUT " <beam>forward hook</beam>\n";
543 } elsif ($beamstart eq "{") {
544 ++$BEAM;
545 print OUT " <beam number=\"$BEAM\">begin</beam>\n";
546 } elsif (($BEAM > 0) && ($beamend eq "}")) {
547 print OUT " <beam number=\"$BEAM\">end</beam>\n";
548 --$BEAM;
549 } elsif ($BEAM > 0) {
550 print OUT " <beam number=\"$BEAM\">continue</beam>\n";
553 my $notationbracket = $fermata || $tienotation || ($trill eq "t") || ($notation ne "");
554 if ($notationbracket) {
555 print OUT " <notations>\n";
557 if ($tienotation) {
558 print OUT $tienotation;
560 if ($fermata) {
561 print OUT " <fermata type=\"upright\"/>\n";
563 if ($trill eq "t") {
564 print OUT ' <ornaments>
565 <trill-mark/>
566 </ornaments>
569 if ($notation ne "") {
570 print OUT " $notation\n";
572 if ($notationbracket) {
573 print OUT " </notations>\n";
576 print OUT ' </note>
579 $toprint .= "note: oct. $oct/acc. $acc/dur. $dur/dots $dot/grace,cue $gracecue/pitch $pitch\n";
582 sub reset_measure_attributes {
583 %active_alterations = {};
584 # TODO: reset all measure-only attributes, like manual accidentals
587 sub alter {
588 my ($pitch, $acc, $keysig) = @_;
590 my $alt = 0;
591 # If we had the same pitch with explicit alteration already in the current
592 # measure, that alteration goes on, unless the current note has an explicit
593 # alteration
594 if ($acc eq "") {
595 $acc = $active_alterations{$pitch};
596 } else {
597 # Store the explicit alteration of the current pitch!
598 $active_alterations{$pitch} = $acc;
601 if (index ($keysig,$pitch) > -1) {
602 $keysig =~ /^(.).*$/gs;
603 if ($1 eq 'x') {
604 $alt = 1;
605 } else {$alt = -1;}
608 my %acc_alt = ("n", 0, "b", -1, "bb", -2, "x", 1, "xx", 2);
609 if ($acc_alt{$acc} ne "") {
610 $alt = $acc_alt{$acc};
613 if ($alt != 0) {
614 return "\t\t\t\t\t<alter>$alt</alter>\n";
616 return "";
619 sub accidental_explicit {
620 my ($acc) = @_;
621 my %accidentals = ("xx", "double-sharp", "x", "sharp", "n", "natural", "b", "flat", "bb", "flat-flat");
622 my $this_acc = $accidentals{$acc};
623 if ($this_acc) {
624 return "\t\t\t\t<accidental>$this_acc</accidental>\n";
625 } else {
626 return "";
630 sub raw_notehead {
631 my ($duration, $dots) = @_;
632 my %du=("0", "long", "9", "breve", "1", "whole", "2", "half", "4", "quarter",
633 "8", "eighth", "6", "16th", "3", "32nd", "5", "64th", "7", "128th");
634 if (($duration ne "") && $du{$duration}) {
635 my $res = " <type>$du{$duration}</type>\n";
636 $res .= repeat (" <dot/>\n", length ($dots));
637 return $res;
640 sub notehead {
641 my ($duration, $dots) = @_;
642 if ($duration.$dots ne "") {
643 my $head = raw_notehead ($duration, $dots);
644 $old_type = $head if $head;
646 return $old_type;
649 sub parse_rhythmic_model {
650 (my $model) = @_;
651 my @m = ();
652 while ($model =~ s/^([0-9])\s*(\.?)\s*([0-9\.\s]*)$/$3/) {
653 my ($this_dur, $this_dots) = ($1, $2);
654 my $dur = raw_duration ($this_dur, $this_dots);
655 my $notehead = raw_notehead ($this_dur, $this_dots);
656 push @m, [$dur, $notehead];
658 return @m;
662 sub raw_duration {
663 my ($duration, $dots) = @_;
664 my %du=("1",4*$divisions,"2",2*$divisions,"4",$divisions,
665 "8",$divisions/2,"6",$divisions/4,"3",$divisions/8,
666 "5",$divisions/16,"7",$divisions/32,
667 "9",$divisions*8,"0",$divisions*16); # breve/long
668 my $res = $du{$duration};
669 if ($res) {
670 my $add = $res;
671 while ($dots ne "") {
672 $add /= 2;
673 $res += $add;
674 $dots =~ /^.(.*)$/gs;
675 $dots = $1;
678 return $res;
680 sub duration {
681 my ($duration, $dots) = @_;
682 if ($duration.$dots eq "7.") {
683 print_error ("Neumic notation is not supported by MusicXML!");
686 if ($duration.$dots ne "") {
687 $old_duration = raw_duration ($duration, $dots);
688 if ($old_duration eq "") {
689 print_error("strange duration '$duration'\n");
692 return $old_duration;
695 sub octave {
696 my ($octave) = @_;
698 if ($octave ne "") {
699 $octave =~ /^(.)(.*)$/gs;
700 if ($1 eq ",") {
701 $old_octave = 4 - length $octave;
702 } else {
703 $old_octave = 3 + length $octave;
706 return $old_octave;
709 sub clef {
710 my ($clef) = @_;
711 my $clefoctave = '';
712 if ($clef =~ /^(\w)(\-|\+)(\d)$/) {
713 ($clefsign, $clefline) = ($1, $3);
714 if ($2 =~ /^\+$/) {
715 print "Warning: Mensural clefs are not supported by MusicXML, using modern clef (input: $clef)\n";
717 if ($clefsign eq 'g') {
718 $clefsign = "G";
719 $clefoctave = " <clef-octave-change>-1</clef-octave-change>\n";
721 } else {
722 ($clefsign, $clefline) = ("G", 2);
724 return ' <clef>
725 <sign>'.$clefsign.'</sign>
726 <line>'.$clefline.'</line>
727 '.$clefoctave.' </clef>
731 sub keysignature {
732 my ($keysig) = @_;
734 # TODO: How is a change to C major represented? by "$ " or "$x " or "$b "?
735 # At the beginning, the $ part is left out, but mid-piece key changes
736 # need to way to clear all accidentals! We accept all three cases above!
737 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);
738 $keysig =~ s/(\s+)|&//gs; # it is unclear what the & means, so we'll ignore it for now.
739 $keysig =~ s/\[|\]//gs; # IGNORING brackets around a key sig.
740 $fifths = $fif{$keysig};
741 if ($fifths eq "") {
742 $fifths = "0";
743 print_error("Strange key signature '$keysig'.\n");
745 return ' <key>
746 <fifths>'.$fifths.'</fifths>
747 </key>
752 sub timesignature {
753 my ($timesig) = @_;
754 my $symbol = "";
756 if ($timesig =~ /^(o(\.)?)$/) {
757 if ($2 eq ".") {
758 $timesig = "9/8";
759 } else {
760 $timesig = "3/4";
762 print "Mensural time signature \"$1\" not supported, using $timesig.\n";
764 if ($timesig =~ /^(\d+\/\d+)( \d+\/\d+)+$/ ) {
765 print "Alternating time signature \"$timesig\" not supported by MusicXML, falling back to $1.\n";
766 $timesig = $1;
768 if ($timesig =~ /^c((\d+)(\/(\d+))?)$/gs) {
769 print "Time signature \"$timesig\" not supported by MusicXML, falling back to $1.\n";
770 $timesig = "$1"; # TODO: it would be better to show the "C". Example: 451.023.814
773 # For missing timesignature, fall back to "c"
774 if ($timesig eq "0" || $timesig eq "" || $timesig eq "c" ) {
775 $symbol = "common";
776 ($beats, $beattype) = (4,4);
777 } elsif ($timesig =~ /^c\/$/gi) {
778 $symbol = "cut";
779 ($beats, $beattype) = (2,2);
780 } elsif ($timesig =~ /^(\d+)\/(\d+)$/gs) {
781 ($beats, $beattype) = ($1, $2);
782 } elsif ($timesig =~/^(\d+)$/gs) {
783 $symbol = "single-number";
784 ($beats, $beattype) = ($1,2);
785 } else {
786 print_error("Time signature '$timesig' looks strange, falling back to 4/4.\n");
787 ($beats, $beattype) = (4,4);
789 if ($symbol) {
790 $symbol = " symbol=\"$symbol\"";
792 $timesig = " <time$symbol>
793 <beats>$beats</beats>
794 <beat-type>$beattype</beat-type>
795 </time>
797 return $timesig;
800 sub print_error {
801 my ($msg) = @_;
803 print "\nAn error occurred; context:\n\n$toprint\n
804 Error: $msg\n";
807 sub read_file {
808 my ($fn) = @_;
809 my $res = "";
810 if ($fn eq "-") {
811 while (<STDIN>) { $res .= $_; } # read all lines
812 } else {
813 if (!(open FH, $fn)) {
814 return "";
816 while (<FH>) { $res .= $_; } # read all lines
817 close (FH);
819 return $res;