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:
- Ouput flats (b). See the first comment below. I have no idea how to do
this properly, as saying something like A# or Bb is just "known", there aren't any
progmatic rules for it.
- Recognize flats on the command line. el-moe suggested adding "rocker keys", which have flats (Ab, Bb, Eb, Gb).
Maybe I'll fix it when I get the time. Our maybe one of you could do it ;-).
#!/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] . " ";
}
-
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.