blib/lib/Music/MelodicDevice/Arpeggiation.pm

filter: Statementstmt 77.9% 85 / 109 Branchbran 55.5% 20 / 36 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 85.7% 24 / 28 Podpod 100% 7 / 7 totaltotal 72.5% 145 / 200 CC 27 SCAR 17.7
CC
27
Cov
71%
converge
20.0
arp
13.7
_pitch_value
6.0
CRAP 5.9
↑ Summary
1 package Music::MelodicDevice::Arpeggiation;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Apply arpeggiation patterns to groups of notes
5  
6 2 our $VERSION = '0.0504';
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Array::Circular ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dumper::Compact qw(ddc);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Music::Note ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use constant TICKS => 96;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 my $DISPATCH = {
18 5 ▶ up => sub { my ($notes) = @_; return [ 0 .. $#$notes ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 2 ▶ down => sub { my ($notes) = @_; return [ reverse(0 .. $#$notes) ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 ▶ updown => sub { my ($notes) = @_; return [ 0 .. $#$notes, reverse(0 .. $#$notes - 1) ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 0 ▶ random => sub { my ($notes) = @_; return [ map { int rand @$notes } @$notes ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 2 converge => \&converge,
23 diverge => \&diverge,
24 pedal_up => \&pedal_up,
25 pedal_down => \&pedal_down,
26 pedal_updown => \&pedal_updown,
27 };
28  
29  
30 has type => (
31 is => 'rw',
32 4 ▶ isa => sub { die "$_[0] is not a known named type" unless exists $DISPATCH->{$_[0]} },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$DISPATCH{$_[0]} 0 4 4
33 4 ▶ default => sub { 'up' },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 );
35  
36  
37 has duration => (
38 is => 'rw',
39 4 ▶ isa => sub { die "$_[0] is not a valid duration" unless $_[0] =~ /^\d+\.?(\d+)?$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\d+\.?(\d+)?$/ 0 4 4
40 4 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 2 );
42  
43  
44 has repeats => (
45 is => 'rw',
46 6 ▶ isa => sub { die "$_[0] is not a positive integer" unless $_[0] =~ /^\d+$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\d+$/ 0 6 6
47 4 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 2 );
49  
50  
51 has verbose => (
52 is => 'rw',
53 4 ▶ isa => sub { die "$_[0] is not a valid boolean" unless $_[0] =~ /^[01]$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^[01]$/ 0 4 4
54 4 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 2 );
56  
57  
58  
59 21 ▶sub arp {
Subroutine arp: called CC=11 SCAR=26.2
Pod documented
60 21 my ($self, $notes, $duration, $type, $repeats) = @_;
61  
62 21 ▶ $duration ||= $self->duration;
Condition33%1/3
$duration ||= $self->duration
l!l&&r!l&&!r
2100
MC/DC0%0/2
$duration ||= $self->duration
$duration $self->duration
Truth table33%1/3
A ||= B
A= $durationB= $self->duration
ABresult
1X1
011
000
63 21 ▶ $type ||= $self->type;
Condition33%1/3
$type ||= $self->type
l!l&&r!l&&!r
2100
MC/DC0%0/2
$type ||= $self->type
$type $self->type
Truth table33%1/3
A ||= B
A= $typeB= $self->type
ABresult
1X1
011
000
64 21 ▶ $repeats ||= $self->repeats;
Condition66%2/3
$repeats ||= $self->repeats
l!l&&r!l&&!r
1200
MC/DC0%0/2
$repeats ||= $self->repeats
$repeats $self->repeats
Truth table66%2/3
A ||= B
A= $repeatsB= $self->repeats
ABresult
1X1
011
000
65  
66 21 ▶ my $pattern = ref $type eq 'ARRAY' ? $type : $self->_build_pattern($type, $notes);
Branch: 1 branch
BranchTrueFalseTotal
ref $type eq 'ARRAY' ? : 0 21 21
67 21 ▶ print "Repeat: $repeats, Type: $type, Pattern: @$pattern\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 21 21
68  
69 21 my $pat = Array::Circular->new(@$pattern);
70  
71 # compute the arp durations
72 21 my $x = $duration * TICKS;
73 21 my $z = sprintf '%0.f', $x / @$pattern;
74 21 ▶ print "Ticks: $x, Duration: $z\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 21 21
75 21 $z = 'd' . $z;
76  
77 21 my @arp;
78 21 for my $i (1 .. $repeats) {
79 23 for my $j (1 .. @$pattern) {
80 81 ▶ push @arp, [ $z, $notes->[ $pat->current ] ]
Branch: 1 branch
BranchTrueFalseTotal
if $pat->current < @$notes 80 1 81
81 if $pat->current < @$notes;
82 81 $pat->next;
83 }
84 }
85 21 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 21 21
86 0 print 'Notes: ', ddc($notes);
87 0 print 'Arp: ', ddc(\@arp);
88 }
89 21 return \@arp;
90 }
91  
92 25 ▶sub _build_pattern {
Subroutine _build_pattern: called CC=1 SCAR=0.0
93 25 my ($self, $type, $notes) = @_;
94 25 return $self->arp_type($type)->($notes);
95 }
96  
97  
98 29 ▶sub arp_type {
Subroutine arp_type: called CC=4 SCAR=13.9
Pod documented
99 29 my ($self, $type, $coderef) = @_;
100 29 ▶ if ($type && $coderef) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type and $coderef) { } 1 28 29
elsif ($type) { } 27 1 28
Condition100%3/3
$type and $coderef
!ll&&!rl&&r
1271
MC/DC100%2/2
$type and $coderef
$type $coderef
Truth table100%3/3
A and B
A= $typeB= $coderef
ABresult
0X0
100
111
101 1 $DISPATCH->{$type} = $coderef;
102 }
103 elsif ($type) {
104 27 return $DISPATCH->{$type};
105 }
106 else {
107 1 return $DISPATCH;
108 }
109 }
110  
111  
112 0 ▶sub converge {
Subroutine converge: not called CC=4 SCAR=30.0
Pod documented
113 0 my ($pitches) = @_;
114  
115 0 my @by_pitch = sort { _pitch_value($pitches->[$a]) <=> _pitch_value($pitches->[$b]) } 0 .. $#$pitches;
116  
117 0 my ($lo, $hi) = (0, $#by_pitch);
118 0 my $take_low = 1;
119 0 my @pattern;
120  
121 0 ▶ while ($lo <= $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo <= $hi) 0 0 0
122 0 ▶ if ($lo == $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo == $hi) 0 0 0
123 0 push @pattern, $by_pitch[$lo];
124 0 last;
125 }
126 0 ▶ push @pattern, $take_low ? $by_pitch[$lo++] : $by_pitch[$hi--];
Branch: 1 branch
BranchTrueFalseTotal
$take_low ? : 0 0 0
127 0 $take_low = !$take_low;
128 }
129  
130 0 return \@pattern;
131 }
132  
133  
134 0 ▶sub diverge {
Subroutine diverge: not called CC=1 SCAR=6.9
Pod documented
135 0 my ($pitches) = @_;
136 0 return [ reverse @{ converge($pitches) } ];
137 }
138  
139  
140 8 ▶sub pedal_up {
Subroutine pedal_up: called CC=3 SCAR=11.0
Pod documented
141 8 my ($pitches) = @_;
142  
143 8 ▶ return [ 0 ] if @$pitches <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$pitches <= 1 2 6 8
144  
145 6 my @pattern;
146 6 push @pattern, 0, $_ for 1 .. $#$pitches;
147  
148 6 return \@pattern;
149 }
150  
151  
152 7 ▶sub pedal_down {
Subroutine pedal_down: called CC=3 SCAR=11.0
Pod documented
153 7 my ($pitches) = @_;
154  
155 7 ▶ return [ 0 ] if @$pitches <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$pitches <= 1 1 6 7
156  
157 6 my $top = $#$pitches;
158 6 my @pattern;
159 6 push @pattern, $top, $_ for reverse 0 .. $top - 1;
160  
161 6 return \@pattern;
162 }
163  
164  
165 4 ▶sub pedal_updown {
Subroutine pedal_updown: called CC=2 SCAR=6.9
Pod documented
166 4 my ($pitches) = @_;
167  
168 4 my $up = pedal_up($pitches);
169 4 ▶ return $up if @$up <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$up <= 1 1 3 4
170  
171 3 my $down = pedal_down($pitches);
172 3 return [ @$up, @{$down}[ 1 .. $#$down ] ];
173 }
174  
175 0 ▶sub _pitch_value {
Subroutine _pitch_value: not called CC=2 SCAR=17.9
176 0 my ($pitch) = @_;
177 0 ▶ return $pitch if $pitch =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $pitch =~ /^\d+$/ 0 0 0
178 0 return Music::Note->new($pitch, 'ISO')->format('midinum');
179 }
180  
181 2 1;
182  
183 __END__
↑ Summary