blib/lib/Music/VoiceGen.pm

filter: Statementstmt 100% 124 / 124 Branchbran 72.7% 32 / 44 Conditioncond 38.8% 14 / 36 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 100% 25 / 25 Podpod 80.0% 4 / 5 totaltotal 78.4% 200 / 255 CC 42 SCAR 20.7
CC
42
Cov
83%
BUILD
20.4
rand
7.0
update
6.1
CRAP 7.9
↑ Summary
1 # -*- Perl -*-
2 #
3 # Musical voice generation.
4 #
5 # Run perldoc(1) on this file for additional documentation.
6  
7 package Music::VoiceGen;
8  
9 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Math::Random::Discrete;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 our $VERSION = '0.02';
21  
22 2 has _choices => ( is => 'rwp', );
23 has _context => (
24 is => 'rwp',
25 clearer => 'clear_context',
26 27 ▶ coerce => sub { ref $_[0] eq 'ARRAY' ? $_[0] : \@_ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 27 0 27
27 9 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 );
29 has contextfn => (
30 is => 'rw',
31 10 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
32 10 ▶ die "context function must be a code ref"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and ref $_[0] eq "CODE" 0 10 10
Condition33%1/3
defined $_[0] and ref $_[0] eq "CODE"
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "CODE"
defined $_[0] ref $_[0] eq "CODE"
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
33 unless defined $_[0] and ref $_[0] eq 'CODE';
34 },
35 9 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 10 ▶ sub { $_[1]->rand, 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
37 9 },
38 2 );
39 2 has intervals => ( is => 'rwp', );
40 has MAX_CONTEXT => (
41 is => 'rw',
42 9 ▶ coerce => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
43 9 ▶ croak "MAX_CONTEXT must be positive integer"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] && &looks_like_number($_[0]) or $_[0] < 1 0 9 9
Condition33%2/6
not defined $_[0] && &looks_like_number($_[0]) or $_[0] < 1
l!l&&r!l&&!r
009
defined $_[0] && &looks_like_number($_[0])
!ll&&!rl&&r
009
MC/DC0%0/3
not defined $_[0] && &looks_like_number($_[0]) or $_[0] < 1
defined $_[0] &looks_like_number($_[0]) $_[0] < 1
Truth table25%1/4
not(A && B) or C
A= defined $_[0]B= &looks_like_number($_[0])C= $_[0] < 1
ABCresult
0XX1
10X1
1111
1100
44 if !defined $_[0]
45 or !looks_like_number $_[0]
46 or $_[0] < 1;
47 9 int $_[0];
48 },
49 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 6 1;
51 },
52 2 );
53 2 has pitches => ( is => 'rwp', );
54 # NOTE use the ->update method to set these after ->new
55 2 has possibles => ( is => 'rwp', );
56 has startfn => (
57 is => 'rw',
58 10 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
59 10 ▶ die "start function must be a code ref"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and ref $_[0] eq "CODE" 0 10 10
Condition33%1/3
defined $_[0] and ref $_[0] eq "CODE"
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "CODE"
defined $_[0] ref $_[0] eq "CODE"
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
60 unless defined $_[0] and ref $_[0] eq 'CODE';
61 },
62 8 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 2 $_[0]->[ CORE::rand @{ $_[0] } ];
65 8 };
66 },
67 2 );
68  
69 9 ▶sub BUILD {
Subroutine BUILD: called CC=16 SCAR=30.2
Pod undocumented
70 9 my ( $self, $param ) = @_;
71  
72 9 ▶ if ( exists $param->{pitches} and exists $param->{intervals} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param->{'pitches'} and exists $param->{'intervals'}) { } 3 6 9
elsif (exists $param->{'possibles'}) { } 5 1 6
Condition66%2/3
exists $param->{"pitches"} and exists $param->{"intervals"}
!ll&&!rl&&r
603
MC/DC50%1/2
exists $param->{"pitches"} and exists $param->{"intervals"}
exists $param->{"pitches"} exists $param->{"intervals"}
Truth table66%2/3
A and B
A= exists $param->{"pitches"}B= exists $param->{"intervals"}
ABresult
0X0
100
111
73 croak "have no pitches to work with"
74 if !defined $param->{pitches}
75 or ref $param->{pitches} ne 'ARRAY'
76 3 ▶ or !@{ $param->{pitches} };
Branch: 1 branch
BranchTrueFalseTotal
if not defined $param->{"pitches"} or ref $param->{"pitches"} ne "ARRAY" or not @{$param->{"pitches"};} 0 3 3
Condition33%2/6
not defined $param->{"pitches"} or ref $param->{"pitches"} ne "ARRAY" or not @{$param->{"pitches"};}
l!l&&r!l&&!r
003
not defined $param->{"pitches"} or ref $param->{"pitches"} ne "ARRAY"
l!l&&r!l&&!r
003
MC/DC0%0/3
not defined $param->{"pitches"} or ref $param->{"pitches"} ne "ARRAY" or not @{$param->{"pitches"};}
not defined $param->{"pitches"} ref $param->{"pitches"} ne "ARRAY" not @{$param->{"pitches"};}
Truth table25%1/4
not(A) or B or not(C)
A= not defined $param->{"pitches"}B= ref $param->{"pitches"} ne "ARRAY"C= not @{$param->{"pitches"};}
ABCresult
1XX1
01X1
0011
0000
77 croak "have no intervals to work with"
78 if !defined $param->{intervals}
79 or ref $param->{intervals} ne 'ARRAY'
80 3 ▶ or !@{ $param->{intervals} };
Branch: 1 branch
BranchTrueFalseTotal
if not defined $param->{"intervals"} or ref $param->{"intervals"} ne "ARRAY" or not @{$param->{"intervals"};} 0 3 3
Condition33%2/6
not defined $param->{"intervals"} or ref $param->{"intervals"} ne "ARRAY" or not @{$param->{"intervals"};}
l!l&&r!l&&!r
003
not defined $param->{"intervals"} or ref $param->{"intervals"} ne "ARRAY"
l!l&&r!l&&!r
003
MC/DC0%0/3
not defined $param->{"intervals"} or ref $param->{"intervals"} ne "ARRAY" or not @{$param->{"intervals"};}
not defined $param->{"intervals"} ref $param->{"intervals"} ne "ARRAY" not @{$param->{"intervals"};}
Truth table25%1/4
not(A) or B or not(C)
A= not defined $param->{"intervals"}B= ref $param->{"intervals"} ne "ARRAY"C= not @{$param->{"intervals"};}
ABCresult
1XX1
01X1
0011
0000
81  
82 3 ▶ my $weightfn = $param->{weightfn} // sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$param->{"weightfn"} // sub { 1; }
l!l&&r!l&&!r
120
MC/DC0%0/2
$param->{"weightfn"} // sub { 1; }
$param->{"weightfn"} sub { 1; }
Truth table66%2/3
A // B
A= $param->{"weightfn"}B= sub { 1; }
ABresult
1X1
011
000
83  
84 3 my ( %allowed_pitches, %allowed_intervals );
85 3 @allowed_pitches{ map int, @{ $param->{pitches} } } = ();
86 3 @allowed_intervals{ map int, @{ $param->{intervals} } } = ();
87  
88 3 for my $pitch ( keys %allowed_pitches ) {
89 106 for my $interval ( keys %allowed_intervals ) {
90 118 my $newpitch = $pitch + $interval;
91 118 ▶ if ( exists $allowed_pitches{$newpitch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $allowed_pitches{$newpitch}) 106 12 118
92 106 $param->{possibles}{$pitch}{$newpitch} =
93 $weightfn->( $pitch, $newpitch, $interval );
94 }
95 }
96 }
97 3 $self->_set_intervals( $param->{intervals} );
98 3 $self->_set_pitches( $param->{pitches} );
99  
100 } elsif ( exists $param->{possibles} ) {
101 croak "possibles must be hash reference"
102 if !defined $param->{possibles}
103 5 ▶ or ref $param->{possibles} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $param->{"possibles"} or ref $param->{"possibles"} ne "HASH" 0 5 5
Condition33%1/3
not defined $param->{"possibles"} or ref $param->{"possibles"} ne "HASH"
l!l&&r!l&&!r
005
MC/DC0%0/2
not defined $param->{"possibles"} or ref $param->{"possibles"} ne "HASH"
not defined $param->{"possibles"} ref $param->{"possibles"} ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined $param->{"possibles"}B= ref $param->{"possibles"} ne "HASH"
ABresult
1X1
011
000
104 5 $self->_set_intervals( [] );
105 5 $self->_set_pitches( [] );
106 } else {
107 1 croak "need 'pitches' and 'intervals' or 'possibles'";
108 }
109  
110 8 $self->update( $param->{possibles}, preserve_pitches => 1 );
111 }
112  
113 44 ▶sub context {
Subroutine context: called CC=4 SCAR=13.9
Pod documented
114 44 my ( $self, $context ) = @_;
115 44 ▶ return $self->_context if !defined $context;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 26 18 44
116 18 ▶ $context = [ @_[ 1 .. $#_ ] ] if ref $context ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $context ne "ARRAY" 1 17 18
117 18 my $mc = $self->MAX_CONTEXT;
118 18 ▶ if ( @$context > $mc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$context > $mc) 6 12 18
119 6 @$context = @$context[ -$mc .. -1 ];
120 }
121 18 $self->_set__context($context);
122 18 return $self;
123 }
124  
125 17 ▶sub rand {
Subroutine rand: called CC=7 SCAR=19.5
Pod documented
126 17 my ($self) = @_;
127 17 my $choices = $self->_choices;
128 17 my $choice;
129 17 my $context = $self->context;
130 17 ▶ if ( !@$context ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$context) { } 4 13 17
131 4 my @possibles = keys %{ $self->possibles };
132 4 ▶ croak "no keys in possibles" if !@possibles;
Branch: 1 branch
BranchTrueFalseTotal
unless @possibles 0 4 4
133 4 $choice = $self->startfn->( \@possibles );
134 } else {
135 13 my $count = 1;
136 13 for my $i ( 0 .. $#$context ) {
137 23 my $key = join ".", @$context[ $i .. $#$context ];
138 23 ▶ if ( exists $choices->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $choices->{$key}) 16 7 23
139 ( $choice, my $abort ) =
140 16 $self->contextfn->( $choice, $choices->{$key}, $count );
141 16 ▶ last if $abort;
Branch: 1 branch
BranchTrueFalseTotal
if $abort 10 6 16
142 6 $count++;
143 }
144 }
145 }
146  
147 # see "Known Issues" in docs for ideas on how to workaround
148 16 ▶ croak "could not find a choice" if !defined $choice;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $choice 0 16 16
149  
150 16 push @$context, $choice;
151 16 $self->context($context);
152  
153 16 return $choice;
154 }
155  
156 1 ▶sub subsets {
Subroutine subsets: called CC=5 SCAR=16.4
Pod documented
157 1 my ( $self, $min, $max, $fn, $list ) = @_;
158 1 ▶ croak "subsets needs min,max,coderef,list" if @_ < 5;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 5 0 1 1
159 1 ▶ $list = [ @_[ 4 .. $#_ ] ] if ref $list ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $list ne "ARRAY" 0 1 1
160 1 for my $lo ( 0 .. @$list - $min ) {
161 4 for my $hi ( $lo + $min - 1 .. min( $lo + $max - 1, $#$list ) ) {
162 9 $fn->( @$list[ $lo .. $hi ] );
163 }
164 }
165 1 return $self;
166 }
167  
168 11 ▶sub update {
Subroutine update: called CC=6 SCAR=18.0
Pod documented
169 11 my ( $self, $possibles, %param ) = @_;
170  
171 11 ▶ croak "possibles must be hash reference"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $possibles or ref $possibles ne "HASH" 0 11 11
Condition33%1/3
not defined $possibles or ref $possibles ne "HASH"
l!l&&r!l&&!r
0011
MC/DC0%0/2
not defined $possibles or ref $possibles ne "HASH"
not defined $possibles ref $possibles ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined $possiblesB= ref $possibles ne "HASH"
ABresult
1X1
011
000
172 if !defined $possibles
173 or ref $possibles ne 'HASH';
174  
175 11 $self->_set_possibles($possibles);
176  
177 11 my %choices;
178 11 for my $fromval ( keys %$possibles ) {
179 123 my ( @choices, @weights );
180 123 for my $toval ( keys %{ $possibles->{$fromval} } ) {
181 126 push @choices, $toval;
182 126 push @weights, $possibles->{$fromval}{$toval};
183 }
184 123 $choices{$fromval} = Math::Random::Discrete->new( \@weights, \@choices );
185 }
186 11 $self->_set__choices( \%choices );
187  
188 11 ▶ unless ( $param{preserve_pitches} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param{"preserve_pitches"}) 2 9 11
189 2 $self->_set_intervals( [] );
190 2 $self->_set_pitches( [] );
191 }
192  
193 11 return $self;
194 }
195  
196 2 1;
197 __END__
↑ Summary