TabsCreator.pm

filter: Statementstmt 88.6% 180 / 203 Branchbran 76.0% 38 / 50 Conditioncond 59.0% 13 / 22 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 89.6% 26 / 29 Podpod 14.2% 2 / 14 totaltotal 80.3% 266 / 331 CC 45 SCAR 22.4
CC
45
Cov
84%
transpose_tab
56.0
render_tabs
8.0
match_notes_to_tuning
8.0
CRAP 9.4
↑ Summary TabsCreator/NoteToToneConverter.pm »
1 package Music::Harmonica::TabsCreator;
2  
3 3 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::Util qw(min max none any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Music::Harmonica::TabsCreator::NoteToToneConverter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Music::Harmonica::TabsCreator::TabParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Music::Harmonica::TabsCreator::Warning;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 our $VERSION = '1.06';
19  
20 3 our @EXPORT_OK = qw(tune_to_tab get_tuning_details tune_to_tab_rendered
21 transpose_tab transpose_tab_rendered list_tunings);
22  
23 # Options to add:
24 # - print B as H (international convention), but probably not Bb which stays Bb.
25  
26 3 Readonly my $TONES_PER_SCALE => 12;
27  
28 # TODO: add a param so that the output prefers a tab starting at 1 rather than
29 # 1° when possible.
30 18 ▶sub extend_chromatic_tuning ($tuning, $fix) {
Subroutine extend_chromatic_tuning: called CC=5 SCAR=16.1
Pod undocumented
31 18 my $size = @{$tuning->{notes}};
32 18 for my $i (0 .. $size - 1) {
33 420 push @{$tuning->{tabs}}, sprintf('(%s)', $tuning->{tabs}[$i]);
34 420 ▶ $tuning->{notes}[$i] =~ m/^(\w)(\d)$/ or die 'Unexpected error';
Branch: 1 branch
BranchTrueFalseTotal
$tuning->{"notes"}[$i] =~ /^(\w)(\d)$/u or die "Unexpected error" 420 0 420
35 420 push @{$tuning->{notes}}, "${1}#${2}";
36 }
37 18 ▶ if ($fix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix) 9 9 18
38 # Some brand use a D instead of a C (== B#) as the draw slide in, to give
39 # one more note (instead of duplicating the C). It’s not really an issue if
40 # the harmonica does not have it as the missing C is there anyway on the
41 # harmonica (and worst case, the tab can’t be played if it requires (-12)).
42 9 ▶ die 'Unexpected error' unless $tuning->{notes}[-1] =~ m/^B#(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tuning->{"notes"}[-1] =~ /^B#(\d+)$/u 0 9 9
43 9 $tuning->{notes}[-1] = 'D'.($1 + 1);
44 }
45 # We put the notes with the slides pushed-in at the beginning of the array so
46 # that equivalent notes with the slide out, which comes later, are used by
47 # default.
48 18 push @{$tuning->{tabs}}, splice(@{$tuning->{tabs}}, 0, $size);
49 18 push @{$tuning->{notes}}, splice(@{$tuning->{notes}}, 0, $size);
50 18 @{$tuning->{bends}} = ((1) x ($size + 1), (0) x ($size));
51 18 $tuning->{is_chromatic} = 1;
52 18 return $tuning;
53 }
54  
55 #<<< let’s not run perltidy over this portion as I want to keep the arrays
56 # aligned for better readability.
57 3 Readonly my %ALL_TUNINGS => (
58 # Written in the key of C to match the default key used in the note_to_tone
59 # function.
60 # Note that when we have the same note appears multiple time (like -2 and 3 in
61 # Richter scale) we always use only the last appearance one when rendering a
62 # tab (but other appearances are still used when reading a tab).
63 richter => {
64 tags => [qw(diatonic 10-holes major)],
65 name => 'Richter',
66 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10)],
67 notes => [qw(C4 D4 E4 G4 G4 B4 C5 D5 E5 F5 G5 A5 C6 B5 E6 D6 G6 F6 C7 A6)],
68 bends => [qw( 0 1 0 2 0 3 0 1 0 0 0 1 0 0 1 0 1 0 2 0)],
69 key => 'C',
70 },
71 melody_maker => {
72 tags => [qw(diatonic 10-holes major)],
73 name => 'Melody Maker',
74 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10)],
75 notes => [qw(C4 D4 E4 G4 A4 B4 C5 D5 E5 F+5 G5 A5 C6 B5 E6 D6 G6 F+6 C7 A6)],
76 bends => [qw( 0 1 0 2 0 1 0 1 0 1 0 1 0 0 1 0 0 0 2 0)],
77 key => 'G',
78 # TODO: Only the C, D, Eb, E, F, G, A and Bb keys exist (not the Db, F#, Ab
79 # and B ones). But nothing prevents the missing ones from being generated
80 # for now.
81 },
82 natural_minor => {
83 tags => [qw(diatonic 10-holes minor)],
84 name => 'Natural Minor',
85 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10)],
86 notes => [qw(C4 D4 Eb4 G4 G4 Bb4 C5 D5 Eb5 F5 G5 A5 C6 Bb5 Eb6 D6 G6 F6 C7 A6)],
87 bends => [qw( 0 1 0 3 0 2 0 1 0 1 0 1 1 0 0 0 1 0 2 0)],
88 # TODO: The real harmonica is labelled as Gm but we don’t support that
89 # annotation for now.
90 key => 'G',
91 },
92 harmonic_minor => {
93 tags => [qw(diatonic 10-holes minor)],
94 name => 'Harmonic Minor',
95 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10)],
96 notes => [qw(C4 D4 Eb4 G4 G4 B4 C5 D5 Eb5 F5 G5 Ab5 C6 B5 Eb6 D6 G6 F6 C7 Ab6)],
97 bends => [qw( 0 1 0 3 0 3 0 1 0 1 0 0 0 0 0 0 1 0 3 0)],
98 # TODO: check the bends that are actually used, this is just my guess.
99 key => 'C',
100 },
101 solo => {
102 tags => [qw(diatonic 10-holes major)],
103 name => 'Solo',
104 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10)],
105 notes => [qw(C4 D4 E4 F4 G4 A4 C5 B4 C5 D5 E5 F5 G5 A5 C6 B5 C6 D6 E6 F6)],
106 bends => [qw( 0 1 1 0 1 1 0 1 0 1 1 0 1 1 0 1 0 1 1 0)],
107 # TODO: check the bends that are actually used, this is just my guess.
108 key => 'C',
109 },
110 paddy => {
111 tags => [qw(diatonic 10-holes major)],
112 name => 'Paddy Richter',
113 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10)],
114 notes => [qw(C4 D4 E4 G4 A4 B4 C5 D5 E5 F5 G5 A5 C6 B5 E6 D6 G6 F6 C7 A6)],
115 bends => [qw( 0 1 0 2 1 1 0 1 0 0 0 1 0 0 1 0 1 0 2 0)],
116 # TODO: check the bends that are actually used, this is just my guess.
117 key => 'C',
118 },
119 solo_8 => extend_chromatic_tuning({
120 tags => [qw(chromatic 8-holes major)],
121 name => 'Solo 8 – Chrometta',
122 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8)],
123 notes => [qw(C4 D4 E4 F4 G4 A4 C5 B4 C5 D5 E5 F5 G5 A5 C6 B5)],
124 key => 'C',
125 },
126 0
127 ),
128 chrometta_10 => extend_chromatic_tuning({
129 tags => [qw(chromatic 10-holes major)],
130 name => 'Chrometta 10',
131 tabs => [qw( 3° -3° 4° -4° 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8)],
132 notes => [qw(G3 A3 C4 B3 C4 D4 E4 F4 G4 A4 C5 B4 C5 D5 E5 F5 G5 A5 C6 B5)],
133 key => 'C',
134 },
135 0
136 ),
137 solo_10 => extend_chromatic_tuning({
138 tags => [qw(chromatic 10-holes major)],
139 name => 'Solo 10',
140 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10)],
141 notes => [qw(C4 D4 E4 F4 G4 A4 C5 B4 C5 D5 E5 F5 G5 A5 C6 B5 C6 D6 E6 F6)],
142 key => 'C',
143 },
144 0
145 ),
146 solo_12 => extend_chromatic_tuning({
147 tags => [qw(chromatic 12-holes major)],
148 name => 'Solo 12',
149 tabs => [qw( 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10 11 -11 12 -12)],
150 notes =>[qw( C4 D4 E4 F4 G4 A4 C5 B4 C5 D5 E5 F5 G5 A5 C6 B5 C6 D6 E6 F6 G6 A6 C7 B6)],
151 key => 'C',
152 },
153 1
154 ),
155 solo_14 => extend_chromatic_tuning({
156 tags => [qw(chromatic 14-holes major)],
157 name => 'Solo 14',
158 tabs => [qw( 3° -3° 4° -4° 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10 11 -11 12 -12)],
159 notes => [qw(G3 A3 C4 B3 C4 D4 E4 F4 G4 A4 C5 B4 C5 D5 E5 F5 G5 A5 C6 B5 C6 D6 E6 F6 G6 A6 C7 B6)],
160 key => 'C',
161 avoid_tones => 5,
162 },
163 1
164 ),
165 solo_16 => extend_chromatic_tuning({
166 tags => [qw(chromatic 16-holes major)],
167 name => 'Solo 16',
168 tabs => [qw( 1° -1° 2° -2° 3° -3° 4° -4° 1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 10 -10 11 -11 12 -12)],
169 notes => [qw(C3 D3 E3 F3 G3 A3 C4 B3 C4 D4 E4 F4 G4 A4 C5 B4 C5 D5 E5 F5 G5 A5 C6 B5 C6 D6 E6 F6 G6 A6 C7 B6)],
170 key => 'C',
171 avoid_tones => 12,
172 },
173 1
174 ),
175 xylophone => {
176 tags => [qw(diatonic 12-bars major other)],
177 name => 'Xylophone',
178 tabs => [qw( 1 2 3 4 5 6 7 8 9 10 11 12)],
179 notes => [qw(C4 D4 E4 F4 G4 A4 B4 C5 D5 E5 F5 G5)],
180 bends => [qw( 0 0 0 0 0 0 0 0 0 0 0 0)],
181 key => 'C',
182 },
183 triola => {
184 tags => [qw(diatonic 12-notes major other)],
185 name => 'Triola',
186 tabs => [qw( 1 2 3 4 5 6 7 8 9 10 11 12)],
187 notes => [qw(G3 A3 B3 C4 D4 E4 F4 G4 A4 B4 C5 D5)],
188 bends => [qw( 0 0 0 0 0 0 0 0 0 0 0 0)],
189 key => 'C',
190 },
191 );
192 #>>>
193  
194 # We can’t use qw() because of the # that triggers a warning.
195 3 Readonly my @KEYS_OFFSET => split / /, q(C Db D Eb E F F# G Ab A Bb B);
196 3 Readonly my %KEYS_TO_TONE => map { $KEYS_OFFSET[$_] => $_ } 0 .. $#KEYS_OFFSET;
197  
198 3 Readonly my $MAX_BENDS => 6; # Probably higher than any realistic value.
199  
200 16 ▶sub get_preferred_key (%options) {
Subroutine get_preferred_key: called CC=4 SCAR=14.3
Pod undocumented
201 16 ▶ my $key = $options{preferred_key} // 'C';
Condition100%2/2
$options{"preferred_key"} // "C"
l!l
115
MC/DC100%1/1
$options{"preferred_key"} // "C"
$options{"preferred_key"}
Truth table100%2/2
A
A= $options{"preferred_key"}
Aresult
11
00
202 16 my $note_converter = Music::Harmonica::TabsCreator::NoteToToneConverter->new();
203 16 my @key_tone = eval { $note_converter->convert($key) };
204 16 ▶ die "Invalid key: $key\n" if $@ || @key_tone != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or @key_tone != 1 0 16 16
Condition33%1/3
$@ or @key_tone != 1
l!l&&r!l&&!r
0016
MC/DC0%0/2
$@ or @key_tone != 1
$@ @key_tone != 1
Truth table33%1/3
A or B
A= $@B= @key_tone != 1
ABresult
1X1
011
000
205 16 return $key_tone[0] % $TONES_PER_SCALE;
206 }
207  
208 17 ▶sub tune_to_tab ($sheet, %options) {
Subroutine tune_to_tab: called CC=3 SCAR=11.0
Pod documented
209 17 my $note_converter = Music::Harmonica::TabsCreator::NoteToToneConverter->new();
210 17 my @tones = $note_converter->convert($sheet);
211 16 ▶ my $tunings = generate_tunings($options{max_bends} // 0, $options{tunings} // []);
Condition100%4/4
$options{"max_bends"} // 0
l!l
313
$options{"tunings"} // []
l!l
610
MC/DC100%1/1
$options{"max_bends"} // 0
$options{"max_bends"}
MC/DC100%1/1
$options{"tunings"} // []
$options{"tunings"}
Truth table100%2/2
A
A= $options{"max_bends"}
Aresult
11
00
Truth table100%2/2
A
A= $options{"tunings"}
Aresult
11
00
212 16 return find_matching_tuning(\@tones, $tunings, get_preferred_key(%options));
213 }
214  
215 0 ▶sub transpose_tab ($tab, $tuning_id, $key, %options) {
Subroutine transpose_tab: not called CC=7 SCAR=40.3
Pod undocumented
216 0 ▶ die "Unknown tuning: $tuning_id\n" unless exists $ALL_TUNINGS{$tuning_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ALL_TUNINGS{$tuning_id} 0 0 0
217 # For the input, we accept any level of bending.
218 0 my $tuning = generate_tunings($MAX_BENDS, [$tuning_id])->{$tuning_id};
219 0 my %tab_to_tones = map { $tuning->{tabs}[$_] => $tuning->{tones}[$_] } 0 .. $#{$tuning->{tabs}};
220 0 my $parser = Music::Harmonica::TabsCreator::TabParser->new(\%tab_to_tones);
221 0 my @tones = $parser->parse($tab);
222 0 my $note_converter = Music::Harmonica::TabsCreator::NoteToToneConverter->new();
223 0 my @key_tone = eval { $note_converter->convert($key) };
224 0 ▶ die "Invalid key: $key\n" if $@ || @key_tone != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or @key_tone != 1 0 0 0
Condition0%0/3
$@ or @key_tone != 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or @key_tone != 1
$@ @key_tone != 1
Truth table0%0/3
A or B
A= $@B= @key_tone != 1
ABresult
1X1
011
000
225 0 my $key_tone = $key_tone[0];
226 0 ▶ @tones = map { looks_like_number($_) ? $_ + $key_tone : $_ } @tones;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_) ? : 0 0 0
227 0 ▶ my $tunings = generate_tunings($options{max_bends} // 0, $options{tunings} // []);
Condition0%0/4
$options{"max_bends"} // 0
l!l
00
$options{"tunings"} // []
l!l
00
MC/DC0%0/1
$options{"max_bends"} // 0
$options{"max_bends"}
MC/DC0%0/1
$options{"tunings"} // []
$options{"tunings"}
Truth table0%0/2
A
A= $options{"max_bends"}
Aresult
11
00
Truth table0%0/2
A
A= $options{"tunings"}
Aresult
11
00
228 0 return find_matching_tuning(\@tones, $tunings, get_preferred_key(%options));
229 }
230  
231 # Given the text representation of one note, and a bend level, generate the text
232 # of the bended note.
233 5133 ▶sub bend ($tab, $b) {
Subroutine bend: called CC=3 SCAR=11.0
Pod undocumented
234 5133 ▶ return $tab if $b == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $b == 0 4492 641 5133
235 641 my $bend = ('"' x ($b / 2)).("'" x ($b % 2));
236 641 ▶ if ($tab =~ m/^\((.+)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tab =~ /^\((.+)\)$/u) { } 420 221 641
237 420 return "(${1}${bend})";
238 } else {
239 221 return ${tab}.${bend};
240 }
241 }
242  
243 # We take the global %ALL_TUNINGS and generate a %tunings hash with the same
244 # keys but where the values only have the tab and a new matching 'tone' entries.
245 # But we have added the notes corresponding to the allowed bends.
246 16 ▶sub generate_tunings ($max_bends, $tunings) {
Subroutine generate_tunings: called CC=7 SCAR=19.5
Pod undocumented
247 16 my %out;
248 16 my $note_converter = Music::Harmonica::TabsCreator::NoteToToneConverter->new();
249 16 ▶ while (my ($k, $v) = each %ALL_TUNINGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ALL_TUNINGS) 224 16 240
250 224 ▶ next if @{$tunings} && none { $_ eq $k } @{$tunings};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if @{$tunings;} and &none(sub { $_ eq $k; } , @{$tunings;}) 78 146 224
Condition100%3/3
@{$tunings;} and &none(sub { $_ eq $k; } , @{$tunings;})
!ll&&!rl&&r
140678
MC/DC100%2/2
@{$tunings;} and &none(sub { $_ eq $k; } , @{$tunings;})
@{$tunings;} &none(sub { $_ eq $k; } , @{$tunings;})
Truth table100%3/3
A and B
A= @{$tunings;}B= &none(sub { $_ eq $k; } , @{$tunings;})
ABresult
0X0
100
111
251 146 for my $i (0 .. $#{$v->{notes}}) {
252 4492 my $base_tone = ($note_converter->convert($v->{notes}[$i]))[0];
253 # We apply a correction so that we have the tones of a C-harmonica as the
254 # offset we will compute in match_notes_to_tuning is assuming that the
255 # tuning was given for a C-harmonica.
256 4492 $base_tone -= $KEYS_TO_TONE{$v->{key}};
257 4492 my $tab = $v->{tabs}[$i];
258 4492 for my $b (0 .. min($max_bends, $v->{bends}[$i])) {
259 5133 push @{$out{$k}{tones}}, $base_tone - $b;
260 5133 push @{$out{$k}{tabs}}, bend($tab, $b);
261 }
262 4492 $out{$k}{is_chromatic} = $v->{is_chromatic};
263 4492 ▶ $out{$k}{avoid_tones} = $v->{avoid_tones} if exists $v->{avoid_tones};
Branch: 1 branch
BranchTrueFalseTotal
if exists $v->{"avoid_tones"} 1392 3100 4492
264 }
265 }
266 16 return \%out;
267 }
268  
269 16 ▶sub find_matching_tuning ($tones, $tunings, $preferred_key) {
Subroutine find_matching_tuning: called CC=3 SCAR=11.0
Pod undocumented
270 16 my %all_matches;
271 16 ▶ while (my ($k, $v) = each %{$tunings}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$tunings;}) 133 15 148
272 133 my @matches = match_notes_to_tuning($tones, $v, $preferred_key);
273 132 for my $m (@matches) {
274 353 push @{$all_matches{$k}{$m->[1]}}, $m->[0];
275 }
276 }
277 15 return %all_matches;
278 }
279  
280 3 ▶sub tune_to_tab_rendered ($sheet, %options) {
Subroutine tune_to_tab_rendered: called CC=2 SCAR=6.9
Pod documented
281 3 my %tabs = eval { tune_to_tab($sheet, %options) };
282 3 ▶ return $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 1 3
283 1 return render_tabs(%tabs);
284 }
285  
286 0 ▶sub transpose_tab_rendered ($tab, $tuning, $key, %options) {
Subroutine transpose_tab_rendered: not called CC=2 SCAR=17.9
Pod undocumented
287 0 my %tabs = eval { transpose_tab($tab, $tuning, $key, %options) };
288 0 ▶ return $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
289 0 return render_tabs(%tabs);
290 }
291  
292 1 ▶sub render_tabs (%tabs) {
Subroutine render_tabs: called CC=8 SCAR=20.8
Pod undocumented
293 1 ▶ if (!%tabs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%tabs) 0 1 1
294 0 return 'No tabs found';
295 }
296  
297 1 my $out;
298  
299 1 for my $type (sort keys %tabs) {
300 13 my %details = get_tuning_details($type);
301 13 my @tags = @{$details{tags}};
302 13 ▶ my $other_idx = first_index { $_ eq 'other' } @tags;
Subroutine __ANON__: called CC=1 SCAR=0.0
303 13 ▶ if ($other_idx == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other_idx == -1) { } 11 2 13
304 11 $out .= sprintf "For %s %s tuning harmonicas:\n", join(' ', @tags), $details{name};
305 } else {
306 2 splice @tags, $other_idx, 1;
307 2 $out .= sprintf "For %s %s:\n", join(' ', @tags), $details{name};
308 }
309 13 for my $key (sort keys %{$tabs{$type}}) {
310 13 $out .= " In the key of ${key}:\n";
311 13 for my $tab (@{$tabs{$type}{$key}}) {
312 16 my $str_tab;
313 16 my $was_nl = 1;
314 16 for my $t (@{$tab}) {
315 128 ▶ $str_tab .= ($was_nl ? ' ' : ' ').$t;
Branch: 1 branch
BranchTrueFalseTotal
$was_nl ? : 16 112 128
316 128 $was_nl = $t =~ m/\v\z/;
317 }
318 16 $str_tab =~ s/\A\s+|\s+\Z//g;
319 16 $out .= ${str_tab}."\n\n";
320 }
321 }
322 }
323  
324 1 return $out;
325 }
326  
327 13 ▶sub get_tuning_details ($key) {
Subroutine get_tuning_details: called CC=1 SCAR=0.0
Pod undocumented
328 13 return %{$ALL_TUNINGS{$key}}{qw(name tags)};
329 }
330  
331 0 ▶sub list_tunings () {
Subroutine list_tunings: not called CC=1 SCAR=6.9
Pod undocumented
332 0 return map { {id => $_, name => $ALL_TUNINGS{$_}{name}, tags => $ALL_TUNINGS{$_}{tags}} }
333 sort keys %ALL_TUNINGS;
334 }
335  
336 # Given all the tones (with C0 = 0) of a melody and the data of a given
337 # harmonica tuning, returns whether the melody can be played on this
338 # harmonica and, if yes, the octave shift to apply to the melody.
339 133 ▶sub match_notes_to_tuning ($tones, $tuning, $preferred_key) {
Subroutine match_notes_to_tuning: called CC=8 SCAR=20.8
Pod undocumented
340 133 my $note_converter = Music::Harmonica::TabsCreator::NoteToToneConverter->new();
341 133 my ($scale_min, $scale_max) = (min(@{$tuning->{tones}}), max(@{$tuning->{tones}}));
342 133 my @real_tones = grep { looks_like_number($_) } @{$tones};
343 133 ▶ die Music::Harmonica::TabsCreator::Warning->new('No melody found in input') unless @real_tones;
Branch: 1 branch
BranchTrueFalseTotal
unless @real_tones 1 132 133
344 132 my ($tones_min, $tones_max) = (min(@real_tones), max(@real_tones));
345 132 my %scale_tones = map { $tuning->{tones}[$_] => $tuning->{tabs}[$_] } 0 .. $#{$tuning->{tones}};
346 132 my ($o_min, $o_max) = ($scale_min - $tones_min, $scale_max - $tones_max);
347 132 my @matches;
348  
349 # max_offset is set for Chromatic harmonicas to avoid generating the
350 # same thing over and over again, transposed by a full octave.
351 132 my $o_max_low = $o_max;
352 132 ▶ $o_max_low = min($o_max, $o_min + $TONES_PER_SCALE - 1) if $tuning->{is_chromatic};
Branch: 1 branch
BranchTrueFalseTotal
if $tuning->{"is_chromatic"} 57 75 132
353 # For some tunings we prefer not to generate the first few tones (typically
354 # anything before middle C on a chromatic harmonica). This settings offset the
355 # search pattern if possible. We note that if is_chromatic was not set, then
356 # this will do nothing because $o_max == $o_max_low.
357 132 ▶ if (exists $tuning->{avoid_tones}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tuning->{"avoid_tones"}) 21 111 132
358 21 my $o = min($tuning->{avoid_tones}, $o_max - $o_max_low);
359 21 $o_max_low += $o;
360 21 $o_min += $o;
361 }
362  
363 132 for my $o ($o_min .. $o_max_low) { # (min($o_max, $o_min + $TONES_PER_SCALE - 1))) {
364 1571 my @tab = tab_from_tones($tones, $o, %scale_tones);
365 1571 ▶ if (@tab) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tab) 495 1076 1571
366 495 my $key = ($TONES_PER_SCALE - $o) % $TONES_PER_SCALE;
367 495 my $match = [\@tab, $KEYS_OFFSET[$key]];
368 495 ▶ if ($tuning->{is_chromatic} && $key == $preferred_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tuning->{"is_chromatic"} and $key == $preferred_key) 51 444 495
Condition100%3/3
$tuning->{"is_chromatic"} and $key == $preferred_key
!ll&&!rl&&r
30214251
MC/DC100%2/2
$tuning->{"is_chromatic"} and $key == $preferred_key
$tuning->{"is_chromatic"} $key == $preferred_key
Truth table100%3/3
A and B
A= $tuning->{"is_chromatic"}B= $key == $preferred_key
ABresult
0X0
100
111
369 51 return $match;
370 }
371 444 push @matches, $match;
372 }
373 }
374 81 return @matches;
375 }
376  
377 1571 ▶sub tab_from_tones($tones, $offset, %scale_tones) {
Subroutine tab_from_tones: called CC=4 SCAR=13.9
Pod undocumented
378 1571 my @tab;
379 1571 for my $t (@{$tones}) {
380 5336 ▶ if (looks_like_number($t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($t)) { } 5315 21 5336
381 5315 ▶ return unless exists $scale_tones{$t + $offset};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $scale_tones{$t + $offset} 1076 4239 5315
382 4239 push @tab, $scale_tones{$t + $offset};
383 } else {
384 21 push @tab, $t;
385 }
386 }
387 495 return @tab;
388 }
389  
390 3 1;
391  
392 # TODO: document the options of the methods.
393  
394 __END__
↑ Summary TabsCreator/NoteToToneConverter.pm »