NWC2MusicXML/Parser.pm

filter: Statementstmt 90.9% 261 / 287 Branchbran 81.8% 90 / 110 Conditioncond 62.2% 79 / 127 MC/DCmcdc 41.7% 28 / 67 Subroutinesub 85.7% 42 / 49 Podpod 100% 2 / 2 totaltotal 78.1% 502 / 642 CC 117 SCAR 21.0
CC
117
Cov
82%
_handle_lyric
56.0
_handle_instrument_change
12.0
_handle_flow_control
12.0
CRAP 8.2
« NWC2MusicXML/Score.pm ↑ Summary NWC2MusicXML/NWC.pm »
1 package Music::NWC2MusicXML::Parser;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 our $VERSION = '0.003.0';
8  
9 14 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Music::NWC2MusicXML::Score;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Music::NWC2MusicXML::Staff;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use Music::NWC2MusicXML::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ---------------------------------------------------------------------------
18 # Constants
19 # ---------------------------------------------------------------------------
20  
21 # Maximum number of records to parse before deciding the file is pathological.
22 14 Readonly::Scalar my $MAX_RECORDS => 1_000_000;
23  
24 # The NWCTXT header line prefix
25 14 Readonly::Scalar my $HEADER_PREFIX => '!NoteWorthyComposer(';
26  
27 # Dynamic markings recognised by NWC
28 14 Readonly::Hash my %VALID_DYNAMICS => map { $_ => 1 }
29 qw(pppp ppp pp p mp mf f ff fff ffff);
30  
31 # Clef names recognised by NWC and their canonical spellings
32 14 Readonly::Hash my %VALID_CLEFS => map { $_ => 1 }
33 qw(Treble Bass Alto Tenor Percussion Tab);
34  
35 # NWC lists key-signature accidentals in the standard circle-of-fifths order.
36 # Signature:F#,C# = 2 sharps = D major (fifths +2)
37 # Signature:Bb = 1 flat = F major (fifths -1)
38 # We compute the fifths value by counting the accidentals rather than using
39 # a lookup table, which handles any combination and avoids the common mistake
40 # of confusing the tonic name with the signature accidental list.
41  
42 # The 'C' signature token means no accidentals (C major / A minor = fifths 0).
43 14 Readonly::Scalar my $KEY_SIG_NATURAL => 'C';
44  
45 # Pitch step letters recognised in NWC note records
46 14 Readonly::Hash my %VALID_STEPS => map { $_ => 1 } qw(A B C D E F G);
47  
48 14 Readonly::Hash my %MESSAGES => (
49 error_empty_input => 'NWCTXT input is empty or undefined',
50 error_no_header => 'NWCTXT does not begin with expected header',
51 error_too_many_records=> 'Input exceeds maximum record limit (%d)',
52 error_bad_record => 'Malformed NWCTXT record at line %d: %s',
53 error_no_staff => 'Musical event %s encountered before any AddStaff record',
54 error_internal => 'Internal error: %s',
55 warn_unknown_record => 'Unknown NWCTXT record type %s at line %d -- stored as UnsupportedEvent',
56 warn_bad_value => 'Unrecognised value for %s: %s at line %d',
57 );
58  
59 =head1 NAME
60  
61 Music::NWC2MusicXML::Parser - NWCTXT text parser producing an internal score
62 representation.
63  
64 =head1 VERSION
65  
66 0.003.0
67  
68 =head1 SYNOPSIS
69  
70 use Music::NWC2MusicXML::Parser;
71  
72 my $parser = Music::NWC2MusicXML::Parser->new;
73 my $score = $parser->parse($nwctxt);
74  
75 =head1 DESCRIPTION
76  
77 C<Music::NWC2MusicXML::Parser> accepts the NWCTXT string produced by
78 C<Music::NWC2MusicXML::NWC> and returns a C<Music::NWC2MusicXML::Score> object.
79  
80 The internal representation is completely independent of MusicXML so that
81 parsing and MusicXML generation can be tested separately.
82  
83 =head2 NWCTXT record structure
84  
85 Each record occupies one line and begins with C<|>:
86  
87 |RecordType|Field1:Value1|Field2:Value2|...
88  
89 Fields are pipe-delimited. Values may be:
90  
91 =over 4
92  
93 =item * unquoted single tokens
94  
95 =item * double-quoted strings (which may contain C<|> and C<:>)
96  
97 =item * comma-separated lists
98  
99 =back
100  
101 The parser does B<not> use C<split(/[|]/)> because quoted values and escape
102 sequences require a proper tokeniser.
103  
104 B<Record type aliases>: some NWC versions export C<DynamicVariance> where
105 others write the abbreviated C<DynVariance>; the parser accepts both and emits
106 the same C<DynVariance> event in either case.
107  
108 B<Text event placement>: C<Text> records carry an optional C<Placement:>
109 field. When it is absent the vertical position is inferred from the C<Pos:>
110 field: a non-negative C<Pos> value sets placement to C<'Above'>; a negative
111 value sets it to C<'Below'>. This handles common NWC files where performance
112 directions such as "Slightly faster" are positioned above the staff via a
113 positive C<Pos> number with no explicit C<Placement:> field.
114  
115 B<EndingBar normalisation>: NWC C<StaffProperties> stores the ending barline
116 style as a multi-word string (e.g. C<"Section Close">). The parser strips
117 internal spaces on ingestion so the value matches the compact style tokens used
118 by C<Music::NWC2MusicXML::MusicXML> (e.g. C<'SectionClose'>). When NWC emits
119 two C<StaffProperties> records for a staff (the second carrying channel and
120 volume data), only the record that actually contains an C<EndingBar:> field
121 updates C<_ending_bar>; the second record is not allowed to clobber the value
122 set by the first.
123  
124 =cut
125  
126 # ---------------------------------------------------------------------------
127 # new
128 # ---------------------------------------------------------------------------
129  
130 =head2 new
131  
132 Construct a parser.
133  
134 =head3 Arguments
135  
136 Named parameters:
137  
138 =over 4
139  
140 =item C<diagnostics> -- a C<Music::NWC2MusicXML::Diagnostics> instance (optional).
141  
142 =back
143  
144 =head3 Returns
145  
146 Blessed C<Music::NWC2MusicXML::Parser> object.
147  
148 =head3 API SPECIFICATION
149  
150 =head4 Input
151  
152 diagnostics : Music::NWC2MusicXML::Diagnostics (optional)
153  
154 =head4 Output
155  
156 Music::NWC2MusicXML::Parser object
157  
158 =head3 FORMAL SPECIFICATION
159  
160 [ParserInit]
161 diagnostics : Diagnostics
162  
163 (placeholder -- populate with Z calculus as implementation matures)
164  
165 =cut
166  
167 190 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
168 190 my ($class, %input) = @_;
169 190 my $args = validate_strict(
170 schema => {
171 diagnostics => { type => 'object', optional => 1 },
172 },
173 input => \%input,
174 );
175 190 ▶ croak $@ unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 190 190
176  
177 my $self = bless {
178 _diagnostics => $args->{diagnostics},
179 190 _score => undef,
180 _line_no => 0,
181 _record_count => 0,
182 }, $class;
183  
184 190 return $self;
185 }
186  
187 # ---------------------------------------------------------------------------
188 # Public: parse
189 # ---------------------------------------------------------------------------
190  
191 =head2 parse
192  
193 Parse a complete NWCTXT string and return the corresponding
194 C<Music::NWC2MusicXML::Score>.
195  
196 =head3 Purpose
197  
198 This is the primary entry point. It iterates over each line of the NWCTXT,
199 dispatches to a type-specific handler, and accumulates the results into a
200 Score object.
201  
202 =head3 Arguments
203  
204 =over 4
205  
206 =item C<$nwctxt> -- scalar string containing the full NWCTXT representation
207 (required).
208  
209 =back
210  
211 =head3 Returns
212  
213 A C<Music::NWC2MusicXML::Score> object.
214  
215 =head3 Side Effects
216  
217 Croaks on fatal structural errors (e.g. missing header).
218 Issues warnings via C<diagnostics> for non-fatal issues (e.g. unknown records).
219  
220 =head3 Usage Example
221  
222 my $score = Music::NWC2MusicXML::Parser->new->parse($nwctxt);
223  
224 =head3 API SPECIFICATION
225  
226 =head4 Input
227  
228 $nwctxt : SCALAR (UTF-8, required)
229 -- Valid domain: non-empty string beginning with the line
230 -- '!NoteWorthyComposer(<version>)' followed by pipe-delimited records
231 -- Invalid partitions:
232 -- undef or '' -> croaks error_empty_input
233 -- non-empty but missing header -> croaks error_no_header
234 -- Boundary: MAX_RECORDS = 1,000,000 pipe-delimited records
235 -- At exactly 1,000,000 records: accepted
236 -- At 1,000,001+ records: croaks error_too_many_records
237  
238 =head4 Output
239  
240 Music::NWC2MusicXML::Score
241  
242 =head3 MESSAGES
243  
244 | Code | Meaning | Resolution |
245 |-----------------------|------------------------------------------|-------------------------------|
246 | error_empty_input | Input is undef or zero-length | Check NWC decoder output |
247 | error_no_header | Header line not found | Verify NWC binary decoder |
248 | error_too_many_records| Record count exceeds MAX_RECORDS | Input may be malicious/corrupt|
249 | error_bad_record | Record cannot be tokenised | File may be corrupt |
250 | error_no_staff | Event seen before first AddStaff | NWCTXT may be truncated |
251 | warn_unknown_record | Unknown record type stored as Unsupported| New NWC version may add types |
252  
253 =head3 FORMAL SPECIFICATION
254  
255 [Parse]
256 nwctxt? : NWCTXT
257 ----------
258 score! : Score
259  
260 (placeholder)
261  
262 =cut
263  
264 183 ▶sub parse {
Subroutine parse: called CC=11 SCAR=24.0
Pod documented
265 183 my ($self, $nwctxt) = @_;
266  
267 183 ▶ croak _fmt_msg('error_empty_input')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nwctxt and length $nwctxt 16 167 183
Condition100%3/3
defined $nwctxt and length $nwctxt
!ll&&!rl&&r
88167
MC/DC100%2/2
defined $nwctxt and length $nwctxt
defined $nwctxt length $nwctxt
Truth table100%3/3
A and B
A= defined $nwctxtB= length $nwctxt
ABresult
0X0
100
111
268 unless defined $nwctxt && length $nwctxt;
269  
270 # Initialise fresh parse state for each call, enabling object reuse.
271 167 $self->{_score} = Music::NWC2MusicXML::Score->new;
272 167 $self->{_line_no} = 0;
273 167 $self->{_record_count} = 0;
274  
275 167 my @lines = split /\r?\n/, $nwctxt;
276  
277 # First non-blank line must be the NWCTXT header
278 167 my $header = shift @lines;
279 167 $self->{_line_no}++;
280  
281 167 ▶ croak _fmt_msg('error_no_header')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header and index($header, $HEADER_PREFIX) == 0 5 162 167
Condition66%2/3
defined $header and index($header, $HEADER_PREFIX) == 0
!ll&&!rl&&r
05162
MC/DC50%1/2
defined $header and index($header, $HEADER_PREFIX) == 0
defined $header index($header, $HEADER_PREFIX) == 0
Truth table66%2/3
A and B
A= defined $headerB= index($header, $HEADER_PREFIX) == 0
ABresult
0X0
100
111
282 unless defined $header
283 && index($header, $HEADER_PREFIX) == 0;
284  
285 # Extract version string from header: !NoteWorthyComposer(2.751)
286 162 my ($version) = $header =~ /\A\Q$HEADER_PREFIX\E([^)]+)/;
287 162 $self->{_score}{_nwc_version} = $version;
288  
289 # Main parsing loop
290 162 for my $line (@lines) {
291 182631 $self->{_line_no}++;
292 182631 ▶ next unless defined $line && length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 1 182630 182631
Condition66%2/3
defined $line and length $line
!ll&&!rl&&r
01182630
MC/DC50%1/2
defined $line and length $line
defined $line length $line
Truth table66%2/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
293 182630 ▶ next if $line =~ /\A\s*\z/; # blank lines are legal
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*\z/ 1 182629 182630
294  
295 croak _fmt_msg('error_too_many_records', $MAX_RECORDS)
296 182629 ▶ if ++$self->{_record_count} > $MAX_RECORDS;
Branch: 1 branch
BranchTrueFalseTotal
if ++$self->{"_record_count"} > $MAX_RECORDS 0 182629 182629
297  
298 # All NWCTXT records begin with '|'
299 182629 ▶ next unless $line =~ /\A\|/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A\|/ 48 182581 182629
300  
301 182581 $self->_dispatch_record($line);
302 }
303  
304 162 return $self->{_score};
305 }
306  
307 # ---------------------------------------------------------------------------
308 # Package-level dispatch table -- built once at module load, not per call.
309 # Subs are compile-time symbols; Readonly ensures the table is never mutated.
310 # ---------------------------------------------------------------------------
311 14 Readonly::Hash my %DISPATCH => (
312 SongInfo => \&_handle_song_info,
313 PgSetup => \&_handle_pg_setup,
314 PgMargins => \&_handle_pg_setup,
315 Font => \&_handle_font,
316 AddStaff => \&_handle_add_staff,
317 StaffProperties => \&_handle_staff_properties,
318 StaffInstrument => \&_handle_staff_instrument,
319 Clef => \&_handle_clef,
320 Key => \&_handle_key,
321 TimeSig => \&_handle_time_sig,
322 Tempo => \&_handle_tempo,
323 Note => \&_handle_note,
324 Rest => \&_handle_rest,
325 Chord => \&_handle_chord,
326 Bar => \&_handle_bar,
327 Dynamic => \&_handle_dynamic,
328 DynVariance => \&_handle_dyn_variance,
329 DynamicVariance => \&_handle_dyn_variance,
330 Text => \&_handle_text,
331 Lyric => \&_handle_lyric,
332 Tie => \&_handle_tie,
333 Slur => \&_handle_slur,
334 Beam => \&_handle_beam,
335 Tuplet => \&_handle_tuplet,
336 Instrument => \&_handle_instrument_change,
337 FlowControl => \&_handle_flow_control,
338 TempoVariance => \&_handle_tempo_variance,
339 Spacer => \&_handle_spacer,
340 RestChord => \&_handle_rest_chord,
341 );
342  
343 # ---------------------------------------------------------------------------
344 # Private: record dispatcher
345 # ---------------------------------------------------------------------------
346  
347 # Strategy: extract the record type (first field after the leading |), then
348 # call a dedicated _handle_* method. Unknown types produce UnsupportedEvent
349 # records and a warning rather than aborting conversion.
350  
351 182581 ▶sub _dispatch_record {
Subroutine _dispatch_record: called CC=5 SCAR=16.3
352 182581 my ($self, $line) = @_;
353  
354 182581 my $fields = $self->_tokenise_record($line);
355 182581 ▶ return unless defined $fields && @$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields and @$fields 0 182581 182581
Condition33%1/3
defined $fields and @$fields
!ll&&!rl&&r
00182581
MC/DC0%0/2
defined $fields and @$fields
defined $fields @$fields
Truth table33%1/3
A and B
A= defined $fieldsB= @$fields
ABresult
0X0
100
111
356  
357 182581 my $type = shift @$fields;
358  
359 182581 ▶ if (exists $DISPATCH{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DISPATCH{$type}) { } 182552 29 182581
360 182552 $DISPATCH{$type}->($self, $fields);
361 } else {
362 29 my $staff = $self->{_score}->current_staff;
363 29 ▶ if (defined $staff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $staff) 3 26 29
364 # Unknown type within a staff: record and warn.
365 3 $self->_warn_unknown($type);
366 3 $self->_append_event(Music::NWC2MusicXML::Event->new(
367 type => 'UnsupportedEvent',
368 nwc_label => $type,
369 data => { raw => join('|', $type, @$fields) },
370 ));
371 }
372 # else: score-level record (Editor, Font, PgMargins, ...) before any
373 # AddStaff -- silently skip; these are not musical events.
374 }
375 }
376  
377 # ---------------------------------------------------------------------------
378 # Private: tokeniser
379 # ---------------------------------------------------------------------------
380  
381 # Strategy: walk character by character to handle quoted strings and escaped
382 # characters correctly. A naive split('|') would break on quoted values
383 # that contain '|' or ':'.
384  
385 182584 ▶sub _tokenise_record {
Subroutine _tokenise_record: called CC=9 SCAR=22.0
386 182584 my ($self, $line) = @_;
387  
388 182584 my @fields;
389 182584 my $pos = 0;
390 182584 my $len = length $line;
391  
392 # Skip leading '|'
393 182584 ▶ $pos++ if substr($line, 0, 1) eq '|';
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, 1) eq "|" 182584 0 182584
394  
395 182584 my $current = '';
396 182584 my $in_quote = 0;
397  
398 182584 ▶ while ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 4678541 182584 4861125
399 4678541 my $ch = substr($line, $pos, 1);
400  
401 4678541 ▶ if ($in_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_quote) { } 12561 4665980 4678541
402 12561 ▶ if ($ch eq '"') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '"') { } 990 11571 12561
elsif ($ch eq '\\' and $pos + 1 < $len) { } 2 11569 11571
403 990 $in_quote = 0;
404 ▶ } elsif ($ch eq '\\' && $pos + 1 < $len) {
Condition66%2/3
$ch eq "\\" and $pos + 1 < $len
!ll&&!rl&&r
1156902
MC/DC50%1/2
$ch eq "\\" and $pos + 1 < $len
$ch eq "\\" $pos + 1 < $len
Truth table66%2/3
A and B
A= $ch eq "\\"B= $pos + 1 < $len
ABresult
0X0
100
111
405 # Escape sequence: consume next char literally
406 2 $pos++;
407 2 $current .= substr($line, $pos, 1);
408 } else {
409 11569 $current .= $ch;
410 }
411 } else {
412 4665980 ▶ if ($ch eq '"') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '"') { } 990 4664990 4665980
elsif ($ch eq '|') { } 336846 4328144 4664990
413 990 $in_quote = 1;
414 } elsif ($ch eq '|') {
415 336846 push @fields, $current;
416 336846 $current = '';
417 } else {
418 4328144 $current .= $ch;
419 }
420 }
421 4678541 $pos++;
422 }
423  
424 182584 push @fields, $current;
425  
426 182584 return \@fields;
427 }
428  
429 # Parse a field list (already tokenised) into a key => value hashref.
430 # Fields have the form "Key:Value" or just "Value" for positional fields.
431 182529 ▶sub _fields_to_hash {
Subroutine _fields_to_hash: called CC=3 SCAR=11.0
432 182529 my ($self, $fields_ref) = @_;
433 182529 my %h;
434 182529 for my $field (@$fields_ref) {
435 336741 ▶ if ($field =~ /\A([^:]+):(.*)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /\A([^:]+):(.*)\z/) { } 333612 3129 336741
436 333612 $h{$1} = $2;
437 } else {
438 3129 $h{_positional} = $field;
439 }
440 }
441 182529 return \%h;
442 }
443  
444 # ---------------------------------------------------------------------------
445 # Private: metadata handlers
446 # ---------------------------------------------------------------------------
447  
448 300 ▶sub _handle_font {
Subroutine _handle_font: called CC=8 SCAR=22.4
449 300 my ($self, $fields) = @_;
450 300 my $h = $self->_fields_to_hash($fields);
451 300 ▶ push @{ $self->{_score}{_fonts} }, {
Branch: 3 branches
BranchTrueFalseTotal
defined $h->{'Size'} ? : 300 0 300
($h->{'Bold'} // 'N') eq 'Y' ? : 75 225 300
($h->{'Italic'} // 'N') eq 'Y' ? : 25 275 300
Condition50%4/8
$h->{"Style"} // ""
l!l
3000
$h->{"Typeface"} // ""
l!l
3000
$h->{"Bold"} // "N"
l!l
3000
$h->{"Italic"} // "N"
l!l
3000
MC/DC0%0/1
$h->{"Style"} // ""
$h->{"Style"}
MC/DC0%0/1
$h->{"Typeface"} // ""
$h->{"Typeface"}
MC/DC0%0/1
$h->{"Bold"} // "N"
$h->{"Bold"}
MC/DC0%0/1
$h->{"Italic"} // "N"
$h->{"Italic"}
Truth table50%1/2
A
A= $h->{"Style"}
Aresult
11
00
Truth table50%1/2
A
A= $h->{"Typeface"}
Aresult
11
00
Truth table50%1/2
A
A= $h->{"Bold"}
Aresult
11
00
Truth table50%1/2
A
A= $h->{"Italic"}
Aresult
11
00
452 style => $h->{Style} // '',
453 typeface => $h->{Typeface} // '',
454 size => defined $h->{Size} ? $h->{Size} + 0 : 0,
455 bold => ($h->{Bold} // 'N') eq 'Y' ? 1 : 0,
456 300 italic => ($h->{Italic} // 'N') eq 'Y' ? 1 : 0,
457 };
458 }
459  
460 92 ▶sub _handle_song_info {
Subroutine _handle_song_info: called CC=3 SCAR=11.0
461 92 my ($self, $fields) = @_;
462 92 my $h = $self->_fields_to_hash($fields);
463 92 for my $key (qw(Title Author Lyricist Copyright Copyright1 Copyright2 Comments)) {
464 $self->{_score}->set_metadata_field($key, $h->{$key})
465 644 ▶ if exists $h->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h->{$key} 245 399 644
466 }
467 }
468  
469 69 ▶sub _handle_pg_setup {
Subroutine _handle_pg_setup: called CC=1 SCAR=0.0
470 69 my ($self, $fields) = @_;
471 69 my $h = $self->_fields_to_hash($fields);
472 69 my $ps = $self->{_score}->page_setup;
473 69 %$ps = (%$ps, %$h);
474 }
475  
476 243 ▶sub _handle_add_staff {
Subroutine _handle_add_staff: called CC=3 SCAR=11.0
477 243 my ($self, $fields) = @_;
478 243 my $h = $self->_fields_to_hash($fields);
479 my $staff = Music::NWC2MusicXML::Staff->new(
480 name => $h->{Name} // 'Staff',
481 243 ▶ group => $h->{Group} // 'Standard',
Condition100%4/4
$h->{"Name"} // "Staff"
l!l
2385
$h->{"Group"} // "Standard"
l!l
120123
MC/DC100%1/1
$h->{"Name"} // "Staff"
$h->{"Name"}
MC/DC100%1/1
$h->{"Group"} // "Standard"
$h->{"Group"}
Truth table100%2/2
A
A= $h->{"Name"}
Aresult
11
00
Truth table100%2/2
A
A= $h->{"Group"}
Aresult
11
00
482 );
483 243 $self->{_score}->add_staff($staff);
484 }
485  
486 222 ▶sub _handle_staff_properties {
Subroutine _handle_staff_properties: called CC=11 SCAR=24.0
487 222 my ($self, $fields) = @_;
488 222 my $h = $self->_fields_to_hash($fields);
489 222 my $staff = $self->_current_staff_or_croak('StaffProperties');
490 222 ▶ $staff->{_visible} = ($h->{Visible} // 'Y') eq 'Y' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($h->{'Visible'} // 'Y') eq 'Y' ? : 222 0 222
Condition100%2/2
$h->{"Visible"} // "Y"
l!l
121101
MC/DC100%1/1
$h->{"Visible"} // "Y"
$h->{"Visible"}
Truth table100%2/2
A
A= $h->{"Visible"}
Aresult
11
00
491 222 ▶ $staff->{_lines} = $h->{Lines} // 5;
Condition100%2/2
$h->{"Lines"} // 5
l!l
121101
MC/DC100%1/1
$h->{"Lines"} // 5
$h->{"Lines"}
Truth table100%2/2
A
A= $h->{"Lines"}
Aresult
11
00
492 222 ▶ if (defined $h->{EndingBar}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h->{"EndingBar"}) 110 112 222
493 110 (my $ending_bar = $h->{EndingBar}) =~ s/\s+//g;
494 110 $staff->{_ending_bar} = $ending_bar;
495 }
496 222 ▶ $staff->{_channel} = $h->{Channel} + 0 if defined $h->{Channel};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"Channel"} 101 121 222
497 222 ▶ $staff->{_with_next_staff} = $h->{WithNextStaff} if defined $h->{WithNextStaff};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"WithNextStaff"} 100 122 222
498 222 ▶ $staff->{_volume} = $h->{Volume} + 0 if defined $h->{Volume};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"Volume"} 101 121 222
499 222 ▶ $staff->{_stereo_pan} = $h->{StereoPan} + 0 if defined $h->{StereoPan};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"StereoPan"} 100 122 222
500 222 ▶ $staff->{_muted} = ($h->{Muted} // 'N') eq 'Y' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($h->{'Muted'} // 'N') eq 'Y' ? : 0 222 222
Condition100%2/2
$h->{"Muted"} // "N"
l!l
101121
MC/DC100%1/1
$h->{"Muted"} // "N"
$h->{"Muted"}
Truth table100%2/2
A
A= $h->{"Muted"}
Aresult
11
00
501 }
502  
503 121 ▶sub _handle_staff_instrument {
Subroutine _handle_staff_instrument: called CC=7 SCAR=19.7
504 121 my ($self, $fields) = @_;
505 121 my $h = $self->_fields_to_hash($fields);
506 121 my $staff = $self->_current_staff_or_croak('StaffInstrument');
507 $staff->{_instrument} = {
508 name => $h->{Name} // '',
509 patch => $h->{Patch} // 0,
510 121 ▶ trans => $h->{Trans} // 0,
Condition66%4/6
$h->{"Name"} // ""
l!l
1210
$h->{"Patch"} // 0
l!l
1210
$h->{"Trans"} // 0
l!l
10021
MC/DC0%0/1
$h->{"Name"} // ""
$h->{"Name"}
MC/DC0%0/1
$h->{"Patch"} // 0
$h->{"Patch"}
MC/DC100%1/1
$h->{"Trans"} // 0
$h->{"Trans"}
Truth table50%1/2
A
A= $h->{"Name"}
Aresult
11
00
Truth table50%1/2
A
A= $h->{"Patch"}
Aresult
11
00
Truth table100%2/2
A
A= $h->{"Trans"}
Aresult
11
00
511 };
512 121 ▶ if (defined $h->{DynVel} && length $h->{DynVel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h->{"DynVel"} and length $h->{"DynVel"}) 100 21 121
Condition66%2/3
defined $h->{"DynVel"} and length $h->{"DynVel"}
!ll&&!rl&&r
210100
MC/DC50%1/2
defined $h->{"DynVel"} and length $h->{"DynVel"}
defined $h->{"DynVel"} length $h->{"DynVel"}
Truth table66%2/3
A and B
A= defined $h->{"DynVel"}B= length $h->{"DynVel"}
ABresult
0X0
100
111
513 100 my @keys = qw(ppp pp p mp mf f ff fff);
514 100 my @vels = split /,/, $h->{DynVel};
515 100 ▶ if (@vels == @keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vels == @keys) 100 0 100
516 100 my %vel_map;
517 100 @vel_map{@keys} = map { $_ + 0 } @vels;
518 100 $staff->{_dyn_vel} = \%vel_map;
519 }
520 }
521 }
522  
523 # ---------------------------------------------------------------------------
524 # Private: initial-state handlers (clef/key/time -- before events begin)
525 # ---------------------------------------------------------------------------
526  
527 366 ▶sub _handle_clef {
Subroutine _handle_clef: called CC=4 SCAR=14.2
528 366 my ($self, $fields) = @_;
529 366 my $h = $self->_fields_to_hash($fields);
530 366 ▶ my $type = $h->{Type} // 'Treble';
Condition50%1/2
$h->{"Type"} // "Treble"
l!l
3660
MC/DC0%0/1
$h->{"Type"} // "Treble"
$h->{"Type"}
Truth table50%1/2
A
A= $h->{"Type"}
Aresult
11
00
531  
532 366 ▶ unless (exists $VALID_CLEFS{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $VALID_CLEFS{$type}) 0 366 366
533 0 carp _fmt_msg('warn_bad_value', 'Clef.Type', $type, $self->{_line_no});
534 }
535  
536 366 my $staff = $self->_current_staff_or_croak('Clef');
537 366 ▶ if (!$staff->has_notes) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $staff->has_notes) { } 187 179 366
538 187 $staff->set_initial_clef($type);
539 } else {
540 179 $self->_append_event(Music::NWC2MusicXML::Event->new(
541 type => 'Clef',
542 data => { nwc_clef => $type },
543 ));
544 }
545 }
546  
547 669 ▶sub _handle_key {
Subroutine _handle_key: called CC=4 SCAR=13.9
548 669 my ($self, $fields) = @_;
549 669 my $h = $self->_fields_to_hash($fields);
550 669 ▶ my $sig = $h->{Signature} // 'C';
Condition50%1/2
$h->{"Signature"} // "C"
l!l
6690
MC/DC0%0/1
$h->{"Signature"} // "C"
$h->{"Signature"}
Truth table50%1/2
A
A= $h->{"Signature"}
Aresult
11
00
551  
552 my $key_data = {
553 signature => $sig,
554 669 ▶ tonic => $h->{Tonic} // '',
Condition100%2/2
$h->{"Tonic"} // ""
l!l
60168
MC/DC100%1/1
$h->{"Tonic"} // ""
$h->{"Tonic"}
Truth table100%2/2
A
A= $h->{"Tonic"}
Aresult
11
00
555 fifths => _fifths_from_signature($sig),
556 };
557  
558 669 my $staff = $self->_current_staff_or_croak('Key');
559 669 ▶ if (!$staff->has_notes) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $staff->has_notes) { } 168 501 669
560 168 $staff->set_initial_key($key_data);
561 } else {
562 501 $self->_append_event(Music::NWC2MusicXML::Event->new(
563 type => 'Key',
564 data => $key_data,
565 ));
566 }
567 }
568  
569 2175 ▶sub _handle_time_sig {
Subroutine _handle_time_sig: called CC=5 SCAR=16.1
570 2175 my ($self, $fields) = @_;
571 2175 my $h = $self->_fields_to_hash($fields);
572 2175 ▶ my $sig = $h->{Signature} // '4/4';
Condition50%1/2
$h->{"Signature"} // "4/4"
l!l
21750
MC/DC0%0/1
$h->{"Signature"} // "4/4"
$h->{"Signature"}
Truth table50%1/2
A
A= $h->{"Signature"}
Aresult
11
00
573  
574 2175 my ($beats, $beat_type) = $sig =~ m{\A(\d+)/(\d+)\z};
575 2175 ▶ unless (defined $beats && defined $beat_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $beats and defined $beat_type) 4 2171 2175
Condition66%2/3
defined $beats and defined $beat_type
!ll&&!rl&&r
402171
MC/DC50%1/2
defined $beats and defined $beat_type
defined $beats defined $beat_type
Truth table66%2/3
A and B
A= defined $beatsB= defined $beat_type
ABresult
0X0
100
111
576 4 carp _fmt_msg('warn_bad_value', 'TimeSig.Signature', $sig, $self->{_line_no});
577 4 ($beats, $beat_type) = (4, 4);
578 }
579  
580 2175 my $ts_data = { beats => $beats + 0, beat_type => $beat_type + 0 };
581  
582 2175 my $staff = $self->_current_staff_or_croak('TimeSig');
583 2175 ▶ if (!$staff->has_notes) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $staff->has_notes) { } 174 2001 2175
584 174 $staff->set_initial_timesig($ts_data);
585 } else {
586 2001 $self->_append_event(Music::NWC2MusicXML::Event->new(
587 type => 'TimeSig',
588 data => $ts_data,
589 ));
590 }
591 }
592  
593 # ---------------------------------------------------------------------------
594 # Private: musical event handlers
595 # ---------------------------------------------------------------------------
596  
597 151 ▶sub _handle_tempo {
Subroutine _handle_tempo: called CC=4 SCAR=13.9
598 151 my ($self, $fields) = @_;
599 151 my $h = $self->_fields_to_hash($fields);
600 $self->_append_event(Music::NWC2MusicXML::Event->new(
601 type => 'Tempo',
602 data => {
603 bpm => $h->{Tempo} // 120,
604 base => $h->{Base} // 'Quarter',
605 151 ▶ text => $h->{Text} // '',
Condition83%5/6
$h->{"Tempo"} // 120
l!l
1510
$h->{"Base"} // "Quarter"
l!l
22129
$h->{"Text"} // ""
l!l
1150
MC/DC0%0/1
$h->{"Tempo"} // 120
$h->{"Tempo"}
MC/DC100%1/1
$h->{"Base"} // "Quarter"
$h->{"Base"}
MC/DC100%1/1
$h->{"Text"} // ""
$h->{"Text"}
Truth table50%1/2
A
A= $h->{"Tempo"}
Aresult
11
00
Truth table100%2/2
A
A= $h->{"Base"}
Aresult
11
00
Truth table100%2/2
A
A= $h->{"Text"}
Aresult
11
00
606 },
607 ));
608 }
609  
610 91800 ▶sub _handle_note {
Subroutine _handle_note: called CC=3 SCAR=11.3
611 91800 my ($self, $fields) = @_;
612 91800 my $h = $self->_fields_to_hash($fields);
613  
614 91800 ▶ my ($base_dur, $dots, $triplet, $artic, $is_grace) = _parse_dur_tokens($h->{Dur} // '4th');
Condition50%1/2
$h->{"Dur"} // "4th"
l!l
918000
MC/DC0%0/1
$h->{"Dur"} // "4th"
$h->{"Dur"}
Truth table50%1/2
A
A= $h->{"Dur"}
Aresult
11
00
615 91800 my $rational = Music::NWC2MusicXML::Event->rational_from_nwc_duration($base_dur, $dots);
616  
617 $self->_append_event(Music::NWC2MusicXML::Event->new(
618 type => 'Note',
619 duration => $rational,
620 data => {
621 nwc_pos => $h->{Pos} // '0',
622 base_dur => $base_dur,
623 dots => $dots,
624 triplet => $triplet,
625 articulations => $artic,
626 is_grace => $is_grace,
627 91800 ▶ opts => _parse_opts($h->{Opts}),
Condition50%1/2
$h->{"Pos"} // 0
l!l
918000
MC/DC0%0/1
$h->{"Pos"} // 0
$h->{"Pos"}
Truth table50%1/2
A
A= $h->{"Pos"}
Aresult
11
00
628 },
629 ));
630 }
631  
632 20231 ▶sub _handle_rest {
Subroutine _handle_rest: called CC=2 SCAR=7.0
633 20231 my ($self, $fields) = @_;
634 20231 my $h = $self->_fields_to_hash($fields);
635  
636 20231 ▶ my ($base_dur, $dots) = _parse_dur_tokens($h->{Dur} // '4th');
Condition50%1/2
$h->{"Dur"} // "4th"
l!l
202310
MC/DC0%0/1
$h->{"Dur"} // "4th"
$h->{"Dur"}
Truth table50%1/2
A
A= $h->{"Dur"}
Aresult
11
00
637 20231 my $rational = Music::NWC2MusicXML::Event->rational_from_nwc_duration($base_dur, $dots);
638  
639 $self->_append_event(Music::NWC2MusicXML::Event->new(
640 type => 'Rest',
641 duration => $rational,
642 data => {
643 base_dur => $base_dur,
644 dots => $dots,
645 20231 opts => _parse_opts($h->{Opts}),
646 },
647 ));
648 }
649  
650 21475 ▶sub _handle_chord {
Subroutine _handle_chord: called CC=3 SCAR=11.2
651 21475 my ($self, $fields) = @_;
652 21475 my $h = $self->_fields_to_hash($fields);
653  
654 21475 ▶ my ($base_dur, $dots, $triplet, $artic, $is_grace) = _parse_dur_tokens($h->{Dur} // '4th');
Condition50%1/2
$h->{"Dur"} // "4th"
l!l
214750
MC/DC0%0/1
$h->{"Dur"} // "4th"
$h->{"Dur"}
Truth table50%1/2
A
A= $h->{"Dur"}
Aresult
11
00
655 21475 my $rational = Music::NWC2MusicXML::Event->rational_from_nwc_duration($base_dur, $dots);
656  
657 # Pos field contains a comma-separated list of position strings.
658 # Each entry: optional accidental prefix (#/b/n/##/bb/x) + signed integer + optional ^ (tie)
659 21475 ▶ my @positions = split /,/, ($h->{Pos} // '0');
Condition50%1/2
$h->{"Pos"} // 0
l!l
214750
MC/DC0%0/1
$h->{"Pos"} // 0
$h->{"Pos"}
Truth table50%1/2
A
A= $h->{"Pos"}
Aresult
11
00
660  
661 $self->_append_event(Music::NWC2MusicXML::Event->new(
662 type => 'Chord',
663 duration => $rational,
664 data => {
665 nwc_positions => \@positions,
666 base_dur => $base_dur,
667 dots => $dots,
668 triplet => $triplet,
669 articulations => $artic,
670 is_grace => $is_grace,
671 21475 opts => _parse_opts($h->{Opts}),
672 },
673 ));
674 }
675  
676 41949 ▶sub _handle_bar {
Subroutine _handle_bar: called CC=3 SCAR=11.0
677 41949 my ($self, $fields) = @_;
678 41949 my $h = $self->_fields_to_hash($fields);
679 # NWC bar records carry an optional Style named field; plain |Bar| has none.
680 41949 ▶ my $style = $h->{Style} // $h->{_positional} // 'normal';
Condition100%5/5
($h->{"Style"} // $h->{"_positional"}) // "normal"
l!l
474037209
$h->{"Style"} // $h->{"_positional"}
l!l&&r!l&&!r
1613312737209
MC/DC100%2/2
($h->{"Style"} // $h->{"_positional"}) // "normal"
$h->{"Style"} $h->{"_positional"}
Truth table100%3/3
A // B
A= $h->{"Style"}B= $h->{"_positional"}
ABresult
1X1
011
000
681 41949 $self->_append_event(Music::NWC2MusicXML::Event->new(
682 type => 'Bar',
683 data => { style => $style },
684 ));
685 }
686  
687 # ---------------------------------------------------------------------------
688 # Private: duration / options parsing helpers
689 # ---------------------------------------------------------------------------
690  
691 133512 ▶sub _parse_dur_tokens {
Subroutine _parse_dur_tokens: called CC=9 SCAR=22.3
692 133512 my ($dur_str) = @_;
693 133512 ▶ my @tokens = split /,/, ($dur_str // '4th');
Condition50%1/2
$dur_str // "4th"
l!l
1335120
MC/DC0%0/1
$dur_str // "4th"
$dur_str
Truth table50%1/2
A
A= $dur_str
Aresult
11
00
694 133512 ▶ my $base = shift(@tokens) // '4th';
Condition50%1/2
shift @tokens // "4th"
l!l
1335120
MC/DC0%0/1
shift @tokens // "4th"
shift @tokens
Truth table50%1/2
A
A= shift @tokens
Aresult
11
00
695 133512 my ($dots, $triplet, $is_grace) = (0, undef, 0);
696 133512 my @artic;
697  
698 133512 for my $tok (@tokens) {
699 36442 ▶ if ($tok eq 'Dotted') { $dots = 1 }
Branch: 4 branches
BranchTrueFalseTotal
if ($tok eq 'Dotted') { } 3552 32890 36442
elsif ($tok eq 'DblDotted') { } 1 32889 32890
elsif ($tok =~ /\ATriplet(?:=(.+))?\z/) { } 452 32437 32889
elsif ($tok eq 'Grace') { } 0 32437 32437
700 1 elsif ($tok eq 'DblDotted') { $dots = 2 }
701 452 ▶ elsif ($tok =~ /\ATriplet(?:=(.+))?\z/) { $triplet = $1 // 'Middle' }
Condition100%2/2
$1 // "Middle"
l!l
301151
MC/DC100%1/1
$1 // "Middle"
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
702 0 elsif ($tok eq 'Grace') { $is_grace = 1 }
703 32437 else { push @artic, $tok }
704 }
705  
706 133512 return ($base, $dots, $triplet, \@artic, $is_grace);
707 }
708  
709 133511 ▶sub _parse_opts {
Subroutine _parse_opts: called CC=5 SCAR=16.1
710 133511 my ($opts_str) = @_;
711 133511 ▶ return {} unless defined $opts_str && length $opts_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts_str and length $opts_str 64981 68530 133511
Condition100%3/3
defined $opts_str and length $opts_str
!ll&&!rl&&r
64980168530
MC/DC100%2/2
defined $opts_str and length $opts_str
defined $opts_str length $opts_str
Truth table100%3/3
A and B
A= defined $opts_strB= length $opts_str
ABresult
0X0
100
111
712 68530 my %opts;
713 68530 for my $opt (split /,/, $opts_str) {
714 133182 ▶ if ($opt =~ /\A(\w+)=(.*)\z/) { $opts{$1} = $2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt =~ /\A(\w+)=(.*)\z/) { } 111903 21279 133182
715 21279 else { $opts{$opt} = 1 }
716 }
717 68530 return \%opts;
718 }
719  
720 2502 ▶sub _handle_dynamic {
Subroutine _handle_dynamic: called CC=4 SCAR=14.2
721 2502 my ($self, $fields) = @_;
722 2502 my $h = $self->_fields_to_hash($fields);
723 $self->_append_event(Music::NWC2MusicXML::Event->new(
724 type => 'Dynamic',
725 data => {
726 marking => $h->{Style} // $h->{_positional} // '',
727 2502 ▶ placement => $h->{Placement} // '',
Condition71%5/7
($h->{"Style"} // $h->{"_positional"}) // ""
l!l
25020
$h->{"Style"} // $h->{"_positional"}
l!l&&r!l&&!r
250110
$h->{"Placement"} // ""
l!l
12501
MC/DC0%0/2
($h->{"Style"} // $h->{"_positional"}) // ""
$h->{"Style"} $h->{"_positional"}
MC/DC100%1/1
$h->{"Placement"} // ""
$h->{"Placement"}
Truth table66%2/3
A // B
A= $h->{"Style"}B= $h->{"_positional"}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $h->{"Placement"}
Aresult
11
00
728 },
729 ));
730 }
731  
732 5 ▶sub _handle_dyn_variance {
Subroutine _handle_dyn_variance: called CC=4 SCAR=14.9
733 5 my ($self, $fields) = @_;
734 5 my $h = $self->_fields_to_hash($fields);
735 $self->_append_event(Music::NWC2MusicXML::Event->new(
736 type => 'DynVariance',
737 data => {
738 style => $h->{Style} // $h->{_positional} // '',
739 5 ▶ placement => $h->{Placement} // '',
Condition57%4/7
($h->{"Style"} // $h->{"_positional"}) // ""
l!l
50
$h->{"Style"} // $h->{"_positional"}
l!l&&r!l&&!r
500
$h->{"Placement"} // ""
l!l
14
MC/DC0%0/2
($h->{"Style"} // $h->{"_positional"}) // ""
$h->{"Style"} $h->{"_positional"}
MC/DC100%1/1
$h->{"Placement"} // ""
$h->{"Placement"}
Truth table33%1/3
A // B
A= $h->{"Style"}B= $h->{"_positional"}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $h->{"Placement"}
Aresult
11
00
740 },
741 ));
742 }
743  
744 5 ▶sub _handle_text {
Subroutine _handle_text: called CC=6 SCAR=17.9
745 5 my ($self, $fields) = @_;
746 5 my $h = $self->_fields_to_hash($fields);
747 5 my $placement = $h->{Placement};
748 5 ▶ if (!defined $placement && defined $h->{Pos}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $placement and defined $h->{"Pos"}) 3 2 5
Condition100%3/3
not defined $placement and defined $h->{"Pos"}
!ll&&!rl&&r
113
MC/DC100%2/2
not defined $placement and defined $h->{"Pos"}
not defined $placement defined $h->{"Pos"}
Truth table100%3/3
not(A) and B
A= not defined $placementB= defined $h->{"Pos"}
ABresult
0X0
100
111
749 3 ▶ $placement = ($h->{Pos} >= 0) ? 'Above' : 'Below';
Branch: 1 branch
BranchTrueFalseTotal
$h->{'Pos'} >= 0 ? : 2 1 3
750 }
751 $self->_append_event(Music::NWC2MusicXML::Event->new(
752 type => 'Text',
753 5 ▶ data => { text => $h->{Text} // '', placement => $placement // '' },
Condition75%3/4
$h->{"Text"} // ""
l!l
50
$placement // ""
l!l
41
MC/DC0%0/1
$h->{"Text"} // ""
$h->{"Text"}
MC/DC100%1/1
$placement // ""
$placement
Truth table50%1/2
A
A= $h->{"Text"}
Aresult
11
00
Truth table100%2/2
A
A= $placement
Aresult
11
00
754 ));
755 }
756  
757 0 ▶sub _handle_lyric {
Subroutine _handle_lyric: not called CC=7 SCAR=40.3
758 0 my ($self, $fields) = @_;
759 0 my $h = $self->_fields_to_hash($fields);
760 0 ▶ my $text = $h->{Text} // '';
Condition0%0/2
$h->{"Text"} // ""
l!l
00
MC/DC0%0/1
$h->{"Text"} // ""
$h->{"Text"}
Truth table0%0/2
A
A= $h->{"Text"}
Aresult
11
00
761 # Syllabic type encoded by leading/trailing hyphens: strip them, infer type.
762 0 my $starts_h = ($text =~ s/\A-//);
763 0 my $ends_h = ($text =~ s/-\z//);
764 0 ▶ my $syllabic = $starts_h && $ends_h ? 'middle'
Branch: 3 branches
BranchTrueFalseTotal
$starts_h && $ends_h ? : 0 0 0
$starts_h ? : 0 0 0
$ends_h ? : 0 0 0
Condition0%0/3
$starts_h and $ends_h
!ll&&!rl&&r
000
MC/DC0%0/2
$starts_h and $ends_h
$starts_h $ends_h
Truth table0%0/3
A and B
A= $starts_hB= $ends_h
ABresult
0X0
100
111
765 : $starts_h ? 'end'
766 : $ends_h ? 'begin'
767 : 'single';
768 $self->_append_event(Music::NWC2MusicXML::Event->new(
769 type => 'Lyric',
770 0 ▶ data => { text => $text, verse => $h->{Verse} // 1, syllabic => $syllabic },
Condition0%0/2
$h->{"Verse"} // 1
l!l
00
MC/DC0%0/1
$h->{"Verse"} // 1
$h->{"Verse"}
Truth table0%0/2
A
A= $h->{"Verse"}
Aresult
11
00
771 ));
772 }
773  
774 0 ▶sub _handle_tie {
Subroutine _handle_tie: not called CC=1 SCAR=6.9
775 0 my ($self, $fields) = @_;
776 0 $self->_append_event(Music::NWC2MusicXML::Event->new(
777 type => 'Tie',
778 data => {},
779 ));
780 }
781  
782 0 ▶sub _handle_slur {
Subroutine _handle_slur: not called CC=1 SCAR=6.9
783 0 my ($self, $fields) = @_;
784 0 my $h = $self->_fields_to_hash($fields);
785 0 $self->_append_event(Music::NWC2MusicXML::Event->new(
786 type => 'Slur',
787 data => { _raw_fields => $h },
788 ));
789 }
790  
791 0 ▶sub _handle_beam {
Subroutine _handle_beam: not called CC=1 SCAR=6.9
792 0 my ($self, $fields) = @_;
793 0 my $h = $self->_fields_to_hash($fields);
794 0 $self->_append_event(Music::NWC2MusicXML::Event->new(
795 type => 'Beam',
796 data => { _raw_fields => $h },
797 ));
798 }
799  
800 0 ▶sub _handle_tuplet {
Subroutine _handle_tuplet: not called CC=1 SCAR=6.9
801 0 my ($self, $fields) = @_;
802 0 my $h = $self->_fields_to_hash($fields);
803 0 $self->_append_event(Music::NWC2MusicXML::Event->new(
804 type => 'Tuplet',
805 data => { _raw_fields => $h },
806 ));
807 }
808  
809 0 ▶sub _handle_instrument_change {
Subroutine _handle_instrument_change: not called CC=3 SCAR=24.8
810 0 my ($self, $fields) = @_;
811 0 my $h = $self->_fields_to_hash($fields);
812 $self->_append_event(Music::NWC2MusicXML::Event->new(
813 type => 'Instrument',
814 0 ▶ data => { name => $h->{Name} // '', patch => $h->{Patch} // 0 },
Condition0%0/4
$h->{"Name"} // ""
l!l
00
$h->{"Patch"} // 0
l!l
00
MC/DC0%0/1
$h->{"Name"} // ""
$h->{"Name"}
MC/DC0%0/1
$h->{"Patch"} // 0
$h->{"Patch"}
Truth table0%0/2
A
A= $h->{"Name"}
Aresult
11
00
Truth table0%0/2
A
A= $h->{"Patch"}
Aresult
11
00
815 ));
816 }
817  
818 0 ▶sub _handle_flow_control {
Subroutine _handle_flow_control: not called CC=3 SCAR=24.8
819 0 my ($self, $fields) = @_;
820 0 my $h = $self->_fields_to_hash($fields);
821 $self->_append_event(Music::NWC2MusicXML::Event->new(
822 type => 'FlowControl',
823 0 ▶ data => { directive => $h->{_positional} // $h->{Style} // '', extra => $h },
Condition0%0/5
($h->{"_positional"} // $h->{"Style"}) // ""
l!l
00
$h->{"_positional"} // $h->{"Style"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($h->{"_positional"} // $h->{"Style"}) // ""
$h->{"_positional"} $h->{"Style"}
Truth table0%0/3
A // B
A= $h->{"_positional"}B= $h->{"Style"}
ABresult
1X1
011
000
824 ));
825 }
826  
827 102 ▶sub _handle_tempo_variance {
Subroutine _handle_tempo_variance: called CC=4 SCAR=14.6
828 102 my ($self, $fields) = @_;
829 102 my $h = $self->_fields_to_hash($fields);
830  
831 102 ▶ my $style = $h->{Style} // '';
Condition50%1/2
$h->{"Style"} // ""
l!l
1020
MC/DC0%0/1
$h->{"Style"} // ""
$h->{"Style"}
Truth table50%1/2
A
A= $h->{"Style"}
Aresult
11
00
832 102 ▶ my $pos = $h->{Pos} // 0;
Condition50%1/2
$h->{"Pos"} // 0
l!l
1020
MC/DC0%0/1
$h->{"Pos"} // 0
$h->{"Pos"}
Truth table50%1/2
A
A= $h->{"Pos"}
Aresult
11
00
833 102 ▶ $self->_append_event(Music::NWC2MusicXML::Event->new(
Branch: 1 branch
BranchTrueFalseTotal
$pos >= 0 ? : 102 0 102
834 type => 'TempoVariance',
835 data => {
836 style => $style,
837 placement => ($pos >= 0 ? 'above' : 'below'),
838 },
839 ));
840 }
841  
842 25 ▶sub _handle_spacer {
Subroutine _handle_spacer: called CC=1 SCAR=0.0
843 # Spacer is a graphical-layout hint with no MusicXML equivalent.
844 # Per the preservation principle (musical meaning > graphical appearance)
845 # it is silently discarded; no event, no warning.
846 25 return;
847 }
848  
849 50 ▶sub _handle_rest_chord {
Subroutine _handle_rest_chord: called CC=1 SCAR=0.0
850 50 my ($self, $fields) = @_;
851 50 my $h = $self->_fields_to_hash($fields);
852  
853 # RestChord is a multi-voice construct: one layer rests while another
854 # plays. Full handling requires voice assignment (Phase 4).
855 # Stored as UnsupportedEvent so the raw field data is preserved for
856 # diagnostics and future implementation.
857 50 $self->_append_event(Music::NWC2MusicXML::Event->new(
858 type => 'UnsupportedEvent',
859 nwc_label => 'RestChord',
860 data => { _raw_fields => $h },
861 ));
862 }
863  
864 # ---------------------------------------------------------------------------
865 # Private: helpers
866 # ---------------------------------------------------------------------------
867  
868 180954 ▶sub _append_event {
Subroutine _append_event: called CC=1 SCAR=0.0
869 180954 my ($self, $event) = @_;
870 180954 my $staff = $self->_current_staff_or_croak($event->type);
871 180954 $staff->add_event($event);
872 }
873  
874 184507 ▶sub _current_staff_or_croak {
Subroutine _current_staff_or_croak: called CC=2 SCAR=7.0
875 184507 my ($self, $context) = @_;
876 184507 my $staff = $self->{_score}->current_staff;
877 184507 ▶ croak _fmt_msg('error_no_staff', $context) unless defined $staff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $staff 0 184507 184507
878 184507 return $staff;
879 }
880  
881 3 ▶sub _warn_unknown {
Subroutine _warn_unknown: called CC=1 SCAR=0.0
882 3 my ($self, $type) = @_;
883 3 carp _fmt_msg('warn_unknown_record', $type, $self->{_line_no});
884 }
885  
886 687 ▶sub _fifths_from_signature {
Subroutine _fifths_from_signature: called CC=7 SCAR=19.5
887 687 my ($sig) = @_;
888 687 ▶ return 0 unless defined $sig && length $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig and length $sig 4 683 687
Condition100%3/3
defined $sig and length $sig
!ll&&!rl&&r
22683
MC/DC100%2/2
defined $sig and length $sig
defined $sig length $sig
Truth table100%3/3
A and B
A= defined $sigB= length $sig
ABresult
0X0
100
111
889 683 ▶ return 0 if $sig eq $KEY_SIG_NATURAL;
Branch: 1 branch
BranchTrueFalseTotal
if $sig eq $KEY_SIG_NATURAL 139 544 683
890  
891 # Signature is a comma-separated list of accidentals in circle-of-fifths order.
892 # Each item ending in '#' is a sharp; each item ending in 'b' (after a letter) is a flat.
893 544 my @acc = split /,/, $sig;
894 544 ▶ return 0 unless @acc;
Branch: 1 branch
BranchTrueFalseTotal
unless @acc 0 544 544
895  
896 544 ▶ if (index($acc[0], '#') >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($acc[0], '#') != -1) { } 207 337 544
elsif (substr($acc[0], -1, 1) eq 'b') { } 307 30 337
897 207 return scalar @acc; # positive = sharps
898 } elsif (substr($acc[0], -1, 1) eq 'b') {
899 307 return -(scalar @acc); # negative = flats
900 }
901 30 return 0;
902 }
903  
904 28 ▶sub _fmt_msg {
Subroutine _fmt_msg: called CC=2 SCAR=7.1
905 28 my ($key, @args) = @_;
906 28 ▶ croak "Unknown message key: $key" unless exists $MESSAGES{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $MESSAGES{$key} 0 28 28
907 28 return sprintf $MESSAGES{$key}, @args;
908 }
909  
910 14 1;
911  
912 __END__
« NWC2MusicXML/Score.pm ↑ Summary NWC2MusicXML/NWC.pm »