blib/lib/MIDI/Praxis/Variation.pm

filter: Statementstmt 99.0% 108 / 109 Branchbran 92.1% 35 / 38 Conditioncond 75.0% 18 / 24 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 20 / 20 Podpod 100% 16 / 16 totaltotal 92.7% 205 / 221 CC 35 SCAR 12.7
CC
35
Cov
94%
transposition
5.2
raugmentation
5.0
rdiminution
5.0
CRAP 3.6
↑ Summary
1 package MIDI::Praxis::Variation;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Variation techniques used in music composition
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.0607';
10  
11 2 ▶use MIDI::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our @EXPORT = qw(
16 augmentation
17 diminution
18 dur
19 inversion
20 note_name_to_number
21 note2num
22 ntup
23 original
24 notes2nums
25 raugmentation
26 rdiminution
27 retrograde
28 retrograde_inversion
29 transposition
30 tye
31 tie_durations
32 );
33 2 our %EXPORT_TAGS = (all => [qw(
34 augmentation
35 diminution
36 dur
37 inversion
38 note_name_to_number
39 note2num
40 ntup
41 original
42 notes2nums
43 raugmentation
44 rdiminution
45 retrograde
46 retrograde_inversion
47 transposition
48 tye
49 tie_durations
50 )] );
51  
52  
53 1 ▶sub note2num { note_name_to_number(@_) }
Subroutine note2num: called CC=1 SCAR=0.0
Pod documented
54  
55 52 ▶sub note_name_to_number {
Subroutine note_name_to_number: called CC=4 SCAR=13.9
Pod documented
56 52 my ($in) = @_;
57  
58 52 ▶ return () unless $in;
Branch: 1 branch
BranchTrueFalseTotal
unless $in 1 51 52
59  
60 51 my $note_number = -1;
61  
62 51 ▶ if ($in =~ /^([A-Za-z]+)(\d+)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /^([A-Za-z]+)(\d+)/s) 50 1 51
63 $note_number = $MIDI::Simple::Note{$1} + $2 * 12
64 50 ▶ if exists $MIDI::Simple::Note{$1};
Branch: 1 branch
BranchTrueFalseTotal
if exists $MIDI::Simple::Note{$1} 50 0 50
65 }
66  
67 51 return $note_number;
68 }
69  
70  
71  
72 1 ▶sub notes2nums { original(@_) }
Subroutine notes2nums: called CC=1 SCAR=0.0
Pod documented
73  
74 13 ▶sub original {
Subroutine original: called CC=2 SCAR=6.9
Pod documented
75 13 my @notes = @_;
76  
77 13 ▶ return () unless @notes;
Branch: 1 branch
BranchTrueFalseTotal
unless @notes 1 12 13
78  
79 12 my @ret = map { note_name_to_number($_) } @notes;
80  
81 12 return @ret;
82 }
83  
84  
85  
86 2 ▶sub retrograde {
Subroutine retrograde: called CC=2 SCAR=6.9
Pod documented
87 2 my @notes = @_;
88  
89 2 my @ret = ();
90  
91 2 ▶ return () unless @notes;
Branch: 1 branch
BranchTrueFalseTotal
unless @notes 1 1 2
92  
93 1 @ret = reverse original(@notes);
94  
95 1 return @ret;
96 }
97  
98  
99  
100 10 ▶sub transposition {
Subroutine transposition: called CC=5 SCAR=16.4
Pod documented
101 10 my ($delta, @notes) = @_;
102  
103 10 ▶ return () unless defined $delta && @notes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delta and @notes 1 9 10
Condition66%2/3
defined $delta and @notes
!ll&&!rl&&r
109
MC/DC50%1/2
defined $delta and @notes
defined $delta @notes
Truth table66%2/3
A and B
A= defined $deltaB= @notes
ABresult
0X0
100
111
104  
105 9 my @ret = ();
106  
107 9 ▶ if ($notes[0] =~ /[A-G]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notes[0] =~ /[A-G]/) { } 9 0 9
108 9 @ret = original(@notes);
109 }
110 else {
111 0 @ret = @notes;
112 }
113  
114 9 for (@ret) {
115 27 $_ += $delta;
116 }
117  
118 9 return @ret;
119 }
120  
121  
122  
123 7 ▶sub inversion {
Subroutine inversion: called CC=3 SCAR=11.0
Pod documented
124 7 my ($axis, @notes) = @_;
125  
126 7 ▶ return () unless $axis && @notes;
Branch: 1 branch
BranchTrueFalseTotal
unless $axis and @notes 1 6 7
Condition66%2/3
$axis and @notes
!ll&&!rl&&r
106
MC/DC50%1/2
$axis and @notes
$axis @notes
Truth table66%2/3
A and B
A= $axisB= @notes
ABresult
0X0
100
111
127  
128 6 my $center = note_name_to_number($axis);
129 6 my $first = note_name_to_number($notes[0]);
130 6 my $delta = $center - $first;
131  
132 6 my @transposed = transposition($delta, @notes);
133  
134 # XXX WTF?
135 6 my @ret = map { 2 * $center - $_ } @transposed;
136  
137 6 return @ret;
138 }
139  
140  
141  
142 4 ▶sub retrograde_inversion {
Subroutine retrograde_inversion: called CC=3 SCAR=11.0
Pod documented
143 4 my ($axis, @notes) = @_;
144  
145 4 ▶ return () unless $axis && @notes;
Branch: 1 branch
BranchTrueFalseTotal
unless $axis and @notes 1 3 4
Condition66%2/3
$axis and @notes
!ll&&!rl&&r
103
MC/DC50%1/2
$axis and @notes
$axis @notes
Truth table66%2/3
A and B
A= $axisB= @notes
ABresult
0X0
100
111
146  
147 3 my @rev_notes = ();
148 3 my @ret = ();
149  
150 3 @rev_notes = reverse @notes;
151  
152 3 @ret = inversion($axis, @rev_notes);
153  
154 3 return @ret;
155 }
156  
157  
158  
159 24 ▶sub dur {
Subroutine dur: called CC=4 SCAR=13.9
Pod documented
160 24 my ($tempo, $arg) = (MIDI::Simple::Tempo, @_);
161  
162 24 ▶ return () unless $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 1 23 24
163  
164 23 my $dur = 0;
165  
166 23 ▶ if ($arg =~ /^d(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ /^d(\d+)$/) { } 3 20 23
elsif (exists $MIDI::Simple::Length{$arg}) { } 20 0 20
167 3 $dur = 0 + $1;
168 }
169 elsif (exists $MIDI::Simple::Length{$arg}) { # length spec
170 20 $dur = 0 + ($tempo * $MIDI::Simple::Length{$arg});
171 }
172  
173 23 return $dur;
174 }
175  
176  
177  
178 1 ▶sub tie_durations { tye(@_) }
Subroutine tie_durations: called CC=1 SCAR=0.0
Pod documented
179  
180 4 ▶sub tye {
Subroutine tye: called CC=3 SCAR=11.0
Pod documented
181 4 my @dur_or_len = @_;
182  
183 4 ▶ return () unless @dur_or_len;
Branch: 1 branch
BranchTrueFalseTotal
unless @dur_or_len 1 3 4
184  
185 3 my $sum = 0;
186  
187 3 for my $dura (@dur_or_len) {
188 5 $sum += dur($dura);
189 }
190  
191 3 return $sum;
192 }
193  
194  
195  
196 9 ▶sub raugmentation {
Subroutine raugmentation: called CC=5 SCAR=16.1
Pod documented
197 9 my ($ratio, @dur_or_len) = @_;
198  
199 9 ▶ return () unless $ratio && 1 < $ratio && @dur_or_len;
Branch: 1 branch
BranchTrueFalseTotal
unless $ratio and 1 < $ratio and @dur_or_len 2 7 9
Condition83%5/6
$ratio and 1 < $ratio and @dur_or_len
!ll&&!rl&&r
207
$ratio and 1 < $ratio
!ll&&!rl&&r
117
MC/DC66%2/3
$ratio and 1 < $ratio and @dur_or_len
$ratio 1 < $ratio @dur_or_len
Truth table75%3/4
A and B and C
A= $ratioB= 1 < $ratioC= @dur_or_len
ABCresult
0XX0
10X0
1100
1111
200  
201 7 my $sum = 0;
202  
203 7 for my $dura (@dur_or_len) {
204 8 $sum += dur($dura) * $ratio;
205 }
206  
207 7 return $sum;
208 }
209  
210  
211  
212 9 ▶sub rdiminution {
Subroutine rdiminution: called CC=5 SCAR=16.1
Pod documented
213 9 my ($ratio, @dur_or_len) = @_;
214  
215 9 ▶ return () unless $ratio && 1 < $ratio && @dur_or_len;
Branch: 1 branch
BranchTrueFalseTotal
unless $ratio and 1 < $ratio and @dur_or_len 2 7 9
Condition83%5/6
$ratio and 1 < $ratio and @dur_or_len
!ll&&!rl&&r
207
$ratio and 1 < $ratio
!ll&&!rl&&r
117
MC/DC66%2/3
$ratio and 1 < $ratio and @dur_or_len
$ratio 1 < $ratio @dur_or_len
Truth table75%3/4
A and B and C
A= $ratioB= 1 < $ratioC= @dur_or_len
ABCresult
0XX0
10X0
1100
1111
216  
217 7 my $sum = 0;
218  
219 7 for my $dura (@dur_or_len) {
220 8 $sum += dur($dura) / $ratio;
221 }
222  
223 7 return sprintf '%.0f', $sum;
224 }
225  
226  
227  
228 3 ▶sub augmentation {
Subroutine augmentation: called CC=3 SCAR=11.0
Pod documented
229 3 my @dur_or_len = @_;
230  
231 3 ▶ return () unless @dur_or_len;
Branch: 1 branch
BranchTrueFalseTotal
unless @dur_or_len 1 2 3
232  
233 2 my @ret = ();
234  
235 2 for my $dura (@dur_or_len) {
236 3 my $elem = 'd';
237 3 $elem .= raugmentation(2, $dura);
238 3 push @ret, $elem;
239 }
240  
241 2 return @ret;
242 }
243  
244  
245  
246 3 ▶sub diminution {
Subroutine diminution: called CC=3 SCAR=11.0
Pod documented
247 3 my @dur_or_len = @_;
248  
249 3 ▶ return () unless @dur_or_len;
Branch: 1 branch
BranchTrueFalseTotal
unless @dur_or_len 1 2 3
250  
251 2 my @ret = ();
252  
253 2 for my $dura (@dur_or_len) {
254 3 my $elem = 'd';
255 3 $elem .= rdiminution(2, $dura);
256 3 push @ret, $elem;
257 }
258  
259 2 return @ret;
260 }
261  
262  
263  
264 5 ▶sub ntup {
Subroutine ntup: called CC=5 SCAR=16.1
Pod documented
265 5 my ($n, @notes) = @_;
266  
267 5 ▶ return () unless defined $n && @notes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and @notes 1 4 5
Condition66%2/3
defined $n and @notes
!ll&&!rl&&r
104
MC/DC50%1/2
defined $n and @notes
defined $n @notes
Truth table66%2/3
A and B
A= defined $nB= @notes
ABresult
0X0
100
111
268  
269 4 my @ret = ();
270  
271 4 ▶ if (@notes >= $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@notes >= $n) 3 1 4
272 3 for my $index (0 .. @notes - $n) {
273 7 push @ret, @notes[$index .. $index + $n - 1];
274 }
275 }
276  
277 4 return @ret;
278 }
279  
280  
281 2 1;
282  
283 __END__
↑ Summary