Table Of Contents

Previous topic

music21.stream

Next topic

music21.tinyNotation

music21.tempo

This module defines objects for describing tempo and changes in tempo.

music21.tempo.interpolateElements(element1, element2, sourceStream, destinationStream, autoAdd=True)

Assume that element1 and element2 are two elements in sourceStream and destinationStream with other elements (say eA, eB, eC) between them. For instance, element1 could be the downbeat at offset 10 in sourceStream (a Stream representing a score) and offset 20.5 in destinationStream (which might be a Stream representing the timing of notes in particular recording at approximately but not exactly qtr = 30). Element2 could be the following downbeat in 4/4, at offset 14 in source but offset 25.0 in the recording:

>>> from music21 import *
>>> sourceStream = stream.Stream()
>>> destinationStream = stream.Stream()
>>> element1 = note.QuarterNote("C4")
>>> element2 = note.QuarterNote("G4")
>>> sourceStream.insert(10, element1)
>>> destinationStream.insert(20.5, element1)
>>> sourceStream.insert(14, element2)
>>> destinationStream.insert(25.0, element2)

Suppose eA, eB, and eC are three quarter notes that lie between element1 and element2 in sourceStream and destinationStream, as in:

>>> eA = note.QuarterNote("D4")
>>> eB = note.QuarterNote("E4")
>>> eC = note.QuarterNote("F4")
>>> sourceStream.insert(11, eA)
>>> sourceStream.insert(12, eB)
>>> sourceStream.insert(13, eC)
>>> destinationStream.append([eA, eB, eC])  # not needed if autoAdd were true

then running this function will cause eA, eB, and eC to have offsets 21.625, 22.75, and 23.875 respectively in destinationStream:

>>> tempo.interpolateElements(element1, element2, sourceStream, destinationStream, autoAdd = False)
>>> for el in [eA, eB, eC]:
...    print el.getOffsetBySite(destinationStream)
21.625
22.75
23.875

if the elements between element1 and element2 do not yet appear in destinationStream, they are automatically added unless autoAdd is False.

(with the default autoAdd, elements are automatically added to new streams):

>>> destStream2 = stream.Stream()
>>> destStream2.insert(10.1, element1)
>>> destStream2.insert(50.5, element2)
>>> tempo.interpolateElements(element1, element2, sourceStream, destStream2)
>>> for el in [eA, eB, eC]:
...    print el.getOffsetBySite(destStream2)
20.2
30.3
40.4

(unless autoAdd is set to false, in which case a Tempo Exception arises...)

>>> destStream3 = stream.Stream()
>>> destStream3.insert(100, element1)
>>> destStream3.insert(500, element2)
>>> tempo.interpolateElements(element1, element2, sourceStream, destStream3, autoAdd = False)
...
TempoException: Could not find element <music21.note.Note D> with id ...
music21.tempo.convertTempoAtBeat(numberSrc, quarterLengthBeatSrc, quarterLengthBeatDst=1.0)

Convert between equivalent tempi, where the speed stays the same but the beat referent and number chnage.

>>> from music21 import *
>>> tempo.convertTempoAtBeat(60, 1, 2) # 60 bpm at quarter, going to half
30.0
>>> tempo.convertTempoAtBeat(60, 1, .25) # 60 bpm at quarter, going to 16th
240.0
>>> tempo.convertTempoAtBeat(60, 1.5, 1) # 60 at dotted quarter, get quarter
90.0
>>> tempo.convertTempoAtBeat(60, 1.5, 2) # 60 at dotted quarter, get half
45.0
>>> tempo.convertTempoAtBeat(60, 1.5, 1/3.) # 60 at dotted quarter, get trip
270.0

MetronomeMark

Inherits from: TempoIndication, Music21Object, JSONSerializer

class music21.tempo.MetronomeMark(text=None, number=None, referent=None, parentheses=False)

A way of specifying a particular tempo with a text string, a referent (a duration) and a number. The referent attribute is a Duration object. As this object is a Music21Object, it also has a .duration property object.

>>> from music21 import *
>>> a = tempo.MetronomeMark("slow", 40, note.HalfNote())
>>> a.number
40
>>> a.referent
<music21.duration.Duration 2.0>
>>> a.referent.type
'half'
>>> a.text
'slow'
>>> mm = tempo.MetronomeMark('adagio')
>>> mm.number
52
>>> mm.numberImplicit
True
>>> tm2 = music21.tempo.MetronomeMark(u"très vite")
>>> tm2.text.endswith('vite')
True
>>> tm2.number
144
>>> tm2 = music21.tempo.MetronomeMark(number=200)
>>> tm2.text
'prestissimo'
>>> tm2.referent
<music21.duration.Duration 1.0>

MetronomeMark attributes

Attributes without Documentation: numberImplicit, parentheses, textImplicit

Attributes inherited from TempoIndication: classSortOrder

Attributes inherited from Music21Object: hideObjectOnPrint, id, isSpanner, isStream, isWrapper, groups

MetronomeMark properties

number

Get and set the number, or the numerical value of the Metronome.

>>> from music21 import *
>>> mm = tempo.MetronomeMark('slow')
>>> mm.number
52
>>> mm.numberImplicit
True
>>> mm.number = 52.5
>>> mm.number
52.5
>>> mm.numberImplicit
False
numberSounding

Get and set the numberSounding, or the numerical value of the Metronome that is used for playback independent of display. If numberSounding is None number is assumed to be numberSounding.

>>> from music21 import *
>>> mm = tempo.MetronomeMark('slow')
>>> mm.number
52
>>> mm.numberImplicit
True
>>> mm.numberSounding == None
True
>>> mm.numberSounding = 120
>>> mm.numberSounding
120
referent
Get or set the referent, or the Duration object that is the reference for the tempo value in BPM.
text

Get or set a text string for this MetronomeMark. Internally implemented as a TempoText object, which stores the text in a TextExpression object.

>>> from music21 import *
>>> mm = tempo.MetronomeMark(number=120)
>>> mm.text == None
True
>>> mm.text = 'medium fast'
>>> mm.text
'medium fast'

Properties inherited from Music21Object: activeSite, beat, beatDuration, beatStr, beatStrength, classes, derivationHierarchy, duration, measureNumber, offset, priority

Properties inherited from JSONSerializer: json

MetronomeMark methods

getQuarterBPM(useNumberSounding=True)

Get a BPM value where the beat is a quarter; must convert from the defined beat to a quarter beat. Will return None if no beat number is defined. This mostly used for generating MusicXML <sound> tags when necessary.

>>> from music21 import *
>>> mm = MetronomeMark(number=60, referent='half')
>>> mm.getQuarterBPM()
120.0
>>> mm.referent = 'quarter'
>>> mm.getQuarterBPM()
60.0
getTextExpression(returnImplicit=False)

If there is a TextExpression available that is not implicit, return it; otherwise, return None.

>>> from music21 import *
>>> mm = MetronomeMark('presto')
>>> mm.number
168
>>> mm.numberImplicit
True
>>> mm.getTextExpression()
<music21.expressions.TextExpression "presto">
>>> mm.textImplicit
False
>>> mm = MetronomeMark(number=90)
>>> mm.numberImplicit
False
>>> mm.textImplicit
True
>>> mm.getTextExpression() == None
True
>>> mm.getTextExpression(returnImplicit=True)
<music21.expressions.TextExpression "moderate">
setQuarterBPM(value, setNumber=True)

Given a value in BPM, use it to set the value of this MetroneMark. BPM values are assumed to be refer only to quarter notes; different beat values, if definded here, will be scaled

>>> from music21 import *
>>> mm = MetronomeMark(number=60, referent='half')
>>> mm.setQuarterBPM(240) # set to 240 for a quarter
>>> mm.number  # a half is half as fast
120.0

Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndActiveSite(), freezeIds(), getAllContextsByClass(), getCommonSiteIds(), getCommonSites(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), getSpannerSites(), hasContext(), hasSpannerSite(), isClassOrSubclass(), mergeAttributes(), purgeLocations(), removeLocationBySite(), removeLocationBySiteId(), removeNonContainedLocations(), setOffsetBySite(), show(), splitAtDurations(), splitAtQuarterLength(), splitByQuarterLengths(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()

Methods inherited from JSONSerializer: jsonAttributes(), jsonComponentFactory(), jsonPrint(), jsonRead(), jsonWrite()

TempoText

Inherits from: TempoIndication, Music21Object, JSONSerializer

class music21.tempo.TempoText(text=None)
>>> import music21
>>> tm = music21.tempo.TempoText("adagio")
>>> tm.text
'adagio'

Common marks such as “adagio,” “moderato,” “molto allegro,” etc. get sensible default values. If not found, uses a default of 90:

TempoText attributes

Attributes inherited from TempoIndication: classSortOrder

Attributes inherited from Music21Object: hideObjectOnPrint, id, isSpanner, isStream, isWrapper, groups

TempoText properties

text

Get or set the text as a string.

>>> import music21
>>> tm = music21.tempo.TempoText("adagio")
>>> tm.text
'adagio'
>>> tm.getTextExpression()
<music21.expressions.TextExpression "adagio">

Properties inherited from Music21Object: activeSite, beat, beatDuration, beatStr, beatStrength, classes, derivationHierarchy, duration, measureNumber, offset, priority

Properties inherited from JSONSerializer: json

TempoText methods

applyTextFormatting(te=None, numberImplicit=False)
Apply the default text formatting to the text expression version of of this repeat
getMetronomeMark()

Return a MetronomeMark object that is configured from this objects Text.

>>> from music21 import *
>>> tt = tempo.TempoText("slow")
>>> mm = tt.getMetronomeMark()
>>> mm.number
52
getTextExpression(numberImplicit=False)
Return a TextExpression object for this text.
isValidText(value)
Return True or False if the supplied text could be used for this TempoText.
setTextExpression(vale)
Given a TextExpression, set it in this object.

Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndActiveSite(), freezeIds(), getAllContextsByClass(), getCommonSiteIds(), getCommonSites(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), getSpannerSites(), hasContext(), hasSpannerSite(), isClassOrSubclass(), mergeAttributes(), purgeLocations(), removeLocationBySite(), removeLocationBySiteId(), removeNonContainedLocations(), setOffsetBySite(), show(), splitAtDurations(), splitAtQuarterLength(), splitByQuarterLengths(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()

Methods inherited from JSONSerializer: jsonAttributes(), jsonComponentFactory(), jsonPrint(), jsonRead(), jsonWrite()

MetricModulation

Inherits from: TempoIndication, Music21Object, JSONSerializer

class music21.tempo.MetricModulation(value=None)

A class for representing the relationship between two MetronomeMarks. Generally this relationship is one of equality. The classicalStyle attribute determines of the first MetronomeMark describes the new tempo, not the old (the reverse of expected usage). The maintainBeat attribute determines if, after an equality statement, the beat is maintained.

MetricModulation attributes

Attributes without Documentation: parentheses, maintainBeat, transitionSymbol, classicalStyle, arrowDirection

Attributes inherited from TempoIndication: classSortOrder

Attributes inherited from Music21Object: hideObjectOnPrint, id, isSpanner, isStream, isWrapper, groups

MetricModulation properties

Properties inherited from Music21Object: activeSite, beat, beatDuration, beatStr, beatStrength, classes, derivationHierarchy, duration, measureNumber, offset, priority

Properties inherited from JSONSerializer: json

MetricModulation methods

Methods inherited from Music21Object: searchParentByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndActiveSite(), freezeIds(), getAllContextsByClass(), getCommonSiteIds(), getCommonSites(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), getSpannerSites(), hasContext(), hasSpannerSite(), isClassOrSubclass(), mergeAttributes(), purgeLocations(), removeLocationBySite(), removeLocationBySiteId(), removeNonContainedLocations(), setOffsetBySite(), show(), splitAtDurations(), splitAtQuarterLength(), splitByQuarterLengths(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()

Methods inherited from JSONSerializer: jsonAttributes(), jsonComponentFactory(), jsonPrint(), jsonRead(), jsonWrite()

TempoIndication

Inherits from: Music21Object, JSONSerializer

class music21.tempo.TempoIndication
A generic base class for all tempo indications to inherit. Can be used to filter out all types of tempo indications.