blib/lib/Music/Scales.pm

filter: Statementstmt 92.1% 118 / 128 Branchbran 75.9% 41 / 54 Conditioncond 80.7% 21 / 26 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 93.3% 14 / 15 Podpod 100% 6 / 6 totaltotal 85.6% 209 / 244 CC 40 SCAR 25.2
CC
40
Cov
87%
get_scale_notes
22.4
get_scale_offsets
20.0
_scale_to_PDL
6.0
CRAP 12.5
↑ Summary
1 package Music::Scales;
2 7 $Music::Scales::VERSION = '0.13';
3 7 our $AUTHORITY = 'cpan:GENE';
4  
5 # ABSTRACT: Generate musical scales
6  
7 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Text::Abbrev;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶ use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 $VERSION = '0.13';
14 7 @ISA = qw(Exporter);
15 7 @EXPORT = qw(get_scale_notes get_scale_nums get_scale_offsets is_scale get_scale_PDL get_scale_MIDI %modes %original_modes %abbrevs @scales);
16 }
17  
18  
19 7 our %modes = qw(ionian 1 major 1 hypolydian 1 dorian 2 hypomyxolydian 2
20 phrygian 3 hypoaeolian 3 lydian 4 hypolocrian 4 mixolydian 5 hypoionian 5
21 aeolian 6 minor 6 m 6 hypodorian 6 locrian 7 hypophrygian 7
22 harmonicminor 8 hm 8 melodicminor 9 mm 9
23 blues 10 pentatonic 11 pmaj 11 chromatic 12 diminished 13 wholetone 14
24 augmented 15 hungarianminor 16 3semitone 17 4semitone 18
25 neapolitanminor 19 nmin 19 neapolitanmajor 20 nmaj 20
26 todi 21 marva 22 persian 23 oriental 24 romanian 25 pelog 26
27 iwato 27 hirajoshi 28 egyptian 29 pminor 30 pentatonicminor 30
28 );
29 7 our %original_modes = %modes;
30 7 our %abbrevs = abbrev(keys %modes);
31 7 ▶while (my ($k,$v) = each %abbrevs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Music::Scales::abbrevs) 1876 7 1883
32 1876 $modes{$k} = $modes{$v};
33 }
34  
35 7 our @scales=([0,2,4,5,7,9,11], # Ionian(1)
36 [0,2,3,5,7,9,10], # Dorian (2)
37 [0,1,3,5,7,8,10], # Phrygian (3)
38 [0,2,4,6,7,9,11], # Lydian (4)
39 [0,2,4,5,7,9,10], # Mixolydian (5)
40 [0,2,3,5,7,8,10], # Aeolian (6)
41 [0,1,3,5,6,8,10], # Locrian (7)
42 [0,2,3,5,7,8,11], # Harmonic Minor (8)
43 [0,2,3,5,7,9,11], # Melodic Minor (9)
44 [0,3,5,6,7,10], # Blues (10)
45 [0,2,4,7,9], # Pentatonic (11)
46 [0,1,2,3,4,5,6,7,8,9,10,11],# Chromatic (12)
47 [0,2,3,5,6,8,9,11], # Diminished (13)
48 [0,2,4,6,8,10], # Whole tone(14)
49 [0,3,4,7,8,11], # Augmented (15)
50 [0,2,3,6,7,8,11], # Hungarian minor (16)
51 [0,3,6,9], # 3 semitone (dimished arpeggio) (17)
52 [0,4,8], # 4 semitone (augmented arpeggio) (18)
53 [0,1,3,5,7,8,11], # Neapolitan minor (19)
54 [0,1,3,5,7,9,11], # Neapolitan major (20)
55 [0,1,3,6,7,8,11], # Todi (Indian) (21)
56 [0,1,4,6,7,9,11], # Marva (Indian) (22)
57 [0,1,4,5,6,8,11], # Persian (23)
58 [0,1,4,5,6,9,10], # Oriental (24)
59 [0,2,3,6,7,9,10], # Romanian (25)
60 [0,1,3,7,10], # Pelog (Balinese) (26)
61 [0,1,5,6,10], # Iwato (Japanese) (27)
62 [0,2,3,7,8], # Hirajoshi (Japanese) (28)
63 [0,2,5,7,10], # Egyptian (29)
64 [0,3,5,7,10], # Pentatonic Minor (30)
65 );
66  
67 14 ▶sub get_scale_nums {
Subroutine get_scale_nums: called CC=4 SCAR=14.6
Pod documented
68 14 my ($mode,$descending) = @_;
69 14 $mode = _get_mode($mode);
70 14 my @dists = @{$scales[$mode-1]};
71 14 ▶ if ($descending && $mode == 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descending and $mode == 9) 0 14 14
Condition66%2/3
$descending and $mode == 9
!ll&&!rl&&r
1130
MC/DC0%0/2
$descending and $mode == 9
$descending $mode == 9
Truth table66%2/3
A and B
A= $descendingB= $mode == 9
ABresult
0X0
100
111
72 0 $dists[5]-- ;$dists[6]--;
73 }
74 14 ▶ ($descending) ? reverse @dists : @dists;
Branch: 1 branch
BranchTrueFalseTotal
$descending ? : 3 11 14
75 }
76  
77 0 ▶sub get_scale_offsets {
Subroutine get_scale_offsets: not called CC=4 SCAR=30.0
Pod documented
78 0 my @scale = get_scale_notes(@_);
79 0 my %key_alts = qw(C 0 D 0 E 0 F 0 G 0 A 0 B 0);
80 0 foreach (@scale) {
81 0 ▶ $key_alts{$_}++ if s/#//;
Branch: 1 branch
BranchTrueFalseTotal
if s/#// 0 0 0
82 0 ▶ $key_alts{$_}-- if s/b//;
Branch: 1 branch
BranchTrueFalseTotal
if s/b// 0 0 0
83 }
84 0 %key_alts;
85 }
86  
87 23 ▶sub _get_mode {
Subroutine _get_mode: called CC=5 SCAR=16.7
88 23 ▶ my $mode = shift() || 1;
Condition100%2/2
shift() || 1
l!l
221
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
89 23 $mode =~ s/[^a-zA-Z0-9]//g;
90 23 ▶ $mode = $modes{lc($mode)} unless $mode =~/^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode =~ /^[0-9]+$/ 5 18 23
91 23 ▶ ($mode && ($mode <= @scales)) ? $mode : 1;
Branch: 1 branch
BranchTrueFalseTotal
$mode && $mode <= @Music::Scales::scales ? : 23 0 23
Condition33%1/3
$mode and $mode <= @Music::Scales::scales
!ll&&!rl&&r
0023
MC/DC0%0/2
$mode and $mode <= @Music::Scales::scales
$mode $mode <= @Music::Scales::scales
Truth table33%1/3
A and B
A= $modeB= $mode <= @Music::Scales::scales
ABresult
0X0
100
111
92 }
93  
94 46 ▶sub _note_to_num {
Subroutine _note_to_num: called CC=3 SCAR=11.4
95 46 my $note = shift();
96 46 my %note2num = ('A','0','A#','1','BB','1','B','2','C','3','C#','4','DB','4','D','5','D#','6','EB','6','E','7','F','8','F#','9','GB','9','G','10','G#','11','AB','11');
97 46 ▶ return $note if ($note =~/^[0-9]+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $note =~ /^[0-9]+$/ 0 46 46
98 46 ▶ (defined $note2num{uc($note)}) ? $note2num{uc($note)} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $note2num{uc $note} ? : 46 0 46
99 }
100  
101 3 ▶sub _note_to_MIDI {
Subroutine _note_to_MIDI: called CC=1 SCAR=0.0
102 3 my ($note,$octave) = @_;
103 3 ((_note_to_num($note)+9) % 12) + (12 * ++$octave );
104 }
105  
106 3 ▶sub get_scale_MIDI {
Subroutine get_scale_MIDI: called CC=1 SCAR=0.0
Pod documented
107 3 my ($note,$octave,$mode,$descending) = @_;
108 3 my $basenum = _note_to_MIDI($note,$octave);
109 3 return map {$basenum + $_} get_scale_nums($mode,$descending);
110 }
111  
112 4 ▶sub get_scale_PDL {
Subroutine get_scale_PDL: called CC=1 SCAR=0.0
Pod documented
113 4 my ($note,$octave,$mode,$descending,$keypref) = @_;
114 4 _scale_to_PDL($octave,get_scale_notes($note,$mode,$descending,$keypref));
115 }
116  
117 9 ▶sub get_scale_notes {
Subroutine get_scale_notes: called CC=22 SCAR=31.1
Pod documented
118 9 my ($keynote,$mode,$descending,$keypref) = @_;
119 9 my @notes = ('A'..'G');
120 9 my @nums = (2,1,2,2,1,2,2);
121  
122 9 $keynote =~ s/^[a-z]/\u$&/;
123 9 ▶ $keypref='' unless defined $keypref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keypref 9 0 9
124 9 my $keynum = _note_to_num(uc($keynote));
125 9 $mode = _get_mode($mode);
126 9 my @dists = get_scale_nums($mode,$descending);
127 9 ▶ @dists = reverse @dists if $descending;
Branch: 1 branch
BranchTrueFalseTotal
if $descending 2 7 9
128 9 my @scale = map {($_+$keynum-$dists[0])%12} @dists;
129 9 ▶ $keypref='b' if (!$keypref && $descending && $mode == 12); #prefer flat descending chromatic
Branch: 1 branch
BranchTrueFalseTotal
if not $keypref and $descending and $mode == 12 1 8 9
Condition83%5/6
not $keypref and $descending and $mode == 12
!ll&&!rl&&r
711
not $keypref and $descending
!ll&&!rl&&r
072
MC/DC66%2/3
not $keypref and $descending and $mode == 12
not $keypref $descending $mode == 12
Truth table75%3/4
not(A) and B and C
A= not $keyprefB= $descendingC= $mode == 12
ABCresult
0XX0
10X0
1100
1111
130  
131 9 my %num2note = (0,'A',1,'A#',2,'B',3,'C',4,'C#',5,'D',6,'D#',7,'E',8,'F',9,'F#',10,'G',11,'G#');
132 9 ▶ %num2note = (0,'A',1,'Bb',2,'B',3,'C',4,'Db',5,'D',6,'Eb',7,'E',8,'F',9,'Gb',10,'G',11,'Ab') if (($keypref eq 'b') || ($keynote =~ /.b/i));
Branch: 1 branch
BranchTrueFalseTotal
if $keypref eq "b" or $keynote =~ /.b/i 2 7 9
Condition100%3/3
$keypref eq "b" or $keynote =~ /.b/i
l!l&&r!l&&!r
117
MC/DC100%2/2
$keypref eq "b" or $keynote =~ /.b/i
$keypref eq "b" $keynote =~ /.b/i
Truth table100%3/3
A or B
A= $keypref eq "b"B= $keynote =~ /.b/i
ABresult
1X1
011
000
133 9 my @mscale = $keynote;
134 9 ▶ if (@scale > 7) { # we're not bothered by niceties, so just convert
Branch: 1 branch
BranchTrueFalseTotal
if (@scale > 7) { } 2 7 9
135 2 @mscale = map {$num2note{$_}} @scale;
136 }
137 else {
138 7 ▶ $keynote = $num2note{$keynote} if $keynote =~/^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $keynote =~ /^[0-9]+$/ 0 7 7
139 7 my $kk = $keynote; $kk =~ s/b|\#//; $kk = ord($kk) - ord('A');
140 7 foreach(0..$kk-1) {# rotate to keynote
141 27 push @notes,shift(@notes);
142 27 push @nums,shift(@nums);
143 }
144 7 push @notes,shift(@notes);
145 7 shift(@dists);
146 7 my $cu = shift(@nums);
147 7 ▶ $cu++ if ($keynote =~ /b/);
Branch: 1 branch
BranchTrueFalseTotal
if $keynote =~ /b/ 1 6 7
148 7 ▶ $cu-- if ($keynote =~ /#/);
Branch: 1 branch
BranchTrueFalseTotal
if $keynote =~ /#/ 3 4 7
149 7 foreach (@dists) {
150 36 my $m = $_ - $cu;
151 36 my $ns = shift(@nums);
152 36 push @nums,$ns;
153 36 my $n = shift(@notes);
154 36 push @notes,$n;
155 36 ▶ while (abs($m) > 2 || (@scale < 7 && abs($m) >= $ns)) { # step up/down notes, 'reducing' flats/sharps
Branch: 1 branch
BranchTrueFalseTotal
if (abs $m > 2 or @scale < 7 and abs $m >= $ns) 6 36 42
Condition83%5/6
abs $m > 2 or @scale < 7 and abs $m >= $ns
l!l&&r!l&&!r
0636
@scale < 7 and abs $m >= $ns
!ll&&!rl&&r
24126
MC/DC66%2/3
abs $m > 2 or @scale < 7 and abs $m >= $ns
abs $m > 2 @scale < 7 abs $m >= $ns
Truth table75%3/4
A or B and C
A= abs $m > 2B= @scale < 7C= abs $m >= $ns
ABCresult
1XX1
0111
00X0
0100
156 6 $n = shift(@notes); push @notes,$n;
157 6 ▶ if ($m > 0) {$m -= $ns;$cu += $ns }
Branch: 2 branches
BranchTrueFalseTotal
if ($m > 0) { } 6 0 6
elsif ($m < 0) { } 0 0 0
158 0 elsif ($m < 0){$m += $ns;$cu -= $ns}
159 6 $ns = shift(@nums); push @nums,$ns;
160 }
161 36 ▶ $n .= '#' x $m if ($m > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $m > 0 7 29 36
162 36 ▶ $n .= 'b' x abs($m) if ($m < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $m < 0 4 32 36
163 36 push @mscale,$n;
164 36 $cu += $ns;
165 }
166 }
167 9 ▶ if ($descending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descending) 2 7 9
168 2 @mscale = reverse @mscale;
169 2 unshift @mscale,pop(@mscale);
170 }
171 9 @mscale;
172 }
173  
174  
175 6 ▶sub is_scale {
Subroutine is_scale: called CC=2 SCAR=6.9
Pod documented
176 6 my $name = shift();
177 6 $name =~ s/[^a-zA-Z0-9]//g;
178 6 ▶ return exists $modes{lc $name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $Music::Scales::modes{lc $name} ? : 5 1 6
179 }
180  
181 4 ▶sub _scale_to_PDL {
Subroutine _scale_to_PDL: called CC=6 SCAR=17.9
182 4 my ($octave,@scale)=@_;
183 4 my @result;
184 my $descending;
185 4 my $n1 = _note_to_num($scale[0]);
186 4 my $n2 = _note_to_num($scale[1]);
187 4 ▶ if ($n2 < $n1 && ($n1-$n2 < 5)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n2 < $n1 and $n1 - $n2 < 5) 1 3 4
Condition100%3/3
$n2 < $n1 and $n1 - $n2 < 5
!ll&&!rl&&r
121
MC/DC100%2/2
$n2 < $n1 and $n1 - $n2 < 5
$n2 < $n1 $n1 - $n2 < 5
Truth table100%3/3
A and B
A= $n2 < $n1B= $n1 - $n2 < 5
ABresult
0X0
100
111
188 1 $descending = 1;
189 1 @scale = reverse @scale;
190 }
191 4 my $last = (_note_to_num($scale[0]) + 9) % 12;
192 4 foreach (@scale) {
193 22 my $n = (_note_to_num($_) + 9) % 12;
194 22 ▶ $octave++ if ($last > $n); #switched over octave at 'c'
Branch: 1 branch
BranchTrueFalseTotal
if $last > $n 3 19 22
195 22 s/\#/s/g;
196 22 s/b/f/g;
197 22 push @result,lc($_).$octave;
198 22 $last = $n;
199 }
200 4 ▶ @result = reverse @result if $descending;
Branch: 1 branch
BranchTrueFalseTotal
if $descending 1 3 4
201 4 @result;
202 }
203  
204 7 1;
205  
206 __END__
↑ Summary