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";
}
Cheers,
Ben,
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.