Factory.pm

filter: Statementstmt 100% 65 / 65 Branchbran 100% 8 / 8 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 100% 86 / 86 CC 7 SCAR 13.9
CC
7
Cov
100%
update
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.0
« Factory/ConfigData.pm ↑ Summary
1 # -*- Perl -*-
2 #
3 # some factory class for your music factory class
4  
5 package Music::Factory;
6 3 our $VERSION = '0.01';
7 3 ▶use 5.26.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Object::Pad 0.66;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # assembly line calls a generator object to fill an array of events up
12 # to a particular duration (musical section)
13 class Music::Factory::AssemblyLine {
14 3 field $events :param; # MIDI events (array ref)
15 3 field $gen :param; # generator object
16 3 field $maxlen :param; # maxiumum duration to generate
17  
18 7 ▶ method update ( $length = $maxlen ) {
Subroutine update: called CC=7 SCAR=19.5
19 7 $gen->reset; # NOTE may be removed
20 7 my ( $epoch, $done, $overflow ) = ( 0, 0 );
21 7 while (1) {
22 9 my ( $dur, $elist ) = $gen->update( $epoch, $length );
23 8 $epoch += $dur;
24 8 ▶ if ( $epoch > $length ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($epoch > $length) { } 1 7 8
elsif ($epoch == $length) { } 5 2 7
25 # kick overflows back to the caller to figure out, if
26 # they care. generators can also do calculations to
27 # avoid this situation, if they care
28 1 $epoch -= $dur;
29 1 $overflow = [ $dur, $elist ];
30 1 last;
31 } elsif ( $epoch == $length ) {
32 5 $done = 1;
33 }
34 7 ▶ push $events->@*, $elist->@* if defined $elist and $elist->@*;
Branch: 1 branch
BranchTrueFalseTotal
if defined $elist and @$elist 5 2 7
Condition100%3/3
defined $elist and @$elist
!ll&&!rl&&r
115
MC/DC100%2/2
defined $elist and @$elist
defined $elist @$elist
Truth table100%3/3
A and B
A= defined $elistB= @$elist
ABresult
0X0
100
111
35 7 ▶ last if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 5 2 7
36 }
37 6 return $epoch, $events, $overflow;
38 3 }
39 }
40  
41 # generator objects are called by an assembly line and generate events
42 # (an array reference) of some length (hopefully of the same duration as
43 # the events generated)
44 3 class Music::Factory::Generator {
45 # NOTE reset() might be gotten rid of as a generator update() can
46 # probably run similar code when $epoch == 0 but that would make the
47 # update function a bit more complicated. if reset is common it
48 # probably should be a distinct method, otherwise not?
49 8 ▶ method reset () { }
Subroutine reset: called CC=1 SCAR=0.0
50  
51 1 ▶ method update ( $epoch, $maxlen ) {
Subroutine update: called CC=1 SCAR=0.0
52 1 die "need a better implementation";
53 3 }
54 }
55  
56 # a note of a particular duration; the pitch and velocity are done by
57 # function callbacks
58 3 class Music::Factory::Note :isa(Music::Factory::Generator) {
59 field $chan :param = 0; # MIDI channel
60 3 field $pitch :param; # callback
61 3 field $velo :param; # callback
62 field $duration :param = 96;
63  
64 4 ▶ method update ( $epoch, $length ) {
Subroutine update: called CC=1 SCAR=0.0
65 4 my $dur = $duration;
66 # how to truncate to teh remaining length instead of
67 # overflowing. whether this makes sense depends on what a
68 # generator is doing. for example, if $dur is "too small" one
69 # may instead want to insert silence of that amount
70 #my $when = $epoch + $dur;
71 #if ( $when > $length ) {
72 # $dur -= $when - $length;
73 # return $dur, [ [ note_off => $dur, $chan, 0, 0 ] ];
74 #}
75 4 my $p = $pitch->($epoch);
76 4 return $duration,
77 [ [ note_on => 0, $chan, $p, $velo->($epoch) ],
78 [ note_off => $dur, $chan, $p, 0 ],
79 ];
80 3 }
81 }
82  
83 # silence to fill the maximum duration given
84 3 class Music::Factory::Rest :isa(Music::Factory::Generator) {
85  
86 2 ▶ method update ( $epoch, $length ) {
Subroutine update: called CC=1 SCAR=0.0
87 2 return $length, [ [ marker => $length, 'silence' ] ];
88 3 }
89 }
90  
91 3 1;
92 __END__
« Factory/ConfigData.pm ↑ Summary