use List::Util qw(max sum); use strict; use warnings; sub note2num { my ($n,$m) = split(//,uc(shift())); my %nums = qw(C 0 D 2 E 4 F 5 G 7 A 9 B 11); my $num = $nums{$n}; $num++ if ($m eq 'S' || $m eq '#'); $num-- if ($m eq 'F' || $m eq 'B'); $num; } sub num2note { my $num = int(shift()) % 12; my %notes = (0,'C',1,'C#',2,'D',3,'D#',4,'E',5,'F',6,'F#',7,'G',8,'G#',9,'A',10,'A#',11,'B'); $notes{$num}; } sub root { my @chord = @_; my @weights; my @rootWeights = (10,0,1,0,3,0,0,5,0,0,2,0); # weights of root-support intervals, similar to those in Parncutt (1988) my @notes; $notes[$_] = 0 foreach (0..11); $notes[note2num($_)] = 1 foreach (@chord); foreach my $pc(0..11) { $weights[$pc] = sum(map{$notes[($pc+$_)%12]*$rootWeights[$_]} (0..11)); } my $ambig=sqrt(sum(@weights)/max(@weights)); my @final = map {num2note($_)} sort {$weights[$b] <=> $weights[$a]} (0..11); ($ambig,@final); } my ($ambiguity, @roots) = root('b','d#','f','g#'); foreach (1..int($ambiguity)) { print "Root $_ = $roots[$_-1]\n"; }