Table Of Contents

Previous topic

music21.node

Next topic

music21.stream

This Page

music21.pitch

Classes and functions for creating and manipulating pitches, pitch-space, and accidentals. Used extensively by note.py

music21.pitch.convertFqToPs(fq)

Utility conversion; does not process internals. Assumes A4 = 440 Hz

>>> convertFqToPs(440)
69.0
>>> convertFqToPs(261.62556530059862)
60.0
music21.pitch.convertNameToPitchClass(pitchName)

Utility conversion: from a pitch name to a pitch class integer between 0 and 11.

>>> convertNameToPitchClass('c4')
0
>>> convertNameToPitchClass('c#')
1
>>> convertNameToPitchClass('d-')
1
>>> convertNameToPitchClass('e--')
2
>>> convertNameToPitchClass('b2##')
1
music21.pitch.convertNameToPs(pitchName)

Utility conversion: from a pitch name to a pitch space number (floating point MIDI pitch values).

>>> convertNameToPs('c4')
60
>>> convertNameToPs('c2#')
37.0
>>> convertNameToPs('d7-')
97.0
>>> convertNameToPs('e1--')
26.0
>>> convertNameToPs('b2##')
49.0
music21.pitch.convertPitchClassToNumber(ps)

Given a pitch class or pitch class value, look for strings. If a string is found, replace it with the default pitch class representation.

>>> convertPitchClassToNumber(3)
3
>>> convertPitchClassToNumber('a')
10
>>> convertPitchClassToNumber('B')
11
music21.pitch.convertPitchClassToStr(pc)

Given a pitch class number, return a string.

>>> convertPitchClassToStr(3)
'3'
>>> convertPitchClassToStr(10)
'A'
music21.pitch.convertPsToFq(ps)

Utility conversion; does not process internals. Assumes A4 = 440 Hz

>>> convertPsToFq(69)
440.0
>>> convertPsToFq(60)
261.62556530059862
>>> convertPsToFq(2)
9.1770239974189884
>>> convertPsToFq(135)
19912.126958213179
music21.pitch.convertPsToOct(ps)

Utility conversion; does not process internals. Assume C4 middle C, so 60 returns 4

>>> [convertPsToOct(59), convertPsToOct(60), convertPsToOct(61)]
[3, 4, 4]
>>> [convertPsToOct(12), convertPsToOct(0), convertPsToOct(-12)]
[0, -1, -2]
>>> convertPsToOct(135)
10
music21.pitch.convertPsToStep(ps)

Utility conversion; does not process internals. Takes in a midiNote number (Assume C4 middle C, so 60 returns 4) Returns a tuple of Step name and either a natural or a sharp

>>> convertPsToStep(60)
('C', <accidental natural>)
>>> convertPsToStep(66)
('F', <accidental sharp>)
>>> convertPsToStep(67)
('G', <accidental natural>)
>>> convertPsToStep(68)
('G', <accidental sharp>)
>>> convertPsToStep(-2)
('A', <accidental sharp>)
>>> convertPsToStep(60.5)
('C', <accidental half-sharp>)
>>> convertPsToStep(61.5)
('C', <accidental one-and-a-half-sharp>)
>>> convertPsToStep(62)
('D', <accidental natural>)
>>> convertPsToStep(62.5)
('D', <accidental half-sharp>)
>>> convertPsToStep(135)
('D', <accidental sharp>)
music21.pitch.convertStepToPs(step, oct, acc=None)

Utility conversion; does not process internals.

>>> convertStepToPs('c', 4, 1)
61
>>> convertStepToPs('d', 2, -2)
36
>>> convertStepToPs('b', 3, 3)
62

Pitch

class music21.pitch.Pitch(name=None)

An object for storing pitch values. All values are represented internally as a scale step (self.step), and octave and an accidental object. In addition, pitches know their pitchSpace representation (self._ps); altering any of the first three changes the pitchSpace representation. Similarly, altering the pitchSpace representation alters the first three.

Create a Pitch. Optional parameter name should include a step and accidental character(s) it can also include a non-negative octave number. (“C#4”, “B–3”, etc.)

>>> p1 = Pitch('a#')
>>> p1
A#
>>> p2 = Pitch(3)
>>> p2
D#

inherits from: Music21Object

Pitch attributes

Attributes without Documentation: defaultOctave

Attributes inherited from Music21Object: id, groups

Pitch properties

name

Name presently returns pitch name and accidental without octave. Perhaps better named getNameClass

>>> a = Pitch('G#')
>>> a.name
'G#'
nameWithOctave
The pitch name with an octave designation. If no octave as been set, no octave value is returned.
step
>>> a = Pitch('C#3')
>>> a._getStep()
'C'
pitchClass
>>> a = Pitch('a3')
>>> a._getPitchClass()
9
>>> dis = Pitch('d3')
>>> dis.pitchClass
2
>>> dis.accidental = Accidental("#")
>>> dis.pitchClass
3
>>> dis.pitchClass = 11
>>> dis.pitchClass
11
>>> dis.name
'B'
octave

returns or sets the octave of the note. Setting the octave updates the pitchSpace attribute.

>>> a = Pitch('g')
>>> a.octave is None
True
>>> a.implicitOctave
4
>>> a.ps  ## will use implicitOctave
67
>>> a.name
'G'
>>> a.octave = 14
>>> a.implicitOctave
14
>>> a.name
'G'
>>> a.ps
187
midi
midi is ps (pitchSpace) as a rounded int; ps can accomodate floats
accidental
>>> a = Pitch('D-2')
>>> a.accidental.alter
-1.0
diatonicNoteNum

Returns an integer that uniquely identifies the note, ignoring accidentals. The number returned is the diatonic interval above C0 (the lowest C on a Boesendorfer Imperial Grand), so G0 = 5, C1 = 8, etc. Numbers can be negative for very low notes. C4 (middleC) = 29, C#4 = 29, C##4 = 29, D-4 = 30, D4 = 30, etc.

>>> c = Pitch('c4')
>>> c.diatonicNoteNum
29
>>> c = Pitch('c#4')
>>> c.diatonicNoteNum
29
>>> d = Pitch('d--4')
>>> d.accidental.name
'double-flat'
>>> d.diatonicNoteNum
30
>>> b = Pitch()
>>> b.step = "B"
>>> b.octave = -1
>>> b.diatonicNoteNum
0
>>> c = Pitch("C")
>>> c.diatonicNoteNum  #implicitOctave
29
freq440
>>> a = Pitch('A4')
>>> a.freq440
440.0
frequency
The frequency property gets or sets the frequency of the pitch in hertz. If the frequency has not been overridden, then it is computed based on A440Hz and equal temperament
german

returns the name of a Pitch in the German system (where B-flat = B, B = H, etc.) (Microtones raise an error).

>>> print Pitch('B-').german
B
>>> print Pitch('B').german
H
>>> print Pitch('E-').german
Es
>>> print Pitch('C#').german
Cis
>>> print Pitch('A--').german
Ases
>>> p1 = Pitch('C')
>>> p1.accidental = Accidental('half-sharp')
>>> p1.german
Traceback (most recent call last):
PitchException: Es geht nicht "german" zu benutzen mit Microtoenen.  Schade!
implicitOctave
returns the octave of the Pitch, or defaultOctave if octave was never set
musicxml
Provide a complete MusicXML representation. Presently, this is based on
mx

returns a musicxml.Note() object

>>> a = Pitch('g#4')
>>> c = a.mx
>>> c.get('pitch').get('step')
'G'
pitchClassString

Return a string representation of the pitch class, where integers greater than 10 are replaced by A and B, respectively. Can be used to set pitch class by a string representation as well (though this is also possible with pitchClass.

>>> a = Pitch('a3')
>>> a.pitchClassString = 'B'
>>> a.pitchClass
11
>>> a.pitchClassString
'B'
ps

The ps property permits getting and setting a pitch space value, a floating point number representing pitch space, where 60 is C4, middle C, integers are half-steps, and floating point values are microtonal tunings (.01 is equal to one cent).

>>> a = Pitch()
>>> a.ps = 45
>>> a
A2
>>> a.ps = 60
>>> a
C4
stepWithOctave

Returns the pitch step (F, G, etc) with octave designation. If no octave as been set, no octave value is returned.

>>> a = Pitch('G#4')
>>> a.stepWithOctave
'G4'
>>> a = Pitch('A#')
>>> a.stepWithOctave
'A'

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

Pitch methods

inheritDisplay(other)

Inherit display properties from another Pitch, including those found on the Accidental object.

>>>
>>> a = Pitch('c#')
>>> a.accidental.displayType = 'always'
>>> b = Pitch('c-')
>>> b.inheritDisplay(a)
>>> b.accidental.displayType
'always'
transpose(value, inPlace=False)

Transpose the pitch 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.

>>> aPitch = Pitch('g4')
>>> bPitch = aPitch.transpose('m3')
>>> bPitch
B-4
>>> aInterval = interval.Interval(-6)
>>> bPitch = aPitch.transpose(aInterval)
>>> bPitch
C#4
>>> aPitch
G4
>>> aPitch.transpose(aInterval, inPlace=True)
>>> aPitch
C#4
updateAccidentalDisplay(pitchPast=[], cautionaryPitchClass=True, cautionaryAll=False)

Given a list of Pitch objects, determine if this pitch’s Accidental object needs to be updated with a natural or other cautionary accidental. Changes to this Pitch object’s Accidental object are made in-place.

>>> a = Pitch('a')
>>> past = [Pitch('a#'), Pitch('c#'), Pitch('c')]
>>> a.updateAccidentalDisplay(past, cautionaryAll=True)
>>> a.accidental, a.accidental.displayStatus
(<accidental natural>, True)
>>> b = Pitch('a')
>>> past = [Pitch('a#'), Pitch('c#'), Pitch('c')]
>>> b.updateAccidentalDisplay(past) # should add a natural
>>> b.accidental, b.accidental.displayStatus
(<accidental natural>, True)
>>> c = Pitch('a4')
>>> past = [Pitch('a3#'), Pitch('c#'), Pitch('c')]
>>> # will not add a natural because match is pitchSpace
>>> c.updateAccidentalDisplay(past, cautionaryPitchClass=False)
>>> c.accidental == None
True

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

Accidental

class music21.pitch.Accidental(specifier=natural)

Accidental class.

inherits from: Music21Object

Accidental attributes

displaySize
Size in display: “cue”, “large”, or a percentage.
displayStatus
Given the displayType, should this accidental be displayed? Can be True, False, or None if not defined. For contexts where the next program down the line cannot evaluate displayType
displayStyle
Style of display: “parentheses”, “bracket”, “both”.
displayType
Display if first in measure; other valid terms: “always”, “never”, “unless-repeated” (show always unless the immediately preceding note is the same), “even-tied” (stronger than always: shows even if it is tied to the previous note)

Attributes without Documentation: displayLocation, modifier, name, alter

Attributes inherited from Music21Object: id

Accidental properties

lily
No documentation.
mx

From music21 to MusicXML

>>> a = Accidental()
>>> a.set('half-sharp')
>>> a.alter == .5
True
>>> mxAccidental = a.mx
>>> mxAccidental.get('content')
'quarter-sharp'

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

Accidental methods

set(name)

Provide a value to the Accidental. Strings values, numbers, and Lilypond Abbreviations are all accepted.

>>> a = Accidental()
>>> a.set('sharp')
>>> a.alter == 1
True
>>> a = Accidental()
>>> a.set(2)
>>> a.modifier == "##"
True
>>> a = Accidental()
>>> a.set(2.0)
>>> a.modifier == "##"
True
>>> a = Accidental('--')
>>> a.alter
-2.0
inheritDisplay(other)

Given another Accidental object, inherit all the display properites of that object. This is needed when transposing Pitches: we need to retain accidental display properties.

>>> a = Accidental('double-flat')
>>> a.displayType = 'always'
>>> b = Accidental('sharp')
>>> b.inheritDisplay(a)
>>> b.displayType
'always'

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