Pathologically Eclectic Rubbish Lister PerlMonks

by zeno (Friar)
 on Jan 26, 2007 at 19:34 UTC ( #596776=CUFP: print w/replies, xml ) Need Help??

I've been playing guitar for 31 years (good lord I'm old). But I only recently got interested in learning the theory behind chord progressions. I wrote this simple piece of code to calculate major triad chord progressions in all of the keys. I suppose it could be extended easily to create minor chord progressions as well. Disclaimer: I am not sure about the music terminology I used in my comments.
```use strict;

sub chord_progression
{
# The parameter is the index of the root note in the @notes array
my \$i = shift;
my @notes = ('C', 'C#/Db', 'D', 'D#/Eb', 'E', 'F', 'F#/Gb',
'G', 'G#/Ab', 'A', 'A#/Bb', 'B');
my @intervals = (0, 2, 4, 5, 7, 9, 11, 12);

# Create a scale based on the intervals in a major scale
my @scale = map {\$notes[(\$i + \$intervals[\$_]) % 12]} (0..7);

# Create triads.  Increment each note within the scale.
# All notes in the chord progression fall within the
# major scale.
my (\$root, \$third, \$fifth) = (0, 2, 4);
my @name = ('Maj', 'min', 'min', 'Maj', 'Maj', 'min', 'Dim', 'Maj'
+);
for (0..7) {
printf "%6s %3s: %6s, %6s, %6s\n",
\$scale[\$root], \$name[\$_], \$scale[\$root],
\$scale[\$third], \$scale[\$fifth];
\$root++;
\$third++;
\$fifth++;
\$root %= 7;
\$third %= 7;
\$fifth %= 7;
}
print "\n";
}

# Call the subroutine for each note in the scale
for (0..11)
{
chord_progression(\$_);
}
This creates output like this:
```     C Maj:      C,      E,      G
D min:      D,      F,      A
E min:      E,      G,      B
F Maj:      F,      A,      C
G Maj:      G,      B,      D
A min:      A,      C,      E
B Dim:      B,      D,      F
C Maj:      C,      E,      G

C#/Db Maj:  C#/Db,      F,  G#/Ab
D#/Eb min:  D#/Eb,  F#/Gb, A#, Bb
F min:      F,  G#/Ab,      C
F#/Gb Maj:  F#/Gb, A#, Bb,  C#/Db
G#/Ab Maj:  G#/Ab,      C,  D#/Eb
A#, Bb min: A#, Bb,  C#/Db,      F
C Dim:      C,  D#/Eb,  F#/Gb
C#/Db Maj:  C#/Db,      F,  G#/Ab

...
```

Replies are listed 'Best First'.
Re: Music: Calculate Chord Progression Triads
by johngg (Canon) on Jan 26, 2007 at 23:27 UTC
Sadly, I never took up a musical instrument, something I now regret very much. I wonder if it is too late to do so now that I am in my fifties.

Moving back to Perl, I would just like to bring the qw{ ... } construct to your attention, which could save you some typing when setting up your arrays. Instead of

```my @notes = ('C', 'C#/Db', 'D', 'D#/Eb', 'E', 'F', 'F#/Gb',
'G', 'G#/Ab', 'A', 'A#/Bb', 'B');

you could do

```my @notes = qw{C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B};

And one slight niggle. You use meaningful variable names throughout your script except for the my \$i = shift;. It seems a shame to make this omission when the rest of the script is so very clear.

I hope this is of interest.

Cheers,

JohnGG

Sadly, I never took up a musical instrument, something I now regret very much. I wonder if it is too late to do so now that I am in my fifties.

It's never too late if you want to. I've taught people in their 40s, 50s and 60s the piano, and sure they won't ever be virtuosos but they make progress, and enjoy playing. So ... just do it!

So ... just do it!

That's what I must do. I love listening to all kinds of music so I should start making my own. I could think of it as giving something back to the musical community ... whether they would want it back is another matter :)

Thanks for the encouragement,

JohnGG

I wonder if it is too late to do so now that I am in my fifties.

Perhaps not. I'm in my thirties, and I recently took up piano, and it's proving to be rather easier than I expected. I expected to have to spend twice as long as a child would spend to learn the same amount, but that appears not to be the case. Something about having already learned other stuff, apparently, makes it go quicker. I started in December, and I'm already playing pieces with chords, and beginning to play with a key signature, which typically comes much more than two months in for a young piano student, OSIAT. And I'm only practicing about fifteen minutes a day, average.

YMMV, but give it a shot. What have you got to lose?

--
We're working on a six-year set of freely redistributable Vacation Bible School materials.
What have you got to lose?

Nothing at all, of course. I have no excuse, nowhere to hide. Perhaps I'll make it a belated New Year's resolution.

Cheers,

JohnGG

Hi JohnGG,

Sorry I took so long to reply. Thanks for your suggestions, I'll try and put them into the code.

As for taking up music, I'd say yes, yes, yes. I was very lucky to take up guitar when I was 11. That said, I played a long time before I understood what I was playing at an emotional level. You already have that at 50.

I also was more concerned about just getting some nice songs out than in understanding music theory. I have more patience for that type of thing now at 42, and probably would have made better progress had I gotten more interested in it. You have the benefit of wisdom to allow you to understand better what you are doing.

Anyway, even if you never end up playing for anyone else, it is self-actualizing to take it up. I hope you decide to learn how to play something. My music is one of the most rewarding things I do, and I do it just for me.

Re: Music: Calculate Chord Progression Triads
by bilfurd (Hermit) on Jan 27, 2007 at 02:21 UTC
Your code actually makes more sense to me than a couple of guitar books I own. (Which, sadly, may say more about me than the code!)

Very cool!

Re: Music: Calculate Chord Progression Triads
by McMahon (Chaplain) on Jan 27, 2007 at 06:24 UTC
Fun!

I happened to have grabbed my bass just as I surfed to this, and so tested all of your triads. Well done!

Now you need to add major 7ths and dominant 7ths, 9ths, and possibly 6ths (because that's really where the minor chords live) :)

Good idea, McMahon. I'll see what I can do-- I also found out about a package called [Music::Image::Chord] that can create images of the chords. I might try and incorporate that.

Re: Music: Calculate Chord Progression Triads
by tweetiepooh (Hermit) on Jan 31, 2007 at 10:13 UTC
Cool.

Suggestion though - offer a switch for Rock vs Jazz so that Rock gives sharps and Jazz the flats. That may help woth clearing the layout a little. Actually it's not that simple really but it may do.

If you really want some fun with music theory you need to learn "The circle of fifths". This is a great tool to work out number of incidentals, the fourth and fifth to the root, relative minors and will all fit onto a single piece of paper.

Re: Music: Calculate Chord Progression Triads
by jimX11 (Friar) on Jan 31, 2007 at 14:28 UTC
Blues in C:C C C C F F C C G G C C

Ok, I blew the 7ths. Eh.

So many 3 chord songs, so little refactoring time...
Produced by:

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://596776]
Approved by Old_Gray_Bear
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2023-12-10 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What's your preferred 'use VERSION' for new CPAN modules in 2023?

Results (38 votes). Check out past polls.

Notices?