| 1 | package Music::Interval::Barycentric; | ||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||
| 3 | |||||||||||||
| 4 | # ABSTRACT: Compute barycentric musical interval space | ||||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 2 | our $VERSION = '0.0405'; | |||||||||||
| 10 | |||||||||||||
| 11 | 2 | ▶ | use List::Util qw( min ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 2 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 2 | our @EXPORT = qw( | |||||||||||
| 16 | barycenter | ||||||||||||
| 17 | distance | ||||||||||||
| 18 | evenness_index | ||||||||||||
| 19 | orbit_distance | ||||||||||||
| 20 | forte_distance | ||||||||||||
| 21 | cyclic_permutation | ||||||||||||
| 22 | inversion | ||||||||||||
| 23 | ); | ||||||||||||
| 24 | |||||||||||||
| 25 | 2 | ▶ | use constant SIZE => 3; # Default chord size | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 2 | ▶ | use constant SCALE => 12; # Default number of scale notes | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | |||||||||||||
| 29 | 38 | ▶ | sub barycenter { | ||||||||||
|
Subroutine
barycenter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 30 | 38 | ▶ | my $size = shift || SIZE; # Default to a triad | ||||||||||
|
Condition100%2/2
shift() || 3
MC/DC100%1/1
shift() || 3
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 31 | 38 | ▶ | my $scale = shift || SCALE; # Default to the common scale notes | ||||||||||
|
Condition100%2/2
shift() || 12
MC/DC100%1/1
shift() || 12
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 32 | 38 | return ($scale / $size) x $size; | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | |||||||||||||
| 36 | 153 | ▶ | sub distance { | ||||||||||
|
Subroutine
distance: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 37 | 153 | my ($chord1, $chord2) = @_; | |||||||||||
| 38 | 153 | my $distance = 0; | |||||||||||
| 39 | 153 | for my $note (0 .. @$chord1 - 1) { | |||||||||||
| 40 | 474 | $distance += ($chord1->[$note] - $chord2->[$note]) ** 2; | |||||||||||
| 41 | } | ||||||||||||
| 42 | 153 | $distance /= 2; | |||||||||||
| 43 | 153 | return sqrt $distance; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | |||||||||||||
| 47 | 14 | ▶ | sub orbit_distance { | ||||||||||
|
Subroutine
orbit_distance: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 48 | 14 | my ($chord1, $chord2) = @_; | |||||||||||
| 49 | 14 | my @distance = (); | |||||||||||
| 50 | 14 | for my $perm (cyclic_permutation(@$chord2)) { | |||||||||||
| 51 | 43 | push @distance, distance($chord1, $perm); | |||||||||||
| 52 | } | ||||||||||||
| 53 | 14 | return min(@distance); | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | |||||||||||||
| 57 | 12 | ▶ | sub forte_distance { | ||||||||||
|
Subroutine
forte_distance: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 58 | 12 | my ($chord1, $chord2) = @_; | |||||||||||
| 59 | 12 | my @distance = (); | |||||||||||
| 60 | 12 | for my $perm (cyclic_permutation(@$chord2)) { | |||||||||||
| 61 | 37 | push @distance, distance($chord1, $perm); | |||||||||||
| 62 | 37 | push @distance, distance($chord1, [reverse @$perm]); | |||||||||||
| 63 | } | ||||||||||||
| 64 | 12 | return min(@distance); | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | |||||||||||||
| 68 | 28 | ▶ | sub cyclic_permutation { | ||||||||||
|
Subroutine
cyclic_permutation: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 69 | 28 | my @set = @_; | |||||||||||
| 70 | 28 | my @cycles = (); | |||||||||||
| 71 | 28 | for my $backward (reverse 0 .. @set - 1) { | |||||||||||
| 72 | 86 | for my $forward (0 .. @set - 1) { | |||||||||||
| 73 | 266 | push @{ $cycles[$backward] }, $set[$forward - $backward]; | |||||||||||
| 74 | } | ||||||||||||
| 75 | } | ||||||||||||
| 76 | 28 | return @cycles; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | |||||||||||||
| 80 | 22 | ▶ | sub evenness_index { | ||||||||||
|
Subroutine
evenness_index: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 81 | 22 | my $chord = shift; | |||||||||||
| 82 | 22 | my @b = barycenter( scalar @$chord ); | |||||||||||
| 83 | 22 | my $i = distance( $chord, \@b ); | |||||||||||
| 84 | 22 | return $i; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | |||||||||||||
| 88 | 22 | ▶ | sub inversion { | ||||||||||
|
Subroutine
inversion: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 89 | 22 | my $chord = shift; | |||||||||||
| 90 | 22 | return [ reverse @$chord ]; | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 2 | 1; | |||||||||||
| 94 | |||||||||||||
| 95 | __END__ | ||||||||||||