3 # A program for processing Musescore XML files and halving the times of all the notes
4 # together with anything else that may be relevant (eg Time Sig, rests, trailing
5 # '_' after lyrics etc).
7 # Having written this and seen that there isn't really any state preserved from
8 # from one XML clause to another, it could all be done in an XSLT stylesheet. But I've
11 # Copyright (c) Dirk Koopman 2016
21 our %half = ( # decode from one note length to its half
22 # there may be mispellings, as I can't be bothered
23 # to look at the code, as I use this for early music
32 sixteenth thirtysecond
33 thirtysecond sixtyfourth
36 our %yesno = ( qw(yes 1 no 0) ); # used for turning translating yes/no text values
39 our $dbg = 1; # show debugging
40 our $removebeam = 1; # if set remove any BeamMode clauses
44 foreach my $fn (@ARGV) {
45 my ($name, $path, $suffix) = fileparse($fn, qr/\.[^.]*/);
47 if ($suffix eq ".mscx") {
49 $ofn = $path . $name . "-halved" . $suffix;
63 my $of = IO::File->new(">$ofn") or die "Cannot open $ofn $!\n";
64 my $p = XML::LibXML->new();
65 my $doc = $p->load_xml(location=>$ifn);
67 foreach my $staff ($doc->findnodes('/museScore/Score/Staff')) {
68 my ($sigN, $sigD); # current time sig values (may be needed later)
69 my $syllabic = 0; # track syllabic mode (whether we are in the middle of a word in lyrics).
70 display($staff) if $dbg;
71 foreach my $measure ($staff->findnodes('./Measure')) {
74 foreach my $node ($measure->findnodes('./*')) {
75 if ($node->nodeType == XML_ELEMENT_NODE) {
76 my $name = $node->nodeName;
77 if ($name eq 'Rest') {
78 my ($dt) = $node->findnodes('./durationType');
80 my $type = $dt->to_literal;
81 if ($type eq 'measure') {
82 my ($nz) = $node->findnodes('./duration/@z');
83 my ($nn) = $node->findnodes('./duration/@n');
84 my $was = $nn->to_literal;
85 my $now = $sigD || $was * 2;
86 my $z = $nz->to_literal;
87 display($staff, $measure, $node, "$type $z/$was -> $z/$now") if $dbg;
90 display($staff, $measure, $node, "$type -> $half{$type}") if $dbg;
91 $dt->firstChild->setData($half{$type});
94 } elsif ($name eq 'Chord') {
95 my ($dt) = $node->findnodes('./durationType');
97 my $type = $dt->to_literal;
98 display($staff, $measure, $node, "type $type -> $half{$type}") if $dbg;
99 $dt->firstChild->setData($half{$type});
101 my ($bm) = $node->findnodes('./BeamMode');
103 my $v = $bm->to_literal;
105 display($staff, $measure, $node, "remove BeamMode '$v'") if $dbg;
106 $node->removeChild($bm);
109 my ($lyrics) = $node->findnodes('./Lyrics');
111 my ($ticks) = $lyrics->findnodes('./ticks');
113 my $v = $ticks->to_literal;
115 display($staff, $measure, $node, $lyrics, "ticks $v -> $newv") if $dbg;
116 $ticks->firstChild->setData($newv);
119 # determine where we are in a word and if there is a <syllabic>
120 # clause, and it is necessary, add an appropriate one
122 # This is for dealing with musicxml imports where there is no
123 # explicit detection of trailing '-' signs, if there are and
124 # there is no <syllabic> add one of the correct sort and remove
125 # any trailing '-' from the text.
127 # Sadly, it's too much hard work to deal with trailing '_' 'cos
128 # mscore calulates the distance in advance because they appear
129 # to be too lazy to have another <syllabic> state to deal with
130 # it. Manual edit will therefore be required. Hopefully, not
132 my ($syl) = $lyrics->findnodes('./syllabic');
134 my $v = $syl->to_literal;
135 if ($v eq 'begin' || $v eq 'middle') {
136 display($staff, $measure, $node, $lyrics, "syllabic $v = $syllabic -> 1") if $dbg;
138 } elsif ($v eq 'end') {
139 display($staff, $measure, $node, $lyrics, "syllabic $v = $syllabic -> 0") if $dbg;
143 my ($text) = $lyrics->findnodes('text/text()');
145 my $v = $text->to_literal;
150 $newv = 'begin' unless $syllabic;
151 $newv = 'middle' if $syllabic;
153 $newtext =~ s/\-+$//;
155 $newv = 'end' if $syllabic;
159 display($staff, $measure, $node, $lyrics, "text '$v' -> '$newtext' create syllabic $newv sylstate $syllabic -> $newstate") if $dbg;
160 $syllabic = $newstate;
161 $text->setData($newtext) if $v ne $newtext;
162 my $newsyl = $doc->createElement('syllabic');
163 $newsyl->appendText($newv);
164 $lyrics->appendChild($newsyl);
169 } elsif ($name eq 'TimeSig') {
170 my ($sN) = $node->findnodes('./sigN');
171 my ($sD) = $node->findnodes('./sigD');
173 my $sn = $sN->to_literal;
174 my $sd = $sD->to_literal;
176 display($staff, $measure, $node, "$sn/$sd -> $sn/$newsd") if $dbg;
179 $sD->firstChild->setData($newsd);
187 print $of $doc->toString($doc);
195 foreach my $node (@_) {
196 if ((ref $node) =~ /XML/ && $node->nodeType == XML_ELEMENT_NODE) {
197 $s .= $node->nodeName . " ";
198 my @attr = $node->findnodes('@*');
200 $s .= $_->nodeName . " ";
201 $s .= $_->to_literal . " ";
215 say "$0: usage <filename.mscx> ...";