This module defines various types of interval objects. Fundamental classes are Interval, DiatonicInterval, and ChromaticInterval. Numerous utility functions are provided for processing and generating intervals. module authors: Michael Scott Cuthbert, Jackie Rogoff, Amy Hailes, and Christopher Ariza
Given two Note objects, returns a ChromaticInterval object.
>>> from music21 import note
>>> aNote = note.Note('c4')
>>> bNote = note.Note('g#5')
>>> notesToChromatic(aNote, bNote)
<music21.interval.ChromaticInterval 20>
Given a GenericInterval and a ChromaticInterval object, return a DiatonicInterval.
>>> aInterval = GenericInterval('descending fifth')
>>> bInterval = ChromaticInterval(-7)
>>> cInterval = intervalsToDiatonic(aInterval, bInterval)
>>> cInterval
<music21.interval.DiatonicInterval P5>
Given two Note objects, returns an Interval object. The same functionality is available by calling the Interval class with two Notes as arguments.
>>> from music21 import note
>>> aNote = note.Note('c4')
>>> bNote = note.Note('g5')
>>> aInterval = notesToInterval(aNote, bNote)
>>> aInterval
<music21.interval.Interval P12>
>>> bInterval = Interval(noteStart=aNote, noteEnd=bNote)
>>> aInterval.niceName == bInterval.niceName
True
Convert a diatonic number to a step name (without accidental) and a octave integer. The lowest C on a Bosendorfer Imperial Grand is assigned 1 the D above it is 2, E is 3, etc. See pitch.diatonicNoteNum for more details
>>> convertDiatonicNumberToStep(15)
('C', 2)
>>> convertDiatonicNumberToStep(23)
('D', 3)
>>> convertDiatonicNumberToStep(0)
('B', -1)
>>> convertDiatonicNumberToStep(1)
('C', 0)
Extremely high and absurdly low numbers also produce “notes”.
>>> convertDiatonicNumberToStep(2100)
('B', 299)
>>> convertDiatonicNumberToStep(-19)
('D', -3)
Convert an interval specified in terms of its name (second, third) into an integer. If integers are passed, assume the are correct.
>>> convertGeneric(3)
3
>>> convertGeneric('third')
3
>>> convertGeneric('3rd')
3
>>> convertGeneric('octave')
8
>>> convertGeneric('twelfth')
12
>>> convertGeneric('descending twelfth')
-12
>>> convertGeneric(12)
12
>>> convertGeneric(-12)
-12
>>> convertGeneric(1)
1
>>> convertGeneric(None)
Traceback (most recent call last):
IntervalException: Cannot get a direction from None
>>> convertGeneric("1")
Traceback (most recent call last):
IntervalException: Cannot get a direction from 1
Given a number of semitones, return a default diatonic specifier.
>>> convertSemitoneToSpecifierGeneric(0)
('P', 1)
>>> convertSemitoneToSpecifierGeneric(-2)
('M', -2)
>>> convertSemitoneToSpecifierGeneric(1)
('m', 2)
>>> convertSemitoneToSpecifierGeneric(7)
('P', 5)
>>> convertSemitoneToSpecifierGeneric(11)
('M', 7)
>>> convertSemitoneToSpecifierGeneric(12)
('P', 8)
>>> convertSemitoneToSpecifierGeneric(13)
('m', 9)
>>> convertSemitoneToSpecifierGeneric(-15)
('m', -10)
>>> convertSemitoneToSpecifierGeneric(24)
('P', 15)
Given an integer or a string, return the integer for the appropriate specifier. This permits specifiers to specified in a flexible manner.
>>> convertSpecifier(3)
(3, 'm')
>>> convertSpecifier('p')
(1, 'P')
>>> convertSpecifier('P')
(1, 'P')
>>> convertSpecifier('M')
(2, 'M')
>>> convertSpecifier('major')
(2, 'M')
>>> convertSpecifier('m')
(3, 'm')
>>> convertSpecifier('Augmented')
(4, 'A')
>>> convertSpecifier('a')
(4, 'A')
>>> convertSpecifier(None)
(None, None)
Returns the interval number from the given staff distance.
>>> convertStaffDistanceToInterval(3)
4
>>> convertStaffDistanceToInterval(7)
8
Given two Note objects, returns the higher note based on actual pitch. If both pitches are the same, returns the first note given.
>>> from music21 import note
>>> aNote = note.Note('c#3')
>>> bNote = note.Note('d--3')
>>> getAbsoluteHigherNote(aNote, bNote)
<music21.note.Note C#>
Given two Note objects, returns the lower note based on actual pitch. If both pitches are the same, returns the first note given.
>>> from music21 import note
>>> aNote = note.Note('c#3')
>>> bNote = note.Note('d--3')
>>> getAbsoluteLowerNote(aNote, bNote)
<music21.note.Note D-->
Given two Note or Pitch objects, this function returns the higher object based on diatonic note numbers. Returns the note higher in pitch if the diatonic number is the same, or the first note if pitch is also the same.
>>> from music21 import pitch
>>> cis = pitch.Pitch("C#")
>>> deses = pitch.Pitch("D--")
>>> higher = getWrittenHigherNote(cis, deses)
>>> higher is deses
True
>>> from music21 import note
>>> aNote = note.Note('c#3')
>>> bNote = note.Note('d-3')
>>> getWrittenHigherNote(aNote, bNote)
<music21.note.Note D->
>>> aNote = note.Note('c#3')
>>> bNote = note.Note('d--3')
>>> getWrittenHigherNote(aNote, bNote)
<music21.note.Note D-->
Given two Note objects, returns the lower note based on diatonic note number. Returns the note lower in pitch if the diatonic number is the same, or the first note if pitch is also the same.
>>> from music21 import note
>>> aNote = note.Note('c#3')
>>> bNote = note.Note('d--3')
>>> getWrittenLowerNote(aNote, bNote)
<music21.note.Note C#>
>>> from music21 import note
>>> aNote = note.Note('c#3')
>>> bNote = note.Note('d-3')
>>> getWrittenLowerNote(aNote, bNote)
<music21.note.Note C#>
Given two Note objects, returns a GenericInterval object.
>>> from music21 import note
>>> aNote = note.Note('c4')
>>> bNote = note.Note('g5')
>>> aInterval = notesToGeneric(aNote, bNote)
>>> aInterval
<music21.interval.GenericInterval 12>
Given an interval string (such as “P5”, “m3”, “A2”) return a Interval object.
>>> aInterval = stringToInterval('P5')
>>> aInterval
<music21.interval.Interval P5>
>>> aInterval = stringToInterval('m3')
>>> aInterval
<music21.interval.Interval m3>
Given a Note and a interval string (such as ‘P5’) or an Interval object, return a new Note object at the appropriate pitch level.
>>> from music21 import note
>>> aNote = note.Note('c4')
>>> bNote = transposeNote(aNote, 'p5')
>>> bNote
<music21.note.Note G>
>>> bNote.pitch
G4
>>> aNote = note.Note('f#4')
>>> bNote = transposeNote(aNote, 'm2')
>>> bNote
<music21.note.Note G>
Given a Pitch and a Interval object, return a new Pitch object at the appropriate pitch level.
>>> from music21 import pitch
>>> aPitch = pitch.Pitch('C4')
>>> aInterval = Interval('P5')
>>> bPitch = transposePitch(aPitch, aInterval)
>>> bPitch
G4
>>> bInterval = stringToInterval('P-5')
>>> cPitch = transposePitch(aPitch, bInterval)
>>> cPitch
F3
Inherits from: Music21Object
An Interval class that encapsulates both Chromatic and Diatonic intervals all in one model. The interval is specified either as named arguments, a DiatonicInterval and a ChromaticInterval, or two Note objects, from which both a ChromaticInterval and DiatonicInterval are derived.
>>> from music21 import *
>>> n1 = note.Note('c3')
>>> n2 = note.Note('c5')
>>> aInterval = interval.Interval(noteStart=n1, noteEnd=n2)
>>> aInterval
<music21.interval.Interval P15>
>>> from music21 import *
>>> aInterval = interval.Interval('m3')
>>> aInterval
<music21.interval.Interval m3>
>>> aInterval = interval.Interval('M3')
>>> aInterval
<music21.interval.Interval M3>
>>> aInterval = interval.Interval('p5')
>>> aInterval
<music21.interval.Interval P5>
>>> aInterval.isChromaticStep
False
>>> aInterval.isDiatonicStep
False
>>> aInterval.isStep
False
>>> aInterval = interval.Interval('half')
>>> aInterval
<music21.interval.Interval m2>
>>> aInterval.isChromaticStep
True
>>> aInterval.isDiatonicStep
True
>>> aInterval.isStep
True
>>> aInterval = interval.Interval('-h')
>>> aInterval
<music21.interval.Interval m-2>
>>> aInterval = interval.Interval(3)
>>> aInterval
<music21.interval.Interval m3>
>>> aInterval = interval.Interval(7)
>>> aInterval
<music21.interval.Interval P5>
>>> n1 = note.Note('c3')
>>> n2 = note.Note('g3')
>>> aInterval = interval.Interval(noteStart=n1, noteEnd=n2)
>>> aInterval
<music21.interval.Interval P5>
>>> aInterval = interval.Interval(noteStart=n1, noteEnd=None)
Traceback (most recent call last):
IntervalException: either both the starting and the ending note.Note must be given or neither can be given. You cannot have one without the other.
>>> aInterval = interval.DiatonicInterval('major', 'third')
>>> bInterval = interval.ChromaticInterval(4)
>>> cInterval = interval.Interval(diatonic=aInterval, chromatic=bInterval)
>>> cInterval
<music21.interval.Interval M3>
>>> cInterval = interval.Interval(diatonic=aInterval, chromatic=None)
Traceback (most recent call last):
IntervalException: either both a DiatonicInterval and a ChromaticInterval object have to be given or neither can be given. You cannot have one without the other.
Interval attributes
Attributes without Documentation: chromatic, diatonic, diatonicType, direction, generic, niceName, type
Attributes inherited from Music21Object: classSortOrder, id, groups
Interval properties
- complement¶
Return a new Interval object that is the complement of this Interval.
>>> aInterval = Interval('M3') >>> bInterval = aInterval.complement >>> bInterval <music21.interval.Interval m6>
- intervalClass¶
Return the interval class from the chromatic interval.
>>> aInterval = Interval('M3') >>> aInterval.intervalClass 4
- noteEnd¶
Assuming this Interval has been defined, set the end note (_noteEnd) to a new value; this will adjust the value of the start note (_noteStart).
>>> from music21 import note >>> aInterval = Interval('M3') >>> aInterval.noteEnd = note.Note('e4') >>> aInterval.noteStart.nameWithOctave 'C4' >>> aInterval = Interval('m2') >>> aInterval.noteEnd = note.Note('A#3') >>> aInterval.noteStart.nameWithOctave 'G##3' >>> n1 = note.Note('g#3') >>> n2 = note.Note('c3') >>> aInterval = Interval(n1, n2) >>> aInterval.directedName # downward augmented fifth 'A-5' >>> aInterval.noteEnd = note.Note('c4') >>> aInterval.noteStart.nameWithOctave 'G#4' >>> aInterval = Interval('M3') >>> aInterval.noteEnd = note.Note('A-3') >>> aInterval.noteStart.nameWithOctave 'F-3'
- noteStart¶
Assuming this Interval has been defined, set the start note (_noteStart) to a new value; this will adjust the value of the end note (_noteEnd).
>>> from music21 import * >>> aInterval = interval.Interval('M3') >>> aInterval.noteStart = note.Note('c4') >>> aInterval.noteEnd.nameWithOctave 'E4' >>> n1 = note.Note('c3') >>> n2 = note.Note('g#3') >>> aInterval = interval.Interval(n1, n2) >>> aInterval.name 'A5' >>> aInterval.noteStart = note.Note('g4') >>> aInterval.noteEnd.nameWithOctave 'D#5' >>> aInterval = interval.Interval('-M3') >>> aInterval.noteStart = note.Note('c4') >>> aInterval.noteEnd.nameWithOctave 'A-3' >>> aInterval = interval.Interval('M-2') >>> aInterval.noteStart = note.Note('A#3') >>> aInterval.noteEnd.nameWithOctave 'G#3' >>> aInterval = interval.Interval('h') >>> aInterval.directedName 'm2' >>> aInterval.noteStart = note.Note('F#3') >>> aInterval.noteEnd.nameWithOctave 'G3'Properties inherited from Music21Object: beat, beatDuration, beatStr, beatStrength, classes, duration, measureNumberLocal, offset, parent, priority
Interval methods
- reinit()¶
- Reinitialize the internal interval objects in case something has changed. Called during __init__ to assign attributes.
- reverse()¶
Return an reversed version of this interval. If given Notes, these notes are reversed.
>>> from music21 import * >>> n1 = note.Note('c3') >>> n2 = note.Note('g3') >>> aInterval = interval.Interval(noteStart=n1, noteEnd=n2) >>> aInterval <music21.interval.Interval P5> >>> bInterval = aInterval.reverse() >>> bInterval <music21.interval.Interval P-5> >>> bInterval.noteStart == aInterval.noteEnd True >>> aInterval = interval.Interval('m3') >>> aInterval.reverse() <music21.interval.Interval m-3>
- transposePitch(p, reverse=False, clearAccidentalDisplay=True)¶
Given a Pitch object, return a new, transposed Pitch, that is transformed according to this Interval. This is the main public interface to all transposition routines found on higher-level objects.
>>> from music21 import * >>> p1 = pitch.Pitch('a#') >>> i = interval.Interval('m3') >>> p2 = i.transposePitch(p1) >>> p2 C#5 >>> p2 = i.transposePitch(p1, reverse=True) >>> p2 F##4Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndParent(), freezeIds(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), hasContext(), isClass(), purgeLocations(), removeLocationBySite(), removeLocationBySiteId(), setOffsetBySite(), show(), splitAtDurations(), splitAtQuarterLength(), splitByQuarterLengths(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()
Inherits from: Music21Object
Chromatic interval class. Unlike a Diatonic interval, this Interval class treats interval spaces in half-steps.
>>> aInterval = ChromaticInterval(-14)
>>> aInterval.semitones
-14
>>> aInterval.undirected
14
>>> aInterval.mod12
10
>>> aInterval.intervalClass
2
>>> aInterval.isChromaticStep
False
>>> aInterval.isStep
False
>>> aInterval = ChromaticInterval(1)
>>> aInterval.isChromaticStep
True
>>> aInterval.isStep
True
ChromaticInterval attributes
Attributes without Documentation: directed, direction, simpleUndirected, semitones, isChromaticStep, cents, simpleDirected, isStep, mod12, undirected, intervalClass
Attributes inherited from Music21Object: classSortOrder, id, groups
ChromaticInterval properties
Properties inherited from Music21Object: beat, beatDuration, beatStr, beatStrength, classes, duration, measureNumberLocal, offset, parent, priority
ChromaticInterval methods
- getDiatonic()¶
Given a Chromatic interval, return a Diatonic interval object. While there is more than one Generic Interval for any given chromatic interval, this is needed to to permit easy chromatic specification of Interval objects.
>>> aInterval = ChromaticInterval(5) >>> aInterval.getDiatonic() <music21.interval.DiatonicInterval P4> >>> aInterval = ChromaticInterval(7) >>> aInterval.getDiatonic() <music21.interval.DiatonicInterval P5> >>> aInterval = ChromaticInterval(11) >>> aInterval.getDiatonic() <music21.interval.DiatonicInterval M7>
- reverse()¶
Return an inverted interval, that is, reversing the direction.
>>> aInterval = ChromaticInterval(-14) >>> aInterval.reverse() <music21.interval.ChromaticInterval 14> >>> aInterval = ChromaticInterval(3) >>> aInterval.reverse() <music21.interval.ChromaticInterval -3>Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndParent(), freezeIds(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), hasContext(), isClass(), purgeLocations(), removeLocationBySite(), removeLocationBySiteId(), setOffsetBySite(), show(), splitAtDurations(), splitAtQuarterLength(), splitByQuarterLengths(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()
Inherits from: Music21Object
A class representing a diatonic interval. Two required arguments are a specifier (such as perfect, major, or minor) and a generic, an interval size (such as 2, 2nd, or second). A DiatonicInterval contains and encapsulates a GenericInterval
The specifier is an integer specifying a value in the prefixSpecs and niceSpecNames lists. The generic is an integer or GenericInterval instance.
>>> aInterval = DiatonicInterval(1, 1)
>>> aInterval.simpleName
'P1'
>>> aInterval = DiatonicInterval('p', 1)
>>> aInterval.simpleName
'P1'
>>> aInterval = DiatonicInterval('major', 3)
>>> aInterval.simpleName
'M3'
>>> aInterval.niceName
'Major Third'
>>> aInterval.semiSimpleName
'M3'
>>> aInterval.directedSimpleName
'M3'
>>> aInterval.invertedOrderedSpecifier
'm'
>>> aInterval.mod7
'M3'
>>> aInterval = DiatonicInterval('major', 'third')
>>> aInterval.niceName
'Major Third'
>>> aInterval = DiatonicInterval('perfect', 'octave')
>>> aInterval.niceName
'Perfect Octave'
>>> aInterval = DiatonicInterval('minor', 10)
>>> aInterval.mod7
'm3'
>>> aInterval.isDiatonicStep
False
>>> aInterval.isStep
False
>>> aInterval = DiatonicInterval('major', 2)
>>> aInterval.isDiatonicStep
True
>>> aInterval.isStep
True
DiatonicInterval attributes
- directedName¶
- The name of the interval in abbreviated form with direction.
- directedNiceName¶
- The name of the interval in full form with direction.
- niceName¶
- The name of the interval in full form.
- generic¶
- A GenericInterval is an interval such as Third, Seventh, Octave, or Tenth. Constructor takes an integer or string specifying the interval and direction. The interval is not specified in half-steps, but in numeric values derived from interval names: a Third is 3; a Seventh is 7, etc. String values for interval names (‘3rd’ or ‘third’) are accepted. staffDistance: the number of lines or spaces apart, eg: E.g. C4 to C4 = 0; C4 to D4 = 1; C4 to B3 = -1
- name¶
- The name of the interval in abbreviated form without direction.
Attributes without Documentation: specifier, directedSimpleName, directedSimpleNiceName, mod7, mod7inversion, simpleName, semiSimpleName, simpleNiceName, semiSimpleNiceName, isDiatonicStep, invertedOrderedSpecIndex, invertedOrderedSpecifier, prefectable, isStep, orderedSpecifierIndex, specificName
Attributes inherited from Music21Object: classSortOrder, id, groups
DiatonicInterval properties
Properties inherited from Music21Object: beat, beatDuration, beatStr, beatStrength, classes, duration, measureNumberLocal, offset, parent, priority
DiatonicInterval methods
- getChromatic()¶
Return a Chromatic interval based on the size of this Interval.
>>> aInterval = DiatonicInterval('major', 'third') >>> aInterval.niceName 'Major Third' >>> aInterval.getChromatic() <music21.interval.ChromaticInterval 4> >>> aInterval = DiatonicInterval('augmented', -5) >>> aInterval.niceName 'Augmented Fifth' >>> aInterval.getChromatic() <music21.interval.ChromaticInterval -8> >>> aInterval = DiatonicInterval('minor', 'second') >>> aInterval.niceName 'Minor Second' >>> aInterval.getChromatic() <music21.interval.ChromaticInterval 1>
- reverse()¶
Return a DiatonicInterval that is an inversion of this Interval.
>>> aInterval = DiatonicInterval('major', 3) >>> aInterval.reverse().directedName 'M-3' >>> aInterval = DiatonicInterval('augmented', 5) >>> aInterval.reverse().directedName 'A-5'Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndParent(), freezeIds(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), hasContext(), isClass(), purgeLocations(), removeLocationBySite(), removeLocationBySiteId(), setOffsetBySite(), show(), splitAtDurations(), splitAtQuarterLength(), splitByQuarterLengths(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()
Inherits from: Music21Object
A GenericInterval is an interval such as Third, Seventh, Octave, or Tenth. Constructor takes an integer or string specifying the interval and direction. The interval is not specified in half-steps, but in numeric values derived from interval names: a Third is 3; a Seventh is 7, etc. String values for interval names (‘3rd’ or ‘third’) are accepted. staffDistance: the number of lines or spaces apart, eg: E.g. C4 to C4 = 0; C4 to D4 = 1; C4 to B3 = -1
>>> from music21 import *
>>> aInterval = interval.GenericInterval(3)
>>> aInterval.direction
1
>>> aInterval.perfectable
False
>>> aInterval.staffDistance
2
>>> aInterval = interval.GenericInterval('Third')
>>> aInterval.staffDistance
2
>>> aInterval = interval.GenericInterval(-12)
>>> aInterval.niceName
'Twelfth'
>>> aInterval.perfectable
True
>>> aInterval.staffDistance
-11
>>> aInterval.mod7
4
>>> bInterval = aInterval.complement()
>>> bInterval.staffDistance
3
>>> aInterval = interval.GenericInterval('descending twelfth')
>>> aInterval.perfectable
True
>>> aInterval.staffDistance
-11
>>> aInterval = interval.GenericInterval(0)
Traceback (most recent call last):
IntervalException: The Zeroth is not an interval
>>> aInterval = interval.GenericInterval(24)
>>> aInterval.niceName
'24'
>>> aInterval.isDiatonicStep
False
>>> aInterval.isStep
False
>>> aInterval = interval.GenericInterval(2)
>>> aInterval.isDiatonicStep
True
>>> aInterval.isStep
True
GenericInterval attributes
Attributes without Documentation: simpleUndirected, octaves, niceName, semiSimpleUndirected, simpleDirected, isSkip, mod7, mod7inversion, simpleNiceName, semiSimpleNiceName, undirected, isDiatonicStep, direction, perfectable, undirectedOctaves, directed, staffDistance, value, semiSimpleDirected, isStep
Attributes inherited from Music21Object: classSortOrder, id, groups
GenericInterval properties
Properties inherited from Music21Object: beat, beatDuration, beatStr, beatStrength, classes, duration, measureNumberLocal, offset, parent, priority
GenericInterval methods
- complement()¶
Returns a new GenericInterval object where descending 3rds are 6ths, etc.
>>> aInterval = GenericInterval('Third') >>> aInterval.complement() <music21.interval.GenericInterval 6>
- getDiatonic(specifier)¶
Given a specifier, return a DiatonicInterval object. Specifier should be provided as a string name, such as ‘dd’, ‘M’, or ‘perfect’.
>>> aInterval = GenericInterval('Third') >>> aInterval.getDiatonic('major') <music21.interval.DiatonicInterval M3> >>> aInterval.getDiatonic('minor') <music21.interval.DiatonicInterval m3> >>> aInterval.getDiatonic('d') <music21.interval.DiatonicInterval d3> >>> aInterval.getDiatonic('a') <music21.interval.DiatonicInterval A3> >>> aInterval.getDiatonic(2) <music21.interval.DiatonicInterval M3> >>> bInterval = GenericInterval('fifth') >>> bInterval.getDiatonic('perfect') <music21.interval.DiatonicInterval P5>
- reverse()¶
Returns a new GenericInterval object that is inverted.
>>> aInterval = GenericInterval('Third') >>> aInterval.reverse() <music21.interval.GenericInterval -3> >>> aInterval = GenericInterval(-13) >>> aInterval.direction -1 >>> aInterval.reverse() <music21.interval.GenericInterval 13>Unisons invert to unisons
>>> aInterval = GenericInterval(1) >>> aInterval.reverse() <music21.interval.GenericInterval 1>Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndParent(), freezeIds(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), hasContext(), isClass(), purgeLocations(), removeLocationBySite(), removeLocationBySiteId(), setOffsetBySite(), show(), splitAtDurations(), splitAtQuarterLength(), splitByQuarterLengths(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()