| 1 | package Music::MelodicDevice::Ornamentation; | ||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | # ABSTRACT: Chromatic and diatonic melodic ornamentation | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.0706'; | |||||||||||||||||||
| 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 Carp qw(croak); | ||||||||||||||||||
|
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 List::SomeUtils qw(first_index); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | ▶ | use MIDI::Simple (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 2 | ▶ | use Music::Duration (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 2 | ▶ | use Music::Scales qw(get_scale_MIDI is_scale); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 2 | with('Music::PitchNum'); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 2 | ▶ | use constant TICKS => 96; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 2 | ▶ | use constant OCTAVES => 10; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | has scale_note => ( | ||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||
| 26 | 3 | ▶ | isa => sub { die "$_[0] is not a valid note" unless $_[0] =~ /^[A-G][#b]?$/ }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 2 | ▶ | default => sub { 'C' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 2 | ); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | has scale_name => ( | ||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||
| 33 | 3 | ▶ | isa => sub { die "$_[0] is not a valid scale name" unless is_scale($_[0]) }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 1 | ▶ | default => sub { 'chromatic' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 2 | ); | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 2 | has _scale => ( | |||||||||||||||||||
| 38 | is => 'lazy', | ||||||||||||||||||||
| 39 | init_args => undef, | ||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 3 | ▶ | sub _build__scale { | ||||||||||||||||||
|
Subroutine
_build__scale: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 43 | 3 | my ($self) = @_; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 3 | my @scale = map { get_scale_MIDI($self->scale_note, $_, $self->scale_name) } -1 .. OCTAVES - 1; | |||||||||||||||||||
| 46 | 3 | ▶ | print 'Scale: ', ddc(\@scale) if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 3 | return \@scale; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | has verbose => ( | ||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||
| 54 | 3 | ▶ | isa => sub { die "$_[0] is not a valid boolean" unless $_[0] =~ /^[01]$/ }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 3 | ▶ | default => sub { 0 }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | 2 | ); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 18 | ▶ | sub grace_note { | ||||||||||||||||||
|
Subroutine
grace_note: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 61 | 18 | my ($self, $duration, $pitch, $offset) = @_; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 18 | ▶ | $offset //= 1; # Default one note above | ||||||||||||||||||
|
Condition50%1/2
$offset //= 1
MC/DC0%0/1
$offset //= 1
$offset
Truth table50%1/2
A
A= $offset
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 18 | ▶ | my $named = $pitch =~ /[A-G]/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 18 | (my $i, $pitch) = $self->_find_pitch($pitch); | |||||||||||||||||||
| 68 | 18 | my $grace_note = $self->_scale->[ $i + $offset ]; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 18 | ▶ | if ($named) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 9 | $pitch = $self->pitchname($pitch); | |||||||||||||||||||
| 72 | 9 | $grace_note = $self->pitchname($grace_note); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # Compute the ornament durations | ||||||||||||||||||||
| 76 | 18 | my $x = $MIDI::Simple::Length{$duration} * TICKS; | |||||||||||||||||||
| 77 | 18 | my $y = $MIDI::Simple::Length{yn} * TICKS; # 64th note | |||||||||||||||||||
| 78 | 18 | my $z = sprintf '%0.f', $x - $y; | |||||||||||||||||||
| 79 | 18 | ▶ | print "Durations: $x, $y, $z\n" if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 18 | $y = 'd' . $y; | |||||||||||||||||||
| 81 | 18 | $z = 'd' . $z; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 18 | my @grace_note = ([$y, $grace_note], [$z, $pitch]); | |||||||||||||||||||
| 84 | 18 | ▶ | print 'Grace note: ', ddc(\@grace_note) if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 18 | return \@grace_note; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 12 | ▶ | sub turn { | ||||||||||||||||||
|
Subroutine
turn: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 91 | 12 | my ($self, $duration, $pitch, $offset) = @_; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 12 | my $number = 4; # Number of notes in the ornament | |||||||||||||||||||
| 94 | 12 | ▶ | $offset //= 1; # Default one note above | ||||||||||||||||||
|
Condition50%1/2
$offset //= 1
MC/DC0%0/1
$offset //= 1
$offset
Truth table50%1/2
A
A= $offset
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 12 | ▶ | my $named = $pitch =~ /[A-G]/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 12 | (my $i, $pitch) = $self->_find_pitch($pitch); | |||||||||||||||||||
| 99 | 12 | my $above = $self->_scale->[ $i + $offset ]; | |||||||||||||||||||
| 100 | 12 | my $below = $self->_scale->[ $i - $offset ]; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 12 | ▶ | if ($named) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 6 | $pitch = $self->pitchname($pitch); | |||||||||||||||||||
| 104 | 6 | $above = $self->pitchname($above); | |||||||||||||||||||
| 105 | 6 | $below = $self->pitchname($below); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # Compute the ornament durations | ||||||||||||||||||||
| 109 | 12 | my $x = $MIDI::Simple::Length{$duration} * TICKS; | |||||||||||||||||||
| 110 | 12 | my $z = sprintf '%0.f', $x / $number; | |||||||||||||||||||
| 111 | 12 | ▶ | print "Durations: $x, $z\n" if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 12 | $z = 'd' . $z; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 12 | my @turn = ([$z, $above], [$z, $pitch], [$z, $below], [$z, $pitch]); | |||||||||||||||||||
| 115 | 12 | ▶ | print 'Turn: ', ddc(\@turn) if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 12 | return \@turn; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 12 | ▶ | sub trill { | ||||||||||||||||||
|
Subroutine
trill: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||
| 122 | 12 | my ($self, $duration, $pitch, $number, $offset) = @_; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 12 | ▶ | $number ||= 2; # Number of notes in the ornament | ||||||||||||||||||
|
Condition50%1/2
$number ||= 2
MC/DC0%0/1
$number ||= 2
$number
Truth table50%1/2
A
A= $number
| |||||||||||||||||||||
| 125 | 12 | ▶ | $offset //= 1; # Default one note above | ||||||||||||||||||
|
Condition50%1/2
$offset //= 1
MC/DC0%0/1
$offset //= 1
$offset
Truth table50%1/2
A
A= $offset
| |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 12 | ▶ | my $named = $pitch =~ /[A-G]/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 12 | (my $i, $pitch) = $self->_find_pitch($pitch); | |||||||||||||||||||
| 130 | 12 | my $alt = $self->_scale->[ $i + $offset ]; | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 12 | ▶ | if ($named) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 6 | $pitch = $self->pitchname($pitch); | |||||||||||||||||||
| 134 | 6 | $alt = $self->pitchname($alt); | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | # Compute the ornament durations | ||||||||||||||||||||
| 138 | 12 | my $x = $MIDI::Simple::Length{$duration} * TICKS; | |||||||||||||||||||
| 139 | 12 | my $z = sprintf '%0.f', ($x / $number / 2); | |||||||||||||||||||
| 140 | 12 | ▶ | print "Durations: $x, $z\n" if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 12 | $z = 'd' . $z; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 12 | my @trill; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 12 | push @trill, [$z, $pitch], [$z, $alt] for 1 .. $number; | |||||||||||||||||||
| 146 | 12 | ▶ | print 'Trill: ', ddc(\@trill) if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 12 | return \@trill; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 12 | ▶ | sub mordent { | ||||||||||||||||||
|
Subroutine
mordent: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 153 | 12 | my ($self, $duration, $pitch, $offset) = @_; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 12 | my $number = 4; # Finest division needed | |||||||||||||||||||
| 156 | 12 | ▶ | $offset //= 1; # Default one note above | ||||||||||||||||||
|
Condition50%1/2
$offset //= 1
MC/DC0%0/1
$offset //= 1
$offset
Truth table50%1/2
A
A= $offset
| |||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 12 | ▶ | my $named = $pitch =~ /[A-G]/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 12 | (my $i, $pitch) = $self->_find_pitch($pitch); | |||||||||||||||||||
| 161 | 12 | my $alt = $self->_scale->[ $i + $offset ]; | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 12 | ▶ | if ($named) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 164 | 6 | $pitch = $self->pitchname($pitch); | |||||||||||||||||||
| 165 | 6 | $alt = $self->pitchname($alt); | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | # Compute the ornament durations | ||||||||||||||||||||
| 169 | 12 | my $x = $MIDI::Simple::Length{$duration} * TICKS; | |||||||||||||||||||
| 170 | 12 | my $y = sprintf '%0.f', $x / $number; | |||||||||||||||||||
| 171 | 12 | my $z = sprintf '%0.f', $x - (2 * $y); | |||||||||||||||||||
| 172 | 12 | ▶ | print "Durations: $x, $y, $z\n" if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 12 | $y = 'd' . $y; | |||||||||||||||||||
| 174 | 12 | $z = 'd' . $z; | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 12 | my @mordent; | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 12 | push @mordent, [$y, $pitch], [$y, $alt], [$z, $pitch]; | |||||||||||||||||||
| 179 | 12 | ▶ | print 'Mordent: ', ddc(\@mordent) if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 12 | return \@mordent; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 4 | ▶ | sub slide { | ||||||||||||||||||
|
Subroutine
slide: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 186 | 4 | my ($self, $duration, $from, $to) = @_; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 4 | my @scale = map { get_scale_MIDI($self->scale_note, $_, 'chromatic') } -1 .. OCTAVES - 1; | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 4 | ▶ | my $named = $from =~ /[A-G]/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 4 | (my $i, $from) = $self->_find_pitch($from, \@scale); | |||||||||||||||||||
| 193 | 4 | (my $j, $to) = $self->_find_pitch($to, \@scale); | |||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 4 | my ($start, $end); | |||||||||||||||||||
| 196 | 4 | ▶ | if ($i <= $j) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | 2 | $start = $i; | |||||||||||||||||||
| 198 | 2 | $end = $j; | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | else { | ||||||||||||||||||||
| 201 | 2 | $start = $j; | |||||||||||||||||||
| 202 | 2 | $end = $i; | |||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | # Compute the ornament durations | ||||||||||||||||||||
| 206 | 4 | my $x = $MIDI::Simple::Length{$duration} * TICKS; | |||||||||||||||||||
| 207 | 4 | my $y = $end - $start + 1; # Number of notes in the slide | |||||||||||||||||||
| 208 | 4 | my $z = sprintf '%0.f', $x / $y; | |||||||||||||||||||
| 209 | 4 | ▶ | print "Durations: $x, $y, $z\n" if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 210 | 4 | $z = 'd' . $z; | |||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 4 | my @slide; | |||||||||||||||||||
| 213 | 4 | ▶ | if ($named) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 214 | 2 | @slide = map { [ $z, $self->pitchname($scale[$_]) ] } $start .. $end; | |||||||||||||||||||
| 215 | } | ||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||
| 217 | 2 | @slide = map { [ $z, $scale[$_] ] } $start .. $end; | |||||||||||||||||||
| 218 | } | ||||||||||||||||||||
| 219 | 4 | ▶ | @slide = reverse @slide if $j < $i; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 220 | 4 | ▶ | print 'Slide: ', ddc(\@slide) if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 4 | return \@slide; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 62 | ▶ | sub _find_pitch { | ||||||||||||||||||
|
Subroutine
_find_pitch: called CC=4 SCAR=14.0
| |||||||||||||||||||||
| 226 | 62 | my ($self, $pitch, $scale) = @_; | |||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 62 | ▶ | $scale //= $self->_scale; | ||||||||||||||||||
|
Condition66%2/3
$scale //= $self->_scale
MC/DC0%0/2
$scale //= $self->_scale
$scale
$self->_scale
Truth table66%2/3
A //= B
A= $scaleB= $self->_scale
| |||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 62 | ▶ | $pitch = $self->pitchnum($pitch) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 231 | if $pitch =~ /[A-G]/; | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 62 | ▶ | my $i = first_index { $_ eq $pitch } @$scale; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 234 | 62 | ▶ | croak "Unknown pitch: $pitch" if $i < 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | 62 | return $i, $pitch; | |||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 2 | 1; | |||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | __END__ | ||||||||||||||||||||