.. _moduleKey:

music21.key
===========

.. WARNING: DO NOT EDIT THIS FILE: AUTOMATICALLY GENERATED

.. module:: music21.key

This module defines objects for representing key signatures, as well as key areas. The :class:`~music21.key.KeySignature` is used in :class:`~music21.stream.Measure` objects for defining notated key signatures. 


.. function:: keyFromString(strKey)

    Given a string representing a key, return the appropriate Key object. 

.. function:: pitchToSharps(value, mode=None)

    Given a pitch or :class:`music21.pitch.Pitch` object, return the number of sharps found in the major key. The `mode` parameter can be None (=Major), 'major', or 'minor'. 

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

.. function:: sharpsToPitch(sharpCount)

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

    >>> from music21 import *
    >>> key.sharpsToPitch(1)
    G 
    >>> key.sharpsToPitch(1)
    G 
    >>> key.sharpsToPitch(2)
    D 
    >>> key.sharpsToPitch(-2)
    B- 
    >>> key.sharpsToPitch(-6)
    G- 
    Note that these are :class:`music21.pitch.Pitch` objects not just names: 
    >>> k1 = key.sharpsToPitch(6)
    >>> k1
    F# 
    >>> k1.step
    'F' 
    >>> k1.accidental
    <accidental sharp> 

KeySignature
------------

Inherits from: :class:`~music21.base.Music21Object`

.. class:: KeySignature(sharps=None)


    

    >>> from music21 import *
    >>> a = key.KeySignature(3)
    >>> a._strDescription()
    '3 sharps' 

    **KeySignature** **attributes**

        .. attribute:: classSortOrder

            Property which returns an number (int or otherwise) depending on the class of the Music21Object that represents a priority for an object based on its class alone -- used as a tie for stream sorting in case two objects have the same offset and priority.  Lower numbers are sorted to the left of higher numbers.  For instance, Clef, KeySignature, TimeSignature all come (in that order) before Note. All undefined classes have classSortOrder of 20 -- same as note.Note 

            >>> from music21 import *
            >>> tc = clef.TrebleClef()
            >>> tc.classSortOrder
            0 
            >>> ks = key.KeySignature(3)
            >>> ks.classSortOrder
            1 
            New classes can define their own default classSortOrder 
            >>> class ExampleClass(base.Music21Object):
            ...     classSortOrderDefault = 5 
            ... 
            >>> ec1 = ExampleClass()
            >>> ec1.classSortOrder
            5 

        Attributes without Documentation: `sharps`, `mode`

        Attributes inherited from :class:`~music21.base.Music21Object`: :attr:`~music21.base.Music21Object.id`, :attr:`~music21.base.Music21Object.groups`

    **KeySignature** **properties**

        .. attribute:: alteredPitches

            Return a list of music21.pitch.Pitch objects that are altered by this KeySignature. That is, all Pitch objects that will receive an accidental. 

            >>> from music21 import *
            >>> a = key.KeySignature(3)
            >>> a.alteredPitches
            [F#, C#, G#] 
            >>> a = key.KeySignature(1)
            >>> a.alteredPitches
            [F#] 
            >>> a = key.KeySignature(9)
            >>> a.alteredPitches
            [F#, C#, G#, D#, A#, E#, B#, F##, C##] 
            >>> a = key.KeySignature(-3)
            >>> a.alteredPitches
            [B-, E-, A-] 
            >>> a = key.KeySignature(-1)
            >>> a.alteredPitches
            [B-] 
            >>> a = key.KeySignature(-6)
            >>> a.alteredPitches
            [B-, E-, A-, D-, G-, C-] 
            >>> a = key.KeySignature(-8)
            >>> a.alteredPitches
            [B-, E-, A-, D-, G-, C-, F-, B--] 

        .. attribute:: lily

            returns the Lilypond representation of a KeySignature object 

            >>> from music21 import *
            >>> d = key.KeySignature(-1)
            >>> d.mode = 'minor'
            >>> print d.lily
            \key d \minor 
            Major is assumed: 
            >>> fsharp = key.KeySignature(6)
            >>> print fsharp.lily
            \key fis \major 

        .. attribute:: mx

            Returns a musicxml.KeySignature object 

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

        .. attribute:: pitchAndMode

            Returns a a two value list containing a :class:`music21.pitch.Pitch` object that names this key and the value of :attr:`~music21.key.KeySignature.mode`. 

            >>> from music21 import *
            >>> key.KeySignature(-7).pitchAndMode
            (C-, None) 
            >>> key.KeySignature(-6).pitchAndMode
            (G-, None) 
            >>> key.KeySignature(-3).pitchAndMode
            (E-, None) 
            >>> key.KeySignature(0).pitchAndMode
            (C, None) 
            >>> key.KeySignature(1).pitchAndMode
            (G, None) 
            >>> csharp = key.KeySignature(4)
            >>> csharp.mode = "minor"
            >>> csharp.pitchAndMode
            (C#, 'minor') 
            >>> csharpPitch = csharp.pitchAndMode[0]
            >>> csharpPitch.accidental
            <accidental sharp> 

        Properties inherited from :class:`~music21.base.Music21Object`: :attr:`~music21.base.Music21Object.classes`, :attr:`~music21.base.Music21Object.duration`, :attr:`~music21.base.Music21Object.offset`, :attr:`~music21.base.Music21Object.parent`, :attr:`~music21.base.Music21Object.priority`

    **KeySignature** **methods**

        .. method:: accidentalByStep(step)

            given a step (C, D, E, F, etc.) return the accidental for that note in this key (using the natural minor for minor) or None if there is none. 

            >>> from music21 import *
            >>> g = key.KeySignature(1)
            >>> g.accidentalByStep("F")
            <accidental sharp> 
            >>> g.accidentalByStep("G")
            >>> f = KeySignature(-1)
            >>> bbNote = note.Note("B-5")
            >>> f.accidentalByStep(bbNote.step)
            <accidental flat> 

            
            Fix a wrong note in F-major: 

            
            >>> wrongBNote = note.Note("B#4")
            >>> if f.accidentalByStep(wrongBNote.step) != wrongBNote.accidental:
            ...    wrongBNote.accidental = f.accidentalByStep(wrongBNote.step) 
            >>> wrongBNote
            <music21.note.Note B-> 

            
            Set all notes to the correct notes for a key using the note's Context: 

            
            >>> from music21 import *
            >>> s1 = stream.Stream()
            >>> s1.append(key.KeySignature(4))  # E-major or C-sharp-minor
            >>> s1.append(note.HalfNote("C"))
            >>> s1.append(note.HalfNote("E-"))
            >>> s1.append(key.KeySignature(-4)) # A-flat-major or F-minor
            >>> s1.append(note.WholeNote("A"))
            >>> s1.append(note.WholeNote("F#"))
            >>> for n in s1.notes:
            ...    n.accidental = n.getContextByClass(key.KeySignature).accidentalByStep(n.step) 
            >>> s1.show()
            

        .. image:: images/keyAccidentalByStep.* 
            :width: 400

        .. method:: transpose(value, inPlace=False)

            Transpose 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 :class:`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 :class:`~music21.base.Music21Object`: :meth:`~music21.base.Music21Object.searchParentByAttr`, :meth:`~music21.base.Music21Object.getContextAttr`, :meth:`~music21.base.Music21Object.setContextAttr`, :meth:`~music21.base.Music21Object.addContext`, :meth:`~music21.base.Music21Object.addLocation`, :meth:`~music21.base.Music21Object.addLocationAndParent`, :meth:`~music21.base.Music21Object.freezeIds`, :meth:`~music21.base.Music21Object.getContextByClass`, :meth:`~music21.base.Music21Object.getOffsetBySite`, :meth:`~music21.base.Music21Object.getSiteIds`, :meth:`~music21.base.Music21Object.getSites`, :meth:`~music21.base.Music21Object.hasContext`, :meth:`~music21.base.Music21Object.isClass`, :meth:`~music21.base.Music21Object.purgeLocations`, :meth:`~music21.base.Music21Object.removeLocationBySite`, :meth:`~music21.base.Music21Object.removeLocationBySiteId`, :meth:`~music21.base.Music21Object.setOffsetBySite`, :meth:`~music21.base.Music21Object.show`, :meth:`~music21.base.Music21Object.unfreezeIds`, :meth:`~music21.base.Music21Object.unwrapWeakref`, :meth:`~music21.base.Music21Object.wrapWeakref`, :meth:`~music21.base.Music21Object.write`


Key
---

Inherits from: :class:`~music21.base.Music21Object`

.. class:: 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. 

    **Key** **attributes**

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

        Attributes inherited from :class:`~music21.base.Music21Object`: :attr:`~music21.base.Music21Object.classSortOrder`, :attr:`~music21.base.Music21Object.id`

    **Key** **properties**

        Properties inherited from :class:`~music21.base.Music21Object`: :attr:`~music21.base.Music21Object.classes`, :attr:`~music21.base.Music21Object.duration`, :attr:`~music21.base.Music21Object.offset`, :attr:`~music21.base.Music21Object.parent`, :attr:`~music21.base.Music21Object.priority`

    **Key** **methods**

        .. method:: generateKey()

            No documentation. 

        .. method:: setKey(name=C, accidental=None, type=major)

            No documentation. 

        Methods inherited from :class:`~music21.base.Music21Object`: :meth:`~music21.base.Music21Object.searchParentByAttr`, :meth:`~music21.base.Music21Object.getContextAttr`, :meth:`~music21.base.Music21Object.setContextAttr`, :meth:`~music21.base.Music21Object.addContext`, :meth:`~music21.base.Music21Object.addLocation`, :meth:`~music21.base.Music21Object.addLocationAndParent`, :meth:`~music21.base.Music21Object.freezeIds`, :meth:`~music21.base.Music21Object.getContextByClass`, :meth:`~music21.base.Music21Object.getOffsetBySite`, :meth:`~music21.base.Music21Object.getSiteIds`, :meth:`~music21.base.Music21Object.getSites`, :meth:`~music21.base.Music21Object.hasContext`, :meth:`~music21.base.Music21Object.isClass`, :meth:`~music21.base.Music21Object.purgeLocations`, :meth:`~music21.base.Music21Object.removeLocationBySite`, :meth:`~music21.base.Music21Object.removeLocationBySiteId`, :meth:`~music21.base.Music21Object.setOffsetBySite`, :meth:`~music21.base.Music21Object.show`, :meth:`~music21.base.Music21Object.unfreezeIds`, :meth:`~music21.base.Music21Object.unwrapWeakref`, :meth:`~music21.base.Music21Object.wrapWeakref`, :meth:`~music21.base.Music21Object.write`


