TabsCreator/NoteToToneConverter.pm

filter: Statementstmt 100% 73 / 73 Branchbran 96.8% 31 / 32 Conditioncond 57.1% 4 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 5 totaltotal 90.3% 121 / 134 CC 20 SCAR 20.2
CC
20
Cov
96%
convert
14.0
alteration_for_note
5.0
new
3.3
CRAP 7.5
« TabsCreator.pm ↑ Summary TabsCreator/TabParser.pm »
1 package Music::Harmonica::TabsCreator::NoteToToneConverter;
2  
3 4 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.06';
12  
13 # This class converts written note (accepting various syntax for the notes) into
14 # tones (degrees) relative to the key of C4.
15  
16 198 ▶sub new ($class, %options) {
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
17 my $self = bless {
18 default_octave => $options{default_octave} // 5, ## no critic (ProhibitMagicNumbers)
19 198 ▶ key => $options{key} // 'C',
Condition50%2/4
$options{"default_octave"} // 5
l!l
0198
$options{"key"} // "C"
l!l
0198
MC/DC0%0/1
$options{"default_octave"} // 5
$options{"default_octave"}
MC/DC0%0/1
$options{"key"} // "C"
$options{"key"}
Truth table50%1/2
A
A= $options{"default_octave"}
Aresult
11
00
Truth table50%1/2
A
A= $options{"key"}
Aresult
11
00
20 }, $class;
21 198 return $self;
22 }
23  
24 # For now we assume that the key is C when entering the sheet music (when
25 # specifying octaves). It’s unclear what are the convention here for other keys.
26  
27 4 Readonly my %NOTE_TO_TONE => (
28 C => 0,
29 Do => 0,
30 Ut => 0,
31 D => 2,
32 'Re' => 2,
33 'Ré' => 2,
34 E => 4,
35 Mi => 4,
36 F => 5,
37 Fa => 5,
38 G => 7,
39 Sol => 7,
40 A => 9,
41 La => 9,
42 B => 11,
43 Si => 11,
44 H => 11,
45 );
46  
47 # TODO support Unicode representation of the sharp (♯), flat (♭), natural (♮)
48 # and clef (𝄞) signs.
49  
50 4 Readonly my %ACCIDENTAL_TO_ALTERATION => (
51 '#' => 1,
52 '♯' => 1,
53 '+' => 1,
54 'b' => -1,
55 '-' => -1,
56 '♭' => -1,
57 '♮' => 0,
58 '=' => 0,
59 '' => 0,
60 );
61  
62 4 Readonly my %SIGNATURE_TO_KEY => (
63 '' => 'C',
64 b => 'F',
65 bb => 'Bb',
66 bbb => 'Eb',
67 bbbb => 'Ab',
68 bbbbb => 'Db',
69 bbbbbb => 'Gb',
70 bbbbbbb => 'Cb',
71 '#' => 'G',
72 '##' => 'D',
73 '###' => 'A',
74 '####' => 'E',
75 '#####' => 'B',
76 '######' => 'F#',
77 '#######' => 'C#',
78 );
79  
80 4 Readonly my @FLAT_ORDER => qw(B E A D G C F);
81 4 Readonly my @SHARP_ORDER => qw(F C G D A E B);
82 4 Readonly my %KEY_TO_ALTERATION => (
83 C => 0,
84 F => -1,
85 Bb => -2,
86 Eb => -3,
87 Ab => -4,
88 Db => -5,
89 Gb => -6,
90 Cb => -7,
91 G => 1,
92 D => 2,
93 A => 3,
94 E => 4,
95 B => 5,
96 'F#' => 6,
97 'C#' => 7,
98 );
99  
100 4 Readonly my @NOTE_NAMES => qw(do Do ut Ut ré Ré re Re mi Mi fa Fa sol Sol la La si Si);
101 4 Readonly my $JOINED_NOTE_NAMES => join('|', @NOTE_NAMES);
102 4 Readonly my $NOTE_NAME_RE => qr/ ${JOINED_NOTE_NAMES} | [A-H] /x;
103 Readonly my $ACCIDENTAL_RE => join '|',
104 4 map { (quotemeta $_).'+' } grep { $_ ne '' } keys %ACCIDENTAL_TO_ALTERATION;
105  
106 4 Readonly my $BASE_OCTAVE => 4;
107 4 Readonly my $TONES_PER_SCALE => 12;
108  
109 1616 ▶sub accidental_to_alteration ($acc) {
Subroutine accidental_to_alteration: called CC=1 SCAR=0.0
Pod undocumented
110 # We know that $acc is a single character possibly repeated.
111 1616 return $ACCIDENTAL_TO_ALTERATION{substr $acc, 0, 1} * length($acc);
112 }
113  
114 4691 ▶sub note_to_tone ($note) {
Subroutine note_to_tone: called CC=1 SCAR=0.0
Pod undocumented
115 4691 return $NOTE_TO_TONE{$note};
116 }
117  
118 3027 ▶sub alteration_for_note ($self, $note) {
Subroutine alteration_for_note: called CC=5 SCAR=16.1
Pod undocumented
119 3027 my $alt = $KEY_TO_ALTERATION{$self->{key}};
120 3027 ▶ if ($alt > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($alt > 0) { } 1 3026 3027
elsif ($alt < 0) { } 15 3011 3026
121 1 ▶ return (any { note_to_tone($_) eq note_to_tone($note) } @SHARP_ORDER[0 .. $alt - 1]) ? 1 : 0;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
&any(sub { note_to_tone($_) eq note_to_tone($note); } , @SHARP_ORDER[0 .. $alt - 1]) ? : 1 0 1
122 } elsif ($alt < 0) {
123 15 ▶ return (any { note_to_tone($_) eq note_to_tone($note) } @FLAT_ORDER[0 .. abs($alt) - 1])
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
&any(sub { note_to_tone($_) eq note_to_tone($note); } , @FLAT_ORDER[0 .. abs($alt) - 1]) ? : 10 5 15
124 ? -1
125 : 0;
126 } else {
127 3011 return 0;
128 }
129 }
130  
131 # Note that calls to convert can return nothing. The general pattern is to call
132 # that in a sub passed to a map call.
133  
134 4549 ▶sub convert ($self, $symbols) {
Subroutine convert: called CC=14 SCAR=26.4
Pod undocumented
135 4549 my @out;
136 4549 pos($symbols) = 0;
137 4549 ▶ while (pos($symbols) < length($symbols)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $symbols < length $symbols) 4752 4548 9300
138 4752 ▶ next if $symbols =~ m/\G\h+/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $symbols =~ /\G\h+/cgu 83 4669 4752
139  
140 4669 ▶ if ($symbols =~ m/\G(\v+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols =~ /\G(\v+)/cgu) 1 4668 4669
141 1 push @out, $1;
142 1 next;
143 }
144  
145 4668 ▶ if ($symbols =~ m/ \G \# \s* ( .*? (?:\r\n|\n|\r|\v|\z) )/xgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols =~ / \G \# \s* ( .*? (?:\r\n|\n|\r|\v|\z) )/cgux) 3 4665 4668
146 3 push @out, $1;
147 3 next;
148 }
149  
150 4665 ▶ if ($symbols =~ m/\G(<|>)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols =~ /\G(<|>)/cgu) 11 4654 4665
151 11 ▶ $self->{default_octave} += $1 eq '>' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '>' ? : 9 2 11
152 11 next;
153 }
154  
155 # TODO: Support specifying the key with a note name, e.g. KF#.
156 4654 ▶ if ($symbols =~ m/\GK(b{1,7}|#{0,7})?/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols =~ /\GK(b{1,7}|#{0,7})?/cgu) 10 4644 4654
157 10 $self->{key} = $SIGNATURE_TO_KEY{$1};
158 10 next;
159 }
160  
161 # There is a bug here that A-3 won’t be parsed as the - will be taken for a flat.
162 4644 ▶ if ($symbols =~ m/\G ( ${NOTE_NAME_RE} ) ( ${ACCIDENTAL_RE} )? ( \d+ )? (,+|’+|'+)?/xgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols =~ /\G ( $NOTE_NAME_RE ) ( $ACCIDENTAL_RE )? ( \d+ )? (,+|\x{2019}+|'+)?/cgux) 4643 1 4644
163 my ($note, $accidental, $octave, $rel_octave) =
164 4643 ▶ (ucfirst($1), $2, $3 // $self->{default_octave}, $4);
Condition66%2/3
$3 // $self->{"default_octave"}
l!l&&r!l&&!r
45011420
MC/DC0%0/2
$3 // $self->{"default_octave"}
$3 $self->{"default_octave"}
Truth table66%2/3
A // B
A= $3B= $self->{"default_octave"}
ABresult
1X1
011
000
165 4643 ▶ if ($rel_octave) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_octave) 3 4640 4643
166 3 ▶ $octave += length($rel_octave) * ($rel_octave =~ /,/ ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$rel_octave =~ /,/u ? : 1 2 3
167 }
168 4643 my $base = $TONES_PER_SCALE * ($octave - $BASE_OCTAVE) + note_to_tone($note);
169 4643 ▶ my $alteration =
Branch: 1 branch
BranchTrueFalseTotal
$accidental ? : 1616 3027 4643
170 $accidental ? accidental_to_alteration($accidental) : $self->alteration_for_note($note);
171 4643 push @out, $base + $alteration;
172 4643 next;
173 }
174  
175 1 my $pos = pos($symbols);
176 1 substr $symbols, $pos, 0, '-->';
177 1 $pos++;
178 1 die "Invalid syntax in the input music at character ${pos}: ${symbols}\n";
179 }
180 4548 ▶ return wantarray ? @out : \@out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4524 24 4548
181 }
182  
183 4 1;
« TabsCreator.pm ↑ Summary TabsCreator/TabParser.pm »