Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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] . " "; }

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-23 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found