blib/lib/Music/Scala.pm

filter: Statementstmt 93.8% 228 / 243 Branchbran 68.6% 114 / 166 Conditioncond 46.3% 19 / 41 MC/DCmcdc 8.0% 2 / 25 Subroutinesub 100% 34 / 34 Podpod 94.4% 17 / 18 totaltotal 78.5% 414 / 527 SCAR 52.8
CC
107
Cov
80%
read_scala
33.9
write_scala
15.6
interval2freq
12.1
CRAP 195.6
↑ Summary blib/lib/Music/Scala/ConfigData.pm »
1 # -*- Perl -*-
2 #
3 # Scala scale (musical tuning and temperament) support for Perl, based
4 # on specification at: http://www.huygens-fokker.org/scala/
5 #
6 # Ratio to cent and cent to ratio equations lifted from "Musimathics,
7 # volume 1", pp. 45-46. MIDI conversion probably from wikipedia.
8  
9 package Music::Scala;
10  
11 2 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Scalar::Util qw/looks_like_number reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 our $VERSION = '1.07';
22  
23 ##############################################################################
24 #
25 # ATTRIBUTES
26 #
27 # NOTE that much of the Moo setup (getters/setters, how "notes" handled,
28 # etc) is to preserve compatibility with how the code worked pre-Moo.
29 # Additional hilarity stemmed from (the mistake of?) offering multiple
30 # methods to get/set the same data in different guises (notes (as cents
31 # or ratios), (notes as) cents, (notes as) ratios).
32  
33 2 has binmode => (
34 is => 'rw',
35 predicate => 1, # has_binmode
36 reader => 'get_binmode',
37 writer => 'set_binmode',
38 );
39  
40 has concertfreq => (
41 is => 'rw',
42 4 ▶ default => sub { 440 },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 isa => sub {
44 7 ▶ die 'frequency must be a positive number (Hz)'
Subroutine __ANON__: called CC=4 SCAR=19.1
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] && &looks_like_number($_[0]) or $_[0] <= 0 0 7 7
Condition33%2/6
not defined $_[0] && &looks_like_number($_[0]) or $_[0] <= 0
l!l&&r!l&&!r
007
defined $_[0] && &looks_like_number($_[0])
!ll&&!rl&&r
007
MC/DC0%0/3
not defined $_[0] && &looks_like_number($_[0]) or $_[0] <= 0
defined $_[0] &looks_like_number($_[0]) $_[0] <= 0
Truth table25%1/4
not(A && B) or C
A= defined $_[0]B= &looks_like_number($_[0])C= $_[0] <= 0
ABCresult
0XX1
10X1
1111
1100
45 if !defined $_[0]
46 or !looks_like_number $_[0]
47 or $_[0] <= 0;
48 },
49 2 reader => 'get_concertfreq',
50 writer => 'set_concertfreq',
51 );
52  
53 has concertpitch => (
54 is => 'rw',
55 6 ▶ default => sub { 69 },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 isa => sub {
57 6 ▶ die 'pitch must be a positive number'
Subroutine __ANON__: called CC=4 SCAR=19.1
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] && &looks_like_number($_[0]) or $_[0] <= 0 0 6 6
Condition33%2/6
not defined $_[0] && &looks_like_number($_[0]) or $_[0] <= 0
l!l&&r!l&&!r
006
defined $_[0] && &looks_like_number($_[0])
!ll&&!rl&&r
006
MC/DC0%0/3
not defined $_[0] && &looks_like_number($_[0]) or $_[0] <= 0
defined $_[0] &looks_like_number($_[0]) $_[0] <= 0
Truth table25%1/4
not(A && B) or C
A= defined $_[0]B= &looks_like_number($_[0])C= $_[0] <= 0
ABCresult
0XX1
10X1
1111
1100
58 if !defined $_[0]
59 or !looks_like_number $_[0]
60 or $_[0] <= 0;
61 },
62 2 reader => 'get_concertpitch',
63 writer => 'set_concertpitch',
64 );
65  
66 has description => (
67 is => 'rw',
68 6 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 isa => sub {
70 15 ▶ die 'description must be string value'
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or defined builtin::reftype($_[0]) 0 15 15
Condition33%1/3
not defined $_[0] or defined builtin::reftype($_[0])
l!l&&r!l&&!r
0015
MC/DC0%0/2
not defined $_[0] or defined builtin::reftype($_[0])
not defined $_[0] defined builtin::reftype($_[0])
Truth table33%1/3
not(A) or B
A= not defined $_[0]B= defined builtin::reftype($_[0])
ABresult
1X1
011
000
71 if !defined $_[0]
72 or defined reftype $_[0];
73 },
74 2 reader => 'get_description',
75 writer => 'set_description',
76 );
77  
78 # Sanity on scala scale file reads; other prudent limits with untrusted
79 # input would be to check the file size, and perhaps to bail if the note
80 # count is some absurd value.
81 has MAX_LINES => (
82 is => 'rw',
83 5 ▶ default => sub { 3000 },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 2 );
85  
86 2 has notes => (
87 is => 'rw',
88 clearer => 1,
89 predicate => 1, # has_notes
90 );
91  
92 ##############################################################################
93 #
94 # METHODS
95  
96 sub BUILD {
97 6 ▶ my ($self, $param) = @_;
Subroutine BUILD: called CC=5 SCAR=17.8
Pod undocumented
98  
99 6 ▶ if (exists $param->{file} and exists $param->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"file"} and exists $param->{"fh"}) 0 6 6
Condition66%2/3
exists $param->{"file"} and exists $param->{"fh"}
!ll&&!rl&&r
510
MC/DC0%0/2
exists $param->{"file"} and exists $param->{"fh"}
exists $param->{"file"} exists $param->{"fh"}
Truth table66%2/3
A and B
A= exists $param->{"file"}B= exists $param->{"fh"}
ABresult
0X0
100
111
100 0 die "new accepts only one of the 'file' or 'fh' arguments\n";
101 }
102  
103 6 ▶ if (exists $param->{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param->{'file'}) { } 1 5 6
elsif (exists $param->{'fh'}) { } 0 5 5
104 1 $self->read_scala(file => $param->{file});
105 } elsif (exists $param->{fh}) {
106 0 $self->read_scala(fh => $param->{fh});
107 }
108 }
109  
110 # Absolute interval list to relative (1 2 3 -> 1 1 1)
111 sub abs2rel {
112 2 ▶ my $self = shift;
Subroutine abs2rel: called CC=4 SCAR=14.1
Pod documented
113 2 ▶ return if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
114 2 my @result = $_[0];
115 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 2 0 2
116 2 for my $i (1 .. $#_) {
117 13 push @result, $_[$i] - $_[ $i - 1 ];
118 }
119 }
120 2 return @result;
121 }
122  
123 sub cents2ratio {
124 2 ▶ my ($self, $cents, $precision) = @_;
Subroutine cents2ratio: called CC=5 SCAR=16.8
Pod documented
125 2 ▶ croak 'cents must be a number' if !looks_like_number $cents;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($cents) 0 2 2
126 2 ▶ if (defined $precision) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $precision) { } 1 1 2
127 1 ▶ croak 'precision must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
if not &looks_like_number($precision) or $precision < 0 0 1 1
Condition33%1/3
not &looks_like_number($precision) or $precision < 0
l!l&&r!l&&!r
001
MC/DC0%0/2
not &looks_like_number($precision) or $precision < 0
not &looks_like_number($precision) $precision < 0
Truth table33%1/3
not(A) or B
A= not &looks_like_number($precision)B= $precision < 0
ABresult
1X1
011
000
128 if !looks_like_number $precision or $precision < 0;
129 1 $precision = int $precision;
130 } else {
131 1 $precision = 2;
132 }
133  
134 2 return sprintf "%.*f", $precision, 10**($cents / 3986.31371386484);
135 }
136  
137 # MIDI calculation, for easy comparison to scala results
138 sub freq2pitch {
139 1 ▶ my ($self, $freq) = @_;
Subroutine freq2pitch: called CC=3 SCAR=12.5
Pod documented
140 1 ▶ croak 'frequency must be a positive number'
Branch: 1 branch
BranchTrueFalseTotal
if not &looks_like_number($freq) or $freq <= 0 0 1 1
Condition33%1/3
not &looks_like_number($freq) or $freq <= 0
l!l&&r!l&&!r
001
MC/DC0%0/2
not &looks_like_number($freq) or $freq <= 0
not &looks_like_number($freq) $freq <= 0
Truth table33%1/3
not(A) or B
A= not &looks_like_number($freq)B= $freq <= 0
ABresult
1X1
011
000
141 if !looks_like_number $freq
142 or $freq <= 0;
143  
144 # no precision, as assume pitch numbers are integers
145 1 return sprintf '%.0f',
146 $self->get_concertpitch +
147 12 * (log($freq / $self->get_concertfreq) / 0.693147180559945);
148 }
149  
150 sub get_cents {
151 3 ▶ my ($self) = @_;
Subroutine get_cents: called CC=2 SCAR=7.0
Pod documented
152 3 ▶ croak 'no scala loaded' if !$self->has_notes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_notes 0 3 3
153 3 return $self->notes2cents(@{ $self->notes });
154 }
155  
156 sub get_notes {
157 6 ▶ my ($self) = @_;
Subroutine get_notes: called CC=2 SCAR=6.9
Pod documented
158 6 ▶ croak 'no scala loaded' if !$self->has_notes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_notes 1 5 6
159 5 return @{ $self->notes };
160 }
161  
162 sub get_ratios {
163 3 ▶ my ($self) = @_;
Subroutine get_ratios: called CC=2 SCAR=7.0
Pod documented
164 3 ▶ croak 'no scala loaded' if !$self->has_notes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_notes 0 3 3
165 3 return $self->notes2ratios(@{ $self->notes });
166 }
167  
168 sub interval2freq {
169 5 ▶ my $self = shift;
Subroutine interval2freq: called CC=12 SCAR=24.9
Pod documented
170 5 ▶ croak 'no scala loaded' if !$self->has_notes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_notes 0 5 5
171  
172 5 my @ratios = $self->notes2ratios(@{ $self->notes });
173  
174 5 my @freqs;
175 5 ▶ for my $i (ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 5 5
176 79 ▶ if ($i == 0) { # special case for unison (ratio 1/1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 5 74 79
177 5 push @freqs, $self->get_concertfreq;
178 } else {
179 74 ▶ my $is_dsc = $i < 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i < 0 ? : 35 39 74
180  
181 # for non-"octave" portion, if any
182 74 my $offset = $i % @ratios;
183  
184 # "Octave" portion, if any - how many times the interval
185 # passes through the complete scale
186 74 my $octave_freq = 0;
187 74 my $octave_count = abs int $i / @ratios;
188  
189 # if non-octave on a negative interval, go one octave past
190 # the target, then use the regular ascending logic to
191 # backtrack to the proper frequency
192 74 ▶ $octave_count++ if $is_dsc and $offset != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $is_dsc and $offset != 0 28 46 74
Condition100%3/3
$is_dsc and $offset != 0
!ll&&!rl&&r
39728
MC/DC100%2/2
$is_dsc and $offset != 0
$is_dsc $offset != 0
Truth table100%3/3
A and B
A= $is_dscB= $offset != 0
ABresult
0X0
100
111
193  
194 74 ▶ if ($octave_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($octave_count > 0) 58 16 74
195 58 my $octaves_ratio = $ratios[-1]**$octave_count;
196 58 ▶ $octaves_ratio = 1 / $octaves_ratio if $is_dsc;
Branch: 1 branch
BranchTrueFalseTotal
if $is_dsc 35 23 58
197 58 $octave_freq = $self->get_concertfreq * $octaves_ratio;
198 }
199  
200 74 my $remainder_freq = 0;
201 74 ▶ if ($offset != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset != 0) 59 15 74
202 59 ▶ $remainder_freq =
Condition66%2/3
$octave_freq || $self->get_concertfreq
l!l&&r!l&&!r
43160
MC/DC0%0/2
$octave_freq || $self->get_concertfreq
$octave_freq $self->get_concertfreq
Truth table66%2/3
A || B
A= $octave_freqB= $self->get_concertfreq
ABresult
1X1
011
000
203 ($octave_freq || $self->get_concertfreq) * $ratios[ $offset - 1 ];
204  
205 # zero as remainder is based from $octave_freq, if
206 # relevant, so already includes such
207 59 $octave_freq = 0;
208 }
209  
210 74 push @freqs, $octave_freq + $remainder_freq;
211 }
212 }
213  
214 5 return @freqs;
215 }
216  
217 sub is_octavish {
218 2 ▶ my $self = shift;
Subroutine is_octavish: called CC=7 SCAR=19.9
Pod documented
219 2 ▶ croak 'no scala loaded' if !$self->has_notes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_notes 0 2 2
220  
221 2 my @ratios = $self->notes2ratios(@{ $self->notes });
222  
223 # not octave bounded (double the frequency, e.g. 440 to 880)
224 2 ▶ return 0 if $ratios[-1] != 2;
Branch: 1 branch
BranchTrueFalseTotal
if $ratios[-1] != 2 1 1 2
225  
226 1 my $min;
227 1 for my $r (@ratios) {
228 # don't know how to handle negative ratios
229 12 ▶ return 0 if $r < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r < 0 0 12 12
230  
231 # multiple scales within the same definition file (probably for
232 # instruments that have two different scales in the same
233 # frequency domain) - but don't know how to handle these
234 12 ▶ return 0 if defined $min and $r <= $min;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and $r <= $min 0 12 12
Condition66%2/3
defined $min and $r <= $min
!ll&&!rl&&r
1110
MC/DC0%0/2
defined $min and $r <= $min
defined $min $r <= $min
Truth table66%2/3
A and B
A= defined $minB= $r <= $min
ABresult
0X0
100
111
235  
236 12 $min = $r;
237 }
238  
239 1 return 1;
240 }
241  
242 sub notes2cents {
243 17 ▶ my $self = shift;
Subroutine notes2cents: called CC=4 SCAR=14.0
Pod documented
244  
245 17 my @cents;
246 17 ▶ for my $n (ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 17 17
247 42 ▶ if ($n =~ m{([0-9]+)/([1-9][0-9]*)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ m[([0-9]+)/([1-9][0-9]*)]) { } 20 22 42
248 20 push @cents, 1200 * ((log($1 / $2) / 2.30258509299405) / 0.301029995663981);
249 } else {
250 22 push @cents, $n;
251 }
252 }
253  
254 17 return @cents;
255 }
256  
257 sub notes2ratios {
258 11 ▶ my $self = shift;
Subroutine notes2ratios: called CC=4 SCAR=14.0
Pod documented
259  
260 11 my @ratios;
261 11 ▶ for my $n (ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 11 11
262 82 ▶ if ($n =~ m{([0-9]+)/([1-9][0-9]*)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ m[([0-9]+)/([1-9][0-9]*)]) { } 25 57 82
263 25 push @ratios, $1 / $2; # ratio, as marked with /
264 } else {
265 57 push @ratios, 10**($n / 3986.31371386484);
266 }
267 }
268  
269 11 return @ratios;
270 }
271  
272 # MIDI for comparison, the other way
273 sub pitch2freq {
274 54 ▶ my ($self, $pitch) = @_;
Subroutine pitch2freq: called CC=3 SCAR=12.5
Pod documented
275 54 ▶ croak "pitch must be MIDI number"
Branch: 1 branch
BranchTrueFalseTotal
if not &looks_like_number($pitch) or $pitch < 0 0 54 54
Condition33%1/3
not &looks_like_number($pitch) or $pitch < 0
l!l&&r!l&&!r
0054
MC/DC0%0/2
not &looks_like_number($pitch) or $pitch < 0
not &looks_like_number($pitch) $pitch < 0
Truth table33%1/3
not(A) or B
A= not &looks_like_number($pitch)B= $pitch < 0
ABresult
1X1
011
000
276 if !looks_like_number $pitch
277 or $pitch < 0;
278  
279 54 return $self->get_concertfreq * (2**(($pitch - $self->get_concertpitch) / 12));
280 }
281  
282 sub ratio2cents {
283 2 ▶ my ($self, $ratio, $precision) = @_;
Subroutine ratio2cents: called CC=5 SCAR=16.8
Pod documented
284 2 ▶ croak 'ratio must be a number' if !looks_like_number $ratio;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($ratio) 0 2 2
285 2 ▶ if (defined $precision) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $precision) { } 1 1 2
286 1 ▶ croak 'precision must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
if not &looks_like_number($precision) or $precision < 0 0 1 1
Condition33%1/3
not &looks_like_number($precision) or $precision < 0
l!l&&r!l&&!r
001
MC/DC0%0/2
not &looks_like_number($precision) or $precision < 0
not &looks_like_number($precision) $precision < 0
Truth table33%1/3
not(A) or B
A= not &looks_like_number($precision)B= $precision < 0
ABresult
1X1
011
000
287 if !looks_like_number $precision or $precision < 0;
288 1 $precision = int $precision;
289 } else {
290 1 $precision = 2;
291 }
292  
293 2 return sprintf "%.*f", $precision,
294 1200 * ((log($ratio) / 2.30258509299405) / 0.301029995663981);
295 }
296  
297 sub read_scala {
298 10 ▶ my $self = shift;
Subroutine read_scala: called CC=27 SCAR=35.2
Pod documented
299 10 my %param;
300 10 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 9 10
301 1 $param{file} = $_[0];
302 } else {
303 9 %param = @_;
304 }
305  
306 10 my $fh;
307 10 ▶ if (exists $param{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param{'file'}) { } 10 0 10
elsif (exists $param{'fh'}) { } 0 0 0
308 10 ▶ open($fh, '<', $param{file}) or croak 'open failed: ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $param{"file"} or croak('open failed: ' . $!) 1 9 10
309 } elsif (exists $param{fh}) {
310 0 $fh = $param{fh};
311 } else {
312 0 croak 'must specify file or fh parameter to read_scala';
313 }
314 9 ▶ if (exists $param{binmode}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param{'binmode'}) { } 1 8 9
elsif ($self->has_binmode) { } 5 3 8
315 1 ▶ binmode $fh, $param{binmode} or croak 'binmode failed: ' . $!;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $param{"binmode"} or croak('binmode failed: ' . $!) 0 1 1
316 } elsif ($self->has_binmode) {
317 5 ▶ binmode $fh, $self->get_binmode or croak 'binmode failed: ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $self->get_binmode or croak('binmode failed: ' . $!) 0 5 5
318 }
319  
320 9 my (@scala, $line_count);
321 9 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 29 0 29
322 29 my $line = readline $fh;
323 29 ▶ croak 'readline failed: ' . $! unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 29 29
324 29 ▶ croak 'input exceeds MAX_LINES' if ++$line_count >= $self->MAX_LINES;
Branch: 1 branch
BranchTrueFalseTotal
if ++$line_count >= $self->MAX_LINES 1 28 29
325 28 ▶ next if $line =~ m/^[!]/; # skip comments
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[!]/ 12 16 28
326  
327 16 chomp $line;
328 16 push @scala, $line;
329  
330 16 ▶ last if @scala == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @scala == 2 8 8 16
331 }
332 # but as might hit the MAX_LINES or eof() instead check again...
333 8 ▶ if (@scala != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@scala != 2) 0 8 8
334 0 croak 'missing description or note count lines';
335 }
336  
337 8 $self->set_description(shift @scala);
338 8 my $NOTECOUNT;
339 8 ▶ if ($scala[-1] =~ m/^\s*([0-9]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scala[-1] =~ /^\s*([0-9]+)/) { } 8 0 8
340 8 $NOTECOUNT = $1;
341 } else {
342 0 croak 'could not parse note count';
343 }
344  
345 8 my @notes;
346 8 my $cur_note = 1;
347 8 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 94 0 94
348 94 my $line = readline $fh;
349 94 ▶ croak 'readline failed: ' . $! unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 94 94
350 94 ▶ croak 'input exceeds MAX_LINES' if ++$line_count >= $self->MAX_LINES;
Branch: 1 branch
BranchTrueFalseTotal
if ++$line_count >= $self->MAX_LINES 0 94 94
351 94 ▶ next if $line =~ m/^[!]/; # skip comments
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[!]/ 6 88 94
352  
353 # All the scales.zip *.scl files as of 2013-02-19 have digits on
354 # both sides of the dot (so there are no ".42" cent values, but
355 # the "these are all valid pitch lines" does include a "408." as
356 # allowed). Some scale files have negative cents, though that is
357 # illegal for ratios. All the ratios are plain numbers (no
358 # period), or if they have a slash, it is followed by another
359 # number (so no "42/" cases). Checked via various greps on the
360 # file contents.
361 88 ▶ if ($line =~ m/^\s* ( -?[0-9]+\. [0-9]* ) /x) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s* ( -?[0-9]+\. [0-9]* ) /x) { } 51 37 88
elsif ($line =~ /^\s* -[0-9] /x) { } 0 37 37
elsif ($line =~ m[^\s* ( [1-9][0-9]* (?:/[0-9]+)? ) ]x) { } 37 0 37
362 51 push @notes, $1; # cents
363 } elsif ($line =~ m{^\s* -[0-9] }x) {
364 # specification says these "should give a read error"
365 0 croak 'invalid negative ratio in note list';
366 } elsif ($line =~ m{^\s* ( [1-9][0-9]* (?:/[0-9]+)? ) }x) {
367 37 my $ratio = $1;
368 37 ▶ $ratio .= '/1' if $ratio !~ m{/}; # implicit qualify of ratios
Branch: 1 branch
BranchTrueFalseTotal
unless $ratio =~ m[/] 2 35 37
369 37 push @notes, $ratio;
370 } else {
371 # Nothing in the spec about non-matching lines, so blow up.
372 # However, there are six files in scales.zip that have
373 # trailing blank lines, though these blank lines occur only
374 # after an appropriate number of note entries. So must exit
375 # loop before reading those invalid? lines. (Did mail the
376 # author about these, so probably has been rectified.)
377 0 croak 'invalid note specification on line ' . $.;
378 }
379  
380 88 ▶ last if $cur_note++ >= $NOTECOUNT;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_note++ >= $NOTECOUNT 8 80 88
381 }
382 8 ▶ if (@notes != $NOTECOUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if (@notes != $NOTECOUNT) 0 8 8
383 0 croak 'expected ' . $NOTECOUNT . ' notes but got ' . scalar(@notes) . " notes";
384 }
385  
386 # edge case: remove any 1/1 (zero cents) at head of the list, as
387 # this implementation treats that as implicit
388 8 ▶ shift @notes if sprintf("%.0f", $self->notes2cents($notes[0])) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if sprintf("%.0f", $self->notes2cents($notes[0])) == 0 1 7 8
389  
390 8 $self->notes(\@notes);
391  
392 8 return $self;
393 }
394  
395 # Relative interval list to absolute (1 1 1 -> 1 2 3)
396 sub rel2abs {
397 2 ▶ my $self = shift;
Subroutine rel2abs: called CC=4 SCAR=14.1
Pod documented
398 2 ▶ return if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
399 2 my @result = $_[0];
400 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 2 0 2
401 2 for my $i (1 .. $#_) {
402 13 push @result, $result[-1] + $_[$i];
403 }
404 }
405 2 return @result;
406 }
407  
408 # Given list of frequencies, assume first is root frequency, then
409 # convert the remainder of the frequencies to cents against that first
410 # frequency.
411 sub set_by_frequency {
412 2 ▶ my $self = shift;
Subroutine set_by_frequency: called CC=6 SCAR=18.2
Pod documented
413 2 ▶ my $freqs = ref $_[0] eq 'ARRAY' ? $_[0] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 1 2
414 2 ▶ croak 'need both root and other frequencies' if @$freqs < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$freqs < 2 0 2 2
415 2 ▶ croak 'root frequency must not be zero' if $freqs->[0] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $freqs->[0] == 0 0 2 2
416  
417 2 my @notes;
418 2 for my $i (1 .. $#{$freqs}) {
419 3 push @notes,
420 1200 *
421 ((log($freqs->[$i] / $freqs->[0]) / 2.30258509299405) / 0.301029995663981);
422 }
423  
424 # edge case: remove any 1/1 (zero cents) at head of the list, as
425 # this implementation treats that as implicit
426 2 ▶ shift @notes if sprintf("%.0f", $self->notes2cents($notes[0])) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if sprintf("%.0f", $self->notes2cents($notes[0])) == 0 0 2 2
427  
428 2 $self->notes(\@notes);
429  
430 2 return $self;
431 }
432  
433 sub set_notes {
434 3 ▶ my $self = shift;
Subroutine set_notes: called CC=7 SCAR=19.8
Pod documented
435 3 my @notes;
436 3 ▶ for my $n (ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 2 3
437 10 ▶ if ($n =~ m{^ -?[0-9]+\. (?:[0-9]+)? $}x) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n =~ /^ -?[0-9]+\. (?:[0-9]+)? $/x) { } 6 4 10
elsif ($n =~ m[^ [1-9][0-9]* (?:/[0-9]+)? $]x) { } 4 0 4
438 6 push @notes, $n;
439 } elsif ($n =~ m{^ [1-9][0-9]* (?:/[0-9]+)? $}x) {
440 4 my $ratio = $n;
441 4 ▶ $ratio .= '/1' if $ratio !~ m{/}; # implicit qualify of ratios
Branch: 1 branch
BranchTrueFalseTotal
unless $ratio =~ m[/] 0 4 4
442 4 push @notes, $ratio;
443 } else {
444 0 croak 'notes must be integer ratios or real numbers';
445 }
446 }
447  
448 # edge case: remove any 1/1 (zero cents) at head of the list, as
449 # this implementation treats that as implicit
450 3 ▶ shift @notes if sprintf("%.0f", $self->notes2cents($notes[0])) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if sprintf("%.0f", $self->notes2cents($notes[0])) == 0 0 3 3
451  
452 3 $self->notes(\@notes);
453 3 return $self;
454 }
455  
456 sub write_scala {
457 2 ▶ my $self = shift;
Subroutine write_scala: called CC=14 SCAR=27.5
Pod documented
458 2 ▶ croak 'no scala loaded' if !$self->has_notes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_notes 0 2 2
459  
460 2 my %param;
461 2 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 2 2
462 0 $param{file} = $_[0];
463 } else {
464 2 %param = @_;
465 }
466  
467 2 my $fh;
468 2 ▶ if (exists $param{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param{'file'}) { } 1 1 2
elsif (exists $param{'fh'}) { } 1 0 1
469 1 ▶ open($fh, '>', $param{file}) or croak 'open failed: ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $param{"file"} or croak('open failed: ' . $!) 0 1 1
470 } elsif (exists $param{fh}) {
471 1 $fh = $param{fh};
472 } else {
473 0 croak 'must specify file or fh parameter to write_scala';
474 }
475 2 ▶ if (exists $param{binmode}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param{'binmode'}) { } 1 1 2
elsif ($self->has_binmode) { } 1 0 1
476 1 ▶ binmode $fh, $param{binmode} or croak 'binmode failed: ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $param{"binmode"} or croak('binmode failed: ' . $!) 0 1 1
477 } elsif ($self->has_binmode) {
478 1 ▶ binmode $fh, $self->get_binmode or croak 'binmode failed: ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $self->get_binmode or croak('binmode failed: ' . $!) 0 1 1
479 }
480  
481 my $filename = basename($param{file})
482 2 ▶ if exists $param{file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param{"file"} 1 1 2
483 2 ▶ my $note_count = @{ $self->notes } || 0;
Condition50%1/2
@{$self->notes;} || 0
l!l
20
MC/DC0%0/1
@{$self->notes;} || 0
@{$self->notes;}
Truth table50%1/2
A
A= @{$self->notes;}
Aresult
11
00
484  
485 2 ▶ say $fh defined $filename
Branch: 1 branch
BranchTrueFalseTotal
defined $filename ? : 1 1 2
486 ? "! $filename"
487 : '!';
488 2 say $fh '!';
489 2 say $fh $self->get_description;
490 2 say $fh ' ', $note_count;
491 2 say $fh '!'; # conventional comment between note count and notes
492  
493 2 for my $note (@{ $self->notes }) {
494 14 say $fh ' ', $note;
495 }
496  
497 2 return $self;
498 }
499  
500 2 1;
501 __END__
↑ Summary blib/lib/Music/Scala/ConfigData.pm »