Wow. This is simple, but oh so beautiful. From the keyboard of the mighty Ernst Terhardt comes this lovely little algorithm to calculate the root of a chord, given a bunch of notes.

What amazed me was that the root note doesn't have to be included! - as evidenced by the 'Tristan' example. I'll be playing with this for days! :)

Update - Now improved! Uses the full subharmonic range, and picks out all possible answers - eg. "a c e g" can be either C(6) or A(m7).

my %nums = qw(A 0 A# 1 BB 1 B 2 C 3 C# 4 DB 4 D 5 D# 6 EB 6 E 7 F 8 F# + 9 GB 9 G 10 G# 11 AB 11); my %notes = qw(0 A 1 A# 2 B 3 C 4 C# 5 D 6 D# 7 E 8 F 9 F# 10 G 11 G#) +; use List::Util qw(max); sub find_root { my @f; foreach my $n (map {$nums{uc($_)}} @_) { ++$f[($n+$_)%12] foreach (0,0,0,2,5,5,8,10); } my $max = max(@f); map {$notes{$_}} grep {$f[$_] == $max} (0..11); } print find_root(qw(c f a)),"\n"; # F - 2nd inversion print find_root(qw(e g a# c)),"\n"; # C - 1st inversion 7th) print find_root(qw(f b d# g#)),"\n";# C#(!)/G# - 'Tristan' chord, C#9( +no bass) *or* G#m6

In reply to 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.