| 1 | package MIDI::Chord::Guitar; | ||||||||||||||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: MIDI pitches for guitar chord voicings | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.0706'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use File::ShareDir qw(dist_dir); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use List::Util qw(any zip); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Music::Note (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Text::CSV_XS (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 2 | with('Music::PitchNum'); | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 2 | has voicing_file => ( | |||||||||||||||||||||||||||||||
| 24 | is => 'lazy', | ||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub _build_voicing_file { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_voicing_file: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 28 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 29 | 0 | my $file = eval { dist_dir('MIDI-Chord-Guitar') . '/midi-guitar-chord-voicings.csv' }; | |||||||||||||||||||||||||||||||
| 30 | 0 | return $file; | |||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 2 | has chords => ( | |||||||||||||||||||||||||||||||
| 35 | is => 'lazy', | ||||||||||||||||||||||||||||||||
| 36 | init_arg => undef, | ||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | sub _build_chords { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_chords: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 40 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 3 | my $file = $self->voicing_file; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 3 | my %data; | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 3 | my $csv = Text::CSV_XS->new({ binary => 1 }); | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 3 | ▶ | open my $fh, '<', $file | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | or croak "Can't read $file: $!"; | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | while (my $row = $csv->getline($fh)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 336 | my $chord = shift @$row; | |||||||||||||||||||||||||||||||
| 53 | 336 | my $fingering = shift @$row; | |||||||||||||||||||||||||||||||
| 54 | 336 | push @{ $data{$chord}{fingering} }, $fingering; | |||||||||||||||||||||||||||||||
| 55 | 336 | my @notes; | |||||||||||||||||||||||||||||||
| 56 | 336 | for my $r (@$row) { | |||||||||||||||||||||||||||||||
| 57 | 2016 | ▶ | push @notes, $r if $r ne ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | 336 | push @{ $data{$chord}{notes} }, \@notes; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 3 | close $fh; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 3 | return \%data; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | sub transform { | ||||||||||||||||||||||||||||||
|
Subroutine
transform: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 69 | 8 | my ($self, $target, $chord_name, $variation) = @_; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 8 | $target = $self->pitchnum($target); | |||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | croak 'Invalid note' unless $target; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 7 | ▶ | $chord_name //= ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$chord_name //= ""
MC/DC0%0/1
$chord_name //= ""
$chord_name
Truth table50%1/2
A
A= $chord_name
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 7 | my @notes; | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 7 | ▶ | if (defined $variation) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 6 | my $pitches = $self->chords->{ 'C' . $chord_name }{notes}[$variation]; | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 6 | my $diff = $target - _lowest_c($pitches); | |||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 6 | @notes = map { $_ + $diff } @$pitches; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||||||
| 86 | 1 | for my $pitches (@{ $self->chords->{ 'C' . $chord_name }{notes} }) { | |||||||||||||||||||||||||||||||
| 87 | 3 | my $diff = $target - _lowest_c($pitches); | |||||||||||||||||||||||||||||||
| 88 | 3 | push @notes, [ map { $_ + $diff } @$pitches ]; | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 7 | return \@notes; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 27 | ▶ | sub _lowest_c { | ||||||||||||||||||||||||||||||
|
Subroutine
_lowest_c: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 96 | 27 | my ($pitches) = @_; | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 27 | my $lowest = 0; | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 27 | for my $c (48, 60, 72) { | |||||||||||||||||||||||||||||||
| 101 | 37 | ▶ | if (any { $_ == $c } @$pitches) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | 25 | $lowest = $c; | |||||||||||||||||||||||||||||||
| 103 | 25 | last; | |||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 27 | return $lowest; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | sub voicings { | ||||||||||||||||||||||||||||||
|
Subroutine
voicings: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 112 | 3 | my ($self, $chord_name, $format) = @_; | |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | $chord_name //= ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$chord_name //= ""
MC/DC0%0/1
$chord_name //= ""
$chord_name
Truth table50%1/2
A
A= $chord_name
| |||||||||||||||||||||||||||||||||
| 115 | 3 | ▶ | $format ||= ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$format ||= ""
MC/DC100%1/1
$format ||= ""
$format
Truth table100%2/2
A
A= $format
| |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 3 | my $voicings = $self->chords->{ 'C' . $chord_name }{notes}; | |||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | if ($format) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 2 | my $temp; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 2 | for my $chord (@$voicings) { | |||||||||||||||||||||||||||||||
| 123 | 6 | my $span; | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 6 | for my $n (@$chord) { | |||||||||||||||||||||||||||||||
| 126 | 30 | my $note = Music::Note->new($n, 'midinum')->format($format); | |||||||||||||||||||||||||||||||
| 127 | 30 | push @$span, $note; | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 6 | push @$temp, $span; | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 2 | $voicings = $temp; | |||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 3 | return $voicings; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 10 | ▶ | sub fingering { | ||||||||||||||||||||||||||||||
|
Subroutine
fingering: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 141 | 10 | my ($self, $target, $chord_name, $variation) = @_; | |||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 10 | $target = $self->pitchnum($target); | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 10 | ▶ | $chord_name //= ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$chord_name //= ""
MC/DC0%0/1
$chord_name //= ""
$chord_name
Truth table50%1/2
A
A= $chord_name
| |||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 10 | my @fingering; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 10 | ▶ | if (defined $variation) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | 8 | my $fingering = $self->chords->{ 'C' . $chord_name }{fingering}[$variation]; | |||||||||||||||||||||||||||||||
| 151 | 8 | my $pitches = $self->chords->{ 'C' . $chord_name }{notes}[$variation]; | |||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 8 | my ($str, $p) = _find_fingering($target, $pitches, $fingering); | |||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 8 | ▶ | push @fingering, $str . '-' . $p if $p >= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||||||
| 158 | 2 | for (zip $self->chords->{ 'C' . $chord_name }{notes}, $self->chords->{ 'C' . $chord_name }{fingering}) { | |||||||||||||||||||||||||||||||
| 159 | 10 | my ($pitches, $fingering) = @$_; | |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 10 | my ($str, $p) = _find_fingering($target, $pitches, $fingering); | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 10 | ▶ | push @fingering, $str . '-' . $p if $p >= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 10 | return \@fingering; | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | # XXX This is overly complicated, questionable logic | ||||||||||||||||||||||||||||||||
| 171 | 18 | ▶ | sub _find_fingering { | ||||||||||||||||||||||||||||||
|
Subroutine
_find_fingering: called CC=9 SCAR=22.9
| |||||||||||||||||||||||||||||||||
| 172 | 18 | my ($target, $pitches, $fingering) = @_; | |||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | 18 | my $diff = $target - _lowest_c($pitches); | |||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 18 | my ($str, $pos) = split /-/, $fingering; | |||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 18 | my $p = $pos + $diff; | |||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 18 | ▶ | if ($pos != 1 && $str !~ /0/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$pos != 1 and not $str =~ /0/
MC/DC50%1/2
$pos != 1 and not $str =~ /0/
$pos != 1
not $str =~ /0/
Truth table66%2/3
A and not(B)
A= $pos != 1B= not $str =~ /0/
| |||||||||||||||||||||||||||||||||
| 181 | 12 | ▶ | if ($p == 0 && $str !~ /0/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$p == 0 and not $str =~ /0/
MC/DC50%1/2
$p == 0 and not $str =~ /0/
$p == 0
not $str =~ /0/
Truth table66%2/3
A and not(B)
A= $p == 0B= not $str =~ /0/
| |||||||||||||||||||||||||||||||||
| 182 | 4 | $str = _decrement_fingering($str); | |||||||||||||||||||||||||||||||
| 183 | 4 | $p++; | |||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||
| 185 | ▶ | elsif ($p != 0 && $str =~ /0/) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$p != 0 and $str =~ /0/
MC/DC0%0/2
$p != 0 and $str =~ /0/
$p != 0
$str =~ /0/
Truth table33%1/3
A and B
A= $p != 0B= $str =~ /0/
| |||||||||||||||||||||||||||||||||
| 186 | 0 | $str = _increment_fingering($str); | |||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | ▶ | elsif ($p > 1 && $str =~ /0/) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$p > 1 and $str =~ /0/
MC/DC50%1/2
$p > 1 and $str =~ /0/
$p > 1
$str =~ /0/
Truth table66%2/3
A and B
A= $p > 1B= $str =~ /0/
| |||||||||||||||||||||||||||||||||
| 190 | 2 | $str = _increment_fingering($str); | |||||||||||||||||||||||||||||||
| 191 | 2 | $p--; | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | 18 | return $str, $p; | |||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | sub _increment_fingering { | ||||||||||||||||||||||||||||||
|
Subroutine
_increment_fingering: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 199 | 2 | my ($fingering) = @_; | |||||||||||||||||||||||||||||||
| 200 | 2 | my $incremented = ''; | |||||||||||||||||||||||||||||||
| 201 | 2 | for my $char (split //, $fingering) { | |||||||||||||||||||||||||||||||
| 202 | 12 | ▶ | $incremented .= $char =~ /\d/ ? $char + 1 : $char; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | 2 | return $incremented; | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 4 | ▶ | sub _decrement_fingering { | ||||||||||||||||||||||||||||||
|
Subroutine
_decrement_fingering: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 208 | 4 | my ($fingering) = @_; | |||||||||||||||||||||||||||||||
| 209 | 4 | my $decremented = ''; | |||||||||||||||||||||||||||||||
| 210 | 4 | for my $char (split //, $fingering) { | |||||||||||||||||||||||||||||||
| 211 | 24 | ▶ | $decremented .= $char =~ /\d/ ? $char - 1 : $char; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||
| 213 | 4 | return $decremented; | |||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | 2 | 1; | |||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | __END__ | ||||||||||||||||||||||||||||||||