Table Of Contents

Previous topic

music21.graph

Next topic

music21.instrument

This Page

music21.key

music21.key.keyFromString(strKey)
Given a string representing a key, return the appropriate Key object.
music21.key.pitchToSharps(value, mode=None)

Given a pitch or music21.pitch.Pitch object, return the number of sharps found in the major key. The mode parameter can be None, ‘major’, or ‘minor’.

>>> pitchToSharps('c')
0
>>> pitchToSharps('c', 'minor')
-3
>>> pitchToSharps('a', 'minor')
0
>>> pitchToSharps('d')
2
>>> pitchToSharps('e-')
-3
>>> pitchToSharps('a')
3
>>> pitchToSharps('e', 'minor')
1
>>> pitchToSharps('f#', 'major')
6
>>> pitchToSharps('g-', 'major')
-6
>>> pitchToSharps('c#')
7
>>> pitchToSharps('g#')
8
music21.key.sharpsToPitch(sharpCount)

Given a number a positive/negative number of sharps, return a Pitch object set to the appropriate major key value.

>>> sharpsToPitch(1)
G
>>> sharpsToPitch(1)
G
>>> sharpsToPitch(2)
D
>>> sharpsToPitch(-2)
B-
>>> sharpsToPitch(-6)
G-
>>> sharpsToPitch(6)
F#

KeySignature

class music21.key.KeySignature(sharps=None)
>>> a = KeySignature(3)
>>> a._strDescription()
'3 sharps'

inherits from: Music21Object

KeySignature attributes

Attributes without Documentation: sharps, mode, alteredTones

Attributes inherited from Music21Object: id, groups

KeySignature properties

mx

Returns a musicxml.KeySignature object

>>> a = KeySignature(3)
>>> a.sharps = -3
>>> mxKey = a.mx
>>> mxKey.get('fifths')
-3
pitchAndMode

Returns a a two value list containg a music21.pitch.Pitch object that names this key and the value of mode.

>>> keyArray = [KeySignature(x) for x in range(-7,8)]
>>> keyArray[0].pitchAndMode
(C-, None)
>>> keyArray[1].pitchAndMode
(G-, None)
>>> keyArray[2].pitchAndMode
(D-, None)
>>> keyArray[3].pitchAndMode
(A-, None)
>>> keyArray[4].pitchAndMode
(E-, None)
>>> keyArray[5].pitchAndMode
(B-, None)
>>> keyArray[6].pitchAndMode
(F, None)
>>> keyArray[7].pitchAndMode
(C, None)
>>> keyArray[8].pitchAndMode
(G, None)

Properties inherited from Music21Object: duration, offset, parent, priority

KeySignature methods

transpose(value, inPlace=False)

Tranpose the KeySignature by the user-provided value. If the value is an integer, the transposition is treated in half steps. If the value is a string, any Interval string specification can be provided. Alternatively, a music21.interval.Interval object can be supplied.

>>> a = KeySignature(2)
>>> a.pitchAndMode
(D, None)
>>> b = a.transpose('p5')
>>> b.pitchAndMode
(A, None)
>>> b.sharps
3
>>> c = b.transpose('-m2')
>>> c.pitchAndMode
(G#, None)
>>> c.sharps
8
>>> d = c.transpose('-a3')
>>> d.pitchAndMode
(E-, None)
>>> d.sharps
-3

Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndParent(), freezeIds(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), hasContext(), isClass(), purgeLocations(), removeLocation(), show(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()

Key

class music21.key.Key(stream1=None)

Note that a key is a sort of hypothetical/conceptual object. It probably has a scale (or scales) associated with it and a KeySignature, but not necessarily.

inherits from: Music21Object

Key attributes

Attributes without Documentation: accidental, typeList, step, stepList, stream1, accidentalList, type

Attributes inherited from Music21Object: id

Key properties

Properties inherited from Music21Object: duration, offset, parent, priority

Key methods

generateKey()
No documentation.
setKey(name=C, accidental=None, type=major)
No documentation.

Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndParent(), freezeIds(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), hasContext(), isClass(), purgeLocations(), removeLocation(), show(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()