NWC2MusicXML/MusicXML.pm

filter: Statementstmt 92.6% 815 / 880 Branchbran 86.9% 287 / 330 Conditioncond 59.5% 233 / 391 MC/DCmcdc 31.7% 67 / 211 Subroutinesub 94.7% 54 / 57 Podpod 100% 2 / 2 totaltotal 77.9% 1458 / 1871 CC 363 SCAR 33.3
CC
363
Cov
83%
_emit_measure
91.7
_emit_note_event
51.2
_emit_identification
30.7
CRAP 27.9
↑ Summary NWC2MusicXML/Score.pm »
1 package Music::NWC2MusicXML::MusicXML;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $VERSION = '0.003.0';
8  
9 13 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Music::NWC2MusicXML::Score;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Music::NWC2MusicXML::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # ---------------------------------------------------------------------------
20 # MusicXML structural constants
21 # ---------------------------------------------------------------------------
22  
23 # MusicXML version targeted (supported by MuseScore 3+)
24 13 Readonly::Scalar my $MUSICXML_VERSION => '4.0';
25  
26 # DOCTYPE public/system identifiers
27 13 Readonly::Scalar my $DOCTYPE_PUBLIC =>
28 '-//Recordare//DTD MusicXML 4.0 Partwise//EN';
29 13 Readonly::Scalar my $DOCTYPE_SYSTEM =>
30 'http://www.musicxml.org/dtds/partwise.dtd';
31  
32 # Default divisions per quarter note used as a fallback; the actual value is
33 # computed per-score from the LCM of all note-duration denominators.
34 13 Readonly::Scalar my $DEFAULT_DIVISIONS => 24;
35  
36 # Maximum number of simultaneous open slurs per staff
37 13 Readonly::Scalar my $MAX_SLUR_NUMBER => 6;
38  
39 13 Readonly::Scalar my $QUARTERS_PER_WHOLE => 4;
40  
41 # Event types that occupy time in a measure.
42 13 Readonly::Hash my %SOUNDING_TYPES => map { $_ => 1 } qw(Note Rest Chord);
43  
44 # ---------------------------------------------------------------------------
45 # Clef mappings: NWC clef name -> { sign, line, [clef-octave-change] }
46 # ---------------------------------------------------------------------------
47 13 Readonly::Hash my %CLEF_MAP => (
48 Treble => { sign => 'G', line => 2 },
49 Bass => { sign => 'F', line => 4 },
50 Alto => { sign => 'C', line => 3 },
51 Tenor => { sign => 'C', line => 4 },
52 Percussion => { sign => 'percussion' },
53 Tab => { sign => 'TAB' },
54 );
55  
56 # ---------------------------------------------------------------------------
57 # Articulation mappings: NWC token (initial-cap) -> { element, group }
58 # group: 'articulations' | 'ornaments' | 'direct' (emitted directly in <notations>)
59 13 Readonly::Hash my %ARTICULATION_MAP => (
60 Tenuto => { element => 'tenuto', group => 'articulations' },
61 Staccato => { element => 'staccato', group => 'articulations' },
62 Accent => { element => 'accent', group => 'articulations' },
63 Marcato => { element => 'strong-accent', group => 'articulations' },
64 Staccatissimo => { element => 'staccatissimo', group => 'articulations' },
65 Fermata => { element => 'fermata', group => 'direct' },
66 Trill => { element => 'trill-mark', group => 'ornaments' },
67 Mordent => { element => 'mordent', group => 'ornaments' },
68 Turn => { element => 'turn', group => 'ornaments' },
69 );
70  
71 # ---------------------------------------------------------------------------
72 # Dynamic markings: NWC -> MusicXML element
73 # ---------------------------------------------------------------------------
74 13 Readonly::Hash my %DYNAMIC_MAP => map { $_ => $_ }
75 qw(pppp ppp pp p mp mf f ff fff ffff);
76  
77 # ---------------------------------------------------------------------------
78 # Tempo base unit: NWC Base field -> { unit, dot, quarter_factor }
79 # quarter_factor converts noted BPM to quarter-note BPM for <sound tempo="..."/>
80 # ---------------------------------------------------------------------------
81 13 Readonly::Hash my %TEMPO_BASE_MAP => (
82 'Whole' => { unit => 'whole', dot => 0, factor => 4 },
83 'Half' => { unit => 'half', dot => 0, factor => 2 },
84 'Quarter' => { unit => 'quarter', dot => 0, factor => 1 },
85 'Eighth' => { unit => 'eighth', dot => 0, factor => 0.5 },
86 'Sixteenth' => { unit => '16th', dot => 0, factor => 0.25 },
87 'Dotted Whole' => { unit => 'whole', dot => 1, factor => 6 },
88 'Dotted Half' => { unit => 'half', dot => 1, factor => 3 },
89 'Dotted Quarter' => { unit => 'quarter', dot => 1, factor => 1.5 },
90 'Dotted Eighth' => { unit => 'eighth', dot => 1, factor => 0.75 },
91 'Dotted Sixteenth' => { unit => '16th', dot => 1, factor => 0.375 },
92 );
93  
94 # ---------------------------------------------------------------------------
95 # Pitch-conversion constants
96 # ---------------------------------------------------------------------------
97  
98 # Reference note (step_index, octave) for NWC position 0 = middle line (3rd from bottom) of each staff.
99 # Step indices: C=0, D=1, E=2, F=3, G=4, A=5, B=6
100 # Verified against Pilgrim.nwc: Bass pos -7 = A2 (A-drone), Treble pos -9 = D4 (tonic).
101 13 Readonly::Hash my %CLEF_REF => (
102 Treble => [ 6, 4 ], # B4 (middle line)
103 Bass => [ 1, 3 ], # D3 (middle line)
104 Alto => [ 0, 4 ], # C4 (middle line)
105 Tenor => [ 5, 3 ], # A3 (middle line)
106 Percussion => [ 6, 4 ], # B4 (treat as treble)
107 Tab => [ 6, 4 ], # B4 (treat as treble)
108 );
109  
110 # Diatonic step names indexed 0-6
111 13 Readonly::Array my @STEP_NAMES => qw(C D E F G A B);
112  
113 # Circle-of-fifths order: sharps = F C G D A E B; flats = B E A D G C F
114 13 Readonly::Array my @SHARP_STEPS => ( 3, 0, 4, 1, 5, 2, 6 );
115 13 Readonly::Array my @FLAT_STEPS => ( 6, 2, 5, 1, 4, 0, 3 );
116  
117 # NWC accidental prefix -> [ semitone_alter, MusicXML accidental name ]
118 # P1: empty prefix and unknown prefixes fall back to key-signature alter (no accidental element).
119 # P2: 'x' is NWC's alias for '##' (double-sharp written as a cross).
120 13 Readonly::Hash my %ACCIDENTAL_MAP => (
121 '#' => [ 1, 'sharp' ],
122 '##' => [ 2, 'double-sharp' ],
123 'x' => [ 2, 'double-sharp' ],
124 'b' => [ -1, 'flat' ],
125 'bb' => [ -2, 'double-flat' ],
126 'n' => [ 0, 'natural' ],
127 );
128  
129 # NWC base-duration name -> MusicXML type string
130 13 Readonly::Hash my %NWC_TYPE_MAP => (
131 Whole => 'whole',
132 Half => 'half',
133 '4th' => 'quarter',
134 '8th' => 'eighth',
135 '16th' => '16th',
136 '32nd' => '32nd',
137 '64th' => '64th',
138 );
139  
140 # Flow-control directive -> MusicXML element and/or words direction
141 13 Readonly::Hash my %FLOW_CONTROL_MAP => (
142 Coda => { element => 'coda' },
143 Segno => { element => 'segno' },
144 DaCapo => { words => 'D.C.' },
145 DaCapoAlFine => { words => 'D.C. al Fine' },
146 DaCapoAlCoda => { words => 'D.C. al Coda' },
147 DalSegno => { words => 'D.S.' },
148 DalSegnoAlFine => { words => 'D.S. al Fine' },
149 DalSegnoAlCoda => { words => 'D.S. al Coda' },
150 Fine => { words => 'Fine' },
151 ToCoda => { element => 'coda', words => 'To Coda' },
152 );
153  
154 13 Readonly::Hash my %MESSAGES => (
155 error_bad_score => 'generate: argument must be a Music::NWC2MusicXML::Score',
156 error_no_staves => 'Score contains no staves -- cannot generate MusicXML',
157 error_write_failed => 'Cannot write to output: %s',
158 error_internal => 'Internal error: %s',
159 warn_unknown_clef => 'Unrecognised NWC clef %s -- defaulting to Treble',
160 warn_unknown_dynamic => 'Unrecognised dynamic marking %s',
161 warn_unsupported_art => 'Unsupported articulation %s',
162 warn_approx_bar => 'Bar style %s approximated as regular',
163 );
164  
165 =head1 NAME
166  
167 Music::NWC2MusicXML::MusicXML - Convert an internal Score object to a MusicXML 4.0 document string.
168  
169 =head1 VERSION
170  
171 0.003.0
172  
173 =head1 SYNOPSIS
174  
175 # --- Pattern 1: full pipeline from a .nwc file ---
176 use Music::NWC2MusicXML::NWC;
177 use Music::NWC2MusicXML::Parser;
178 use Music::NWC2MusicXML::MusicXML;
179  
180 my $nwctxt = Music::NWC2MusicXML::NWC->read('my_score.nwc');
181 my $score = Music::NWC2MusicXML::Parser->new->parse($nwctxt);
182 my $xml = Music::NWC2MusicXML::MusicXML->new->generate($score);
183  
184 # Write raw bytes -- the string is already pure ASCII (numeric entities
185 # for any non-ASCII source characters).
186 open my $fh, '>:raw', 'output.musicxml' or die $!;
187 print $fh $xml;
188 close $fh;
189  
190 # --- Pattern 2: custom indentation ---
191 my $gen = Music::NWC2MusicXML::MusicXML->new(indent => "\t");
192 my $xml = $gen->generate($score);
193  
194 # --- Pattern 3: validate the output with an external tool ---
195 # (run in the shell after writing the file)
196 # xmllint --noout output.musicxml
197  
198 # --- Pattern 4: generate and keep in memory for further processing ---
199 my $xml_string = Music::NWC2MusicXML::MusicXML->new->generate($score);
200 my @lines = split /\n/, $xml_string;
201 my ($part_list) = grep { /part-list/ } @lines;
202  
203 =head1 DESCRIPTION
204  
205 C<Music::NWC2MusicXML::MusicXML> is the last stage of the NWC-to-MusicXML
206 pipeline. It takes a C<Music::NWC2MusicXML::Score> object -- the internal
207 representation built by C<Music::NWC2MusicXML::Parser> -- and returns a
208 self-contained MusicXML 4.0 document as a plain string.
209  
210 The generator knows nothing about the NWCTXT or NWC binary format. Every
211 musical decision (pitches, durations, articulations, dynamics, tempo, key,
212 clef, copyright text) was already made by the parser. The generator only
213 serialises the Score object tree into valid XML.
214  
215 =head2 Divisions
216  
217 MusicXML requires one integer, C<< <divisions> >>, that says how many ticks
218 equal one quarter note. To represent every note duration exactly -- including
219 unusual tuplet values -- the generator scans all note durations across all
220 staves, collects the denominators of their rational representations, and
221 computes their least common multiple (LCM). That LCM becomes
222 C<< <divisions> >>. No duration is ever rounded or truncated.
223  
224 =head2 Page layout and credits
225  
226 The generator emits a C<< <defaults> >> block that records the actual page
227 size and margin values expressed in MusicXML tenths. Without this block a
228 renderer cannot interpret the absolute coordinate values used by credit
229 elements, so title and copyright placement would be undefined.
230  
231 Page dimensions default to A4 (210 x 297 mm) with 1.27 cm margins, which
232 match NWC's own defaults. If the source NWC file contained a C<PgMargins>
233 record the parser stores the margin values in the Score's C<page_setup>
234 hashref and the generator reads them from there.
235  
236 Each title, subtitle, and copyright line is emitted as its own separate
237 C<< <credit> >> element:
238  
239 =over 4
240  
241 =item * Title -- C<< <credit page="1"> >>; large font; centred near the top
242 of page 1.
243  
244 =item * Subtitle (the NWC Author field) -- C<< <credit page="1"> >>; medium
245 font; centred directly below the title.
246  
247 =item * Each copyright line -- C<< <credit> >> with B<no page attribute>;
248 this instructs conforming renderers to display the line on B<every> page.
249 One separate C<< <credit> >> element is used per line; putting multiple
250 C<< <credit-words> >> inside a single C<< <credit> >> causes many renderers
251 to display only the last one.
252  
253 =back
254  
255 =head2 Articulations
256  
257 NWC encodes articulations as initial-capital tokens in the C<Dur:> field
258 (for example C<Tenuto>, C<Staccato>, C<Accent>). The generator groups them
259 into the correct MusicXML wrapper:
260  
261 =over 4
262  
263 =item * C<< <articulations> >> -- tenuto, staccato, accent, strong-accent,
264 staccatissimo.
265  
266 =item * C<< <ornaments> >> -- trill-mark, mordent, turn.
267  
268 =item * Direct child of C<< <notations> >> -- fermata.
269  
270 =back
271  
272 The C<Slur> token is never emitted as an articulation; it is handled by the
273 slur-annotation pre-pass (see L</Slurs and ties> below).
274  
275 =head2 Slurs and ties
276  
277 A single pre-pass over all events in a staff (C<_annotate_events>) detects
278 slur arcs and tie pairs before the events are grouped into measures. This
279 means arcs that cross a bar line are handled correctly. Each event is
280 annotated with flags that the measure emitter reads when writing
281 C<< <slur> >> and C<< <tied> >> elements.
282  
283 Tie detection uses the raw NWC position string as a key. A C<^> suffix on a
284 position string (e.g. C<Pos:-7^>) means the note is tied forward; the
285 generator strips the suffix to match the tied-to note.
286  
287 =head2 Dynamic variance (text markings)
288  
289 NWC C<DynVariance> (also exported as C<DynamicVariance> by some NWC versions)
290 records represent written-out dynamic variance text — not hairpins.
291 C<Style:Crescendo> produces C<< <words font-style="italic">cresc.</words> >>;
292 C<Style:Diminuendo> produces C<< <words font-style="italic">dim.</words> >>;
293 C<Style:crescOff> (the end-of-phrase marker for an extended text cresc.) emits
294 nothing, since MusicXML text directions require no explicit stop element.
295  
296 Do not confuse these with hairpin crescendo/diminuendo wedges: those come from
297 C<Opts:Crescendo> / C<Opts:Diminuendo> attached to individual note or rest
298 events (see L</Hairpins (wedges)> below).
299  
300 =head2 Hairpins (wedges)
301  
302 NWC does not use standalone records for in-staff hairpins. Instead it
303 attaches C<Opts:Crescendo> or C<Opts:Diminuendo> to every note and rest that
304 sits under the arc. A second pre-pass (C<_annotate_wedges>) detects the
305 start and end of each arc by watching for transitions: the first event
306 carrying a hairpin flag starts the arc; the first event that drops the flag
307 closes it. The wedge stop is emitted as a C<crescOff> direction immediately
308 after the last note of the arc.
309  
310 =head2 Tempo marks
311  
312 A C<Tempo> event carries three data keys: C<bpm> (integer BPM), C<base> (NWC
313 beat-unit string such as C<'Quarter'> or C<'Dotted Half'>), and C<text>
314 (optional free-text string from the NWC C<Text:> field, e.g.
315 C<'With Expression'>; empty string when absent).
316  
317 When C<text> is non-empty, C<_emit_tempo> emits it as a
318 C<< <words font-style="italic"> >> C<< <direction-type> >> block before the
319 C<< <metronome> >> block, both within the same C<< <direction> >> element.
320 The metronome element uses C<parentheses="yes"> in this case so renderers
321 display the marking as e.g. I<With Expression (crotchet = 92)>. When C<text>
322 is empty only the metronome block is emitted and C<parentheses="no"> is used.
323  
324 =head2 Tempo variance
325  
326 Markings such as C<Accelerando>, C<Ritardando>, C<Ritenuto>, C<Rallentando>,
327 and C<RitardandoToTempo> (rendered as "a tempo") are stored as
328 C<TempoVariance> events by the parser. The generator converts them to italic
329 C<< <words> >> direction elements using the C<%TEMPO_VARIANCE_TEXT> table.
330 Both C<Ritardando> and C<Ritenuto> abbreviate to "rit.".
331  
332 The C<Style:Fermata> value is a special case: it is not a tempo-change
333 direction but a symbol placed over the note or rest that immediately follows
334 the C<TempoVariance> record. C<_annotate_fermatas> detects these records in
335 a pre-pass and marks the following sounding event so that C<_emit_note_event>
336 and C<_emit_rest_event> emit C<< <notations><fermata/></notations> >> on it.
337 The C<TempoVariance:Fermata> event itself is then skipped during measure
338 emission and never produces a C<< <direction> >> element.
339  
340 =head2 Part name resolution
341  
342 Display names for each staff are resolved in three steps:
343  
344 =over 4
345  
346 =item 1. Use the NWC staff name, if it is not a generic default such as
347 C<Staff> or C<Staff-2>.
348  
349 =item 2. Fall back to the MIDI instrument name.
350  
351 =item 3. Fall back to the positional name C<Staff-N> (1-based).
352  
353 =back
354  
355 After all candidates are found, any name that appears on more than one staff
356 is replaced with C<Staff-N> to guarantee unique C<< <part-name> >> values.
357  
358 =head1 COMMON PITFALLS
359  
360 =over 4
361  
362 =item B<Opening the output file in text mode>
363  
364 The output string is pure ASCII (all non-ASCII characters are escaped as
365 numeric XML entities). Opening the output file with C<< '>:encoding(UTF-8)' >>
366 is harmless but unnecessary; opening it with C<< '>:encoding(Latin-1)' >> or
367 a similar 8-bit encoding and then printing a string that contains non-ASCII
368 bytes would corrupt the file. The safest choice is C<< '>:raw' >>.
369  
370 =item B<Multiple copyright lines in one credit element>
371  
372 If you call C<_emit_credits> and place two C<< <credit-words> >> children
373 inside a single C<< <credit> >> element, most renderers (including MuseScore
374 and Finale) display only the B<last> C<< <credit-words> >> and silently
375 discard the rest. This module uses one C<< <credit> >> per copyright line to
376 avoid this.
377  
378 =item B<Missing defaults section>
379  
380 The absolute coordinates in C<< <credit> >> elements (C<default-x>,
381 C<default-y>) are measured in MusicXML "tenths" from the bottom-left corner
382 of the page. They are meaningless to a renderer unless a C<< <defaults> >>
383 block defines the page size and the tenths-per-mm scaling factor. This module
384 always emits C<< <defaults> >> before any C<< <credit> >> elements.
385  
386 =item B<Two separate rights elements in identification>
387  
388 C<< <identification> >> accepts only one C<< <rights> >> child in practice;
389 a second one shadows the first. This module joins multiple copyright lines
390 with a newline character inside a single C<< <rights> >> element.
391  
392 =item B<Slur token treated as an articulation>
393  
394 NWC encodes slurs as C<Slur> in the same token list as articulations. Do not
395 add C<Slur> to C<%ARTICULATION_MAP>; the slur-annotation pre-pass handles it.
396 If C<Slur> were also emitted as an articulation element the output XML would be
397 invalid.
398  
399 =item B<Score with no staves>
400  
401 Calling C<generate> on a C<Score> object that has no staves will C<croak>
402 immediately. Always check that the parser produced at least one staff before
403 calling the generator.
404  
405 =back
406  
407 =head1 ENCODING
408  
409 =over 4
410  
411 =item B<Input (Score metadata fields)>
412  
413 Metadata strings (Title, Author, Copyright1, Copyright2, etc.) may contain any
414 Unicode characters, including non-ASCII letters, accented characters, and the
415 copyright symbol (U+00A9). The NWC binary decoder may deliver these as Latin-1
416 bytes; once stored in Perl scalars they are handled correctly as long as they
417 pass through C<_xml_escape> before being written to the output.
418  
419 =item B<Output (the generated XML string)>
420  
421 The string returned by C<generate> is B<pure 7-bit ASCII>. Every character
422 whose code point is above 127 is converted to a numeric XML character
423 reference (C<&#N;>), for example C<&#169;> for the copyright symbol. The XML
424 declaration at the top of the document reads C<encoding="UTF-8">, which
425 remains correct because numeric character references are valid in any XML
426 encoding.
427  
428 =item B<Emojis and full Unicode>
429  
430 Emoji and supplementary-plane characters (code points above U+FFFF) are not
431 tested but will be escaped correctly by C<_xml_escape> as long as Perl has
432 decoded them to proper Unicode code points (i.e. C<utf8::decode> has been
433 applied or the string was read with a C<:utf8> layer). Raw multi-byte UTF-8
434 bytes that have B<not> been decoded will be escaped byte-by-byte and will
435 produce incorrect numeric references.
436  
437 =back
438  
439 =cut
440  
441 # ---------------------------------------------------------------------------
442 # new
443 # ---------------------------------------------------------------------------
444  
445 =head2 new
446  
447 Create a new generator object.
448  
449 =head3 Purpose
450  
451 Factory constructor. Creates a configured generator ready to call C<generate>
452 one or more times. The same generator instance can be used to process
453 multiple Score objects; each C<generate> call is independent.
454  
455 =head3 Arguments
456  
457 All arguments are named (passed as a flat key/value list) and optional.
458  
459 =over 4
460  
461 =item C<indent>
462  
463 The string used for one level of XML indentation. Defaults to two spaces.
464 Pass C<"\t"> for tab indentation. Only affects whitespace; the XML content
465 is identical regardless of the indent setting.
466  
467 =item C<diagnostics>
468  
469 A C<Music::NWC2MusicXML::Diagnostics> instance for routing warning messages.
470 When omitted, warnings are sent directly to C<carp>.
471  
472 =back
473  
474 =head3 Returns
475  
476 A blessed C<Music::NWC2MusicXML::MusicXML> object.
477  
478 =head3 Side Effects
479  
480 None.
481  
482 =head3 Usage Example
483  
484 # Default (two-space indent)
485 my $gen = Music::NWC2MusicXML::MusicXML->new;
486  
487 # Tab indent
488 my $gen = Music::NWC2MusicXML::MusicXML->new(indent => "\t");
489  
490 =head3 API SPECIFICATION
491  
492 =head4 Input
493  
494 indent : SCALAR (optional, default ' ')
495 diagnostics : OBJECT Music::NWC2MusicXML::Diagnostics (optional)
496  
497 =head4 Output
498  
499 Music::NWC2MusicXML::MusicXML object
500  
501 =head3 MESSAGES
502  
503 This method does not emit any diagnostic messages.
504  
505 =cut
506  
507 228 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
508 228 my ($class, %input) = @_;
509 228 my $args = validate_strict(
510 schema => {
511 diagnostics => { type => 'object', optional => 1 },
512 indent => { type => 'scalar', optional => 1, default => ' ' },
513 },
514 input => \%input,
515 );
516 228 ▶ croak $@ unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 228 228
517  
518 my $self = bless {
519 _diagnostics => $args->{diagnostics},
520 _indent => $args->{indent},
521 228 }, $class;
522  
523 228 return $self;
524 }
525  
526 # ---------------------------------------------------------------------------
527 # Public: generate
528 # ---------------------------------------------------------------------------
529  
530 =head2 generate
531  
532 Convert a C<Music::NWC2MusicXML::Score> object to a MusicXML 4.0 document and
533 return the complete document as a string.
534  
535 =head3 Purpose
536  
537 Top-level entry point. Orchestrates, in order:
538  
539 =over 4
540  
541 =item 1. XML declaration and DOCTYPE header.
542  
543 =item 2. Page layout geometry (C<< <defaults> >>).
544  
545 =item 3. Work title (C<< <work> >>).
546  
547 =item 4. Identification metadata: composer, lyricist, rights (C<< <identification> >>).
548  
549 =item 5. Visual credits: title, subtitle, and copyright lines (C<< <credit> >> elements).
550  
551 =item 6. Part list: one C<< <score-part> >> per staff (C<< <part-list> >>).
552  
553 =item 7. Musical content: one C<< <part> >> per staff, each containing numbered
554 measures with notes, rests, dynamics, tempo, articulations, slurs, ties,
555 and wedge hairpins.
556  
557 =back
558  
559 Each staff is processed by a two-stage pipeline:
560  
561 =over 4
562  
563 =item Stage 1 -- annotation pre-passes.
564  
565 C<_annotate_events> detects slur arcs and tie pairs across the entire staff
566 before measure grouping. C<_annotate_wedges> detects hairpin (crescendo /
567 diminuendo) arcs stored as per-note C<Opts:> flags. C<_annotate_fermatas>
568 associates C<TempoVariance:Fermata> records with the sounding event that
569 follows each one. All three passes store their results in a shared C<%ann>
570 hash keyed by stringified event reference.
571  
572 =item Stage 2 -- measure emission.
573  
574 Events are gathered into measure-sized groups separated by C<Bar> events.
575 For each measure, C<_emit_measure> serialises notes, rests, directions, and
576 mid-staff attribute changes, consulting C<%ann> for slur, tie, and wedge
577 annotations.
578  
579 =back
580  
581 =head3 Arguments
582  
583 =over 4
584  
585 =item C<$score>
586  
587 A C<Music::NWC2MusicXML::Score> object (required). Must contain at least one
588 staff; otherwise the method croaks.
589  
590 =back
591  
592 =head3 Returns
593  
594 A scalar string holding the complete MusicXML 4.0 document. The string is
595 pure 7-bit ASCII: all non-ASCII source characters are replaced with numeric
596 XML character references (C<&#N;>). The XML declaration at the top of the
597 string declares C<encoding="UTF-8">, which is correct.
598  
599 The string ends with a single newline character.
600  
601 =head3 Side Effects
602  
603 May issue warnings via C<carp> for unsupported or unrecognised values
604 (unknown clef, unknown dynamic marking, unsupported articulation token,
605 approximate barline style).
606  
607 =head3 Usage Example
608  
609 my $xml = $gen->generate($score);
610  
611 # Write to a file -- ':raw' is sufficient; the string is pure ASCII.
612 open my $fh, '>:raw', 'out.musicxml' or die $!;
613 print $fh $xml;
614 close $fh;
615  
616 =head3 API SPECIFICATION
617  
618 =head4 Input
619  
620 $score : Music::NWC2MusicXML::Score (required, must have staff_count > 0)
621  
622 =head4 Output
623  
624 SCALAR -- complete MusicXML 4.0 document, pure 7-bit ASCII, newline-terminated
625  
626 =head3 MESSAGES
627  
628 | Code | Meaning | Resolution |
629 |---------------------|------------------------------------------|---------------------------------|
630 | error_bad_score | Argument is not a Score object | Pass the object returned by Parser |
631 | error_no_staves | Score has zero staves | Confirm the parser found AddStaff records |
632 | warn_unknown_clef | NWC clef name not in CLEF_MAP | Treble used as fallback |
633 | warn_unknown_dynamic| Dynamic marking not in DYNAMIC_MAP | Direction element omitted |
634 | warn_unsupported_art| Articulation token not in ARTICULATION_MAP| Mark omitted; warning issued |
635 | warn_approx_bar | Barline style has no direct MusicXML map | Regular barline used |
636  
637 =cut
638  
639 184 ▶sub generate {
Subroutine generate: called CC=5 SCAR=16.1
Pod documented
640 184 my ($self, $score) = @_;
641  
642 184 ▶ croak _fmt_msg('error_bad_score')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($score) and $score->isa("Music::NWC2MusicXML::Score") 8 176 184
Condition100%3/3
builtin::blessed($score) and $score->isa("Music::NWC2MusicXML::Score")
!ll&&!rl&&r
71176
MC/DC100%2/2
builtin::blessed($score) and $score->isa("Music::NWC2MusicXML::Score")
builtin::blessed($score) $score->isa("Music::NWC2MusicXML::Score")
Truth table100%3/3
A and B
A= builtin::blessed($score)B= $score->isa("Music::NWC2MusicXML::Score")
ABresult
0X0
100
111
643 unless blessed($score) && $score->isa('Music::NWC2MusicXML::Score');
644  
645 176 ▶ croak _fmt_msg('error_no_staves')
Branch: 1 branch
BranchTrueFalseTotal
unless $score->staff_count > 0 4 172 176
646 unless $score->staff_count > 0;
647  
648 # Calculate the divisions value from all note durations in the score.
649 172 my $divisions = $self->_calculate_divisions($score);
650  
651 172 my @out;
652  
653 172 push @out, '<?xml version="1.0" encoding="UTF-8"?>';
654 172 push @out, sprintf('<!DOCTYPE score-partwise PUBLIC "%s" "%s">',
655 $DOCTYPE_PUBLIC, $DOCTYPE_SYSTEM);
656 172 push @out, sprintf('<score-partwise version="%s">', $MUSICXML_VERSION);
657  
658 172 ▶ my $layout = _compute_page_layout($score->page_setup // {});
Condition50%1/2
$score->page_setup // {}
l!l
1720
MC/DC0%0/1
$score->page_setup // {}
$score->page_setup
Truth table50%1/2
A
A= $score->page_setup
Aresult
11
00
659  
660 172 push @out, $self->_emit_work($score->metadata);
661 172 push @out, $self->_emit_identification($score->metadata);
662 172 push @out, $self->_emit_defaults($layout, $score->fonts);
663 172 push @out, $self->_emit_credits($score->metadata, $layout);
664 172 push @out, $self->_emit_part_list($score->staves);
665 172 push @out, $self->_emit_parts($score->staves, $divisions);
666  
667 172 push @out, '</score-partwise>';
668  
669 172 return join("\n", @out) . "\n";
670 }
671  
672 # ---------------------------------------------------------------------------
673 # Private: page layout constants and helpers
674 # ---------------------------------------------------------------------------
675  
676 # Standard MusicXML scaling: 40 tenths per staff space, 7.2175 mm per space.
677 13 Readonly::Scalar my $MM_PER_SPACE => 7.2175;
678 13 Readonly::Scalar my $TENTHS_PER_SPACE => 40;
679 13 Readonly::Scalar my $TENTHS_PER_MM => $TENTHS_PER_SPACE / $MM_PER_SPACE;
680  
681 # Default to A4 paper (210 x 297 mm); most common in international music publishing.
682 13 Readonly::Scalar my $DEFAULT_PAGE_W_MM => 210.0;
683 13 Readonly::Scalar my $DEFAULT_PAGE_H_MM => 297.0;
684 13 Readonly::Scalar my $DEFAULT_MARGIN_CM => 1.27; # standard NWC default
685  
686 # Derive page geometry in tenths from PgSetup/PgMargins fields (cm margins).
687 # Returns a hashref: page_height, page_width, margin_l, margin_r, margin_t,
688 # margin_b, center_x, right_x.
689 175 ▶sub _compute_page_layout {
Subroutine _compute_page_layout: called CC=6 SCAR=18.3
690 175 my ($ps) = @_;
691 175 ▶ $ps //= {};
Condition50%1/2
$ps //= {}
l!l
1750
MC/DC0%0/1
$ps //= {}
$ps
Truth table50%1/2
A
A= $ps
Aresult
11
00
692  
693 # Margins: NWC stores them in cm (Left/Top/Right/Bottom from PgMargins).
694 # When a side is absent, fall back to Left, then the hardcoded default.
695 175 ▶ my $left = $ps->{Left} // $DEFAULT_MARGIN_CM;
Condition66%2/3
$ps->{"Left"} // $DEFAULT_MARGIN_CM
l!l&&r!l&&!r
271480
MC/DC0%0/2
$ps->{"Left"} // $DEFAULT_MARGIN_CM
$ps->{"Left"} $DEFAULT_MARGIN_CM
Truth table66%2/3
A // B
A= $ps->{"Left"}B= $DEFAULT_MARGIN_CM
ABresult
1X1
011
000
696 175 ▶ my $right = $ps->{Right} // $left;
Condition66%2/3
$ps->{"Right"} // $left
l!l&&r!l&&!r
251500
MC/DC0%0/2
$ps->{"Right"} // $left
$ps->{"Right"} $left
Truth table66%2/3
A // B
A= $ps->{"Right"}B= $left
ABresult
1X1
011
000
697 175 ▶ my $top = $ps->{Top} // $left;
Condition66%2/3
$ps->{"Top"} // $left
l!l&&r!l&&!r
241510
MC/DC0%0/2
$ps->{"Top"} // $left
$ps->{"Top"} $left
Truth table66%2/3
A // B
A= $ps->{"Top"}B= $left
ABresult
1X1
011
000
698 175 ▶ my $bottom = $ps->{Bottom} // $left;
Condition66%2/3
$ps->{"Bottom"} // $left
l!l&&r!l&&!r
241510
MC/DC0%0/2
$ps->{"Bottom"} // $left
$ps->{"Bottom"} $left
Truth table66%2/3
A // B
A= $ps->{"Bottom"}B= $left
ABresult
1X1
011
000
699  
700 175 my $margin_l = $left * 10 * $TENTHS_PER_MM;
701 175 my $margin_r = $right * 10 * $TENTHS_PER_MM;
702 175 my $margin_t = $top * 10 * $TENTHS_PER_MM;
703 175 my $margin_b = $bottom * 10 * $TENTHS_PER_MM;
704  
705 175 my $page_h = $DEFAULT_PAGE_H_MM * $TENTHS_PER_MM;
706 175 my $page_w = $DEFAULT_PAGE_W_MM * $TENTHS_PER_MM;
707  
708 return {
709 175 page_height => $page_h,
710 page_width => $page_w,
711 margin_l => $margin_l,
712 margin_r => $margin_r,
713 margin_t => $margin_t,
714 margin_b => $margin_b,
715 center_x => $page_w / 2,
716 right_x => $page_w - $margin_r,
717 };
718 }
719  
720 172 ▶sub _emit_defaults {
Subroutine _emit_defaults: called CC=16 SCAR=27.9
721 172 my ($self, $layout, $fonts) = @_;
722 172 ▶ $fonts //= [];
Condition50%1/2
$fonts //= []
l!l
1720
MC/DC0%0/1
$fonts //= []
$fonts
Truth table50%1/2
A
A= $fonts
Aresult
11
00
723 172 my @out;
724 172 my $i = $self->{_indent};
725  
726 172 my $ph = sprintf '%.2f', $layout->{page_height};
727 172 my $pw = sprintf '%.2f', $layout->{page_width};
728 172 my $ml = sprintf '%.2f', $layout->{margin_l};
729 172 my $mr = sprintf '%.2f', $layout->{margin_r};
730 172 my $mt = sprintf '%.2f', $layout->{margin_t};
731 172 my $mb = sprintf '%.2f', $layout->{margin_b};
732  
733 172 push @out, '<defaults>';
734 172 push @out, "${i}<scaling>";
735 172 push @out, "${i}${i}<millimeters>$MM_PER_SPACE</millimeters>";
736 172 push @out, "${i}${i}<tenths>$TENTHS_PER_SPACE</tenths>";
737 172 push @out, "${i}</scaling>";
738 172 push @out, "${i}<page-layout>";
739 172 push @out, "${i}${i}<page-height>$ph</page-height>";
740 172 push @out, "${i}${i}<page-width>$pw</page-width>";
741 172 push @out, "${i}${i}<page-margins type=\"both\">";
742 172 push @out, "${i}${i}${i}<left-margin>$ml</left-margin>";
743 172 push @out, "${i}${i}${i}<right-margin>$mr</right-margin>";
744 172 push @out, "${i}${i}${i}<top-margin>$mt</top-margin>";
745 172 push @out, "${i}${i}${i}<bottom-margin>$mb</bottom-margin>";
746 172 push @out, "${i}${i}</page-margins>";
747 172 push @out, "${i}</page-layout>";
748  
749 # Font declarations from NWC Font records (word-font then lyric-font per schema order)
750 172 my ($word_font, $lyric_font);
751 172 for my $font (@$fonts) {
752 289 ▶ next unless length($font->{typeface} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($font->{"typeface"} // "") 0 289 289
Condition50%1/2
$font->{"typeface"} // ""
l!l
2890
MC/DC0%0/1
$font->{"typeface"} // ""
$font->{"typeface"}
Truth table50%1/2
A
A= $font->{"typeface"}
Aresult
11
00
753 289 ▶ my $style = $font->{style} // '';
Condition50%1/2
$font->{"style"} // ""
l!l
2890
MC/DC0%0/1
$font->{"style"} // ""
$font->{"style"}
Truth table50%1/2
A
A= $font->{"style"}
Aresult
11
00
754 289 ▶ if ($style eq 'StaffLyric' && !defined $lyric_font) {
Branch: 2 branches
BranchTrueFalseTotal
if ($style eq 'StaffLyric' and not defined $lyric_font) { } 25 264 289
elsif ($style =~ /\AStaff/i and not defined $word_font) { } 24 456 480
Condition66%2/3
$style eq "StaffLyric" and not defined $lyric_font
!ll&&!rl&&r
264025
MC/DC50%1/2
$style eq "StaffLyric" and not defined $lyric_font
$style eq "StaffLyric" not defined $lyric_font
Truth table66%2/3
A and not(B)
A= $style eq "StaffLyric"B= not defined $lyric_font
ABresult
0X0
100
111
755 25 $lyric_font = $font;
756 ▶ } elsif ($style =~ /\AStaff/i && !defined $word_font) {
Condition100%3/3
$style =~ /\AStaff/i and not defined $word_font
!ll&&!rl&&r
2162424
MC/DC100%2/2
$style =~ /\AStaff/i and not defined $word_font
$style =~ /\AStaff/i not defined $word_font
Truth table100%3/3
A and not(B)
A= $style =~ /\AStaff/iB= not defined $word_font
ABresult
0X0
100
111
757 24 $word_font = $font;
758 }
759 }
760 172 for my $pair ([$word_font, 'word-font'], [$lyric_font, 'lyric-font']) {
761 344 my ($font, $elem) = @$pair;
762 344 ▶ next unless defined $font;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $font 295 49 344
763 49 ▶ my $tf = _xml_escape($font->{typeface} // '');
Condition50%1/2
$font->{"typeface"} // ""
l!l
490
MC/DC0%0/1
$font->{"typeface"} // ""
$font->{"typeface"}
Truth table50%1/2
A
A= $font->{"typeface"}
Aresult
11
00
764 49 ▶ my $sz = $font->{size} ? " font-size=\"$font->{size}\"" : '';
Branch: 1 branch
BranchTrueFalseTotal
$font->{'size'} ? : 49 0 49
765 49 ▶ my $wt = $font->{bold} ? ' font-weight="bold"' : '';
Branch: 1 branch
BranchTrueFalseTotal
$font->{'bold'} ? : 24 25 49
766 49 ▶ my $fst = $font->{italic} ? ' font-style="italic"' : '';
Branch: 1 branch
BranchTrueFalseTotal
$font->{'italic'} ? : 24 25 49
767 49 push @out, "${i}<$elem font-family=\"$tf\"${sz}${wt}${fst}/>";
768 }
769  
770 172 push @out, '</defaults>';
771 172 return @out;
772 }
773  
774 # ---------------------------------------------------------------------------
775 # Private: top-level sections
776 # ---------------------------------------------------------------------------
777  
778 172 ▶sub _emit_work {
Subroutine _emit_work: called CC=3 SCAR=11.0
779 172 my ($self, $meta) = @_;
780 172 my @out;
781 172 my $i = $self->{_indent};
782 172 push @out, '<work>';
783 push @out, "${i}<work-title>" . _xml_escape($meta->{Title} // '') . '</work-title>'
784 172 ▶ if $meta->{Title};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"Title"} 71 101 172
Condition50%1/2
$meta->{"Title"} // ""
l!l
710
MC/DC0%0/1
$meta->{"Title"} // ""
$meta->{"Title"}
Truth table50%1/2
A
A= $meta->{"Title"}
Aresult
11
00
785 172 push @out, '</work>';
786 172 return @out;
787 }
788  
789 172 ▶sub _emit_identification {
Subroutine _emit_identification: called CC=17 SCAR=34.2
790 172 my ($self, $meta) = @_;
791 172 my @out;
792 172 my $i = $self->{_indent};
793 172 push @out, '<identification>';
794 push @out, "${i}<creator type=\"composer\">"
795 . _xml_escape($meta->{Author} // '') . '</creator>'
796 172 ▶ if $meta->{Author};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"Author"} 36 136 172
Condition50%1/2
$meta->{"Author"} // ""
l!l
360
MC/DC0%0/1
$meta->{"Author"} // ""
$meta->{"Author"}
Truth table50%1/2
A
A= $meta->{"Author"}
Aresult
11
00
797 push @out, "${i}<creator type=\"lyricist\">"
798 . _xml_escape($meta->{Lyricist} // '') . '</creator>'
799 172 ▶ if $meta->{Lyricist};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"Lyricist"} 0 172 172
Condition0%0/2
$meta->{"Lyricist"} // ""
l!l
00
MC/DC0%0/1
$meta->{"Lyricist"} // ""
$meta->{"Lyricist"}
Truth table0%0/2
A
A= $meta->{"Lyricist"}
Aresult
11
00
800 # Combine all copyright lines into one <rights> element; multiple <rights>
801 # elements cause renderers to discard all but the last.
802 172 my @cr_lines;
803 172 ▶ if (defined $meta->{Copyright1} || defined $meta->{Copyright2}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $meta->{'Copyright1'} or defined $meta->{'Copyright2'}) { } 40 132 172
elsif (defined $meta->{'Copyright'} and length $meta->{'Copyright'}) { } 0 264 264
Condition66%2/3
defined $meta->{"Copyright1"} or defined $meta->{"Copyright2"}
l!l&&r!l&&!r
400132
MC/DC50%1/2
defined $meta->{"Copyright1"} or defined $meta->{"Copyright2"}
defined $meta->{"Copyright1"} defined $meta->{"Copyright2"}
Truth table66%2/3
A or B
A= defined $meta->{"Copyright1"}B= defined $meta->{"Copyright2"}
ABresult
1X1
011
000
804 push @cr_lines, $meta->{Copyright1}
805 40 ▶ if defined $meta->{Copyright1} && length $meta->{Copyright1};
Branch: 1 branch
BranchTrueFalseTotal
if defined $meta->{"Copyright1"} and length $meta->{"Copyright1"} 40 0 40
Condition33%1/3
defined $meta->{"Copyright1"} and length $meta->{"Copyright1"}
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $meta->{"Copyright1"} and length $meta->{"Copyright1"}
defined $meta->{"Copyright1"} length $meta->{"Copyright1"}
Truth table33%1/3
A and B
A= defined $meta->{"Copyright1"}B= length $meta->{"Copyright1"}
ABresult
0X0
100
111
806 push @cr_lines, $meta->{Copyright2}
807 40 ▶ if defined $meta->{Copyright2} && length $meta->{Copyright2};
Branch: 1 branch
BranchTrueFalseTotal
if defined $meta->{"Copyright2"} and length $meta->{"Copyright2"} 36 4 40
Condition66%2/3
defined $meta->{"Copyright2"} and length $meta->{"Copyright2"}
!ll&&!rl&&r
4036
MC/DC50%1/2
defined $meta->{"Copyright2"} and length $meta->{"Copyright2"}
defined $meta->{"Copyright2"} length $meta->{"Copyright2"}
Truth table66%2/3
A and B
A= defined $meta->{"Copyright2"}B= length $meta->{"Copyright2"}
ABresult
0X0
100
111
808 ▶ } elsif (defined $meta->{Copyright} && length $meta->{Copyright}) {
Condition33%1/3
defined $meta->{"Copyright"} and length $meta->{"Copyright"}
!ll&&!rl&&r
13200
MC/DC0%0/2
defined $meta->{"Copyright"} and length $meta->{"Copyright"}
defined $meta->{"Copyright"} length $meta->{"Copyright"}
Truth table33%1/3
A and B
A= defined $meta->{"Copyright"}B= length $meta->{"Copyright"}
ABresult
0X0
100
111
809 0 push @cr_lines, $meta->{Copyright};
810 }
811 172 ▶ push @out, "${i}<rights>" . _xml_escape(join "\n", @cr_lines) . '</rights>'
Branch: 1 branch
BranchTrueFalseTotal
if @cr_lines 40 132 172
812 if @cr_lines;
813 172 push @out, "${i}<encoding>";
814 172 push @out, "${i}${i}<software>Music::NWC2MusicXML $VERSION</software>";
815 172 push @out, "${i}</encoding>";
816 172 ▶ if (defined $meta->{Comments} && length($meta->{Comments} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{"Comments"} and length($meta->{"Comments"} // "")) 0 172 172
Condition20%1/5
defined $meta->{"Comments"} and length($meta->{"Comments"} // "")
!ll&&!rl&&r
17200
$meta->{"Comments"} // ""
l!l
00
MC/DC0%0/2
defined $meta->{"Comments"} and length($meta->{"Comments"} // "")
defined $meta->{"Comments"} $meta->{"Comments"}
Truth table33%1/3
A and B
A= defined $meta->{"Comments"}B= $meta->{"Comments"}
ABresult
0X0
100
111
817 0 push @out, "${i}<miscellaneous>";
818 push @out, "${i}${i}<miscellaneous-field name=\"comments\">"
819 0 . _xml_escape($meta->{Comments}) . '</miscellaneous-field>';
820 0 push @out, "${i}</miscellaneous>";
821 }
822 172 push @out, '</identification>';
823 172 return @out;
824 }
825  
826 172 ▶sub _emit_credits {
Subroutine _emit_credits: called CC=15 SCAR=28.2
827 172 my ($self, $meta, $layout) = @_;
828 172 ▶ $layout //= _compute_page_layout({});
Condition33%1/3
$layout //= _compute_page_layout(scalar {})
l!l&&r!l&&!r
17200
MC/DC0%0/2
$layout //= _compute_page_layout(scalar {})
$layout _compute_page_layout(scalar {})
Truth table33%1/3
A //= B
A= $layoutB= _compute_page_layout(scalar {})
ABresult
1X1
011
000
829 172 my @out;
830 172 my $i = $self->{_indent};
831  
832 172 my $cx = sprintf '%.2f', $layout->{center_x};
833 172 my $ty = sprintf '%.2f', $layout->{page_height} - $layout->{margin_t};
834 172 my $sy = sprintf '%.2f', $layout->{page_height} - $layout->{margin_t} - 60;
835 172 my $bot = $layout->{margin_b};
836  
837 # Title credit on page 1 (large, centred near top)
838 172 ▶ if (defined $meta->{Title} && length $meta->{Title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{"Title"} and length $meta->{"Title"}) 71 101 172
Condition66%2/3
defined $meta->{"Title"} and length $meta->{"Title"}
!ll&&!rl&&r
101071
MC/DC50%1/2
defined $meta->{"Title"} and length $meta->{"Title"}
defined $meta->{"Title"} length $meta->{"Title"}
Truth table66%2/3
A and B
A= defined $meta->{"Title"}B= length $meta->{"Title"}
ABresult
0X0
100
111
839 71 push @out, '<credit page="1">';
840 71 push @out, "${i}<credit-type>title</credit-type>";
841 push @out, "${i}<credit-words"
842 . " default-x=\"$cx\" default-y=\"$ty\""
843 . ' justify="center" valign="top"'
844 . ' font-size="24"'
845 71 . '>' . _xml_escape($meta->{Title}) . '</credit-words>';
846 71 push @out, '</credit>';
847 }
848  
849 # Subtitle credit on page 1 (centred, just below title)
850 172 ▶ if (defined $meta->{Author} && length $meta->{Author}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{"Author"} and length $meta->{"Author"}) 36 136 172
Condition66%2/3
defined $meta->{"Author"} and length $meta->{"Author"}
!ll&&!rl&&r
136036
MC/DC50%1/2
defined $meta->{"Author"} and length $meta->{"Author"}
defined $meta->{"Author"} length $meta->{"Author"}
Truth table66%2/3
A and B
A= defined $meta->{"Author"}B= length $meta->{"Author"}
ABresult
0X0
100
111
851 36 push @out, '<credit page="1">';
852 36 push @out, "${i}<credit-type>subtitle</credit-type>";
853 push @out, "${i}<credit-words"
854 . " default-x=\"$cx\" default-y=\"$sy\""
855 . ' justify="center" valign="top"'
856 . ' font-size="14"'
857 36 . '>' . _xml_escape($meta->{Author}) . '</credit-words>';
858 36 push @out, '</credit>';
859 }
860  
861 # Copyright lines: each gets its OWN <credit> element (no page attribute ->
862 # appears on every page). Multiple <credit-words> in one <credit> cause
863 # renderers to show only the last element.
864 172 my @cr_lines;
865 172 ▶ if (defined $meta->{Copyright1} || defined $meta->{Copyright2}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $meta->{'Copyright1'} or defined $meta->{'Copyright2'}) { } 40 132 172
elsif (defined $meta->{'Copyright'} and length $meta->{'Copyright'}) { } 0 264 264
Condition66%2/3
defined $meta->{"Copyright1"} or defined $meta->{"Copyright2"}
l!l&&r!l&&!r
400132
MC/DC50%1/2
defined $meta->{"Copyright1"} or defined $meta->{"Copyright2"}
defined $meta->{"Copyright1"} defined $meta->{"Copyright2"}
Truth table66%2/3
A or B
A= defined $meta->{"Copyright1"}B= defined $meta->{"Copyright2"}
ABresult
1X1
011
000
866 push @cr_lines, $meta->{Copyright1}
867 40 ▶ if defined $meta->{Copyright1} && length $meta->{Copyright1};
Branch: 1 branch
BranchTrueFalseTotal
if defined $meta->{"Copyright1"} and length $meta->{"Copyright1"} 40 0 40
Condition33%1/3
defined $meta->{"Copyright1"} and length $meta->{"Copyright1"}
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $meta->{"Copyright1"} and length $meta->{"Copyright1"}
defined $meta->{"Copyright1"} length $meta->{"Copyright1"}
Truth table33%1/3
A and B
A= defined $meta->{"Copyright1"}B= length $meta->{"Copyright1"}
ABresult
0X0
100
111
868 push @cr_lines, $meta->{Copyright2}
869 40 ▶ if defined $meta->{Copyright2} && length $meta->{Copyright2};
Branch: 1 branch
BranchTrueFalseTotal
if defined $meta->{"Copyright2"} and length $meta->{"Copyright2"} 36 4 40
Condition66%2/3
defined $meta->{"Copyright2"} and length $meta->{"Copyright2"}
!ll&&!rl&&r
4036
MC/DC50%1/2
defined $meta->{"Copyright2"} and length $meta->{"Copyright2"}
defined $meta->{"Copyright2"} length $meta->{"Copyright2"}
Truth table66%2/3
A and B
A= defined $meta->{"Copyright2"}B= length $meta->{"Copyright2"}
ABresult
0X0
100
111
870 ▶ } elsif (defined $meta->{Copyright} && length $meta->{Copyright}) {
Condition33%1/3
defined $meta->{"Copyright"} and length $meta->{"Copyright"}
!ll&&!rl&&r
13200
MC/DC0%0/2
defined $meta->{"Copyright"} and length $meta->{"Copyright"}
defined $meta->{"Copyright"} length $meta->{"Copyright"}
Truth table33%1/3
A and B
A= defined $meta->{"Copyright"}B= length $meta->{"Copyright"}
ABresult
0X0
100
111
871 0 push @cr_lines, $meta->{Copyright};
872 }
873  
874 # Stack lines from bottom margin upward: last line at margin, each prior
875 # line 14 tenths higher.
876 172 my $line_step = 14;
877 172 my $n = scalar @cr_lines;
878 172 for my $idx (0 .. $#cr_lines) {
879 76 my $y = sprintf '%.2f', $bot + $line_step * ($n - 1 - $idx);
880 76 push @out, '<credit>';
881 76 push @out, "${i}<credit-type>rights</credit-type>";
882 76 push @out, "${i}<credit-words"
883 . " default-x=\"$cx\" default-y=\"$y\""
884 . ' justify="center" valign="bottom"'
885 . ' font-size="10"'
886 . '>' . _xml_escape($cr_lines[$idx]) . '</credit-words>';
887 76 push @out, '</credit>';
888 }
889  
890 172 return @out;
891 }
892  
893 172 ▶sub _emit_part_list {
Subroutine _emit_part_list: called CC=10 SCAR=23.0
894 172 my ($self, $staves) = @_;
895 172 my @out;
896 172 my $i = $self->{_indent};
897  
898 172 my @names = _resolve_part_names($staves);
899 172 my @groups = _compute_groups($staves);
900  
901 172 push @out, '<part-list>';
902 172 my $part_id = 1;
903 172 for my $staff (@$staves) {
904 258 my $idx = $part_id - 1;
905  
906 # Groups starting before this score-part (outer before inner)
907 258 for my $g (sort { $a->{num} <=> $b->{num} } grep { $_->{start} == $idx } @groups) {
908 48 push @out, "${i}<part-group type=\"start\" number=\"$g->{num}\">";
909 48 push @out, "${i}${i}<group-symbol>$g->{symbol}</group-symbol>";
910 48 push @out, "${i}${i}<group-barline>$g->{bars}</group-barline>";
911 48 push @out, "${i}</part-group>";
912 }
913  
914 258 my $id = "P$part_id";
915 258 my $name = _xml_escape($names[$idx]);
916 258 push @out, "${i}<score-part id=\"$id\">";
917 258 ▶ my $print_attr = ($staff->{_visible} // 1) ? '' : ' print-object="no"';
Branch: 1 branch
BranchTrueFalseTotal
$staff->{'_visible'} // 1 ? : 257 1 258
Condition50%1/2
$staff->{"_visible"} // 1
l!l
2580
MC/DC0%0/1
$staff->{"_visible"} // 1
$staff->{"_visible"}
Truth table50%1/2
A
A= $staff->{"_visible"}
Aresult
11
00
918 258 push @out, "${i}${i}<part-name${print_attr}>$name</part-name>";
919 258 my $instr = $staff->instrument;
920 258 ▶ if ($instr && $instr->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instr and $instr->{"name"}) 105 153 258
Condition66%2/3
$instr and $instr->{"name"}
!ll&&!rl&&r
0153105
MC/DC50%1/2
$instr and $instr->{"name"}
$instr $instr->{"name"}
Truth table66%2/3
A and B
A= $instrB= $instr->{"name"}
ABresult
0X0
100
111
921 105 push @out, "${i}${i}<score-instrument id=\"${id}-I1\">";
922 push @out, "${i}${i}${i}<instrument-name>"
923 105 . _xml_escape($instr->{name}) . '</instrument-name>';
924 105 push @out, "${i}${i}</score-instrument>";
925 105 ▶ if (defined $instr->{patch}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $instr->{"patch"}) 104 1 105
926 104 ▶ my $chan = $staff->{_channel} // 1;
Condition100%2/2
$staff->{"_channel"} // 1
l!l
968
MC/DC100%1/1
$staff->{"_channel"} // 1
$staff->{"_channel"}
Truth table100%2/2
A
A= $staff->{"_channel"}
Aresult
11
00
927 104 push @out, "${i}${i}<midi-instrument id=\"${id}-I1\">";
928 104 push @out, "${i}${i}${i}<midi-channel>$chan</midi-channel>";
929 push @out, "${i}${i}${i}<midi-program>"
930 104 . ($instr->{patch} + 1) . '</midi-program>';
931 104 push @out, "${i}${i}</midi-instrument>";
932 }
933 }
934 258 push @out, "${i}</score-part>";
935  
936 # Groups stopping after this score-part (inner before outer)
937 258 for my $g (sort { $b->{num} <=> $a->{num} } grep { $_->{end} == $idx } @groups) {
938 48 push @out, "${i}<part-group type=\"stop\" number=\"$g->{num}\"/>";
939 }
940  
941 258 $part_id++;
942 }
943 172 push @out, '</part-list>';
944 172 return @out;
945 }
946  
947 # Compute bracket/brace grouping structure from WithNextStaff flags.
948 # Returns a list of group hashrefs: {num, start, end, symbol, bars}.
949 # Bracket groups are numbered before Brace groups so outer comes first.
950 172 ▶sub _compute_groups {
Subroutine _compute_groups: called CC=10 SCAR=23.1
951 172 my ($staves) = @_;
952 172 my @all_groups;
953 172 my $group_num = 1;
954  
955 172 for my $sym (qw(Bracket Brace)) {
956 344 my $i = 0;
957 344 ▶ while ($i < @$staves) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$staves) 372 344 716
958 372 ▶ my $wnxs = $staves->[$i]{_with_next_staff} // '';
Condition100%2/2
$staves->[$i]{"_with_next_staff"} // ""
l!l
48324
MC/DC100%1/1
$staves->[$i]{"_with_next_staff"} // ""
$staves->[$i]{"_with_next_staff"}
Truth table100%2/2
A
A= $staves->[$i]{"_with_next_staff"}
Aresult
11
00
959 372 my %flags = map { $_ => 1 } split /,/, $wnxs;
960  
961 372 ▶ if ($flags{$sym}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{$sym}) { } 48 324 372
962 48 my $start = $i;
963 48 ▶ my $connect_bars = $flags{ConnectBars} ? 'yes' : 'no';
Branch: 1 branch
BranchTrueFalseTotal
$flags{'ConnectBars'} ? : 48 0 48
964 # Extend the group while consecutive staves carry the same flag.
965 48 ▶ while ($i < @$staves - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$staves - 1) 144 48 192
966 144 ▶ my $cur = $staves->[$i]{_with_next_staff} // '';
Condition50%1/2
$staves->[$i]{"_with_next_staff"} // ""
l!l
1440
MC/DC0%0/1
$staves->[$i]{"_with_next_staff"} // ""
$staves->[$i]{"_with_next_staff"}
Truth table50%1/2
A
A= $staves->[$i]{"_with_next_staff"}
Aresult
11
00
967 144 my %cf = map { $_ => 1 } split /,/, $cur;
968 144 ▶ last unless $cf{$sym};
Branch: 1 branch
BranchTrueFalseTotal
unless $cf{$sym} 0 144 144
969 144 $i++;
970 }
971 48 ▶ push @all_groups, {
Branch: 1 branch
BranchTrueFalseTotal
if $i > $start 48 0 48
972 num => $group_num++,
973 start => $start,
974 end => $i,
975 symbol => lc($sym),
976 bars => $connect_bars,
977 } if $i > $start;
978 48 $i++;
979 } else {
980 324 $i++;
981 }
982 }
983 }
984  
985 172 return @all_groups;
986 }
987  
988 # ---------------------------------------------------------------------------
989 # Private: part-name resolution
990 # ---------------------------------------------------------------------------
991  
992 # Resolve a display name for every staff, applying a three-level priority:
993 # 1. The NWC staff name, if it is not a generic default.
994 # 2. The MIDI instrument name, if distinct across all staves using it.
995 # 3. "Staff-N" (1-based) as the unconditional last resort.
996 #
997 # After candidates are chosen, any name that appears more than once is
998 # replaced with "Staff-N" to guarantee unique part names in the output.
999 176 ▶sub _resolve_part_names {
Subroutine _resolve_part_names: called CC=5 SCAR=16.1
1000 176 my ($staves) = @_;
1001  
1002 # Build candidates
1003 176 my @candidates;
1004 176 my $n = 1;
1005 176 for my $staff (@$staves) {
1006 265 push @candidates, _candidate_part_name($staff, $n++);
1007 }
1008  
1009 # Count how many staves share each candidate name
1010 176 my %freq;
1011 176 $freq{$_}++ for @candidates;
1012  
1013 # Replace any duplicated name with a positional "Staff-N" fallback
1014 176 my @resolved;
1015 176 my $pos = 1;
1016 176 for my $cand (@candidates) {
1017 265 ▶ push @resolved, $freq{$cand} > 1 ? "Staff-$pos" : $cand;
Branch: 1 branch
BranchTrueFalseTotal
$freq{$cand} > 1 ? : 102 163 265
1018 265 $pos++;
1019 }
1020  
1021 176 return @resolved;
1022 }
1023  
1024 # Return a candidate name for one staff before deduplication.
1025 265 ▶sub _candidate_part_name {
Subroutine _candidate_part_name: called CC=7 SCAR=20.0
1026 265 my ($staff, $part_num) = @_;
1027 265 ▶ my $name = $staff->name // '';
Condition50%1/2
$staff->name // ""
l!l
2650
MC/DC0%0/1
$staff->name // ""
$staff->name
Truth table50%1/2
A
A= $staff->name
Aresult
11
00
1028  
1029 # Accept the NWC name unless it matches NWC's own generic defaults
1030 # ("Staff", "Staff-0" .. "Staff-99").
1031 265 ▶ return $name if length $name && $name !~ /^Staff(?:-\d+)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if length $name and not $name =~ /^Staff(?:-\d+)?$/i 159 106 265
Condition66%2/3
length $name and not $name =~ /^Staff(?:-\d+)?$/i
!ll&&!rl&&r
0106159
MC/DC50%1/2
length $name and not $name =~ /^Staff(?:-\d+)?$/i
length $name not $name =~ /^Staff(?:-\d+)?$/i
Truth table66%2/3
A and not(B)
A= length $nameB= not $name =~ /^Staff(?:-\d+)?$/i
ABresult
0X0
100
111
1032  
1033 # Try the instrument name recorded in the MIDI settings.
1034 106 ▶ my $instr = $staff->instrument // {};
Condition50%1/2
$staff->instrument // {}
l!l
1060
MC/DC0%0/1
$staff->instrument // {}
$staff->instrument
Truth table50%1/2
A
A= $staff->instrument
Aresult
11
00
1035 return $instr->{name}
1036 106 ▶ if defined $instr->{name} && length $instr->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $instr->{"name"} and length $instr->{"name"} 98 8 106
Condition66%2/3
defined $instr->{"name"} and length $instr->{"name"}
!ll&&!rl&&r
8098
MC/DC50%1/2
defined $instr->{"name"} and length $instr->{"name"}
defined $instr->{"name"} length $instr->{"name"}
Truth table66%2/3
A and B
A= defined $instr->{"name"}B= length $instr->{"name"}
ABresult
0X0
100
111
1037  
1038 # Positional fallback: 1-based "Staff-N".
1039 8 return "Staff-$part_num";
1040 }
1041  
1042 172 ▶sub _emit_parts {
Subroutine _emit_parts: called CC=2 SCAR=6.9
1043 172 my ($self, $staves, $divisions) = @_;
1044 172 my @out;
1045 172 my $part_id = 1;
1046 172 for my $staff (@$staves) {
1047 258 push @out, $self->_emit_part("P$part_id", $staff, $divisions);
1048 258 $part_id++;
1049 }
1050 172 return @out;
1051 }
1052  
1053 258 ▶sub _emit_part {
Subroutine _emit_part: called CC=17 SCAR=28.5
1054 258 my ($self, $id, $staff, $divisions) = @_;
1055 258 my @out;
1056  
1057 258 push @out, "<part id=\"$id\">";
1058  
1059 258 my $measure_no = 1;
1060 258 my @pending = ();
1061 258 my $first = 1;
1062 258 my $prev_bar = 'normal';
1063  
1064 # $clef_start / $key_start = state at the beginning of the current measure.
1065 # $clef_now / $key_now = state updated live as we scan events.
1066 # They diverge when Clef/Key events appear mid-measure; at each Bar we
1067 # commit the live values as the start-of-next-measure state.
1068 258 ▶ my $clef_start = $staff->initial_clef // 'Treble';
Condition100%2/2
$staff->initial_clef // "Treble"
l!l
23523
MC/DC100%1/1
$staff->initial_clef // "Treble"
$staff->initial_clef
Truth table100%2/2
A
A= $staff->initial_clef
Aresult
11
00
1069 258 ▶ my $key_start = ($staff->initial_key // {})->{fifths} // 0;
Condition100%4/4
+($staff->initial_key // {})->{"fifths"} // 0
l!l
20157
$staff->initial_key // {}
l!l
20157
MC/DC100%1/1
+($staff->initial_key // {})->{"fifths"} // 0
$staff->initial_key
Truth table100%2/2
A
A= $staff->initial_key
Aresult
11
00
1070 258 my $clef_now = $clef_start;
1071 258 my $key_now = $key_start;
1072 258 my $ts_start = $staff->initial_timesig;
1073 258 my $ts_now = $ts_start;
1074  
1075 # Expose per-staff DynVel map for _emit_dynamic to use during this part.
1076 258 $self->{_dyn_vel} = $staff->{_dyn_vel};
1077  
1078 # Pre-annotate all events with slur/tie and wedge metadata in one pass
1079 # so that arcs crossing bar lines are handled correctly.
1080 258 my $ann = $self->_annotate_events($staff->events);
1081 258 $self->_annotate_wedges($staff->events, $ann);
1082 258 $self->_annotate_lyrics($staff->events, $ann);
1083 258 $self->_annotate_fermatas($staff->events, $ann);
1084  
1085 258 for my $event (@{ $staff->events }) {
1086 167938 my $type = $event->type;
1087  
1088 167938 ▶ if ($type eq 'Bar') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'Bar') { } 37375 130563 167938
elsif ($type eq 'TimeSig') { } 1921 128642 130563
elsif ($type eq 'Clef') { } 171 128471 128642
elsif ($type eq 'Key') { } 481 127990 128471
1089 37375 ▶ my $bar_style = $event->data->{style} // 'normal';
Condition50%1/2
$event->data->{"style"} // "normal"
l!l
373750
MC/DC0%0/1
$event->data->{"style"} // "normal"
$event->data->{"style"}
Truth table50%1/2
A
A= $event->data->{"style"}
Aresult
11
00
1090 37375 push @out, $self->_emit_measure(
1091 $measure_no++, \@pending, $staff, $divisions,
1092 $first, $clef_start, $key_start, $prev_bar, $bar_style, $ann,
1093 $ts_start
1094 );
1095 37375 @pending = ();
1096 37375 $first = 0;
1097 37375 $prev_bar = $bar_style;
1098 37375 $clef_start = $clef_now; # carry updated state to next measure
1099 37375 $key_start = $key_now;
1100 37375 $ts_start = $ts_now;
1101  
1102 } elsif ($type eq 'TimeSig') {
1103 1921 ▶ $ts_now = $event->data // $ts_now;
Condition33%1/3
$event->data // $ts_now
l!l&&r!l&&!r
192100
MC/DC0%0/2
$event->data // $ts_now
$event->data $ts_now
Truth table33%1/3
A // B
A= $event->dataB= $ts_now
ABresult
1X1
011
000
1104 1921 push @pending, $event;
1105  
1106 } elsif ($type eq 'Clef') {
1107 171 ▶ $clef_now = $event->data->{nwc_clef} // $clef_now;
Condition33%1/3
$event->data->{"nwc_clef"} // $clef_now
l!l&&r!l&&!r
17100
MC/DC0%0/2
$event->data->{"nwc_clef"} // $clef_now
$event->data->{"nwc_clef"} $clef_now
Truth table33%1/3
A // B
A= $event->data->{"nwc_clef"}B= $clef_now
ABresult
1X1
011
000
1108 171 push @pending, $event; # included so _emit_measure can emit <attributes>
1109  
1110 } elsif ($type eq 'Key') {
1111 481 ▶ $key_now = ($event->data // {})->{fifths} // 0;
Condition50%2/4
+($event->data // {})->{"fifths"} // 0
l!l
4810
$event->data // {}
l!l
4810
MC/DC0%0/1
+($event->data // {})->{"fifths"} // 0
$event->data
Truth table50%1/2
A
A= $event->data
Aresult
11
00
1112 481 push @pending, $event;
1113  
1114 } else {
1115 127990 push @pending, $event;
1116 }
1117 }
1118  
1119 258 ▶ if (@pending || $measure_no == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending or $measure_no == 1) 141 117 258
Condition100%3/3
@pending or $measure_no == 1
l!l&&r!l&&!r
1374117
MC/DC100%2/2
@pending or $measure_no == 1
@pending $measure_no == 1
Truth table100%3/3
A or B
A= @pendingB= $measure_no == 1
ABresult
1X1
011
000
1120 push @out, $self->_emit_measure(
1121 $measure_no, \@pending, $staff, $divisions,
1122 $first, $clef_start, $key_start, $prev_bar,
1123 141 ▶ $staff->{_ending_bar} // 'normal', $ann, $ts_start
Condition100%2/2
$staff->{"_ending_bar"} // "normal"
l!l
9843
MC/DC100%1/1
$staff->{"_ending_bar"} // "normal"
$staff->{"_ending_bar"}
Truth table100%2/2
A
A= $staff->{"_ending_bar"}
Aresult
11
00
1124 );
1125 }
1126  
1127 258 delete $self->{_dyn_vel};
1128 258 push @out, "</part>";
1129 258 return @out;
1130 }
1131  
1132 37516 ▶sub _emit_measure {
Subroutine _emit_measure: called CC=59 SCAR=45.2
1133 37516 my ($self, $number, $events, $staff, $divisions, $is_first,
1134 $clef, $key_fifths, $prev_bar, $bar_style, $ann, $ts) = @_;
1135 37516 ▶ my $curr_clef = $clef // 'Treble';
Condition50%1/2
$clef // "Treble"
l!l
375160
MC/DC0%0/1
$clef // "Treble"
$clef
Truth table50%1/2
A
A= $clef
Aresult
11
00
1136 37516 ▶ my $curr_key = $key_fifths // 0;
Condition50%1/2
$key_fifths // 0
l!l
375160
MC/DC0%0/1
$key_fifths // 0
$key_fifths
Truth table50%1/2
A
A= $key_fifths
Aresult
11
00
1137 37516 ▶ my $curr_ts = $ts // $staff->initial_timesig;
Condition66%2/3
$ts // $staff->initial_timesig
l!l&&r!l&&!r
37461055
MC/DC50%1/2
$ts // $staff->initial_timesig
$ts $staff->initial_timesig
Truth table66%2/3
A // B
A= $tsB= $staff->initial_timesig
ABresult
1X1
011
000
1138 37516 ▶ $prev_bar //= 'normal';
Condition50%1/2
$prev_bar //= "normal"
l!l
375160
MC/DC0%0/1
$prev_bar //= "normal"
$prev_bar
Truth table50%1/2
A
A= $prev_bar
Aresult
11
00
1139 37516 ▶ $bar_style //= 'normal';
Condition50%1/2
$bar_style //= "normal"
l!l
375160
MC/DC0%0/1
$bar_style //= "normal"
$bar_style
Truth table50%1/2
A
A= $bar_style
Aresult
11
00
1140 37516 ▶ $ann //= {};
Condition50%1/2
$ann //= {}
l!l
375160
MC/DC0%0/1
$ann //= {}
$ann
Truth table50%1/2
A
A= $ann
Aresult
11
00
1141  
1142 37516 my @out;
1143 37516 my $i = $self->{_indent};
1144 37516 my $pad = $i;
1145 37516 my $bar_accidentals = {}; # pitch_key -> alter; reset each measure
1146  
1147 # NWC writes a whole rest for a silent bar whatever the time signature,
1148 # so a whole rest that is alone in its measure lasts the full measure.
1149 37516 my @sounding = grep { $SOUNDING_TYPES{ $_->type } } @$events;
1150 37516 ▶ my $bar_rest = (@sounding == 1 && _is_whole_rest($sounding[0])) ? $sounding[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@sounding == 1 && _is_whole_rest($sounding[0]) ? : 11789 25727 37516
Condition100%3/3
@sounding == 1 and _is_whole_rest($sounding[0])
!ll&&!rl&&r
20483524411789
MC/DC100%2/2
@sounding == 1 and _is_whole_rest($sounding[0])
@sounding == 1 _is_whole_rest($sounding[0])
Truth table100%3/3
A and B
A= @sounding == 1B= _is_whole_rest($sounding[0])
ABresult
0X0
100
111
1151  
1152 37516 push @out, "${pad}<measure number=\"$number\">";
1153  
1154 37516 ▶ if ($prev_bar eq 'MasterRepeatOpen' || $prev_bar eq 'LocalRepeatOpen') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_bar eq "MasterRepeatOpen" or $prev_bar eq "LocalRepeatOpen") 384 37132 37516
Condition66%2/3
$prev_bar eq "MasterRepeatOpen" or $prev_bar eq "LocalRepeatOpen"
l!l&&r!l&&!r
384037132
MC/DC50%1/2
$prev_bar eq "MasterRepeatOpen" or $prev_bar eq "LocalRepeatOpen"
$prev_bar eq "MasterRepeatOpen" $prev_bar eq "LocalRepeatOpen"
Truth table66%2/3
A or B
A= $prev_bar eq "MasterRepeatOpen"B= $prev_bar eq "LocalRepeatOpen"
ABresult
1X1
011
000
1155 384 push @out, "${pad}${i}<barline location=\"left\">";
1156 384 push @out, "${pad}${i}${i}<bar-style>heavy-light</bar-style>";
1157 384 push @out, "${pad}${i}${i}<repeat direction=\"forward\"/>";
1158 384 push @out, "${pad}${i}</barline>";
1159 }
1160  
1161 37516 ▶ if ($is_first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first) 258 37258 37516
1162 258 push @out, $self->_emit_attributes($staff, $divisions, $pad . $i);
1163 # Emit MIDI playback volume/pan at the start of the first measure.
1164 258 my ($vol, $pan) = ($staff->{_volume}, $staff->{_stereo_pan});
1165 258 ▶ if (defined $vol || defined $pan) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vol or defined $pan) 96 162 258
Condition66%2/3
defined $vol or defined $pan
l!l&&r!l&&!r
960162
MC/DC50%1/2
defined $vol or defined $pan
defined $vol defined $pan
Truth table66%2/3
A or B
A= defined $volB= defined $pan
ABresult
1X1
011
000
1166 96 my @sa;
1167 96 ▶ push @sa, sprintf('dynamics="%d"', int($vol * 100 / 127 + 0.5)) if defined $vol;
Branch: 1 branch
BranchTrueFalseTotal
if defined $vol 96 0 96
1168 96 ▶ push @sa, sprintf('pan="%d"', int(($pan - 64) * 90 / 63 + 0.5)) if defined $pan;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pan 96 0 96
1169 96 ▶ push @out, "${pad}${i}<sound " . join(' ', @sa) . '/>' if @sa;
Branch: 1 branch
BranchTrueFalseTotal
if @sa 96 0 96
1170 }
1171 }
1172  
1173 37516 for my $event (@$events) {
1174 130563 my $type = $event->type;
1175 130563 ▶ my $ev_ann = $ann->{"$event"} // {};
Condition100%2/2
$$ann{"$event"} // {}
l!l
17497113066
MC/DC100%1/1
$$ann{"$event"} // {}
$$ann{"$event"}
Truth table100%2/2
A
A= $$ann{"$event"}
Aresult
11
00
1176  
1177 130563 ▶ if ($type eq 'TimeSig') {
Branch: 13 branches
BranchTrueFalseTotal
if ($type eq 'TimeSig') { } 1921 128642 130563
elsif ($type eq 'Clef') { } 171 128471 128642
elsif ($type eq 'Key') { } 481 127990 128471
elsif ($type eq 'Tempo') { } 130 127860 127990
elsif ($type eq 'Dynamic') { } 2402 125458 127860
elsif ($type eq 'DynVariance') { } 3 125455 125458
elsif ($type eq 'TempoVariance') { } 109 125346 125455
elsif ($type eq 'Text') { } 0 125346 125346
elsif ($type eq 'FlowControl') { } 0 125346 125346
elsif ($type eq 'Instrument') { } 0 125346 125346
elsif ($type eq 'Note') { } 85258 40088 125346
elsif ($type eq 'Rest') { } 19424 20664 40088
elsif ($type eq 'Chord') { } 20616 48 20664
1178 1921 ▶ $curr_ts = $event->data // $curr_ts;
Condition33%1/3
$event->data // $curr_ts
l!l&&r!l&&!r
192100
MC/DC0%0/2
$event->data // $curr_ts
$event->data $curr_ts
Truth table33%1/3
A // B
A= $event->dataB= $curr_ts
ABresult
1X1
011
000
1179 1921 ▶ push @out, $self->_emit_time_change($event->data // {}, $pad . $i);
Condition50%1/2
$event->data // {}
l!l
19210
MC/DC0%0/1
$event->data // {}
$event->data
Truth table50%1/2
A
A= $event->data
Aresult
11
00
1180  
1181 } elsif ($type eq 'Clef') {
1182 171 ▶ $curr_clef = $event->data->{nwc_clef} // $curr_clef;
Condition33%1/3
$event->data->{"nwc_clef"} // $curr_clef
l!l&&r!l&&!r
17100
MC/DC0%0/2
$event->data->{"nwc_clef"} // $curr_clef
$event->data->{"nwc_clef"} $curr_clef
Truth table33%1/3
A // B
A= $event->data->{"nwc_clef"}B= $curr_clef
ABresult
1X1
011
000
1183 171 push @out, $self->_emit_clef_change($curr_clef, $pad . $i);
1184  
1185 } elsif ($type eq 'Key') {
1186 481 ▶ my $kd = $event->data // {};
Condition50%1/2
$event->data // {}
l!l
4810
MC/DC0%0/1
$event->data // {}
$event->data
Truth table50%1/2
A
A= $event->data
Aresult
11
00
1187 481 ▶ $curr_key = $kd->{fifths} // 0;
Condition50%1/2
$kd->{"fifths"} // 0
l!l
4810
MC/DC0%0/1
$kd->{"fifths"} // 0
$kd->{"fifths"}
Truth table50%1/2
A
A= $kd->{"fifths"}
Aresult
11
00
1188 481 push @out, $self->_emit_key_change($kd, $pad . $i);
1189  
1190 } elsif ($type eq 'Tempo') {
1191 130 ▶ my $d = $event->data // {};
Condition50%1/2
$event->data // {}
l!l
1300
MC/DC0%0/1
$event->data // {}
$event->data
Truth table50%1/2
A
A= $event->data
Aresult
11
00
1192 push @out, $self->_emit_tempo(
1193 130 ▶ $d->{bpm}, $d->{base}, $d->{text} // '', $pad . $i);
Condition100%2/2
$d->{"text"} // ""
l!l
1273
MC/DC100%1/1
$d->{"text"} // ""
$d->{"text"}
Truth table100%2/2
A
A= $d->{"text"}
Aresult
11
00
1194  
1195 } elsif ($type eq 'Dynamic') {
1196 2402 ▶ my $d = $event->data // {};
Condition50%1/2
$event->data // {}
l!l
24020
MC/DC0%0/1
$event->data // {}
$event->data
Truth table50%1/2
A
A= $event->data
Aresult
11
00
1197 push @out, $self->_emit_dynamic(
1198 2402 $d->{marking}, $d->{placement}, $pad . $i);
1199  
1200 } elsif ($type eq 'DynVariance') {
1201 3 ▶ my $d = $event->data // {};
Condition50%1/2
$event->data // {}
l!l
30
MC/DC0%0/1
$event->data // {}
$event->data
Truth table50%1/2
A
A= $event->data
Aresult
11
00
1202 push @out, $self->_emit_dyn_variance(
1203 3 $d->{style}, $d->{placement}, $pad . $i);
1204  
1205 } elsif ($type eq 'TempoVariance') {
1206 109 ▶ my $d = $event->data // {};
Condition50%1/2
$event->data // {}
l!l
1090
MC/DC0%0/1
$event->data // {}
$event->data
Truth table50%1/2
A
A= $event->data
Aresult
11
00
1207 109 ▶ next if ($d->{style} // '') eq 'Fermata'; # handled as <fermata/> on following note
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{"style"} // "") eq "Fermata" 5 104 109
Condition50%1/2
$d->{"style"} // ""
l!l
1090
MC/DC0%0/1
$d->{"style"} // ""
$d->{"style"}
Truth table50%1/2
A
A= $d->{"style"}
Aresult
11
00
1208 push @out, $self->_emit_tempo_variance(
1209 104 $d->{style}, $d->{placement}, $pad . $i);
1210  
1211 } elsif ($type eq 'Text') {
1212 0 ▶ my $d = $event->data // {};
Condition0%0/2
$event->data // {}
l!l
00
MC/DC0%0/1
$event->data // {}
$event->data
Truth table0%0/2
A
A= $event->data
Aresult
11
00
1213 push @out, $self->_emit_text_direction(
1214 0 $d->{text}, $d->{placement}, $pad . $i);
1215  
1216 } elsif ($type eq 'FlowControl') {
1217 0 ▶ my $d = $event->data // {};
Condition0%0/2
$event->data // {}
l!l
00
MC/DC0%0/1
$event->data // {}
$event->data
Truth table0%0/2
A
A= $event->data
Aresult
11
00
1218 0 push @out, $self->_emit_flow_control($d->{directive}, $pad . $i);
1219  
1220 } elsif ($type eq 'Instrument') {
1221 0 ▶ my $d = $event->data // {};
Condition0%0/2
$event->data // {}
l!l
00
MC/DC0%0/1
$event->data // {}
$event->data
Truth table0%0/2
A
A= $event->data
Aresult
11
00
1222 push @out, $self->_emit_instrument_change(
1223 0 $d->{name}, $d->{patch}, $pad . $i);
1224  
1225 } elsif ($type eq 'Note') {
1226 push @out, $self->_emit_wedge($ev_ann->{wedge_start}, undef, $pad . $i)
1227 85258 ▶ if $ev_ann->{wedge_start};
Branch: 1 branch
BranchTrueFalseTotal
if $ev_ann->{"wedge_start"} 701 84557 85258
1228 85258 push @out, $self->_emit_note_event(
1229 $event, $curr_clef, $curr_key, $divisions, $pad . $i, 0, $ev_ann, $bar_accidentals);
1230 push @out, $self->_emit_wedge('crescOff', undef, $pad . $i)
1231 85258 ▶ if $ev_ann->{wedge_stop_after};
Branch: 1 branch
BranchTrueFalseTotal
if $ev_ann->{"wedge_stop_after"} 701 84557 85258
1232 } elsif ($type eq 'Rest') {
1233 push @out, $self->_emit_wedge($ev_ann->{wedge_start}, undef, $pad . $i)
1234 19424 ▶ if $ev_ann->{wedge_start};
Branch: 1 branch
BranchTrueFalseTotal
if $ev_ann->{"wedge_start"} 0 19424 19424
1235 19424 ▶ my $measure_ticks = (defined $bar_rest && $event == $bar_rest)
Branch: 1 branch
BranchTrueFalseTotal
defined $bar_rest && $event == $bar_rest ? : 11789 7635 19424
Condition66%2/3
defined $bar_rest and $event == $bar_rest
!ll&&!rl&&r
7635011789
MC/DC50%1/2
defined $bar_rest and $event == $bar_rest
defined $bar_rest $event == $bar_rest
Truth table66%2/3
A and B
A= defined $bar_restB= $event == $bar_rest
ABresult
0X0
100
111
1236 ? _measure_ticks($curr_ts, $divisions) : undef;
1237 19424 push @out, $self->_emit_rest_event(
1238 $event, $divisions, $pad . $i, $ev_ann, $measure_ticks);
1239 push @out, $self->_emit_wedge('crescOff', undef, $pad . $i)
1240 19424 ▶ if $ev_ann->{wedge_stop_after};
Branch: 1 branch
BranchTrueFalseTotal
if $ev_ann->{"wedge_stop_after"} 0 19424 19424
1241 } elsif ($type eq 'Chord') {
1242 push @out, $self->_emit_wedge($ev_ann->{wedge_start}, undef, $pad . $i)
1243 20616 ▶ if $ev_ann->{wedge_start};
Branch: 1 branch
BranchTrueFalseTotal
if $ev_ann->{"wedge_start"} 240 20376 20616
1244 20616 push @out, $self->_emit_chord_event(
1245 $event, $curr_clef, $curr_key, $divisions, $pad . $i, $ev_ann, $bar_accidentals);
1246 push @out, $self->_emit_wedge('crescOff', undef, $pad . $i)
1247 20616 ▶ if $ev_ann->{wedge_stop_after};
Branch: 1 branch
BranchTrueFalseTotal
if $ev_ann->{"wedge_stop_after"} 240 20376 20616
1248 }
1249 }
1250  
1251 37516 ▶ if ($bar_style eq 'MasterRepeatClose') {
Branch: 3 branches
BranchTrueFalseTotal
if ($bar_style eq 'MasterRepeatClose') { } 384 37132 37516
elsif ($bar_style eq 'Double') { } 768 36364 37132
elsif ($bar_style eq 'SectionClose' or $bar_style eq 'LocalRepeatClose') { } 98 36266 36364
1252 384 push @out, "${pad}${i}<barline location=\"right\">";
1253 384 push @out, "${pad}${i}${i}<bar-style>light-heavy</bar-style>";
1254 384 push @out, "${pad}${i}${i}<repeat direction=\"backward\"/>";
1255 384 push @out, "${pad}${i}</barline>";
1256 } elsif ($bar_style eq 'Double') {
1257 768 push @out, "${pad}${i}<barline location=\"right\">";
1258 768 push @out, "${pad}${i}${i}<bar-style>light-light</bar-style>";
1259 768 push @out, "${pad}${i}</barline>";
1260 ▶ } elsif ($bar_style eq 'SectionClose' || $bar_style eq 'LocalRepeatClose') {
Condition66%2/3
$bar_style eq "SectionClose" or $bar_style eq "LocalRepeatClose"
l!l&&r!l&&!r
98036266
MC/DC50%1/2
$bar_style eq "SectionClose" or $bar_style eq "LocalRepeatClose"
$bar_style eq "SectionClose" $bar_style eq "LocalRepeatClose"
Truth table66%2/3
A or B
A= $bar_style eq "SectionClose"B= $bar_style eq "LocalRepeatClose"
ABresult
1X1
011
000
1261 98 push @out, "${pad}${i}<barline location=\"right\">";
1262 98 push @out, "${pad}${i}${i}<bar-style>light-heavy</bar-style>";
1263 98 push @out, "${pad}${i}</barline>";
1264 }
1265  
1266 37516 push @out, "${pad}</measure>";
1267 37516 return @out;
1268 }
1269  
1270 # ---------------------------------------------------------------------------
1271 # Private: slur / tie annotation pass
1272 # ---------------------------------------------------------------------------
1273  
1274 # Walk all events in a staff once and build an annotation hashref keyed by
1275 # stringified event reference. Each value is a hashref with:
1276 # slur_start => N this note opens slur arc N
1277 # slur_stop => N this note closes slur arc N (placed on the landing note, not the last Slur-bearing note)
1278 # tie_stop_keys => { pos_key => 1, ... } tie stops arriving at this note
1279 # tie_start_keys => { pos_key => 1, ... } tie starts leaving from this note
1280 #
1281 # "pos_key" is the raw position string with any ^ suffix stripped, used as an
1282 # opaque key to match the tied-to note. Ties that cross measure boundaries are
1283 # handled correctly because we walk the entire event list before grouping.
1284  
1285 261 ▶sub _annotate_events {
Subroutine _annotate_events: called CC=19 SCAR=29.5
1286 261 my ($self, $events) = @_;
1287 261 my %ann;
1288  
1289 261 my $in_slur = 0;
1290 261 my $slur_num = 0;
1291 261 my $last_slur_ev = undef;
1292 261 my $last_slur_num = 0;
1293 261 my %pending_tie; # pos_key => 1 for notes awaiting a tie-stop
1294  
1295 261 for my $ev (@$events) {
1296 167944 my $type = $ev->type;
1297 167944 ▶ next unless $type eq 'Note' || $type eq 'Chord' || $type eq 'Rest';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "Note" or $type eq "Chord" or $type eq "Rest" 42640 125304 167944
Condition100%6/6
$type eq "Note" or $type eq "Chord" or $type eq "Rest"
l!l&&r!l&&!r
1058801942442640
$type eq "Note" or $type eq "Chord"
l!l&&r!l&&!r
852642061662064
MC/DC100%3/3
$type eq "Note" or $type eq "Chord" or $type eq "Rest"
$type eq "Note" $type eq "Chord" $type eq "Rest"
Truth table100%4/4
A or B or C
A= $type eq "Note"B= $type eq "Chord"C= $type eq "Rest"
ABCresult
1XX1
01X1
0011
0000
1298  
1299 125304 my $key = "$ev"; # stringified reference, unique per object
1300  
1301 # Tie tracking (not applicable to rests)
1302 125304 ▶ unless ($type eq 'Rest') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type eq "Rest") 105880 19424 125304
1303 my @pos_strs = $type eq 'Chord'
1304 20616 ▶ ? @{$ev->data->{nwc_positions} // []}
Condition50%2/4
$ev->data->{"nwc_positions"} // []
l!l
206160
$ev->data->{"nwc_pos"} // 0
l!l
852640
MC/DC0%0/1
$ev->data->{"nwc_positions"} // []
$ev->data->{"nwc_positions"}
MC/DC0%0/1
$ev->data->{"nwc_pos"} // 0
$ev->data->{"nwc_pos"}
Truth table50%1/2
A
A= $ev->data->{"nwc_positions"}
Aresult
11
00
Truth table50%1/2
A
A= $ev->data->{"nwc_pos"}
Aresult
11
00
1305 105880 ▶ : ($ev->data->{nwc_pos} // '0');
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'Chord' ? : 20616 85264 105880
1306  
1307 105880 for my $ps (@pos_strs) {
1308 134392 (my $pk = $ps) =~ s/\^\z//; # strip tie marker to get the key
1309  
1310 134392 ▶ if (delete $pending_tie{$pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $pending_tie{$pk}) 4489 129903 134392
1311 4489 $ann{$key}{tie_stop_keys}{$pk} = 1;
1312 }
1313 134392 ▶ if ($ps =~ /\^\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ps =~ /\^\z/) 4537 129855 134392
1314 4537 $ann{$key}{tie_start_keys}{$pk} = 1;
1315 4537 $pending_tie{$pk} = 1;
1316 }
1317 }
1318 }
1319  
1320 # Slur tracking (not applicable to rests -- rests are inside slur spans
1321 # but don't carry the arc endpoint markers)
1322 125304 ▶ next if $type eq 'Rest';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "Rest" 19424 105880 125304
1323  
1324 105880 ▶ my $has_slur = any { $_ eq 'Slur' } @{$ev->data->{articulations} // []};
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
$ev->data->{"articulations"} // []
l!l
10584436
MC/DC100%1/1
$ev->data->{"articulations"} // []
$ev->data->{"articulations"}
Truth table100%2/2
A
A= $ev->data->{"articulations"}
Aresult
11
00
1325  
1326 105880 ▶ if ($has_slur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_slur) { } 20369 85511 105880
1327 20369 ▶ unless ($in_slur) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_slur) 5048 15321 20369
1328 5048 $slur_num = ($slur_num % $MAX_SLUR_NUMBER) + 1;
1329 5048 $ann{$key}{slur_start} = $slur_num;
1330 5048 $in_slur = 1;
1331 }
1332 20369 $last_slur_ev = $key; # kept only for the end-of-staff fallback below
1333 20369 $last_slur_num = $slur_num;
1334 } else {
1335 85511 ▶ if ($in_slur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_slur) 5044 80467 85511
1336 # Stop goes on the landing note (first note WITHOUT Slur), not the
1337 # last note that carries Slur. Placing it on the Slur-bearing note
1338 # produces a zero-length arc when only one note has the token.
1339 5044 $ann{$key}{slur_stop} = $last_slur_num;
1340 5044 $in_slur = 0;
1341 5044 $last_slur_ev = undef;
1342 }
1343 }
1344 }
1345  
1346 # Close any slur still open at the end of the staff
1347 261 ▶ $ann{$last_slur_ev}{slur_stop} = $last_slur_num if $in_slur && defined $last_slur_ev;
Branch: 1 branch
BranchTrueFalseTotal
if $in_slur and defined $last_slur_ev 4 257 261
Condition66%2/3
$in_slur and defined $last_slur_ev
!ll&&!rl&&r
25704
MC/DC50%1/2
$in_slur and defined $last_slur_ev
$in_slur defined $last_slur_ev
Truth table66%2/3
A and B
A= $in_slurB= defined $last_slur_ev
ABresult
0X0
100
111
1348  
1349 261 return \%ann;
1350 }
1351  
1352 # Associate each Lyric event with the immediately preceding Note or Chord event.
1353 # Lyric events in NWCTXT always follow the note they annotate.
1354 258 ▶sub _annotate_lyrics {
Subroutine _annotate_lyrics: called CC=11 SCAR=32.1
1355 258 my ($self, $events, $ann) = @_;
1356 258 ▶ $ann //= {};
Condition50%1/2
$ann //= {}
l!l
2580
MC/DC0%0/1
$ann //= {}
$ann
Truth table50%1/2
A
A= $ann
Aresult
11
00
1357  
1358 258 my $last_note_key;
1359 258 for my $ev (@$events) {
1360 167938 my $type = $ev->type;
1361 167938 ▶ if ($type eq 'Note' || $type eq 'Chord') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Note' or $type eq 'Chord') { } 105874 62064 167938
elsif ($type eq 'Lyric' and defined $last_note_key) { } 0 124128 124128
Condition100%3/3
$type eq "Note" or $type eq "Chord"
l!l&&r!l&&!r
852582061662064
MC/DC100%2/2
$type eq "Note" or $type eq "Chord"
$type eq "Note" $type eq "Chord"
Truth table100%3/3
A or B
A= $type eq "Note"B= $type eq "Chord"
ABresult
1X1
011
000
1362 105874 $last_note_key = "$ev";
1363 ▶ } elsif ($type eq 'Lyric' && defined $last_note_key) {
Condition33%1/3
$type eq "Lyric" and defined $last_note_key
!ll&&!rl&&r
6206400
MC/DC0%0/2
$type eq "Lyric" and defined $last_note_key
$type eq "Lyric" defined $last_note_key
Truth table33%1/3
A and B
A= $type eq "Lyric"B= defined $last_note_key
ABresult
0X0
100
111
1364 0 ▶ my $d = $ev->data // {};
Condition0%0/2
$ev->data // {}
l!l
00
MC/DC0%0/1
$ev->data // {}
$ev->data
Truth table0%0/2
A
A= $ev->data
Aresult
11
00
1365 0 ▶ push @{ $ann->{$last_note_key}{lyrics} }, {
Condition0%0/6
$d->{"text"} // ""
l!l
00
$d->{"verse"} // 1
l!l
00
$d->{"syllabic"} // "single"
l!l
00
MC/DC0%0/1
$d->{"text"} // ""
$d->{"text"}
MC/DC0%0/1
$d->{"verse"} // 1
$d->{"verse"}
MC/DC0%0/1
$d->{"syllabic"} // "single"
$d->{"syllabic"}
Truth table0%0/2
A
A= $d->{"text"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"verse"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"syllabic"}
Aresult
11
00
1366 text => $d->{text} // '',
1367 verse => $d->{verse} // 1,
1368 0 syllabic => $d->{syllabic} // 'single',
1369 };
1370 }
1371 }
1372 258 return $ann;
1373 }
1374  
1375 # Annotate wedge (hairpin) start/stop transitions into an existing %$ann hash.
1376 # NWC stores hairpins as Opts:Crescendo / Opts:Diminuendo on each note/rest/chord
1377 # under the arc, NOT as standalone DynVariance records.
1378 # Sets wedge_start => 'Crescendo'|'Diminuendo' on the first event of each arc,
1379 # and wedge_stop_after => 1 on the last event of each arc.
1380 261 ▶sub _annotate_wedges {
Subroutine _annotate_wedges: called CC=18 SCAR=28.9
1381 261 my ($self, $events, $ann) = @_;
1382 261 ▶ $ann //= {};
Condition50%1/2
$ann //= {}
l!l
2610
MC/DC0%0/1
$ann //= {}
$ann
Truth table50%1/2
A
A= $ann
Aresult
11
00
1383  
1384 261 my $wedge_now = undef; # 'Crescendo' | 'Diminuendo' | undef
1385 261 my $prev_wedge_key = undef; # stringified ref of last event under the arc
1386  
1387 261 for my $ev (@$events) {
1388 167945 my $type = $ev->type;
1389 167945 ▶ next unless $type eq 'Note' || $type eq 'Rest' || $type eq 'Chord';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "Note" or $type eq "Rest" or $type eq "Chord" 42640 125305 167945
Condition100%6/6
$type eq "Note" or $type eq "Rest" or $type eq "Chord"
l!l&&r!l&&!r
1046892061642640
$type eq "Note" or $type eq "Rest"
l!l&&r!l&&!r
852651942463256
MC/DC100%3/3
$type eq "Note" or $type eq "Rest" or $type eq "Chord"
$type eq "Note" $type eq "Rest" $type eq "Chord"
Truth table100%4/4
A or B or C
A= $type eq "Note"B= $type eq "Rest"C= $type eq "Chord"
ABCresult
1XX1
01X1
0011
0000
1390  
1391 125305 my $key = "$ev";
1392 125305 ▶ my $opts = $ev->data->{opts} // {};
Condition100%2/2
$ev->data->{"opts"} // {}
l!l
12526540
MC/DC100%1/1
$ev->data->{"opts"} // {}
$ev->data->{"opts"}
Truth table100%2/2
A
A= $ev->data->{"opts"}
Aresult
11
00
1393 my $ev_wedge = $opts->{Crescendo} ? 'Crescendo'
1394 125305 ▶ : $opts->{Diminuendo} ? 'Diminuendo'
Branch: 2 branches
BranchTrueFalseTotal
$opts->{'Crescendo'} ? : 3033 122272 125305
$opts->{'Diminuendo'} ? : 2692 119580 122272
1395 : undef;
1396  
1397 125305 ▶ if (defined $wedge_now && (!defined $ev_wedge || $ev_wedge ne $wedge_now)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wedge_now and !defined($ev_wedge) || $ev_wedge ne $wedge_now) 940 124365 125305
Condition100%6/6
defined $wedge_now and !defined($ev_wedge) || $ev_wedge ne $wedge_now
!ll&&!rl&&r
1195844781940
not defined $ev_wedge or $ev_wedge ne $wedge_now
l!l&&r!l&&!r
891494781
MC/DC100%3/3
defined $wedge_now and !defined($ev_wedge) || $ev_wedge ne $wedge_now
defined $wedge_now not defined $ev_wedge $ev_wedge ne $wedge_now
Truth table100%4/4
A and not(B) or C
A= defined $wedge_nowB= not defined $ev_wedgeC= $ev_wedge ne $wedge_now
ABCresult
0XX0
1000
11X1
1011
1398 940 ▶ $ann->{$prev_wedge_key}{wedge_stop_after} = 1 if defined $prev_wedge_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev_wedge_key 940 0 940
1399 940 $wedge_now = undef;
1400 }
1401 125305 ▶ if (!defined $wedge_now && defined $ev_wedge) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $wedge_now and defined $ev_wedge) 944 124361 125305
Condition100%3/3
not defined $wedge_now and defined $ev_wedge
!ll&&!rl&&r
4781119580944
MC/DC100%2/2
not defined $wedge_now and defined $ev_wedge
not defined $wedge_now defined $ev_wedge
Truth table100%3/3
not(A) and B
A= not defined $wedge_nowB= defined $ev_wedge
ABresult
0X0
100
111
1402 944 $ann->{$key}{wedge_start} = $ev_wedge;
1403 944 $wedge_now = $ev_wedge;
1404 }
1405  
1406 125305 ▶ $prev_wedge_key = defined $ev_wedge ? $key : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ev_wedge ? : 5725 119580 125305
1407 }
1408  
1409 # Close any arc still open at end of staff
1410 261 ▶ $ann->{$prev_wedge_key}{wedge_stop_after} = 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $wedge_now and defined $prev_wedge_key 4 257 261
Condition66%2/3
defined $wedge_now and defined $prev_wedge_key
!ll&&!rl&&r
25704
MC/DC50%1/2
defined $wedge_now and defined $prev_wedge_key
defined $wedge_now defined $prev_wedge_key
Truth table66%2/3
A and B
A= defined $wedge_nowB= defined $prev_wedge_key
ABresult
0X0
100
111
1411 if defined $wedge_now && defined $prev_wedge_key;
1412  
1413 261 return $ann;
1414 }
1415  
1416 # Associate each TempoVariance:Fermata with the immediately following Note,
1417 # Rest, or Chord. NWC places the TempoVariance record before the note it
1418 # decorates; the fermata is a <notations><fermata/> element on that note, not
1419 # a text direction.
1420 268 ▶sub _annotate_fermatas {
Subroutine _annotate_fermatas: called CC=10 SCAR=23.1
1421 268 my ($self, $events, $ann) = @_;
1422 268 ▶ $ann //= {};
Condition50%1/2
$ann //= {}
l!l
2680
MC/DC0%0/1
$ann //= {}
$ann
Truth table50%1/2
A
A= $ann
Aresult
11
00
1423  
1424 268 my $pending_fermata = 0;
1425 268 for my $ev (@$events) {
1426 167961 my $type = $ev->type;
1427 167961 ▶ if ($type eq 'Note' || $type eq 'Rest' || $type eq 'Chord') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Note' or $type eq 'Rest' or $type eq 'Chord') { } 125311 42650 167961
elsif ($type eq 'TempoVariance' and ($ev->data->{'style'} // '') eq 'Fermata') { } 14 85167 85181
Condition100%6/6
$type eq "Note" or $type eq "Rest" or $type eq "Chord"
l!l&&r!l&&!r
1046942061742650
$type eq "Note" or $type eq "Rest"
l!l&&r!l&&!r
852691942563267
MC/DC100%3/3
$type eq "Note" or $type eq "Rest" or $type eq "Chord"
$type eq "Note" $type eq "Rest" $type eq "Chord"
Truth table100%4/4
A or B or C
A= $type eq "Note"B= $type eq "Rest"C= $type eq "Chord"
ABCresult
1XX1
01X1
0011
0000
1428 125311 ▶ if ($pending_fermata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending_fermata) 12 125299 125311
1429 12 $ann->{"$ev"}{fermata} = 1;
1430 12 $pending_fermata = 0;
1431 }
1432 ▶ } elsif ($type eq 'TempoVariance'
Condition80%4/5
$type eq "TempoVariance" and ($ev->data->{"style"} // "") eq "Fermata"
!ll&&!rl&&r
4253110514
$ev->data->{"style"} // ""
l!l
1190
MC/DC50%1/2
$type eq "TempoVariance" and ($ev->data->{"style"} // "") eq "Fermata"
$type eq "TempoVariance" $ev->data->{"style"}
Truth table66%2/3
A and B
A= $type eq "TempoVariance"B= $ev->data->{"style"}
ABresult
0X0
100
111
1433 && ($ev->data->{style} // '') eq 'Fermata') {
1434 14 $pending_fermata = 1;
1435 }
1436 }
1437 268 return $ann;
1438 }
1439  
1440 # ---------------------------------------------------------------------------
1441 # Private: note / rest / chord XML emission
1442 # ---------------------------------------------------------------------------
1443  
1444 134386 ▶sub _emit_note_event {
Subroutine _emit_note_event: called CC=47 SCAR=39.3
1445 134386 my ($self, $event, $clef, $key_fifths, $divisions, $pad, $is_chord_member, $ev_ann, $bar_accidentals) = @_;
1446 134386 ▶ $ev_ann //= {};
Condition50%1/2
$ev_ann //= {}
l!l
1343860
MC/DC0%0/1
$ev_ann //= {}
$ev_ann
Truth table50%1/2
A
A= $ev_ann
Aresult
11
00
1447 134386 ▶ $bar_accidentals //= {};
Condition50%1/2
$bar_accidentals //= {}
l!l
1343860
MC/DC0%0/1
$bar_accidentals //= {}
$bar_accidentals
Truth table50%1/2
A
A= $bar_accidentals
Aresult
11
00
1448 134386 my $d = $event->data;
1449 134386 my @out;
1450 134386 my $i = $self->{_indent};
1451  
1452 134386 ▶ my $pitch = $self->_pos_to_pitch($d->{nwc_pos} // '0', $clef, $key_fifths);
Condition50%1/2
$d->{"nwc_pos"} // 0
l!l
1343860
MC/DC0%0/1
$d->{"nwc_pos"} // 0
$d->{"nwc_pos"}
Truth table50%1/2
A
A= $d->{"nwc_pos"}
Aresult
11
00
1453  
1454 # Accidentals carry through the bar: once a pitch class is altered in a measure,
1455 # all subsequent notes at the same step+octave inherit that alteration.
1456 134386 my $pitch_key = $pitch->{step} . $pitch->{octave};
1457 134386 ▶ if (defined $pitch->{accidental}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $pitch->{'accidental'}) { } 8954 125432 134386
elsif (exists $bar_accidentals->{$pitch_key}) { } 4610 120822 125432
1458 8954 $bar_accidentals->{$pitch_key} = $pitch->{alter};
1459 } elsif (exists $bar_accidentals->{$pitch_key}) {
1460 4610 $pitch->{alter} = $bar_accidentals->{$pitch_key};
1461 }
1462 134386 my $ticks = _rational_to_ticks($event->duration, $divisions);
1463 134386 ▶ my $type = $NWC_TYPE_MAP{ $d->{base_dur} // '4th' } // 'quarter';
Condition50%2/4
$NWC_TYPE_MAP{$d->{"base_dur"} // "4th"} // "quarter"
l!l
1343860
$d->{"base_dur"} // "4th"
l!l
1343860
MC/DC0%0/1
$NWC_TYPE_MAP{$d->{"base_dur"} // "4th"} // "quarter"
$NWC_TYPE_MAP{$d->{"base_dur"} // "4th"}
MC/DC0%0/1
$d->{"base_dur"} // "4th"
$d->{"base_dur"}
Truth table50%1/2
A
A= $NWC_TYPE_MAP{$d->{"base_dur"} // "4th"}
Aresult
11
00
Truth table50%1/2
A
A= $d->{"base_dur"}
Aresult
11
00
1464  
1465 # Tie flags for this specific position key
1466 134386 ▶ (my $pk = $d->{nwc_pos} // '0') =~ s/\^\z//;
Condition50%1/2
$d->{"nwc_pos"} // 0
l!l
1343860
MC/DC0%0/1
$d->{"nwc_pos"} // 0
$d->{"nwc_pos"}
Truth table50%1/2
A
A= $d->{"nwc_pos"}
Aresult
11
00
1467 134386 ▶ my $tie_stop = ($ev_ann->{tie_stop_keys} // {})->{$pk};
Condition100%2/2
$ev_ann->{"tie_stop_keys"} // {}
l!l
5104883338
MC/DC100%1/1
$ev_ann->{"tie_stop_keys"} // {}
$ev_ann->{"tie_stop_keys"}
Truth table100%2/2
A
A= $ev_ann->{"tie_stop_keys"}
Aresult
11
00
1468 134386 ▶ my $tie_start = ($ev_ann->{tie_start_keys} // {})->{$pk};
Condition100%2/2
$ev_ann->{"tie_start_keys"} // {}
l!l
5128883098
MC/DC100%1/1
$ev_ann->{"tie_start_keys"} // {}
$ev_ann->{"tie_start_keys"}
Truth table100%2/2
A
A= $ev_ann->{"tie_start_keys"}
Aresult
11
00
1469  
1470 # Slur flags: only the first note of a chord carries the arc endpoints
1471 134386 ▶ my $slur_start = !$is_chord_member && $ev_ann->{slur_start};
Condition100%3/3
!$is_chord_member && $ev_ann->{'slur_start'}
!ll&&!rl&&r
285121008275047
MC/DC100%2/2
!$is_chord_member && $ev_ann->{'slur_start'}
!$is_chord_member $ev_ann->{'slur_start'}
Truth table100%3/3
not(A) && B
A= !$is_chord_memberB= $ev_ann->{'slur_start'}
ABresult
0X0
100
111
1472 134386 ▶ my $slur_stop = !$is_chord_member && $ev_ann->{slur_stop};
Condition100%3/3
!$is_chord_member && $ev_ann->{'slur_stop'}
!ll&&!rl&&r
285121008275047
MC/DC100%2/2
!$is_chord_member && $ev_ann->{'slur_stop'}
!$is_chord_member $ev_ann->{'slur_stop'}
Truth table100%3/3
not(A) && B
A= !$is_chord_memberB= $ev_ann->{'slur_stop'}
ABresult
0X0
100
111
1473  
1474 134386 push @out, "${pad}<note>";
1475 134386 ▶ push @out, "${pad}${i}<chord/>" if $is_chord_member;
Branch: 1 branch
BranchTrueFalseTotal
if $is_chord_member 28512 105874 134386
1476 134386 ▶ push @out, "${pad}${i}<grace/>" if $d->{is_grace};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"is_grace"} 1 134385 134386
1477 134386 push @out, "${pad}${i}<pitch>";
1478 134386 push @out, "${pad}${i}${i}<step>$pitch->{step}</step>";
1479 134386 ▶ push @out, "${pad}${i}${i}<alter>$pitch->{alter}</alter>" if $pitch->{alter};
Branch: 1 branch
BranchTrueFalseTotal
if $pitch->{"alter"} 27461 106925 134386
1480 134386 push @out, "${pad}${i}${i}<octave>$pitch->{octave}</octave>";
1481 134386 push @out, "${pad}${i}</pitch>";
1482 134386 ▶ unless ($d->{is_grace}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($d->{"is_grace"}) 134385 1 134386
1483 134385 push @out, "${pad}${i}<duration>$ticks</duration>";
1484 # <tie> elements come after <duration> and before <voice> per MusicXML schema
1485 134385 ▶ push @out, "${pad}${i}<tie type=\"stop\"/>" if $tie_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $tie_stop 4488 129897 134385
1486 134385 ▶ push @out, "${pad}${i}<tie type=\"start\"/>" if $tie_start;
Branch: 1 branch
BranchTrueFalseTotal
if $tie_start 4536 129849 134385
1487 }
1488 134386 push @out, "${pad}${i}<voice>1</voice>";
1489 134386 push @out, "${pad}${i}<type>$type</type>";
1490 134386 ▶ push @out, "${pad}${i}<dot/>" for 1 .. ($d->{dots} // 0);
Condition100%2/2
$d->{"dots"} // 0
l!l
13436620
MC/DC100%1/1
$d->{"dots"} // 0
$d->{"dots"}
Truth table100%2/2
A
A= $d->{"dots"}
Aresult
11
00
1491 push @out, "${pad}${i}<accidental>$pitch->{accidental}</accidental>"
1492 134386 ▶ if $pitch->{accidental};
Branch: 1 branch
BranchTrueFalseTotal
if $pitch->{"accidental"} 8954 125432 134386
1493  
1494 # <notations> block
1495 134386 my @nots;
1496 134386 ▶ push @nots, "${pad}${i}${i}<tied type=\"stop\"/>" if $tie_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $tie_stop 4488 129898 134386
1497 134386 ▶ push @nots, "${pad}${i}${i}<tied type=\"start\"/>" if $tie_start;
Branch: 1 branch
BranchTrueFalseTotal
if $tie_start 4536 129850 134386
1498 134386 ▶ push @nots, "${pad}${i}${i}<slur number=\"$slur_stop\" type=\"stop\"/>" if $slur_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $slur_stop 5047 129339 134386
1499 134386 ▶ push @nots, "${pad}${i}${i}<slur number=\"$slur_start\" type=\"start\"/>" if $slur_start;
Branch: 1 branch
BranchTrueFalseTotal
if $slur_start 5047 129339 134386
1500 134386 ▶ push @nots, "${pad}${i}${i}<fermata/>" if !$is_chord_member && $ev_ann->{fermata};
Branch: 1 branch
BranchTrueFalseTotal
if not $is_chord_member and $ev_ann->{"fermata"} 2 134384 134386
Condition100%3/3
not $is_chord_member and $ev_ann->{"fermata"}
!ll&&!rl&&r
285121058722
MC/DC100%2/2
not $is_chord_member and $ev_ann->{"fermata"}
not $is_chord_member $ev_ann->{"fermata"}
Truth table100%3/3
not(A) and B
A= not $is_chord_memberB= $ev_ann->{"fermata"}
ABresult
0X0
100
111
1501  
1502 # Articulations: only on the first note of a chord (is_chord_member is false)
1503 my @artic_tokens = !$is_chord_member
1504 134386 ▶ ? grep { $_ ne 'Slur' } @{ $d->{articulations} // [] }
Branch: 1 branch
BranchTrueFalseTotal
!$is_chord_member ? : 105874 28512 134386
Condition100%2/2
$d->{"articulations"} // []
l!l
10583836
MC/DC100%1/1
$d->{"articulations"} // []
$d->{"articulations"}
Truth table100%2/2
A
A= $d->{"articulations"}
Aresult
11
00
1505 : ();
1506 134386 ▶ if (@artic_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@artic_tokens) 10639 123747 134386
1507 10639 my (@artic_els, @ornament_els, @direct_els);
1508 10639 for my $tok (@artic_tokens) {
1509 10784 my $map = $ARTICULATION_MAP{$tok};
1510 10784 ▶ if (!defined $map) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $map) 1 10783 10784
1511 1 carp _fmt_msg('warn_unsupported_art', $tok);
1512 1 next;
1513 }
1514 10783 ▶ if ($map->{group} eq 'articulations') { push @artic_els, $map->{element} }
Branch: 2 branches
BranchTrueFalseTotal
if ($map->{'group'} eq 'articulations') { } 10781 2 10783
elsif ($map->{'group'} eq 'ornaments') { } 1 1 2
1515 1 elsif ($map->{group} eq 'ornaments') { push @ornament_els, $map->{element} }
1516 1 else { push @direct_els, $map->{element} }
1517 }
1518 10639 ▶ if (@artic_els) {
Branch: 1 branch
BranchTrueFalseTotal
if (@artic_els) 10636 3 10639
1519 10636 push @nots, "${pad}${i}${i}<articulations>";
1520 10636 push @nots, "${pad}${i}${i}${i}<$_/>" for @artic_els;
1521 10636 push @nots, "${pad}${i}${i}</articulations>";
1522 }
1523 10639 ▶ if (@ornament_els) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ornament_els) 1 10638 10639
1524 1 push @nots, "${pad}${i}${i}<ornaments>";
1525 1 push @nots, "${pad}${i}${i}${i}<$_/>" for @ornament_els;
1526 1 push @nots, "${pad}${i}${i}</ornaments>";
1527 }
1528 10639 push @nots, "${pad}${i}${i}<$_/>" for @direct_els;
1529 }
1530  
1531 134386 ▶ if (@nots) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nots) 27765 106621 134386
1532 27765 push @out, "${pad}${i}<notations>";
1533 27765 push @out, @nots;
1534 27765 push @out, "${pad}${i}</notations>";
1535 }
1536  
1537 # Lyrics: only on the first note of a chord (is_chord_member is false).
1538 134386 ▶ unless ($is_chord_member) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_chord_member) 105874 28512 134386
1539 105874 ▶ for my $lyric (@{ $ev_ann->{lyrics} // [] }) {
Condition50%1/2
$ev_ann->{"lyrics"} // []
l!l
0105874
MC/DC0%0/1
$ev_ann->{"lyrics"} // []
$ev_ann->{"lyrics"}
Truth table50%1/2
A
A= $ev_ann->{"lyrics"}
Aresult
11
00
1540 0 ▶ my $num = $lyric->{verse} // 1;
Condition0%0/2
$lyric->{"verse"} // 1
l!l
00
MC/DC0%0/1
$lyric->{"verse"} // 1
$lyric->{"verse"}
Truth table0%0/2
A
A= $lyric->{"verse"}
Aresult
11
00
1541 0 ▶ my $syllabic = $lyric->{syllabic} // 'single';
Condition0%0/2
$lyric->{"syllabic"} // "single"
l!l
00
MC/DC0%0/1
$lyric->{"syllabic"} // "single"
$lyric->{"syllabic"}
Truth table0%0/2
A
A= $lyric->{"syllabic"}
Aresult
11
00
1542 0 ▶ my $text = _xml_escape($lyric->{text} // '');
Condition0%0/2
$lyric->{"text"} // ""
l!l
00
MC/DC0%0/1
$lyric->{"text"} // ""
$lyric->{"text"}
Truth table0%0/2
A
A= $lyric->{"text"}
Aresult
11
00
1543 0 push @out, "${pad}${i}<lyric number=\"$num\">";
1544 0 push @out, "${pad}${i}${i}<syllabic>$syllabic</syllabic>";
1545 0 push @out, "${pad}${i}${i}<text>$text</text>";
1546 0 push @out, "${pad}${i}</lyric>";
1547 }
1548 }
1549  
1550 134386 push @out, "${pad}</note>";
1551 134386 return @out;
1552 }
1553  
1554 19424 ▶sub _emit_rest_event {
Subroutine _emit_rest_event: called CC=8 SCAR=20.9
1555 19424 my ($self, $event, $divisions, $pad, $ev_ann, $measure_ticks) = @_;
1556 19424 ▶ $ev_ann //= {};
Condition50%1/2
$ev_ann //= {}
l!l
194240
MC/DC0%0/1
$ev_ann //= {}
$ev_ann
Truth table50%1/2
A
A= $ev_ann
Aresult
11
00
1557 19424 my $d = $event->data;
1558 19424 my @out;
1559 19424 my $i = $self->{_indent};
1560  
1561 19424 my $ticks = _rational_to_ticks($event->duration, $divisions);
1562 19424 ▶ my $type = $NWC_TYPE_MAP{ $d->{base_dur} // '4th' } // 'quarter';
Condition50%2/4
$NWC_TYPE_MAP{$d->{"base_dur"} // "4th"} // "quarter"
l!l
194240
$d->{"base_dur"} // "4th"
l!l
194240
MC/DC0%0/1
$NWC_TYPE_MAP{$d->{"base_dur"} // "4th"} // "quarter"
$NWC_TYPE_MAP{$d->{"base_dur"} // "4th"}
MC/DC0%0/1
$d->{"base_dur"} // "4th"
$d->{"base_dur"}
Truth table50%1/2
A
A= $NWC_TYPE_MAP{$d->{"base_dur"} // "4th"}
Aresult
11
00
Truth table50%1/2
A
A= $d->{"base_dur"}
Aresult
11
00
1563  
1564 19424 push @out, "${pad}<note>";
1565 19424 ▶ if (defined $measure_ticks) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $measure_ticks) { } 11789 7635 19424
1566 11789 push @out, "${pad}${i}<rest measure=\"yes\"/>";
1567 11789 push @out, "${pad}${i}<duration>$measure_ticks</duration>";
1568 11789 push @out, "${pad}${i}<voice>1</voice>";
1569 } else {
1570 7635 push @out, "${pad}${i}<rest/>";
1571 7635 push @out, "${pad}${i}<duration>$ticks</duration>";
1572 7635 push @out, "${pad}${i}<voice>1</voice>";
1573 7635 push @out, "${pad}${i}<type>$type</type>";
1574 7635 ▶ push @out, "${pad}${i}<dot/>" for 1 .. ($d->{dots} // 0);
Condition50%1/2
$d->{"dots"} // 0
l!l
76350
MC/DC0%0/1
$d->{"dots"} // 0
$d->{"dots"}
Truth table50%1/2
A
A= $d->{"dots"}
Aresult
11
00
1575 }
1576 19424 ▶ if ($ev_ann->{fermata}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev_ann->{"fermata"}) 2 19422 19424
1577 2 push @out, "${pad}${i}<notations>";
1578 2 push @out, "${pad}${i}${i}<fermata/>";
1579 2 push @out, "${pad}${i}</notations>";
1580 }
1581 19424 push @out, "${pad}</note>";
1582 19424 return @out;
1583 }
1584  
1585 20616 ▶sub _emit_chord_event {
Subroutine _emit_chord_event: called CC=10 SCAR=23.1
1586 20616 my ($self, $event, $clef, $key_fifths, $divisions, $pad, $ev_ann, $bar_accidentals) = @_;
1587 20616 ▶ $ev_ann //= {};
Condition50%1/2
$ev_ann //= {}
l!l
206160
MC/DC0%0/1
$ev_ann //= {}
$ev_ann
Truth table50%1/2
A
A= $ev_ann
Aresult
11
00
1588 20616 ▶ $bar_accidentals //= {};
Condition50%1/2
$bar_accidentals //= {}
l!l
206160
MC/DC0%0/1
$bar_accidentals //= {}
$bar_accidentals
Truth table50%1/2
A
A= $bar_accidentals
Aresult
11
00
1589 20616 my $d = $event->data;
1590 20616 my @out;
1591  
1592 20616 ▶ my $positions = $d->{nwc_positions} // ['0'];
Condition50%1/2
$d->{"nwc_positions"} // [0]
l!l
206160
MC/DC0%0/1
$d->{"nwc_positions"} // [0]
$d->{"nwc_positions"}
Truth table50%1/2
A
A= $d->{"nwc_positions"}
Aresult
11
00
1593 20616 my $first = 1;
1594  
1595 20616 for my $pos_str (@$positions) {
1596 # Build a per-position annotation that inherits slur flags (first note only)
1597 # and picks the tie flags for this specific position key.
1598 49128 (my $pk = $pos_str) =~ s/\^\z//;
1599 my %pos_ann = (
1600 tie_stop_keys => { $pk => ($ev_ann->{tie_stop_keys} // {})->{$pk} // 0 },
1601 tie_start_keys => { $pk => ($ev_ann->{tie_start_keys} // {})->{$pk} // 0 },
1602 ($first ? (
1603 slur_start => $ev_ann->{slur_start},
1604 slur_stop => $ev_ann->{slur_stop},
1605 lyrics => $ev_ann->{lyrics},
1606 fermata => $ev_ann->{fermata},
1607 49128 ▶ ) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 20616 28512 49128
Condition100%8/8
+($ev_ann->{"tie_stop_keys"} // {})->{$pk} // 0
l!l
256846560
$ev_ann->{"tie_stop_keys"} // {}
l!l
288046248
+($ev_ann->{"tie_start_keys"} // {})->{$pk} // 0
l!l
237646752
$ev_ann->{"tie_start_keys"} // {}
l!l
280846320
MC/DC100%1/1
+($ev_ann->{"tie_stop_keys"} // {})->{$pk} // 0
$ev_ann->{"tie_stop_keys"}
MC/DC100%1/1
+($ev_ann->{"tie_start_keys"} // {})->{$pk} // 0
$ev_ann->{"tie_start_keys"}
Truth table100%2/2
A
A= $ev_ann->{"tie_stop_keys"}
Aresult
11
00
Truth table100%2/2
A
A= $ev_ann->{"tie_start_keys"}
Aresult
11
00
1608 );
1609 49128 push @out, $self->_emit_note_event(
1610 _chord_note_event($event, $pos_str),
1611 $clef, $key_fifths, $divisions, $pad, !$first, \%pos_ann, $bar_accidentals
1612 );
1613 49128 $first = 0;
1614 }
1615 20616 return @out;
1616 }
1617  
1618 # Build a synthetic Note event for one pitch member of a Chord.
1619 # Uses Event->new so that validation, rational reduction and the
1620 # public accessor contract are all preserved.
1621 49128 ▶sub _chord_note_event {
Subroutine _chord_note_event: called CC=3 SCAR=11.7
1622 49128 my ($chord_event, $pos_str) = @_;
1623 49128 my $d = $chord_event->data;
1624 return Music::NWC2MusicXML::Event->new(
1625 type => 'Note',
1626 duration => $chord_event->duration,
1627 data => {
1628 nwc_pos => $pos_str,
1629 base_dur => $d->{base_dur},
1630 dots => $d->{dots} // 0,
1631 articulations => $d->{articulations} // [],
1632 is_grace => $d->{is_grace},
1633 },
1634 49128 ▶ );
Condition50%2/4
$d->{"dots"} // 0
l!l
491280
$d->{"articulations"} // []
l!l
491280
MC/DC0%0/1
$d->{"dots"} // 0
$d->{"dots"}
MC/DC0%0/1
$d->{"articulations"} // []
$d->{"articulations"}
Truth table50%1/2
A
A= $d->{"dots"}
Aresult
11
00
Truth table50%1/2
A
A= $d->{"articulations"}
Aresult
11
00
1635 }
1636  
1637 # ---------------------------------------------------------------------------
1638 # Private: pitch conversion
1639 # ---------------------------------------------------------------------------
1640  
1641 # Convert an NWC position string (e.g. "#-6", "b3", "-9^") to a MusicXML
1642 # pitch descriptor { step, octave, alter, accidental }.
1643 #
1644 # Position 0 = middle line (3rd from bottom) of the staff (clef-specific).
1645 # Negative positions go DOWN the staff; positive positions go UP.
1646 # Formula: diatonic_index = octave*7 + step_index + pos_num
1647 134403 ▶sub _pos_to_pitch {
Subroutine _pos_to_pitch: called CC=7 SCAR=19.7
1648 134403 my ($self, $pos_str, $clef, $key_fifths) = @_;
1649 134403 ▶ $pos_str //= '0';
Condition50%1/2
$pos_str //= 0
l!l
1344030
MC/DC0%0/1
$pos_str //= 0
$pos_str
Truth table50%1/2
A
A= $pos_str
Aresult
11
00
1650 134403 ▶ $clef //= 'Treble';
Condition50%1/2
$clef //= "Treble"
l!l
1344030
MC/DC0%0/1
$clef //= "Treble"
$clef
Truth table50%1/2
A
A= $clef
Aresult
11
00
1651 134403 ▶ $key_fifths //= 0;
Condition50%1/2
$key_fifths //= 0
l!l
1344030
MC/DC0%0/1
$key_fifths //= 0
$key_fifths
Truth table50%1/2
A
A= $key_fifths
Aresult
11
00
1652  
1653 # Parse: optional accidental prefix + signed integer + optional tie marker
1654 134403 my ($acc_prefix, $pos_num) = ('', 0);
1655 134403 ▶ if ($pos_str =~ /\A([#bnx]*)(-?\d+)\^?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_str =~ /\A([#bnx]*)(-?\d+)\^?\z/) 134403 0 134403
1656 134403 ($acc_prefix, $pos_num) = ($1, $2 + 0);
1657 }
1658  
1659 134403 ▶ my $ref = $CLEF_REF{$clef} // $CLEF_REF{Treble};
Condition66%2/3
$CLEF_REF{$clef} // $CLEF_REF{"Treble"}
l!l&&r!l&&!r
13440210
MC/DC0%0/2
$CLEF_REF{$clef} // $CLEF_REF{"Treble"}
$CLEF_REF{$clef} $CLEF_REF{"Treble"}
Truth table66%2/3
A // B
A= $CLEF_REF{$clef}B= $CLEF_REF{"Treble"}
ABresult
1X1
011
000
1660 134403 my $index = $ref->[1] * 7 + $ref->[0] + $pos_num;
1661  
1662 134403 my $octave = floor($index / 7);
1663 134403 my $step_i = $index - $octave * 7;
1664  
1665 134403 my $step = $STEP_NAMES[$step_i];
1666 134403 my $key_alt = _key_alter_for_step($step_i, $key_fifths);
1667  
1668 # Dispatch: known prefixes from %ACCIDENTAL_MAP; empty/unknown -> key-sig alter.
1669 134403 my ($alter, $accidental);
1670 134403 ▶ if (my $acc = $ACCIDENTAL_MAP{$acc_prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $acc = $ACCIDENTAL_MAP{$acc_prefix}) { } 8963 125440 134403
1671 8963 ($alter, $accidental) = @$acc;
1672 } else {
1673 125440 $alter = $key_alt;
1674 125440 $accidental = undef;
1675 }
1676  
1677 134403 return { step => $step, octave => $octave, alter => $alter, accidental => $accidental };
1678 }
1679  
1680 134403 ▶sub _key_alter_for_step {
Subroutine _key_alter_for_step: called CC=6 SCAR=17.9
1681 134403 my ($step_i, $key_fifths) = @_;
1682 134403 ▶ return 0 unless $key_fifths;
Branch: 1 branch
BranchTrueFalseTotal
unless $key_fifths 25414 108989 134403
1683  
1684 # P1: $key_fifths != 0 (guarded above). P2: sharp/flat differ only in sign+table.
1685 108989 ▶ my ($sign, $steps) = $key_fifths > 0
Branch: 1 branch
BranchTrueFalseTotal
$key_fifths > 0 ? : 55588 53401 108989
1686 ? (1, \@SHARP_STEPS)
1687 : (-1, \@FLAT_STEPS);
1688 108989 ▶ my $n = abs($key_fifths) > 7 ? 7 : abs($key_fifths);
Branch: 1 branch
BranchTrueFalseTotal
abs $key_fifths > 7 ? : 1 108988 108989
1689 108989 for my $k (0 .. $n - 1) {
1690 216522 ▶ return $sign if $steps->[$k] == $step_i;
Branch: 1 branch
BranchTrueFalseTotal
if $steps->[$k] == $step_i 23860 192662 216522
1691 }
1692 85129 return 0;
1693 }
1694  
1695 184977 ▶sub _is_whole_rest {
Subroutine _is_whole_rest: called CC=7 SCAR=20.3
1696 184977 my ($event) = @_;
1697 184977 ▶ return 0 unless $event->type eq 'Rest';
Branch: 1 branch
BranchTrueFalseTotal
unless $event->type eq "Rest" 153762 31215 184977
1698 31215 ▶ my $d = $event->data // {};
Condition50%1/2
$event->data // {}
l!l
312150
MC/DC0%0/1
$event->data // {}
$event->data
Truth table50%1/2
A
A= $event->data
Aresult
11
00
1699 31215 ▶ return (($d->{base_dur} // '') eq 'Whole' && !($d->{dots} // 0)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($d->{'base_dur'} // '') eq 'Whole' && !($d->{'dots'} // 0) ? : 23579 7636 31215
Condition57%4/7
($d->{"base_dur"} // "") eq "Whole" and not $d->{"dots"} // 0
!ll&&!rl&&r
7636023579
$d->{"base_dur"} // ""
l!l
312150
$d->{"dots"} // 0
l!l
235790
MC/DC0%0/2
($d->{"base_dur"} // "") eq "Whole" and not $d->{"dots"} // 0
$d->{"base_dur"} $d->{"dots"}
Truth table33%1/3
A and not(B)
A= $d->{"base_dur"}B= $d->{"dots"}
ABresult
0X0
110
101
1700 }
1701  
1702 # Length of one measure in ticks, or undef when the time signature is
1703 # unknown or the length is not a whole number of ticks.
1704 11793 ▶sub _measure_ticks {
Subroutine _measure_ticks: called CC=5 SCAR=16.2
1705 11793 my ($ts, $divisions) = @_;
1706 11793 ▶ return undef unless $ts && $ts->{beats} && $ts->{beat_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $ts and $ts->{"beats"} and $ts->{"beat_type"} 1 11792 11793
Condition66%4/6
$ts and $ts->{"beats"} and $ts->{"beat_type"}
!ll&&!rl&&r
1011792
$ts and $ts->{"beats"}
!ll&&!rl&&r
1011792
MC/DC33%1/3
$ts and $ts->{"beats"} and $ts->{"beat_type"}
$ts $ts->{"beats"} $ts->{"beat_type"}
Truth table50%2/4
A and B and C
A= $tsB= $ts->{"beats"}C= $ts->{"beat_type"}
ABCresult
0XX0
10X0
1100
1111
1707 11792 my $num = $ts->{beats} * $QUARTERS_PER_WHOLE * $divisions;
1708 11792 ▶ return undef if $num % $ts->{beat_type};
Branch: 1 branch
BranchTrueFalseTotal
if $num % $ts->{"beat_type"} 1 11791 11792
1709 11791 return $num / $ts->{beat_type};
1710 }
1711  
1712 153815 ▶sub _rational_to_ticks {
Subroutine _rational_to_ticks: called CC=1 SCAR=0.0
1713 153815 my ($rational, $divisions) = @_;
1714 153815 return int($rational->[0] * $divisions / $rational->[1] + 0.5);
1715 }
1716  
1717 130 ▶sub _emit_tempo {
Subroutine _emit_tempo: called CC=8 SCAR=20.9
1718 130 my ($self, $bpm, $base, $text, $pad) = @_;
1719 130 my $i = $self->{_indent};
1720 130 ▶ $bpm //= 120;
Condition50%1/2
$bpm //= 120
l!l
1300
MC/DC0%0/1
$bpm //= 120
$bpm
Truth table50%1/2
A
A= $bpm
Aresult
11
00
1721 130 ▶ $base //= 'Quarter';
Condition50%1/2
$base //= "Quarter"
l!l
1300
MC/DC0%0/1
$base //= "Quarter"
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
1722 130 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
1300
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
1723  
1724 130 ▶ my $map = $TEMPO_BASE_MAP{$base} // $TEMPO_BASE_MAP{Quarter};
Condition33%1/3
$TEMPO_BASE_MAP{$base} // $TEMPO_BASE_MAP{"Quarter"}
l!l&&r!l&&!r
13000
MC/DC0%0/2
$TEMPO_BASE_MAP{$base} // $TEMPO_BASE_MAP{"Quarter"}
$TEMPO_BASE_MAP{$base} $TEMPO_BASE_MAP{"Quarter"}
Truth table33%1/3
A // B
A= $TEMPO_BASE_MAP{$base}B= $TEMPO_BASE_MAP{"Quarter"}
ABresult
1X1
011
000
1725 130 my $unit = $map->{unit};
1726 130 my $dot = $map->{dot};
1727 130 my $sound_bpm = int($bpm * $map->{factor} + 0.5);
1728  
1729 130 my @out;
1730 130 push @out, "${pad}<direction placement=\"above\">";
1731 130 ▶ if (length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) 1 129 130
1732 1 push @out, "${pad}${i}<direction-type>";
1733 1 push @out, "${pad}${i}${i}<words font-style=\"italic\">" . _xml_escape($text) . " </words>";
1734 1 push @out, "${pad}${i}</direction-type>";
1735 }
1736 130 ▶ my $parens = length($text) ? 'yes' : 'no';
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 1 129 130
1737 130 push @out, "${pad}${i}<direction-type>";
1738 130 push @out, "${pad}${i}${i}<metronome parentheses=\"$parens\">";
1739 130 push @out, "${pad}${i}${i}${i}<beat-unit>$unit</beat-unit>";
1740 130 ▶ push @out, "${pad}${i}${i}${i}<beat-unit-dot/>" if $dot;
Branch: 1 branch
BranchTrueFalseTotal
if $dot 1 129 130
1741 130 push @out, "${pad}${i}${i}${i}<per-minute>$bpm</per-minute>";
1742 130 push @out, "${pad}${i}${i}</metronome>";
1743 130 push @out, "${pad}${i}</direction-type>";
1744 130 push @out, "${pad}${i}<sound tempo=\"$sound_bpm\"/>";
1745 130 push @out, "${pad}</direction>";
1746 130 return @out;
1747 }
1748  
1749 2407 ▶sub _emit_dynamic {
Subroutine _emit_dynamic: called CC=8 SCAR=20.8
1750 2407 my ($self, $marking, $placement, $pad) = @_;
1751 2407 my $i = $self->{_indent};
1752 2407 ▶ $marking //= '';
Condition50%1/2
$marking //= ""
l!l
24070
MC/DC0%0/1
$marking //= ""
$marking
Truth table50%1/2
A
A= $marking
Aresult
11
00
1753 2407 ▶ $placement //= '';
Condition50%1/2
$placement //= ""
l!l
24070
MC/DC0%0/1
$placement //= ""
$placement
Truth table50%1/2
A
A= $placement
Aresult
11
00
1754  
1755 2407 ▶ unless (exists $DYNAMIC_MAP{$marking}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $DYNAMIC_MAP{$marking}) 2 2405 2407
1756 2 ▶ carp _fmt_msg('warn_unknown_dynamic', $marking) if $marking ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $marking ne "" 1 1 2
1757 2 return ();
1758 }
1759  
1760 2405 ▶ my $place = (lc($placement) eq 'above') ? 'above' : 'below';
Branch: 1 branch
BranchTrueFalseTotal
lc $placement eq 'above' ? : 2 2403 2405
1761 2405 my @out;
1762 2405 push @out, "${pad}<direction placement=\"$place\">";
1763 2405 push @out, "${pad}${i}<direction-type>";
1764 2405 push @out, "${pad}${i}${i}<dynamics>";
1765 2405 push @out, "${pad}${i}${i}${i}<$marking/>";
1766 2405 push @out, "${pad}${i}${i}</dynamics>";
1767 2405 push @out, "${pad}${i}</direction-type>";
1768 2405 my $dyn_vel = $self->{_dyn_vel};
1769 2405 ▶ if (defined $dyn_vel && defined $dyn_vel->{$marking}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dyn_vel and defined $dyn_vel->{$marking}) 2401 4 2405
Condition66%2/3
defined $dyn_vel and defined $dyn_vel->{$marking}
!ll&&!rl&&r
402401
MC/DC50%1/2
defined $dyn_vel and defined $dyn_vel->{$marking}
defined $dyn_vel defined $dyn_vel->{$marking}
Truth table66%2/3
A and B
A= defined $dyn_velB= defined $dyn_vel->{$marking}
ABresult
0X0
100
111
1770 2401 my $pct = int($dyn_vel->{$marking} * 100 / 127 + 0.5);
1771 2401 push @out, "${pad}${i}<sound dynamics=\"$pct\"/>";
1772 }
1773 2405 push @out, "${pad}</direction>";
1774 2405 return @out;
1775 }
1776  
1777 # Map hairpin-annotation style -> MusicXML wedge type (used by _annotate_wedges output on notes,
1778 # NOT for standalone DynVariance events -- those are text markings handled by _emit_dyn_variance).
1779 13 Readonly::Hash my %WEDGE_MAP => (
1780 Crescendo => 'crescendo',
1781 Diminuendo => 'diminuendo',
1782 crescOff => 'stop',
1783 );
1784  
1785 1885 ▶sub _emit_wedge {
Subroutine _emit_wedge: called CC=5 SCAR=16.1
1786 1885 my ($self, $style, $placement, $pad) = @_;
1787 1885 my $i = $self->{_indent};
1788 1885 ▶ $style //= '';
Condition50%1/2
$style //= ""
l!l
18850
MC/DC0%0/1
$style //= ""
$style
Truth table50%1/2
A
A= $style
Aresult
11
00
1789 1885 ▶ $placement //= '';
Condition100%2/2
$placement //= ""
l!l
31882
MC/DC100%1/1
$placement //= ""
$placement
Truth table100%2/2
A
A= $placement
Aresult
11
00
1790  
1791 1885 my $wedge_type = $WEDGE_MAP{$style};
1792 1885 ▶ return () unless defined $wedge_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wedge_type 1 1884 1885
1793  
1794 1884 ▶ my $place = (lc($placement) eq 'above') ? 'above' : 'below';
Branch: 1 branch
BranchTrueFalseTotal
lc $placement eq 'above' ? : 2 1882 1884
1795 1884 my @out;
1796 1884 push @out, "${pad}<direction placement=\"$place\">";
1797 1884 push @out, "${pad}${i}<direction-type>";
1798 1884 push @out, "${pad}${i}${i}<wedge type=\"$wedge_type\" number=\"1\"/>";
1799 1884 push @out, "${pad}${i}</direction-type>";
1800 1884 push @out, "${pad}</direction>";
1801 1884 return @out;
1802 }
1803  
1804 # Map NWC DynVariance style -> display text. crescOff has no text representation.
1805 13 Readonly::Hash my %DYN_VARIANCE_TEXT => (
1806 Crescendo => 'cresc.',
1807 Diminuendo => 'dim.',
1808 );
1809  
1810 3 ▶sub _emit_dyn_variance {
Subroutine _emit_dyn_variance: called CC=5 SCAR=16.2
1811 3 my ($self, $style, $placement, $pad) = @_;
1812 3 ▶ $style //= '';
Condition50%1/2
$style //= ""
l!l
30
MC/DC0%0/1
$style //= ""
$style
Truth table50%1/2
A
A= $style
Aresult
11
00
1813 3 ▶ $placement //= 'below';
Condition50%1/2
$placement //= "below"
l!l
30
MC/DC0%0/1
$placement //= "below"
$placement
Truth table50%1/2
A
A= $placement
Aresult
11
00
1814 3 my $i = $self->{_indent};
1815  
1816 3 my $text = $DYN_VARIANCE_TEXT{$style};
1817 3 ▶ return () unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 2 3
1818  
1819 2 ▶ my $place = (lc($placement) eq 'above') ? 'above' : 'below';
Branch: 1 branch
BranchTrueFalseTotal
lc $placement eq 'above' ? : 0 2 2
1820 2 my @out;
1821 2 push @out, "${pad}<direction placement=\"$place\">";
1822 2 push @out, "${pad}${i}<direction-type>";
1823 2 push @out, "${pad}${i}${i}<words font-style=\"italic\">$text</words>";
1824 2 push @out, "${pad}${i}</direction-type>";
1825 2 push @out, "${pad}</direction>";
1826 2 return @out;
1827 }
1828  
1829 13 Readonly::Hash my %TEMPO_VARIANCE_TEXT => (
1830 Accelerando => 'accel.',
1831 Ritardando => 'rit.',
1832 Ritenuto => 'rit.',
1833 Rallentando => 'rall.',
1834 RitardandoToTempo => 'a tempo',
1835 Stringendo => 'string.',
1836 Breath => "\x{2019}", # right single quotation mark used as breath comma
1837 Caesura => '//',
1838 );
1839  
1840 104 ▶sub _emit_tempo_variance {
Subroutine _emit_tempo_variance: called CC=6 SCAR=18.7
1841 104 my ($self, $style, $placement, $pad) = @_;
1842 104 ▶ $style //= '';
Condition50%1/2
$style //= ""
l!l
1040
MC/DC0%0/1
$style //= ""
$style
Truth table50%1/2
A
A= $style
Aresult
11
00
1843 104 ▶ $placement //= 'above';
Condition50%1/2
$placement //= "above"
l!l
1040
MC/DC0%0/1
$placement //= "above"
$placement
Truth table50%1/2
A
A= $placement
Aresult
11
00
1844 104 my $i = $self->{_indent};
1845  
1846 104 ▶ my $text = _xml_escape($TEMPO_VARIANCE_TEXT{$style} // $style);
Condition33%1/3
$TEMPO_VARIANCE_TEXT{$style} // $style
l!l&&r!l&&!r
10400
MC/DC0%0/2
$TEMPO_VARIANCE_TEXT{$style} // $style
$TEMPO_VARIANCE_TEXT{$style} $style
Truth table33%1/3
A // B
A= $TEMPO_VARIANCE_TEXT{$style}B= $style
ABresult
1X1
011
000
1847 104 ▶ return () unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 104 104
1848  
1849 104 ▶ my $place = (lc($placement) eq 'above') ? 'above' : 'below';
Branch: 1 branch
BranchTrueFalseTotal
lc $placement eq 'above' ? : 104 0 104
1850 104 my @out;
1851 104 push @out, "${pad}<direction placement=\"$place\">";
1852 104 push @out, "${pad}${i}<direction-type>";
1853 104 push @out, "${pad}${i}${i}<words font-style=\"italic\">$text</words>";
1854 104 push @out, "${pad}${i}</direction-type>";
1855 104 push @out, "${pad}</direction>";
1856 104 return @out;
1857 }
1858  
1859 0 ▶sub _emit_text_direction {
Subroutine _emit_text_direction: not called CC=5 SCAR=34.0
1860 0 my ($self, $text, $placement, $pad) = @_;
1861 0 ▶ $text //= '';
Condition0%0/2
$text //= ""
l!l
00
MC/DC0%0/1
$text //= ""
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
1862 0 ▶ $placement //= '';
Condition0%0/2
$placement //= ""
l!l
00
MC/DC0%0/1
$placement //= ""
$placement
Truth table0%0/2
A
A= $placement
Aresult
11
00
1863 0 ▶ return () unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
1864 0 my $i = $self->{_indent};
1865 0 ▶ my $place = (lc($placement) eq 'above') ? 'above' : 'below';
Branch: 1 branch
BranchTrueFalseTotal
lc $placement eq 'above' ? : 0 0 0
1866 0 my @out;
1867 0 push @out, "${pad}<direction placement=\"$place\">";
1868 0 push @out, "${pad}${i}<direction-type>";
1869 0 push @out, "${pad}${i}${i}<words>" . _xml_escape($text) . "</words>";
1870 0 push @out, "${pad}${i}</direction-type>";
1871 0 push @out, "${pad}</direction>";
1872 0 return @out;
1873 }
1874  
1875 0 ▶sub _emit_flow_control {
Subroutine _emit_flow_control: not called CC=5 SCAR=34.0
1876 0 my ($self, $directive, $pad) = @_;
1877 0 ▶ $directive //= '';
Condition0%0/2
$directive //= ""
l!l
00
MC/DC0%0/1
$directive //= ""
$directive
Truth table0%0/2
A
A= $directive
Aresult
11
00
1878 0 my $map = $FLOW_CONTROL_MAP{$directive};
1879 0 ▶ return () unless defined $map;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map 0 0 0
1880 0 my $i = $self->{_indent};
1881 0 my @out;
1882 0 push @out, "${pad}<direction placement=\"above\">";
1883 0 ▶ if (defined $map->{element}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $map->{"element"}) 0 0 0
1884 0 push @out, "${pad}${i}<direction-type>";
1885 0 push @out, "${pad}${i}${i}<$map->{element}/>";
1886 0 push @out, "${pad}${i}</direction-type>";
1887 }
1888 0 ▶ if (defined $map->{words}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $map->{"words"}) 0 0 0
1889 0 push @out, "${pad}${i}<direction-type>";
1890 push @out, "${pad}${i}${i}<words font-style=\"italic\">"
1891 0 . _xml_escape($map->{words}) . "</words>";
1892 0 push @out, "${pad}${i}</direction-type>";
1893 }
1894 0 push @out, "${pad}</direction>";
1895 0 return @out;
1896 }
1897  
1898 0 ▶sub _emit_instrument_change {
Subroutine _emit_instrument_change: not called CC=3 SCAR=24.8
1899 0 my ($self, $name, $patch, $pad) = @_;
1900 0 my $i = $self->{_indent};
1901 0 ▶ my $prog = ($patch // 0) + 1;
Condition0%0/2
$patch // 0
l!l
00
MC/DC0%0/1
$patch // 0
$patch
Truth table0%0/2
A
A= $patch
Aresult
11
00
1902 0 ▶ my $text = $name ? _xml_escape($name) : "Program $prog";
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
1903 0 my @out;
1904 0 push @out, "${pad}<direction placement=\"above\">";
1905 0 push @out, "${pad}${i}<direction-type>";
1906 0 push @out, "${pad}${i}${i}<other-direction>$text</other-direction>";
1907 0 push @out, "${pad}${i}</direction-type>";
1908 0 push @out, "${pad}</direction>";
1909 0 return @out;
1910 }
1911  
1912 1921 ▶sub _emit_time_change {
Subroutine _emit_time_change: called CC=3 SCAR=11.0
1913 1921 my ($self, $ts_data, $pad) = @_;
1914 1921 my @out;
1915 1921 my $i = $self->{_indent};
1916 1921 ▶ my $beats = $ts_data->{beats} // 4;
Condition50%1/2
$ts_data->{"beats"} // 4
l!l
19210
MC/DC0%0/1
$ts_data->{"beats"} // 4
$ts_data->{"beats"}
Truth table50%1/2
A
A= $ts_data->{"beats"}
Aresult
11
00
1917 1921 ▶ my $beat_type = $ts_data->{beat_type} // 4;
Condition50%1/2
$ts_data->{"beat_type"} // 4
l!l
19210
MC/DC0%0/1
$ts_data->{"beat_type"} // 4
$ts_data->{"beat_type"}
Truth table50%1/2
A
A= $ts_data->{"beat_type"}
Aresult
11
00
1918  
1919 1921 push @out, "${pad}<attributes>";
1920 1921 push @out, "${pad}${i}<time>";
1921 1921 push @out, "${pad}${i}${i}<beats>$beats</beats>";
1922 1921 push @out, "${pad}${i}${i}<beat-type>$beat_type</beat-type>";
1923 1921 push @out, "${pad}${i}</time>";
1924 1921 push @out, "${pad}</attributes>";
1925 1921 return @out;
1926 }
1927  
1928 171 ▶sub _emit_clef_change {
Subroutine _emit_clef_change: called CC=3 SCAR=11.2
1929 171 my ($self, $clef_name, $pad) = @_;
1930 171 my @out;
1931 171 my $i = $self->{_indent};
1932 171 ▶ my $clef = $CLEF_MAP{$clef_name} // $CLEF_MAP{Treble};
Condition33%1/3
$CLEF_MAP{$clef_name} // $CLEF_MAP{"Treble"}
l!l&&r!l&&!r
17100
MC/DC0%0/2
$CLEF_MAP{$clef_name} // $CLEF_MAP{"Treble"}
$CLEF_MAP{$clef_name} $CLEF_MAP{"Treble"}
Truth table33%1/3
A // B
A= $CLEF_MAP{$clef_name}B= $CLEF_MAP{"Treble"}
ABresult
1X1
011
000
1933  
1934 171 push @out, "${pad}<attributes>";
1935 171 push @out, "${pad}${i}<clef>";
1936 171 push @out, "${pad}${i}${i}<sign>$clef->{sign}</sign>";
1937 push @out, "${pad}${i}${i}<line>$clef->{line}</line>"
1938 171 ▶ if defined $clef->{line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $clef->{"line"} 171 0 171
1939 171 push @out, "${pad}${i}</clef>";
1940 171 push @out, "${pad}</attributes>";
1941 171 return @out;
1942 }
1943  
1944 481 ▶sub _emit_key_change {
Subroutine _emit_key_change: called CC=2 SCAR=6.9
1945 481 my ($self, $key_data, $pad) = @_;
1946 481 my @out;
1947 481 my $i = $self->{_indent};
1948 481 ▶ my $fifths = $key_data->{fifths} // 0;
Condition50%1/2
$key_data->{"fifths"} // 0
l!l
4810
MC/DC0%0/1
$key_data->{"fifths"} // 0
$key_data->{"fifths"}
Truth table50%1/2
A
A= $key_data->{"fifths"}
Aresult
11
00
1949  
1950 481 push @out, "${pad}<attributes>";
1951 481 push @out, "${pad}${i}<key>";
1952 481 push @out, "${pad}${i}${i}<fifths>$fifths</fifths>";
1953 481 push @out, "${pad}${i}</key>";
1954 481 push @out, "${pad}</attributes>";
1955 481 return @out;
1956 }
1957  
1958 258 ▶sub _emit_attributes {
Subroutine _emit_attributes: called CC=9 SCAR=22.1
1959 258 my ($self, $staff, $divisions, $pad) = @_;
1960 258 my @out;
1961 258 my $i = $self->{_indent};
1962  
1963 258 push @out, "${pad}<attributes>";
1964 258 push @out, "${pad}${i}<divisions>$divisions</divisions>";
1965  
1966 258 ▶ if (my $key = $staff->initial_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $staff->initial_key) 201 57 258
1967 201 push @out, "${pad}${i}<key>";
1968 201 push @out, "${pad}${i}${i}<fifths>$key->{fifths}</fifths>";
1969 201 push @out, "${pad}${i}</key>";
1970 }
1971  
1972 258 ▶ if (my $ts = $staff->initial_timesig) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ts = $staff->initial_timesig) 203 55 258
1973 203 push @out, "${pad}${i}<time>";
1974 203 push @out, "${pad}${i}${i}<beats>$ts->{beats}</beats>";
1975 203 push @out, "${pad}${i}${i}<beat-type>$ts->{beat_type}</beat-type>";
1976 203 push @out, "${pad}${i}</time>";
1977 }
1978  
1979 258 ▶ if (my $clef_name = $staff->initial_clef) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $clef_name = $staff->initial_clef) 235 23 258
1980 235 push @out, "${pad}${i}<clef>";
1981 235 my $clef = $CLEF_MAP{$clef_name};
1982 235 ▶ unless (defined $clef) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clef) 1 234 235
1983 1 carp _fmt_msg('warn_unknown_clef', $clef_name);
1984 1 $clef = $CLEF_MAP{Treble};
1985 }
1986 235 push @out, "${pad}${i}${i}<sign>$clef->{sign}</sign>";
1987 push @out, "${pad}${i}${i}<line>$clef->{line}</line>"
1988 235 ▶ if defined $clef->{line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $clef->{"line"} 233 2 235
1989 235 push @out, "${pad}${i}</clef>";
1990 }
1991  
1992 258 ▶ my $trans = ($staff->instrument // {})->{trans} // 0;
Condition75%3/4
+($staff->instrument // {})->{"trans"} // 0
l!l
104154
$staff->instrument // {}
l!l
2580
MC/DC0%0/1
+($staff->instrument // {})->{"trans"} // 0
$staff->instrument
Truth table50%1/2
A
A= $staff->instrument
Aresult
11
00
1993 258 ▶ if ($trans) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trans) 0 258 258
1994 0 push @out, "${pad}${i}<transpose>";
1995 0 push @out, "${pad}${i}${i}<chromatic>$trans</chromatic>";
1996 0 push @out, "${pad}${i}</transpose>";
1997 }
1998  
1999 258 push @out, "${pad}</attributes>";
2000 258 return @out;
2001 }
2002  
2003 # ---------------------------------------------------------------------------
2004 # Private: divisions calculation
2005 # ---------------------------------------------------------------------------
2006  
2007 # Strategy: collect the denominator of every rational duration encountered
2008 # across all staves, then compute their LCM. The divisions value is that LCM,
2009 # guaranteeing exact integer representation for every duration tick count.
2010  
2011 176 ▶sub _calculate_divisions {
Subroutine _calculate_divisions: called CC=10 SCAR=23.2
2012 176 my ($self, $score) = @_;
2013  
2014 176 my %seen_denoms;
2015 176 for my $staff (@{ $score->staves }) {
2016 261 for my $event (@{ $staff->events }) {
2017 167944 my $d = $event->duration->[1];
2018 167944 ▶ $seen_denoms{$d} = 1 if $d > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $d > 0 167944 0 167944
2019 }
2020  
2021 # A bar rest lasts a full measure, so the measure length of every
2022 # time signature in force must also be a whole number of ticks.
2023 261 ▶ next unless grep { _is_whole_rest($_) } @{ $staff->events };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {_is_whole_rest($_);} @{$staff->events;} 161 100 261
2024 2021 my @timesigs = grep { defined } $staff->initial_timesig,
2025 100 map { $_->data } grep { $_->type eq 'TimeSig' } @{ $staff->events };
2026 100 for my $ts (@timesigs) {
2027 2021 ▶ next unless $ts->{beats} && $ts->{beat_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $ts->{"beats"} and $ts->{"beat_type"} 0 2021 2021
Condition33%1/3
$ts->{"beats"} and $ts->{"beat_type"}
!ll&&!rl&&r
002021
MC/DC0%0/2
$ts->{"beats"} and $ts->{"beat_type"}
$ts->{"beats"} $ts->{"beat_type"}
Truth table33%1/3
A and B
A= $ts->{"beats"}B= $ts->{"beat_type"}
ABresult
0X0
100
111
2028 my $d = $ts->{beat_type}
2029 2021 / _gcd($ts->{beats} * $QUARTERS_PER_WHOLE, $ts->{beat_type});
2030 2021 $seen_denoms{$d} = 1;
2031 }
2032 }
2033  
2034 176 ▶ return $DEFAULT_DIVISIONS unless %seen_denoms;
Branch: 1 branch
BranchTrueFalseTotal
unless %seen_denoms 5 171 176
2035  
2036 171 my @denoms = keys %seen_denoms;
2037 171 my $lcm = $denoms[0];
2038 171 for my $d (@denoms[1..$#denoms]) {
2039 56 $lcm = _lcm($lcm, $d);
2040 }
2041  
2042 171 return $lcm;
2043 }
2044  
2045 # ---------------------------------------------------------------------------
2046 # Private: XML helpers
2047 # ---------------------------------------------------------------------------
2048  
2049 # Pre-built table for single-pass XML character escaping.
2050 # Covers the five XML-special ASCII chars; non-ASCII get numeric char refs.
2051 13 Readonly::Hash my %XML_ESCAPE_MAP => (
2052 '&' => '&amp;',
2053 '<' => '&lt;',
2054 '>' => '&gt;',
2055 '"' => '&quot;',
2056 "'" => '&apos;',
2057 );
2058  
2059 864 ▶sub _xml_escape {
Subroutine _xml_escape: called CC=3 SCAR=11.0
2060 864 my ($s) = @_;
2061 864 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 2 862 864
2062 # Pass 1: strip XML 1.0 illegal control bytes (cannot appear even as &#N;).
2063 862 $s =~ s/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]//g;
2064 # Pass 2: escape XML specials and numeric-ref non-ASCII in a single scan.
2065 862 ▶ $s =~ s/([&<>"']|[^\x00-\x7F])/$XML_ESCAPE_MAP{$1} \/\/ sprintf "&#%d;", ord($1)/ge;
Condition100%2/2
$XML_ESCAPE_MAP{$1} // sprintf("&#%d;", ord $1)
l!l
3982
MC/DC100%1/1
$XML_ESCAPE_MAP{$1} // sprintf("&#%d;", ord $1)
$XML_ESCAPE_MAP{$1}
Truth table100%2/2
A
A= $XML_ESCAPE_MAP{$1}
Aresult
11
00
2066 862 return $s;
2067 }
2068  
2069 59 ▶sub _lcm {
Subroutine _lcm: called CC=1 SCAR=0.0
2070 59 my ($a, $b) = @_;
2071 59 return $a / _gcd($a, $b) * $b;
2072 }
2073  
2074 2082 ▶sub _gcd {
Subroutine _gcd: called CC=2 SCAR=6.9
2075 2082 my ($a, $b) = @_;
2076 2082 ▶ ($a, $b) = ($b, $a % $b) while $b;
Branch: 1 branch
BranchTrueFalseTotal
$b and do { () = ($b, $a % $b) } 2111 2082 4193
2077 2082 return $a;
2078 }
2079  
2080 15 ▶sub _fmt_msg {
Subroutine _fmt_msg: called CC=2 SCAR=7.1
2081 15 my ($key, @args) = @_;
2082 15 ▶ croak "Unknown message key: $key" unless exists $MESSAGES{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $MESSAGES{$key} 0 15 15
2083 15 return sprintf $MESSAGES{$key}, @args;
2084 }
2085  
2086 13 1;
2087  
2088 __END__
↑ Summary NWC2MusicXML/Score.pm »