go ahead... be a heretic PerlMonks

### Re: Chord root

by benn (Vicar)
 on Aug 11, 2003 at 19:19 UTC ( #282998=note: print w/replies, xml ) Need Help??

in reply to Chord root

Richard Parncutt has got a bunch of 'C' code building on Prof. Terhardt's work, including a chord root program that examines progressions. This is the 'single chord' core of his version, which gives a level of ambiguity as well. I'm still playing with both this and the above...for single chords, the above may still be better - the now-infamous 'Tristan' example comes out as G# (second = B) - both valid, (and I would possibly argue more for the G#m6 spelling than a C#9 ), but research continues - *one* of them will make it into Music::Chord, I'm sure :)
```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-su
+pport 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";
}
[download]```
Cheers,
Ben,

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://282998]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-06-21 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (118 votes). Check out past polls.

Notices?