NWC2MusicXML/MusicXML.pm

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