| 1 | # -*- Perl -*- | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # Performs Neo-Riemann operations on set classes. | ||||||||||||||||||||
| 4 | # https://en.wikipedia.org/wiki/Neo-Riemannian_theory | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Music::NeoRiemannianTonnetz; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | use 5.010000; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | use Carp qw/croak/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use List::Util qw/min/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use Scalar::Util qw/reftype/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 1 | our $VERSION = '0.27'; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | my $DEG_IN_SCALE = 12; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # For the transform table. "SEE ALSO" section in docs has links for | ||||||||||||||||||||
| 22 | # [refs]. These are expanded as a simple grammar, until a code reference | ||||||||||||||||||||
| 23 | # is reached; the code reference then looks up what needs to be done via | ||||||||||||||||||||
| 24 | # the operations table. | ||||||||||||||||||||
| 25 | 1 | my %TRANSFORMATIONS = ( | |||||||||||||||||||
| 26 | # 3-11 operations | ||||||||||||||||||||
| 27 | P => \&_apply_operation, # Parallel [WP] | ||||||||||||||||||||
| 28 | R => \&_apply_operation, # Relative [WP] | ||||||||||||||||||||
| 29 | L => \&_apply_operation, # Leittonwechsel [WP] | ||||||||||||||||||||
| 30 | N => 'RLP', # Nebenverwandt [WP] | ||||||||||||||||||||
| 31 | S => 'LPR', # Slide [WP] | ||||||||||||||||||||
| 32 | H => 'LPL', # [WP] | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | # 4-27 operations [Childs 1998] | ||||||||||||||||||||
| 35 | S23 => \&_apply_operation, | ||||||||||||||||||||
| 36 | S32 => \&_apply_operation, | ||||||||||||||||||||
| 37 | S34 => \&_apply_operation, | ||||||||||||||||||||
| 38 | S43 => \&_apply_operation, | ||||||||||||||||||||
| 39 | S56 => \&_apply_operation, | ||||||||||||||||||||
| 40 | S65 => \&_apply_operation, | ||||||||||||||||||||
| 41 | C32 => \&_apply_operation, | ||||||||||||||||||||
| 42 | C34 => \&_apply_operation, | ||||||||||||||||||||
| 43 | C65 => \&_apply_operation, | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # The important bits (these munge set classes to a different form of the | ||||||||||||||||||||
| 47 | # same parent prime form of a set class, e.g. toggling 0,3,7 to 0,4,7). | ||||||||||||||||||||
| 48 | # The operation names come from the literature, as well as the magic | ||||||||||||||||||||
| 49 | # numbers required to change the set classes correctly. | ||||||||||||||||||||
| 50 | 1 | my %OPERATIONS = ( | |||||||||||||||||||
| 51 | L => { '0,3,7' => { 7 => 1 }, '0,4,7' => { 0 => -1 } }, | ||||||||||||||||||||
| 52 | P => { '0,3,7' => { 3 => 1 }, '0,4,7' => { 4 => -1 } }, | ||||||||||||||||||||
| 53 | R => { '0,3,7' => { 0 => -2 }, '0,4,7' => { 7 => 2 } }, | ||||||||||||||||||||
| 54 | S23 => | ||||||||||||||||||||
| 55 | { '0,3,6,8' => { 0 => -1, 3 => -1 }, '0,2,5,8' => { 5 => 1, 8 => 1 } }, | ||||||||||||||||||||
| 56 | S32 => | ||||||||||||||||||||
| 57 | { '0,3,6,8' => { 6 => 1, 8 => 1 }, '0,2,5,8' => { 0 => -1, 2 => -1 } }, | ||||||||||||||||||||
| 58 | S34 => | ||||||||||||||||||||
| 59 | { '0,3,6,8' => { 0 => 1, 8 => 1 }, '0,2,5,8' => { 0 => -1, 8 => -1 } }, | ||||||||||||||||||||
| 60 | S43 => | ||||||||||||||||||||
| 61 | { '0,3,6,8' => { 3 => -1, 6 => -1 }, '0,2,5,8' => { 2 => 1, 5 => 1 } }, | ||||||||||||||||||||
| 62 | S56 => | ||||||||||||||||||||
| 63 | { '0,3,6,8' => { 0 => -1, 6 => -1 }, '0,2,5,8' => { 2 => 1, 8 => 1 } }, | ||||||||||||||||||||
| 64 | S65 => | ||||||||||||||||||||
| 65 | { '0,3,6,8' => { 3 => 1, 8 => 1 }, '0,2,5,8' => { 0 => -1, 5 => -1 } }, | ||||||||||||||||||||
| 66 | C32 => | ||||||||||||||||||||
| 67 | { '0,3,6,8' => { 6 => -1, 8 => 1 }, '0,2,5,8' => { 0 => -1, 2 => 1 } }, | ||||||||||||||||||||
| 68 | C34 => | ||||||||||||||||||||
| 69 | { '0,3,6,8' => { 0 => -1, 8 => 1 }, '0,2,5,8' => { 0 => -1, 8 => 1 } }, | ||||||||||||||||||||
| 70 | C65 => | ||||||||||||||||||||
| 71 | { '0,3,6,8' => { 3 => -1, 8 => 1 }, '0,2,5,8' => { 0 => -1, 5 => 1 } }, | ||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | ######################################################################## | ||||||||||||||||||||
| 75 | # | ||||||||||||||||||||
| 76 | # SUBROUTINES | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 50 | ▶ | sub _apply_operation { | ||||||||||||||||||
|
Subroutine
_apply_operation: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 79 | 50 | my ( $self, $token, $pset_str, $pset2orig ) = @_; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 50 | ▶ | if ( !exists $self->{op}->{$token}->{$pset_str} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 1 | croak "no set class [$pset_str] for token '$token'"; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # apply pitch modifications from the operations table | ||||||||||||||||||||
| 86 | 49 | for my $i ( keys %{ $self->{op}->{$token}->{$pset_str} } ) { | |||||||||||||||||||
| 87 | 67 | for my $p ( @{ $pset2orig->{$i} } ) { | |||||||||||||||||||
| 88 | 67 | $p += $self->{op}->{$token}->{$pset_str}->{$i}; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | # reformulate the (updated) original pitches into new pitch set | ||||||||||||||||||||
| 93 | 49 | my @new_set; | |||||||||||||||||||
| 94 | 49 | for my $r ( values %$pset2orig ) { | |||||||||||||||||||
| 95 | 165 | push @new_set, @$r; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 49 | @new_set = sort { $a <=> $b } @new_set; | |||||||||||||||||||
| 99 | 49 | return \@new_set; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||
| 103 | 1 | my ( $class, %param ) = @_; | |||||||||||||||||||
| 104 | 1 | my $self = { op => \%OPERATIONS, x => \%TRANSFORMATIONS }; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # should not need to alter, but who knows | ||||||||||||||||||||
| 107 | 1 | ▶ | $self->{_DEG_IN_SCALE} = int( $param{DEG_IN_SCALE} // $DEG_IN_SCALE ); | ||||||||||||||||||
|
Condition33%1/3
$param{"DEG_IN_SCALE"} // $DEG_IN_SCALE
MC/DC0%0/2
$param{"DEG_IN_SCALE"} // $DEG_IN_SCALE
$param{"DEG_IN_SCALE"}
$DEG_IN_SCALE
Truth table33%1/3
A // B
A= $param{"DEG_IN_SCALE"}B= $DEG_IN_SCALE
| |||||||||||||||||||||
| 108 | 1 | ▶ | if ( $self->{_DEG_IN_SCALE} < 2 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 0 | croak 'degrees in scale must be greater than one'; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 1 | bless $self, $class; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 1 | return $self; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | # Based on normal_form of Music::AtonalUtil but always transposes to | ||||||||||||||||||||
| 118 | # zero (cannot use prime_form, as that goes one step too far and | ||||||||||||||||||||
| 119 | # conflates [0,4,7] with [0,3,7] which here must be distinct). | ||||||||||||||||||||
| 120 | 50 | ▶ | sub normalize { | ||||||||||||||||||
|
Subroutine
normalize: called CC=17 SCAR=29.4
Pod
documented
| |||||||||||||||||||||
| 121 | 50 | my $self = shift; | |||||||||||||||||||
| 122 | 50 | ▶ | my $pset = ref $_[0] eq 'ARRAY' ? $_[0] : [@_]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 50 | ▶ | croak 'pitch set must contain something' if !@$pset; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 50 | my %origmap; | |||||||||||||||||||
| 127 | 50 | for my $p (@$pset) { | |||||||||||||||||||
| 128 | 168 | push @{ $origmap{ $p % $self->{_DEG_IN_SCALE} } }, $p; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | 50 | ▶ | if ( keys %origmap == 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 0 | ▶ | return wantarray ? ( keys %origmap, \%origmap ) : keys %origmap; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | 50 | my @nset = sort { $a <=> $b } keys %origmap; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 50 | my @equivs; | |||||||||||||||||||
| 136 | 50 | for my $i ( 0 .. $#nset ) { | |||||||||||||||||||
| 137 | 168 | for my $j ( 0 .. $#nset ) { | |||||||||||||||||||
| 138 | 576 | $equivs[$i][$j] = $nset[ ( $i + $j ) % @nset ]; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | 50 | my @order = reverse 1 .. $#nset; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 50 | my @normal; | |||||||||||||||||||
| 144 | 50 | for my $i (@order) { | |||||||||||||||||||
| 145 | 50 | my $min_span = $self->{_DEG_IN_SCALE}; | |||||||||||||||||||
| 146 | 50 | my @min_span_idx; | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 50 | for my $eidx ( 0 .. $#equivs ) { | |||||||||||||||||||
| 149 | my $span = | ||||||||||||||||||||
| 150 | 168 | ( $equivs[$eidx][$i] - $equivs[$eidx][0] ) % $self->{_DEG_IN_SCALE}; | |||||||||||||||||||
| 151 | 168 | ▶ | if ( $span < $min_span ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 152 | 80 | $min_span = $span; | |||||||||||||||||||
| 153 | 80 | @min_span_idx = $eidx; | |||||||||||||||||||
| 154 | } elsif ( $span == $min_span ) { | ||||||||||||||||||||
| 155 | 9 | push @min_span_idx, $eidx; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 50 | ▶ | if ( @min_span_idx == 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 160 | 50 | @normal = @{ $equivs[ $min_span_idx[0] ] }; | |||||||||||||||||||
| 161 | 50 | last; | |||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||
| 163 | 0 | @equivs = @equivs[@min_span_idx]; | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 50 | ▶ | if ( !@normal ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | # nothing unique, pick lowest starting pitch, which is first index | ||||||||||||||||||||
| 169 | # by virtue of the numeric sort performed above. | ||||||||||||||||||||
| 170 | 0 | @normal = @{ $equivs[0] }; | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | # but must map <b dis fis> (and anything else not <c e g>) so b is 0, | ||||||||||||||||||||
| 174 | # dis 4, etc. and also update the original pitch mapping - this is | ||||||||||||||||||||
| 175 | # the major addition to the otherwise stock normal_form code. | ||||||||||||||||||||
| 176 | 50 | ▶ | if ( $normal[0] != 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | 40 | my $trans = $self->{_DEG_IN_SCALE} - $normal[0]; | |||||||||||||||||||
| 178 | 40 | my %newmap; | |||||||||||||||||||
| 179 | 40 | for my $i (@normal) { | |||||||||||||||||||
| 180 | 138 | my $prev = $i; | |||||||||||||||||||
| 181 | 138 | $i = ( $i + $trans ) % $self->{_DEG_IN_SCALE}; | |||||||||||||||||||
| 182 | 138 | $newmap{$i} = $origmap{$prev}; | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | 40 | %origmap = %newmap; | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | return | ||||||||||||||||||||
| 188 | 50 | ▶ | wantarray ? ( join( ',', @normal ), \%origmap ) : join( ',', @normal ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | # Turns string of tokens (e.g. 'RLP') into a list of tasks (CODE refs, | ||||||||||||||||||||
| 192 | # or more strings, which are recursed on until CODE refs or error). | ||||||||||||||||||||
| 193 | # Returns array reference of such tasks. Called by transform() if user | ||||||||||||||||||||
| 194 | # has not already done this and passes transform() a string of tokens. | ||||||||||||||||||||
| 195 | 42 | ▶ | sub taskify_tokens { | ||||||||||||||||||
|
Subroutine
taskify_tokens: called CC=8 SCAR=22.6
Pod
documented
| |||||||||||||||||||||
| 196 | 42 | my ( $self, $tokens, $tasks ) = @_; | |||||||||||||||||||
| 197 | 42 | ▶ | $tasks //= []; | ||||||||||||||||||
|
Condition100%2/2
$tasks //= []
MC/DC100%1/1
$tasks //= []
$tasks
Truth table100%2/2
A
A= $tasks
| |||||||||||||||||||||
| 198 | 42 | ▶ | $tokens = [ $tokens =~ m/([A-Z][a-z0-9]*)/g ] if !defined reftype $tokens; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | # XXX optimize input? - runs of P can be reduced, as those just toggle | ||||||||||||||||||||
| 201 | # the third - even number of P a no-op, odd number of P can be | ||||||||||||||||||||
| 202 | # replaced with 'P'. Other optimizations are likely possible. | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 42 | for my $t (@$tokens) { | |||||||||||||||||||
| 205 | 61 | ▶ | if ( exists $self->{x}{$t} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | 61 | ▶ | if ( ref $self->{x}{$t} eq 'CODE' ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 207 | 54 | push @$tasks, [ $t, $self->{x}{$t} ]; | |||||||||||||||||||
| 208 | ▶ | } elsif ( !defined reftype $self->{x}{$t} | |||||||||||||||||||
|
Condition33%1/3
not defined builtin::reftype($self->{"x"}{$t}) or ref $self->{"x"}{$t} eq "ARRAY"
MC/DC0%0/2
not defined builtin::reftype($self->{"x"}{$t}) or ref $self->{"x"}{$t} eq "ARRAY"
not defined builtin::reftype($self->{"x"}{$t})
ref $self->{"x"}{$t} eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not defined builtin::reftype($self->{"x"}{$t})B= ref $self->{"x"}{$t} eq "ARRAY"
| |||||||||||||||||||||
| 209 | or ref $self->{x}{$t} eq 'ARRAY' ) { | ||||||||||||||||||||
| 210 | 7 | $self->taskify_tokens( $self->{x}{$t}, $tasks ); | |||||||||||||||||||
| 211 | } else { | ||||||||||||||||||||
| 212 | 0 | croak 'unknown token in transformation table'; | |||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | } else { | ||||||||||||||||||||
| 215 | 0 | croak "unimplemented transformation token '$t'"; | |||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | } | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 42 | return $tasks; | |||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 2 | ▶ | sub techno { shift; (qw/tonn tz/) x ( 8 * ( shift || 1 ) ) } | ||||||||||||||||||
|
Subroutine
techno: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
shift() || 1
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | 33 | ▶ | sub transform { | ||||||||||||||||||
|
Subroutine
transform: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||
| 225 | 33 | my $self = shift; | |||||||||||||||||||
| 226 | 33 | my $tokens = shift; | |||||||||||||||||||
| 227 | 33 | ▶ | croak 'tokens must be defined' unless defined $tokens; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 228 | 33 | ▶ | my $pset = ref $_[0] eq 'ARRAY' ? $_[0] : [@_]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 229 | 33 | ▶ | croak 'pitch set must contain something' if !@$pset; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | # Assume list of tasks (code refs to call) if array ref, otherwise try | ||||||||||||||||||||
| 232 | # to generate such a list. | ||||||||||||||||||||
| 233 | 33 | my $tasks; | |||||||||||||||||||
| 234 | 33 | ▶ | if ( ref $tokens eq 'ARRAY' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 235 | 0 | $tasks = $tokens; | |||||||||||||||||||
| 236 | } else { | ||||||||||||||||||||
| 237 | 0 | ▶ | try { $tasks = $self->taskify_tokens($tokens) } catch { croak $_ }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||
| 238 | } | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | 33 | my $new_pset = $pset; | |||||||||||||||||||
| 241 | 33 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 242 | 33 | for my $task (@$tasks) { | |||||||||||||||||||
| 243 | 50 | $new_pset = | |||||||||||||||||||
| 244 | $task->[1]->( $self, $task->[0], $self->normalize($new_pset) ); | ||||||||||||||||||||
| 245 | } | ||||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | 1 | ▶ | catch { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 248 | 1 | croak $_; | |||||||||||||||||||
| 249 | 33 | }; | |||||||||||||||||||
| 250 | 32 | return $new_pset; | |||||||||||||||||||
| 251 | } | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | 1 | 1; | |||||||||||||||||||
| 254 | __END__ | ||||||||||||||||||||