Building musical chords starting from (a lot of) rules

Dan Upton upton at
Fri Nov 14 19:25:09 CET 2008

On Fri, Nov 14, 2008 at 1:00 PM, Mr. SpOOn <mr.spoon21 at> wrote:
> Hi,
> I'm writing a method to create musical chords.
> This method must follow a specific set of syntax rules. At least, this
> is my idea, but maybe there's a better way.
> Anyway, in the code I have class Chord which is a set.
> The costrunction of a chord is based on a root note and a structure,
> so by default, giving just a note, it creates a major chord adding the
> third and fifth note.
> So, in pseudo-code: Chord(C) -> [C, E, G]
> And this is the base case. All the other rules must specify what kind
> of note to add or which one should be modified.
> For example:   C min, means to add the third minor note: C Eb G
> C 9 is a base chord plus a the ninth note, but this implies the
> presence of the seventh too, so it results in: C E G B D
> But I can also say: C 9 no7, so it should just be: C E G D, without the seventh.
> There are also more complex rules. For the eleventh, for example, it
> should add also the ninth and seventh note. In the normal case it adds
> their major version, but I can specify to add an augmented nine, so
> this modification must have precedence over the base case.
> Anyway, I think I can use a chain of  if-clauses, one per rule and at
> the end remove the notes marked with "no". But this seems to me a very
> bad solution, not so pythonic. Before I proceed for this way, do you
> have any suggestion? Hope the problem is not too complicated.
> Thanks,
> Carlo

I don't know if this is any better, but you could represent a chord
formula as a 15-tuple consisting of (-, n, b, #) for instance, so

maj = (n, -, n, -, n, -, -, -, -, -, -, -, -, -, -)
min = (n, -, b, -, n, -, -, -, -, -, -, -, -, -, -)
#9 = (n, -, n, -, n, -, n, -, #, -, -, -, -, -, -)

and just have a lookup table.  (Or a 7-tuple and do mod arithmetic to
get the extensions)  Then you could remove (well, I suppose with a
tuple it would be build a new one, or you could use a list?) anything
with "no."

Just a thought...I don't ever do anything very complicated in Python
though so I don't know if that's Pythonic either ;)


More information about the Python-list mailing list