Music knowledge representation

D'Arcy J.M. Cain darcy at
Mon Sep 29 18:49:40 CEST 2008

On Sun, 28 Sep 2008 16:37:11 +0200
"Mr.SpOOn" <mr.spoon21 at> wrote:
> Hi,
> I'm working on an application to analyse music (melodies, chord sequences etc.)

Sounds interesting.  Will this be Open Source?

> I need classes to represent different musical entities. I'm using a
> class Note to represent all the notes. Inside it stores the name of
> the natural version of the note (C, D, E, F...) and an integer to
> calculate the accidentals.

Have you considered having the object take a key option with default to
'C' so that you don't have to mark as many accidentals - or, more
correctly, mark actual accidentals rather than always marking the
"black keys?"

The other advantage here is that the notes can be given as numbers and
octaves (middle C would be (1, 3, 0) in the key of C for example) and
you can transpose by changing the argument when instantiating the class.

> Then I have a class Pitch, to represent the different 12 pitch
> classes, because different notes, such as C# and Db, belong to the
> same pitch class.

Couldn't the note class simply have a list of all the notes and have a
simple method calculate the actual pitch?

> In these classes I also have some arithmetic method to perform
> additions or subtractions between pitches and integers.
> I also need to represent intervals between notes. An interval must
> have a degree (first, second, third), that may be represented with a
> simple integer and a size counted in semitones. Then, with these
> informations it can retrieve its name, for example: perfect fifth.

Seems easy given the pitch location indeces.  Subtract the two and do a
lookup into a list of intervals.  E.g:  ["Unison", "Semi-tone",
"Second", "Minor third", Major third", ...]

> The degree is calculated between natural notes. So the degree of
> Interval(C, E) is "third", or 3. This may be simple, if I put the
> notes in an ordered sequence. But, when I have to calculate, for
> example Interval(E, C). It should count till the end of the sequence,
> so if I have:
> C D E F G A B
> after the B it should continue with C. I'm not sure how to implement
> this. Is there a suitable data structure for this purpose?

    def interval(self, lower, higher)
        if lower > higher:
            # uncomment one of the two following lines depending
            # on the behaviour you want
            #lower,higher = higher,lower
            #higher += 12

        # could use some error trapping
        return self.interval_name[higher - lower]

Note that lower and higher could be a note object that you have to
convert to integers first.

Hope this gives you some ideas.

D'Arcy J.M. Cain <darcy at>         |  Democracy is three wolves                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.

More information about the Python-list mailing list