4

Is there a function or easy way to transpose a stream to a given key?

I want to use it in a loop, e.g, take a set of major streams and transpose all of then to C major (so then I can do some statistical work with them).

All the transpose tools I saw work with intervals or number of tones, not fixed keys. It shouldn't be so hard to write my function, but I suppose that it has to be already done... Thanks

VR_1312
  • 151
  • 1
  • 13

1 Answers1

6

If s is a Stream (such as a Score or Part), then s.transpose('P4') will move it up a Perfect Fourth, etc. If you know the key of s as k major, then i = interval.Interval(k, 'C') will let you do s.transpose(i) to move from k to C. If you don't know the key of s, then k = s.analyze('key') will do a pretty decent job of figuring it out (using the Krumhansl probe-tone method). Putting it all together.

from music21 import *
for fn in filenameList:
    s = converter.parse(fn)
    k = s.analyze('key')
    i = interval.Interval(k.tonic, pitch.Pitch('C'))
    sNew = s.transpose(i)
    # do something with sNew

This assumes that your piece is likely to be in major. If not you can either treat it as the parallel major (f-minor -> F-major) or find in k.alternativeInterpretations the best major key analysis. Or transpose it to a minor if it's minor, etc.

  • 1
    Thanks Michael. I wrote my own code a few days ago (but its not working ok, so maybe I should try this). About the major/minor issue, I use 'analyze' before transposing so I keep only major or minor streams – VR_1312 May 31 '16 at 19:03
  • 1
    Just a little improvement: The code doesn't work because 'k' and 'C' are not Note objects. A correct way may be replacing 'C' by the note.Note('C') and getting a note object from k. For example: tonic = note.Note(pitch = k.pitchFromDegree(1)), and using 'tonic' instead of 'k' in the interval. – VR_1312 Jun 02 '16 at 23:15
  • Thanks! Fixed (in the example) – Michael Scott Asato Cuthbert Jun 03 '16 at 00:39