| 1 | package Music::Intervals::Numeric; | ||||||||||
| 2 | 2 | $Music::Intervals::Numeric::VERSION = '0.1101'; | |||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||
| 4 | |||||||||||
| 5 | # ABSTRACT: Breakdown of numeric musical intervals | ||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Algorithm::Combinatorics qw( combinations ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Math::Factor::XS qw( prime_factors ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Number::Fraction (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use Music::Intervals::Ratios; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | has notes => ( | ||||||||||
| 20 | is => 'ro', | ||||||||||
| 21 | 4 | ▶ | default => sub { [qw( 1/1 5/4 3/2 )] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ); | |||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 2 | has ratios => ( | |||||||||
| 26 | is => 'ro', | ||||||||||
| 27 | builder => 1, | ||||||||||
| 28 | ); | ||||||||||
| 29 | 5 | ▶ | sub _build_ratios { | ||||||||
|
Subroutine
_build_ratios: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 5 | my ($self) = @_; | |||||||||
| 31 | 2 | ▶ | no warnings 'once'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | my $ratios = { map { | ||||||||||
| 33 | 5 | $Music::Intervals::Ratios::ratio->{$_}{ratio} => $Music::Intervals::Ratios::ratio->{$_}{name} | |||||||||
| 34 | 2540 | } keys %$Music::Intervals::Ratios::ratio }; | |||||||||
| 35 | 5 | return $ratios; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 2 | has _dyads => ( | |||||||||
| 39 | is => 'ro', | ||||||||||
| 40 | lazy => 1, | ||||||||||
| 41 | builder => 1, | ||||||||||
| 42 | ); | ||||||||||
| 43 | 4 | ▶ | sub _build__dyads { | ||||||||
|
Subroutine
_build__dyads: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 4 | my $self = shift; | |||||||||
| 45 | 4 | my %dyads = $self->dyads($self->notes); | |||||||||
| 46 | 4 | return \%dyads; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 5 | ▶ | has _semitones => ( is => 'ro', default => sub { 12 } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 1 | ▶ | has _temper => ( is => 'ro', lazy => 1, default => sub { my $self = shift; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 1 | $self->_semitones * 100 / log(2) }, | |||||||||
| 52 | 2 | ); | |||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | 2 | ▶ | sub frequencies { | ||||||||
|
Subroutine
frequencies: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 56 | 2 | my ($self) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 2 | my %frequencies = map { $_ => $self->ratios->{$_} } @{ $self->notes }; | |||||||||
| 59 | |||||||||||
| 60 | 2 | return \%frequencies; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 2 | ▶ | sub intervals { | ||||||||
|
Subroutine
intervals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 64 | 2 | my ($self) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 2 | my %dyads = %{ $self->_dyads }; | |||||||||
| 67 | |||||||||||
| 68 | my %intervals = map { | ||||||||||
| 69 | 2 | $_ => { | |||||||||
| 70 | 3 | $dyads{$_} => $self->ratios->{ $dyads{$_} } | |||||||||
| 71 | } | ||||||||||
| 72 | } keys %dyads; | ||||||||||
| 73 | |||||||||||
| 74 | 2 | return \%intervals; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 4 | ▶ | sub cent_vals { | ||||||||
|
Subroutine
cent_vals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 78 | 4 | my ($self) = @_; | |||||||||
| 79 | |||||||||||
| 80 | 4 | my %dyads = %{ $self->_dyads }; | |||||||||
| 81 | |||||||||||
| 82 | my %cent_vals = map { | ||||||||||
| 83 | 4 | $_ => log( eval $dyads{$_} ) * $self->_temper | |||||||||
| 84 | } keys %dyads; | ||||||||||
| 85 | |||||||||||
| 86 | 4 | return \%cent_vals; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 2 | ▶ | sub prime_factor { | ||||||||
|
Subroutine
prime_factor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 90 | 2 | my ($self) = @_; | |||||||||
| 91 | |||||||||||
| 92 | 2 | my %dyads = %{ $self->_dyads }; | |||||||||
| 93 | |||||||||||
| 94 | my %prime_factor = map { | ||||||||||
| 95 | 2 | $_ => { | |||||||||
| 96 | 3 | $dyads{$_} => scalar ratio_factorize( $dyads{$_} ) | |||||||||
| 97 | } | ||||||||||
| 98 | } keys %dyads; | ||||||||||
| 99 | |||||||||||
| 100 | 2 | return \%prime_factor; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | 4 | ▶ | sub dyads { | ||||||||
|
Subroutine
dyads: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 105 | 4 | my $self = shift; | |||||||||
| 106 | 4 | my ($c) = @_; | |||||||||
| 107 | |||||||||||
| 108 | 4 | ▶ | return () if @$c <= 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 3 | my @pairs = combinations( $c, 2 ); | |||||||||
| 111 | |||||||||||
| 112 | 3 | my %dyads; | |||||||||
| 113 | 3 | for my $i (@pairs) { | |||||||||
| 114 | # Construct our "dyadic" fraction. | ||||||||||
| 115 | 9 | my $numerator = Number::Fraction->new( $i->[1] ); | |||||||||
| 116 | 9 | my $denominator = Number::Fraction->new( $i->[0] ); | |||||||||
| 117 | 9 | my $fraction = $numerator / $denominator; | |||||||||
| 118 | |||||||||||
| 119 | 9 | $dyads{"@$i"} = $fraction->to_string; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 3 | return %dyads; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | |||||||||||
| 126 | 3 | ▶ | sub ratio_factorize { | ||||||||
|
Subroutine
ratio_factorize: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 127 | 3 | my $dyad = shift; | |||||||||
| 128 | |||||||||||
| 129 | 3 | my ( $numerator, $denominator ) = split /\//, $dyad; | |||||||||
| 130 | 3 | $numerator = [ prime_factors($numerator) ]; | |||||||||
| 131 | 3 | $denominator = [ prime_factors($denominator) ]; | |||||||||
| 132 | |||||||||||
| 133 | return wantarray | ||||||||||
| 134 | 3 | ▶ | ? ( $numerator, $denominator ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | : sprintf( '(%s) / (%s)', | ||||||||||
| 136 | join( '*', @$numerator ), | ||||||||||
| 137 | join( '*', @$denominator ) | ||||||||||
| 138 | ); | ||||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 2 | 1; | |||||||||
| 142 | |||||||||||
| 143 | __END__ | ||||||||||