Music21 Analyze Key always returns c minor?
Asked Answered
L

2

1

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)
Librium answered 23/11, 2019 at 17:47 Comment(2)
Can you write an output of your code? If I use your code I get D-major triad E-minor triad F#-minor triad G-major triad A-major triad B-minor triad c minorColan
yeah that's exactly what I get but I was expecting D-major as the final keyLibrium
D
1

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')
Digression answered 13/3, 2021 at 2:32 Comment(0)
D
0

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.

Dornick answered 30/11, 2019 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.