George Harris
George Harris

Reputation: 43

Music21 Analyze Key always returns c minor?

I've been trying to use the Python module Music21 to try and get the key from a set of chords, but no matter what I put in it always seems to return c minor. Any ideas what I'm doing wrong?

I've tried a variety of input strings, the print statement spits out all the right chord names but the resulting key is always c minor!

I'm using Python 3.7.4 on Windows with VSCode.

string = 'D, Em, F#m, G, A, Bm'

s = stream.Stream()

for c in string.split(','):
    print(harmony.ChordSymbol(c).pitchedCommonName)
    s.append(harmony.ChordSymbol(c))

key = s.analyze('key')

print(key)

Upvotes: 1

Views: 905

Answers (2)

Jacob Walls
Jacob Walls

Reputation: 861

It works if you give the ChordSymbol some length of time. The analysis weights the components by time, so a time of zero (default for ChordSymbols) will give you nonsense.

    d = harmony.ChordSymbol('D')
    d.quarterLength = 2
    s = stream.Stream([d])
    s.analyze('key')

Upvotes: 1

VR_1312
VR_1312

Reputation: 161

It looks like music21 Analyze is not working ok with ChordSymbol.

As an alternative, you can manually set all the chord's notes, and analyze that. The code:

string = 'D, Em, F#m, G, A, Bm'
 s = stream.Stream()

for d in string.split(','):
    print(harmony.ChordSymbol(d).pitchedCommonName)
    for p in harmony.ChordSymbol(d).pitches:
        n = note.Note()
        n.pitch = p 
        s.append(n)
key = s.analyze('key')
print(key)

returns a D major key, as expected.

Upvotes: 0

Related Questions