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
20 use File::Temp qw{ :mktemp };
27 our %half = ( # decode from one note length to its half
44 our %yesno = ( qw(yes 1 no 0) ); # used for turning translating yes/no text values
47 our $dbg = 0; # show debugging
48 our $removebeam = 1; # if set remove any BeamMode clauses
52 binmode STDOUT, "utf8";
54 foreach my $fn (@ARGV) {
57 usage() if $fn =~ /^\-+[\?h]/i;
58 $dbg ^= 1 if $fn =~ /^\-+x/;
59 $removebeam ^= 1 if $fn =~ /^\-+b/;
61 my ($ifn, $ofn, $tfn);
63 my ($name, $path, $suffix) = fileparse($fn, qr/\.[^.]*/);
64 if ($suffix eq ".mscx" || $suffix eq ".mscz") {
66 $ofn = $path . $name . "-halved.mscx";
68 # extract out the zipped up .mscx file from an .mscz archive
69 if ($suffix eq '.mscz') {
70 $tfn = mktemp("/tmp/msczXXXXXXX");
73 system("unzip -p $ifn $xifn > $tfn");
74 $ifn = $tfn; # the tmp file is the actual input.
77 usage("Only Musescore .mscx or .mscz files allowed (got: $fn)");
80 process($ifn, $ofn, $fn);
89 my ($ifn, $ofn, $fn) = @_;
91 my $p = XML::LibXML->new();
92 my $doc = eval { $p->load_xml(location=>$ifn) };
94 usage("Invalid Musescore file detected (in $fn) $@") unless $doc;
98 my ($muse) = $doc->findnodes('/museScore');
100 my ($v) = $muse->findnodes('./@version');
101 $version = $v->to_literal if $v;
103 if (!$version || $version < 2) {
104 $version ||= "Unknown";
105 usage("Version $version detected in $fn, this program will only work with MuseScore 2 (or greater) files");
108 my $of = IO::File->new(">$ofn") or usage("Cannot open $ofn $!");
110 foreach my $staff ($doc->findnodes('/museScore/Score/Staff')) {
111 my ($sigN, $sigD); # current time sig values (may be needed later)
112 my $syllabic = 0; # track syllabic mode (whether we are in the middle of a word in lyrics).
114 display($staff) if $dbg;
116 foreach my $measure ($staff->findnodes('./Measure')) {
119 # obtain the measure no and any len attr. Change the len attribute
120 my ($l) = $measure->findnodes('./@len');
122 my ($t,$b) = split m{/}, $l->to_literal;
129 foreach my $node ($measure->findnodes('./*')) {
130 if ($node->nodeType == XML_ELEMENT_NODE) {
131 my $name = $node->nodeName;
132 if ($name eq 'Rest') {
133 my ($dt) = $node->findnodes('./durationType');
135 my $type = $dt->to_literal;
136 if ($type eq 'measure') {
137 my ($nz) = $node->findnodes('./duration/@z');
138 my ($nn) = $node->findnodes('./duration/@n');
139 my $was = $nn->to_literal;
141 my $z = $nz->to_literal;
142 display($staff, $measure, $node, "$type $z/$was -> $z/$now") if $dbg;
145 display($staff, $measure, $node, "$type -> $half{$type}") if $dbg;
146 $dt->firstChild->setData($half{$type});
149 } elsif ($name eq 'Chord') {
150 my ($dt) = $node->findnodes('./durationType');
152 my $type = $dt->to_literal;
153 display($staff, $measure, $node, "type $type -> $half{$type}") if $dbg;
154 $dt->firstChild->setData($half{$type});
156 my ($bm) = $node->findnodes('./BeamMode');
158 my $v = $bm->to_literal;
160 display($staff, $measure, $node, "remove BeamMode '$v'") if $dbg;
161 $node->removeChild($bm);
164 my ($lyrics) = $node->findnodes('./Lyrics');
166 my ($ticks) = $lyrics->findnodes('./ticks');
168 my $v = $ticks->to_literal;
170 display($staff, $measure, $node, $lyrics, "ticks $v -> $newv") if $dbg;
171 $ticks->firstChild->setData($newv);
174 # determine where we are in a word and if there is a <syllabic>
175 # clause, note its value (which is "in word" or "not in word")
177 # This is for dealing with musicxml imports where there is no
178 # explicit detection of trailing '-' signs, if there are such signs and
179 # there is no <syllabic> clause, add one of the correct sort and remove
180 # any trailing '-' from the text.
182 # Sadly, it's too much hard work to deal with any trailing '_' 'cos
183 # mscore calulates the distance in advance because they appear
184 # to be too lazy to have another <syllabic> state to deal with
185 # it. Manual edit will therefore be required. Hopefully, not
187 my ($syl) = $lyrics->findnodes('./syllabic');
189 my $v = $syl->to_literal;
190 if ($v eq 'begin' || $v eq 'middle') {
191 display($staff, $measure, $node, $lyrics, "syllabic $v = $syllabic -> 1") if $dbg;
193 } elsif ($v eq 'end') {
194 display($staff, $measure, $node, $lyrics, "syllabic $v = $syllabic -> 0") if $dbg;
198 my ($text) = $lyrics->findnodes('text/text()');
200 my $v = $text->to_literal;
205 $newv = 'begin' unless $syllabic;
206 $newv = 'middle' if $syllabic;
208 $newtext =~ s/[-–]+$//;
210 $newv = 'end' if $syllabic;
214 display($staff, $measure, $node, $lyrics, "text '$v' -> '$newtext' create syllabic $newv sylstate $syllabic -> $newstate") if $dbg;
215 $syllabic = $newstate;
216 $text->setData($newtext) if $v ne $newtext;
217 my $newsyl = $doc->createElement('syllabic');
218 $newsyl->appendText($newv);
219 $lyrics->appendChild($newsyl);
224 } elsif ($name eq 'TimeSig') {
225 my ($sN) = $node->findnodes('./sigN');
226 my ($sD) = $node->findnodes('./sigD');
228 my $sn = $sN->to_literal;
229 my $sd = $sD->to_literal;
231 display($staff, $measure, $node, "$sn/$sd -> $sn/$newsd") if $dbg;
234 $sD->firstChild->setData($newsd);
242 print $of $doc->toString($doc);
250 foreach my $node (@_) {
251 if ((ref $node) =~ /XML/ && $node->nodeType == XML_ELEMENT_NODE) {
252 $s .= $node->nodeName . " ";
253 my @attr = $node->findnodes('@*');
255 $s .= $_->nodeName . " ";
256 $s .= $_->to_literal . " ";
271 my ($name, $path, $suffix) = fileparse($0, qr/\.[^.]*/);
272 $name = "$name$suffix: ";
278 say "${name}version $VERSION usage: [-b] [-x] <filename.msc[xz]> ...\n";
279 say "\tA program to halve the note values of a MuseScore 2.x file.\n";
280 say "\tThis designed to be used to convert 'early music' note values";
281 say "\tinto something more 'modern'. It will also sort out things like";
282 say "\tinter-syllablic hyphenation if it comes across trailing hyphens";
283 say "\tsuch as come from imported Finale musicxml files.";
284 say "\n\tfilenames: 'a.mscz' (or 'a.mscx') will be written to 'a-halved.mscx'.";
285 say "\tYou can do several files at a time!\n";
286 say "\n\tArguments:";
287 say "\t-b - normally any beaming is converted to auto, use this to retain beaming info";
288 say "\t-x - enable debugging (actually more a stream of conscienceness)";