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,

In reply to Re: Chord root by benn
in thread Chord root by benn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.