| 1 | package Music::MelodicDevice::Arpeggiation; | ||||||||||||||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Apply arpeggiation patterns to groups of notes | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.0504'; | |||||||||||||||||||||||||||||||
| 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 Array::Circular (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Data::Dumper::Compact qw(ddc); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Music::Note (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use constant TICKS => 96; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | my $DISPATCH = { | ||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | up => sub { my ($notes) = @_; return [ 0 .. $#$notes ] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | down => sub { my ($notes) = @_; return [ reverse(0 .. $#$notes) ] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | updown => sub { my ($notes) = @_; return [ 0 .. $#$notes, reverse(0 .. $#$notes - 1) ] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | random => sub { my ($notes) = @_; return [ map { int rand @$notes } @$notes ] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 22 | 2 | converge => \&converge, | |||||||||||||||||||||||||||||||
| 23 | diverge => \&diverge, | ||||||||||||||||||||||||||||||||
| 24 | pedal_up => \&pedal_up, | ||||||||||||||||||||||||||||||||
| 25 | pedal_down => \&pedal_down, | ||||||||||||||||||||||||||||||||
| 26 | pedal_updown => \&pedal_updown, | ||||||||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | has type => ( | ||||||||||||||||||||||||||||||||
| 31 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | isa => sub { die "$_[0] is not a known named type" unless exists $DISPATCH->{$_[0]} }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | default => sub { 'up' }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | 2 | ); | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | has duration => ( | ||||||||||||||||||||||||||||||||
| 38 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | isa => sub { die "$_[0] is not a valid duration" unless $_[0] =~ /^\d+\.?(\d+)?$/ }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 4 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 41 | 2 | ); | |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | has repeats => ( | ||||||||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 46 | 6 | ▶ | isa => sub { die "$_[0] is not a positive integer" unless $_[0] =~ /^\d+$/ }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 48 | 2 | ); | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | has verbose => ( | ||||||||||||||||||||||||||||||||
| 52 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | isa => sub { die "$_[0] is not a valid boolean" unless $_[0] =~ /^[01]$/ }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 55 | 2 | ); | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 21 | ▶ | sub arp { | ||||||||||||||||||||||||||||||
|
Subroutine
arp: called CC=11 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 60 | 21 | my ($self, $notes, $duration, $type, $repeats) = @_; | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 21 | ▶ | $duration ||= $self->duration; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$duration ||= $self->duration
MC/DC0%0/2
$duration ||= $self->duration
$duration
$self->duration
Truth table33%1/3
A ||= B
A= $durationB= $self->duration
| |||||||||||||||||||||||||||||||||
| 63 | 21 | ▶ | $type ||= $self->type; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$type ||= $self->type
MC/DC0%0/2
$type ||= $self->type
$type
$self->type
Truth table33%1/3
A ||= B
A= $typeB= $self->type
| |||||||||||||||||||||||||||||||||
| 64 | 21 | ▶ | $repeats ||= $self->repeats; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$repeats ||= $self->repeats
MC/DC0%0/2
$repeats ||= $self->repeats
$repeats
$self->repeats
Truth table66%2/3
A ||= B
A= $repeatsB= $self->repeats
| |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 21 | ▶ | my $pattern = ref $type eq 'ARRAY' ? $type : $self->_build_pattern($type, $notes); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 21 | ▶ | print "Repeat: $repeats, Type: $type, Pattern: @$pattern\n" if $self->verbose; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 21 | my $pat = Array::Circular->new(@$pattern); | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | # compute the arp durations | ||||||||||||||||||||||||||||||||
| 72 | 21 | my $x = $duration * TICKS; | |||||||||||||||||||||||||||||||
| 73 | 21 | my $z = sprintf '%0.f', $x / @$pattern; | |||||||||||||||||||||||||||||||
| 74 | 21 | ▶ | print "Ticks: $x, Duration: $z\n" if $self->verbose; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 21 | $z = 'd' . $z; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 21 | my @arp; | |||||||||||||||||||||||||||||||
| 78 | 21 | for my $i (1 .. $repeats) { | |||||||||||||||||||||||||||||||
| 79 | 23 | for my $j (1 .. @$pattern) { | |||||||||||||||||||||||||||||||
| 80 | 81 | ▶ | push @arp, [ $z, $notes->[ $pat->current ] ] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | if $pat->current < @$notes; | ||||||||||||||||||||||||||||||||
| 82 | 81 | $pat->next; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | 21 | ▶ | if ($self->verbose) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 0 | print 'Notes: ', ddc($notes); | |||||||||||||||||||||||||||||||
| 87 | 0 | print 'Arp: ', ddc(\@arp); | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | 21 | return \@arp; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 25 | ▶ | sub _build_pattern { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_pattern: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 93 | 25 | my ($self, $type, $notes) = @_; | |||||||||||||||||||||||||||||||
| 94 | 25 | return $self->arp_type($type)->($notes); | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 29 | ▶ | sub arp_type { | ||||||||||||||||||||||||||||||
|
Subroutine
arp_type: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 99 | 29 | my ($self, $type, $coderef) = @_; | |||||||||||||||||||||||||||||||
| 100 | 29 | ▶ | if ($type && $coderef) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$type and $coderef
MC/DC100%2/2
$type and $coderef
$type
$coderef
Truth table100%3/3
A and B
A= $typeB= $coderef
| |||||||||||||||||||||||||||||||||
| 101 | 1 | $DISPATCH->{$type} = $coderef; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | elsif ($type) { | ||||||||||||||||||||||||||||||||
| 104 | 27 | return $DISPATCH->{$type}; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||||||
| 107 | 1 | return $DISPATCH; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub converge { | ||||||||||||||||||||||||||||||
|
Subroutine
converge: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 113 | 0 | my ($pitches) = @_; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 0 | my @by_pitch = sort { _pitch_value($pitches->[$a]) <=> _pitch_value($pitches->[$b]) } 0 .. $#$pitches; | |||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 0 | my ($lo, $hi) = (0, $#by_pitch); | |||||||||||||||||||||||||||||||
| 118 | 0 | my $take_low = 1; | |||||||||||||||||||||||||||||||
| 119 | 0 | my @pattern; | |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | while ($lo <= $hi) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($lo == $hi) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 123 | 0 | push @pattern, $by_pitch[$lo]; | |||||||||||||||||||||||||||||||
| 124 | 0 | last; | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | push @pattern, $take_low ? $by_pitch[$lo++] : $by_pitch[$hi--]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | 0 | $take_low = !$take_low; | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 0 | return \@pattern; | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub diverge { | ||||||||||||||||||||||||||||||
|
Subroutine
diverge: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 135 | 0 | my ($pitches) = @_; | |||||||||||||||||||||||||||||||
| 136 | 0 | return [ reverse @{ converge($pitches) } ]; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 8 | ▶ | sub pedal_up { | ||||||||||||||||||||||||||||||
|
Subroutine
pedal_up: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 141 | 8 | my ($pitches) = @_; | |||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 8 | ▶ | return [ 0 ] if @$pitches <= 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 6 | my @pattern; | |||||||||||||||||||||||||||||||
| 146 | 6 | push @pattern, 0, $_ for 1 .. $#$pitches; | |||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 6 | return \@pattern; | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 7 | ▶ | sub pedal_down { | ||||||||||||||||||||||||||||||
|
Subroutine
pedal_down: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 153 | 7 | my ($pitches) = @_; | |||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 7 | ▶ | return [ 0 ] if @$pitches <= 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 6 | my $top = $#$pitches; | |||||||||||||||||||||||||||||||
| 158 | 6 | my @pattern; | |||||||||||||||||||||||||||||||
| 159 | 6 | push @pattern, $top, $_ for reverse 0 .. $top - 1; | |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 6 | return \@pattern; | |||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 4 | ▶ | sub pedal_updown { | ||||||||||||||||||||||||||||||
|
Subroutine
pedal_updown: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 166 | 4 | my ($pitches) = @_; | |||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 4 | my $up = pedal_up($pitches); | |||||||||||||||||||||||||||||||
| 169 | 4 | ▶ | return $up if @$up <= 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 3 | my $down = pedal_down($pitches); | |||||||||||||||||||||||||||||||
| 172 | 3 | return [ @$up, @{$down}[ 1 .. $#$down ] ]; | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | sub _pitch_value { | ||||||||||||||||||||||||||||||
|
Subroutine
_pitch_value: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 176 | 0 | my ($pitch) = @_; | |||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | return $pitch if $pitch =~ /^\d+$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 0 | return Music::Note->new($pitch, 'ISO')->format('midinum'); | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 2 | 1; | |||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||||||