blib/lib/Music/Duration/Partition.pm

filter: Statementstmt 94.2% 114 / 121 Branchbran 78.5% 22 / 28 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 96.1% 25 / 26 Podpod 100% 3 / 3 totaltotal 89.5% 171 / 191 CC 20 SCAR 16.7
CC
20
Cov
90%
motif
14.1
add_to_score
6.0
__ANON__
3.0
CRAP 5.3
↑ Summary
1 package Music::Duration::Partition;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Partition a musical duration into rhythmic phrases
5  
6 2 our $VERSION = '0.0823';
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 List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Math::Random::Discrete ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use MIDI::Util qw(midi_dump reverse_dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Music::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use constant TICKS => 96;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 has size => (
20 is => 'ro',
21 12 ▶ default => sub { 4 },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 );
23  
24  
25 has pool => (
26 is => 'ro',
27 15 ▶ isa => sub { die 'Empty pool not allowed' unless ref( $_[0] ) eq 'ARRAY' && @{ $_[0] } > 0 },
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" and @{$_[0];} > 0 1 14 15
Condition66%2/3
ref $_[0] eq "ARRAY" and @{$_[0];} > 0
!ll&&!rl&&r
0114
MC/DC50%1/2
ref $_[0] eq "ARRAY" and @{$_[0];} > 0
ref $_[0] eq "ARRAY" @{$_[0];} > 0
Truth table66%2/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];} > 0
ABresult
0X0
100
111
28 1 ▶ default => sub { [ keys %{ midi_dump('length') } ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 2 );
30  
31 2 has _min_size => (
32 is => 'ro',
33 builder => 1,
34 lazy => 1,
35 );
36  
37 12 ▶sub _build__min_size {
Subroutine _build__min_size: called CC=1 SCAR=0.0
38 12 my ($self) = @_;
39 12 my @sizes = map { $self->_duration($_) } @{ $self->pool };
40 12 return min(@sizes);
41 }
42  
43 2 has _mrd => (
44 is => 'ro',
45 builder => 1,
46 lazy => 1,
47 );
48  
49 12 ▶sub _build__mrd {
Subroutine _build__mrd: called CC=2 SCAR=6.9
50 12 my ($self) = @_;
51 die 'Sizes of weights and pool not equal'
52 12 ▶ unless @{ $self->weights } == @{ $self->pool };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->weights;} == @{$self->pool;} 1 11 12
53 11 return Math::Random::Discrete->new($self->weights, $self->pool);
54 }
55  
56  
57 2 has pool_select => (
58 is => 'rw',
59 builder => 1,
60 lazy => 1,
61 );
62  
63 12 ▶sub _build_pool_select {
Subroutine _build_pool_select: called CC=1 SCAR=0.0
64 12 my ($self) = @_;
65 12 ▶ return sub { $self->_mrd->rand };
Subroutine __ANON__: called CC=1 SCAR=0.0
66 };
67  
68  
69 2 has weights => (
70 is => 'ro',
71 builder => 1,
72 lazy => 1,
73 );
74  
75 9 ▶sub _build_weights {
Subroutine _build_weights: called CC=1 SCAR=0.0
76 9 my ($self) = @_;
77 # Equal probability for all pool members
78 9 return [ (1) x @{ $self->pool } ];
79 }
80  
81  
82 2 has groups => (
83 is => 'ro',
84 builder => 1,
85 lazy => 1,
86 );
87  
88 12 ▶sub _build_groups {
Subroutine _build_groups: called CC=1 SCAR=0.0
89 12 my ($self) = @_;
90 12 return [ (0) x @{ $self->pool } ];
91 }
92  
93 2 has _pool_group => (
94 is => 'ro',
95 builder => 1,
96 lazy => 1,
97 );
98  
99 12 ▶sub _build__pool_group {
Subroutine _build__pool_group: called CC=2 SCAR=6.9
100 12 my ($self) = @_;
101 12 my %pool_group;
102 12 for my $i (0 .. @{ $self->pool } - 1) {
103 15 $pool_group{ $self->pool->[$i] } = $self->groups->[$i];
104 }
105 12 return \%pool_group;
106 }
107  
108  
109 has remainder => (
110 is => 'ro',
111 14 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 2 );
113  
114  
115 has abbreviation => (
116 is => 'ro',
117 14 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 2 );
119  
120  
121 has verbose => (
122 is => 'ro',
123 14 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 2 );
125  
126 # hash reference of duration lengths (keyed by duration name)
127 has _durations => (
128 is => 'ro',
129 15 ▶ default => sub { midi_dump('length') },
Subroutine __ANON__: called CC=1 SCAR=0.0
130 2 );
131  
132  
133 15 ▶sub motif {
Subroutine motif: called CC=13 SCAR=26.5
Pod documented
134 15 my ($self) = @_;
135  
136 15 my $motif = [];
137  
138 15 my $format = '%.4f';
139  
140 15 my $sum = 0;
141 15 my $group_num = 0;
142 15 my $group_name = '';
143  
144 15 ▶ while ( $sum < $self->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum < $self->size) 72 10 82
145 72 my $name = $self->pool_select->($self); # Chooses a note duration
146  
147 # Compute grouping
148 71 ▶ if ($group_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_num) { } 0 71 71
149 0 $group_num--;
150 0 $name = $group_name;
151 }
152 else {
153 71 ▶ if ($self->_pool_group->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pool_group->{$name}) { } 0 71 71
154 0 $group_num = $self->_pool_group->{$name} - 1;
155 0 $group_name = $name;
156 }
157 else {
158 71 $group_num = 0;
159 71 $group_name = '';
160 }
161 }
162  
163 71 my $size = $self->_duration($name); # Get the duration of the note
164 71 my $diff = $self->size - $sum; # How much is left?
165  
166 # The difference is less than the min_size
167 71 ▶ if (sprintf( $format, $diff ) < sprintf( $format, $self->_min_size )) {
Branch: 1 branch
BranchTrueFalseTotal
if (sprintf($format, $diff) < sprintf($format, $self->_min_size)) 4 67 71
168 4 ▶ warn "WARNING: Leftover duration: $diff\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
169 if $self->verbose;
170 4 my $tick_diff = TICKS * $diff;
171 4 my $dura = 'd' . sprintf('%.0f', $tick_diff);
172 4 ▶ if ($self->abbreviation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->abbreviation) 3 1 4
173 3 my $length = reverse_dump('length');
174 3 my %length = map { TICKS * $_ => $length->{$_} } keys %$length;
175 $dura = $length{$tick_diff}
176 3 ▶ if exists $length{$tick_diff};
Branch: 1 branch
BranchTrueFalseTotal
if exists $length{$tick_diff} 1 2 3
177 }
178 4 ▶ push @$motif, $dura
Branch: 1 branch
BranchTrueFalseTotal
if $self->remainder and sprintf($format, $tick_diff) > 0 2 2 4
Condition66%2/3
$self->remainder and sprintf($format, $tick_diff) > 0
!ll&&!rl&&r
022
MC/DC50%1/2
$self->remainder and sprintf($format, $tick_diff) > 0
$self->remainder sprintf($format, $tick_diff) > 0
Truth table66%2/3
A and B
A= $self->remainderB= sprintf($format, $tick_diff) > 0
ABresult
0X0
100
111
179 if $self->remainder && sprintf($format, $tick_diff) > 0;
180 4 last;
181 }
182  
183 # The note duration is greater than the difference
184 next
185 67 ▶ if sprintf( $format, $size ) > sprintf( $format, $diff );
Branch: 1 branch
BranchTrueFalseTotal
if sprintf($format, $size) > sprintf($format, $diff) 0 67 67
186  
187 # Increment the sum by the note duration
188 67 $sum += $size;
189  
190 67 ▶ warn(__PACKAGE__,' ',__LINE__," $name, $size, $sum\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 67 67
191 if $self->verbose;
192  
193 # Add the note to the motif if the sum is less than the total duration size
194 67 ▶ push @$motif, $name
Branch: 1 branch
BranchTrueFalseTotal
if $sum <= $self->size 67 0 67
195 if $sum <= $self->size;
196 }
197  
198 14 return $motif;
199 }
200  
201  
202 1 ▶sub motifs {
Subroutine motifs: called CC=2 SCAR=7.0
Pod documented
203 1 my ($self, $n) = @_;
204 1 ▶ $n ||= 1;
Condition50%1/2
$n ||= 1
l!l
10
MC/DC0%0/1
$n ||= 1
$n
Truth table50%1/2
A
A= $n
Aresult
11
00
205 1 my @motifs = map { $self->motif } 1 .. $n;
206 1 return @motifs;
207 }
208  
209  
210 0 ▶sub add_to_score {
Subroutine add_to_score: not called CC=2 SCAR=17.9
Pod documented
211 0 my ($self, $score, $motif, $pitches) = @_;
212 0 for my $i (0 .. $#$motif) {
213 0 $score->n($motif->[$i], $pitches->[$i]);
214 }
215 }
216  
217 86 ▶sub _duration {
Subroutine _duration: called CC=2 SCAR=6.9
218 86 my ( $self, $name ) = @_;
219  
220 86 my $dura;
221  
222 86 ▶ if ($name =~ /^d(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^d(\d+)$/) { } 3 83 86
223 3 $dura = $1;
224 }
225 else {
226 83 $dura = $self->_durations->{$name};
227 }
228  
229 86 return $dura;
230 }
231  
232 2 1;
233  
234 __END__
↑ Summary