I started playing bass a while ago, and now I am getting into the music theory part. Since I got tired of figuring out the keys/modes myself, I wrote some perl to do it.

The scripts takes two command line arguments, the key and then the mode. If the key is not specified, C is assumed, and if the mode is not specified, major is assumed. See the source for valid keys.

Update: I fixed a bug in the notes array (had "G G", instead of "G G#"). Thanks for the msg jeffa.
Update Again: I fixed the bug where invalid keys/modes weren't recognized.
Yet Another Update: Added ionian (instead of just "major").

Things that still need to be fixed:

#!/usr/bin/perl $key = uc(shift || "C"); $mode = lc(shift || "major"); @notes = qw(A A# B C C# D D# E F F# G G# A A# B C C# D D# E F F# G G#) +; %modes = ("major", -1, "ionian", -1, "dorian", 0, "phrygian", 1, "lydi +an", 2, "mixolydian", 3, "aeolian", 4, "locrian", 5); @steps = qw(2 2 1 2 2 2 1); $notematch = join "|", @notes; if (($key !~ /($notematch)/) || !(exists $modes{$mode})) {die "Invalid + key or mode!\n"} while ($notes[0] ne $key) { push(@notes, shift(@notes)); } for (0 .. $modes{"$mode"}) { push(@steps, shift(@steps)); } unshift(@steps, 0); foreach $i (@steps) { $index += $i; print $notes[$index] . " "; }

In reply to Musical key/mode finder. by IndyZ

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.