blib/lib/Music/RecRhythm.pm

filter: Statementstmt 100% 93 / 93 Branchbran 100% 24 / 24 Conditioncond 75.0% 9 / 12 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 20 / 20 Podpod 83.3% 5 / 6 totaltotal 96.2% 155 / 161 CC 21 SCAR 13.0
CC
21
Cov
98%
validate_set
8.2
_recurse
6.0
__ANON__
3.0
CRAP 3.7
↑ Summary
1 # -*- Perl -*-
2 #
3 # rhythms within rhythms within rhythms
4 #
5 # Run perldoc(1) on this file for additional documentation.
6  
7 package Music::RecRhythm;
8  
9 3 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use List::Util 1.26 qw(sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 our $VERSION = '0.05';
20  
21 3 with 'MooX::Rebuild'; # for ->rebuild a.k.a. clone
22  
23 3 has extra => ( is => 'rw' );
24  
25 has next => (
26 is => 'rw',
27 11 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 11 my ( $self, $next ) = @_;
29 11 $next->prev($self);
30 },
31 3 );
32 3 has prev => ( is => 'rw', weak_ref => 1 );
33  
34 has set => (
35 is => 'rw',
36 21 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
37 21 my ($set) = @_;
38 21 ▶ die "need a set of positive integers"
Branch: 1 branch
BranchTrueFalseTotal
unless "Music::RecRhythm"->validate_set($set) 1 20 21
39 if !Music::RecRhythm->validate_set($set);
40 20 for my $n (@$set) {
41 60 $n = int $n;
42 }
43 20 return $set;
44 },
45 20 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 20 my ( $self, $set ) = @_;
47 20 $self->_set_count( scalar @$set );
48 20 $self->_set_sum( sum0(@$set) );
49 },
50 3 );
51 3 has count => ( is => 'rwp' );
52 3 has sum => ( is => 'rwp' );
53  
54 # flag to skip the callback (though the rhythm will still be present in
55 # the recursion)
56 has is_silent => (
57 is => 'rw',
58 18 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 26 ▶ coerce => sub { $_[0] ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 6 20 26
60 3 );
61  
62 21 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
63 21 my ( $self, $param ) = @_;
64 21 ▶ die "need a set of positive integers" if !exists $param->{set};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $param->{"set"} 1 20 21
65 }
66  
67 ########################################################################
68 #
69 # METHODS
70  
71 5 ▶sub audible_levels {
Subroutine audible_levels: called CC=3 SCAR=11.0
Pod documented
72 5 my ($self) = @_;
73 5 my $count = 0;
74 5 ▶ while ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 13 5 18
75 13 ▶ $count++ unless $self->is_silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_silent 10 3 13
76 13 $self = $self->next;
77 }
78 5 return $count;
79 }
80  
81 11 ▶sub beatfactor {
Subroutine beatfactor: called CC=2 SCAR=6.9
Pod documented
82 11 my ($self) = @_;
83 11 my %factors;
84 11 my $prev_sum = 1;
85 11 ▶ while ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 29 11 40
86 29 my $sum = $self->sum * $prev_sum;
87 29 @factors{ @{ $self->set }, $sum } = ();
88 29 $self = $self->next;
89 29 $prev_sum = $sum;
90 }
91 11 return Math::BigInt->bone()->blcm( keys %factors )->numify;
92 }
93  
94 5 ▶sub levels {
Subroutine levels: called CC=2 SCAR=6.9
Pod documented
95 5 my ($self) = @_;
96 5 my $count = 0;
97 5 ▶ while ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 13 5 18
98 13 $count++;
99 13 $self = $self->next;
100 }
101 5 return $count;
102 }
103  
104 6 ▶sub recurse {
Subroutine recurse: called CC=1 SCAR=0.0
Pod documented
105 6 my ( $self, $callback, $extra ) = @_;
106 6 my $bf = $self->beatfactor;
107 6 _recurse( $self, $callback, $extra, $bf, 0, 0 );
108 }
109  
110 78 ▶sub _recurse {
Subroutine _recurse: called CC=6 SCAR=17.9
111 78 my ( $rset, $callback, $extra, $totaltime, $level, $audible_level, @beats ) =
112 @_;
113 78 my %param = ( level => $level, audible_level => $audible_level );
114 78 for my $p (qw/next set/) {
115 156 $param{$p} = $rset->$p;
116 }
117 78 my $sil = $rset->is_silent;
118 78 ▶ $audible_level++ if !$sil;
Branch: 1 branch
BranchTrueFalseTotal
unless $sil 75 3 78
119 78 my $unittime = $totaltime / $rset->sum;
120 78 for my $n ( 0 .. $#{ $param{set} } ) {
121 475 $param{beat} = $param{set}[$n];
122 475 $param{index} = $n;
123 475 $param{duration} = int( $unittime * $param{beat} );
124 475 ▶ if ( !$sil ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sil) 472 3 475
125 472 $callback->( $rset, \%param, $extra, @beats, $param{beat} );
126 }
127 475 ▶ if ( defined $param{next} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"next"}) 72 403 475
128 _recurse( $param{next}, $callback, $extra, $param{duration}, $level + 1,
129 72 $audible_level, @beats, $param{beat} );
130 }
131 }
132 }
133  
134 26 ▶sub validate_set {
Subroutine validate_set: called CC=8 SCAR=21.0
Pod documented
135 26 my ( $class, $set ) = @_;
136 26 ▶ return 0 if !defined $set or ref $set ne 'ARRAY' or !@$set;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $set or ref $set ne "ARRAY" or not @$set 4 22 26
Condition100%6/6
not defined $set or ref $set ne "ARRAY" or not @$set
l!l&&r!l&&!r
3122
not defined $set or ref $set ne "ARRAY"
l!l&&r!l&&!r
1223
MC/DC100%3/3
not defined $set or ref $set ne "ARRAY" or not @$set
not defined $set ref $set ne "ARRAY" not @$set
Truth table100%4/4
not(A) or B or not(C)
A= not defined $setB= ref $set ne "ARRAY"C= not @$set
ABCresult
1XX1
01X1
0011
0000
137 22 for my $x (@$set) {
138 64 ▶ return 0 if !defined $x or !looks_like_number $x or $x < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x && &looks_like_number($x) or $x < 1 1 63 64
Condition50%3/6
not defined $x && &looks_like_number($x) or $x < 1
l!l&&r!l&&!r
0163
defined $x && &looks_like_number($x)
!ll&&!rl&&r
0064
MC/DC33%1/3
not defined $x && &looks_like_number($x) or $x < 1
defined $x &looks_like_number($x) $x < 1
Truth table50%2/4
not(A && B) or C
A= defined $xB= &looks_like_number($x)C= $x < 1
ABCresult
0XX1
10X1
1111
1100
139 }
140 21 return 1;
141 }
142  
143 3 1;
144 __END__
↑ Summary