| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $elist and @$elist
MC/DC100%2/2
defined $elist and @$elist
defined $elist
@$elist
Truth table100%3/3
A and B
A= defined $elistB= @$elist
| |||||||||||||||||||||||||||||
| 35 | 7 | ▶ | last if $done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||