Previous topic

music21.pitch

Next topic

music21.roman

Table Of Contents

Table Of Contents

music21.repeat

This module provides the base class for all RepeatMark objects: entities that denote repeats.

Some RepeatMark objects are Expression objects; others are Bar objects. See for instance, Repeat which represents a normal barline repeat.

music21.repeat.insertRepeatEnding(s, start, end, endingNumber=1, inPlace=False)

Designates a range of measures as being repeated endings (i.e. first and second endings) within a stream s, where s either contains measures, or contains parts which contain measures. Start and end are integers corresponding to the first and last measure number of the “repeatNum” ending. e.g. if start=6, end=7, and repeatNum=2, the method adds a second ending from measures 6 to 7.

Does not (yet) add a RepeatMark to the end of the first ending.

Example: create first and second endings over measures 4-6 and measures 11-13 of a chorale, respectively.

>>> from music21 import *
>>> c1 = corpus.parse(corpus.getBachChorales()[1])
>>> repeat.insertRepeatEnding(c1,  4,  6, 1, inPlace=True)
>>> repeat.insertRepeatEnding(c1, 11, 13, 2, inPlace=True)

We now have 8 repeatBrackets since each part gets its own first and second ending.

>>> repeatBrackets = c1.flat.getElementsByClass(spanner.RepeatBracket)
>>> len(repeatBrackets)
8
>>> len(c1.parts[0].getElementsByClass(spanner.RepeatBracket))
2

RepeatExpression

Inherits from: RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.RepeatExpression

This class models any mark added to a Score to mark repeat start and end points that are designated by text expressions or symbols, such as D.S. Al Coda, etc.

N.B. Repeat(Barline) objects are not RepeatExpression objects, but both are RepeatMark subclasses.

This class stores internally a TextExpression. This object is used for rendering text output in translation. A properly configured TextExpression object can also be used to create an instance of a RepeatExpressions.

RepeatExpression attributes

Attributes without Documentation: useSymbol

Attributes inherited from Music21Object: classSortOrder, isSpanner, isStream, isVariant, hideObjectOnPrint, groups, id

RepeatExpression properties

RepeatExpression methods

applyTextFormatting(te=None)

Apply the default text formatting to the text expression version of of this repeat

getText()

Get the text used for this expression.

getTextExpression()

Return a copy of the TextExpression stored in this object.

isValidText(value)

Return True or False if the supplied text could be used for this RepeatExpression.

setText(value)

Set the text of this repeat expression. This is also the primary way that the stored TextExpression object is created.

setTextExpression(value)

Directly set a TextExpression object.

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

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

RepeatExpressionMarker

Inherits from: RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.RepeatExpressionMarker

Some repeat expressions are markers of positions in the score to jump to; these classes model those makers, such as Coda, Segno, and Fine, which are subclassed below.

Coda

Inherits from: RepeatExpressionMarker, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.Coda(text=None)

The coda symbol, or the word coda, as placed in a score.

>>> from music21 import *
>>> rm = repeat.Coda()

Segno

Inherits from: RepeatExpressionMarker, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.Segno

The segno sign as placed in a score.

>>> from music21 import *
>>> rm = repeat.Segno()
>>> rm.useSymbol
True

Fine

Inherits from: RepeatExpressionMarker, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.Fine

The fine word as placed in a score.

>>> from music21 import *
>>> rm = repeat.Fine()

RepeatExpressionCommand

Inherits from: RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.RepeatExpressionCommand

Some repeat expressions are commands, instructing the reader to go somewhere else. DaCapo and related are examples.

RepeatExpressionCommand attributes

Attributes without Documentation: repeatAfterJump

Attributes inherited from RepeatExpression: useSymbol

Attributes inherited from Music21Object: classSortOrder, isSpanner, isStream, isVariant, hideObjectOnPrint, groups, id

RepeatExpressionCommand properties

RepeatExpressionCommand methods

Methods inherited from RepeatExpression: applyTextFormatting(), getText(), getTextExpression(), isValidText(), setText(), setTextExpression()

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

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

DaCapo

Inherits from: RepeatExpressionCommand, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.DaCapo(text=None)

The Da Capo command, indicating a return to the beginning and a continuation to the end. By default, repeatAfterJump is False, indicating that any repeats encountered on the Da Capo repeat not be repeated.

DaCapoAlFine

Inherits from: RepeatExpressionCommand, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.DaCapoAlFine(text=None)

The Da Capo al Fine command, indicating a return to the beginning and a continuation to the Fine object. By default, repeatAfterJump is False, indicating that any repeats encountered on the Da Capo repeat not be repeated.

>>> from music21 import *
>>> rm = repeat.DaCapoAlFine()

DaCapoAlCoda

Inherits from: RepeatExpressionCommand, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.DaCapoAlCoda(text=None)

The Da Capo al Coda command, indicating a return to the beginning and a continuation to the Coda object. The music resumes at a second Coda object. By default, repeatAfterJump is False, indicating that any repeats encountered on the Da Capo repeat not be repeated.

>>> from music21 import *
>>> rm = repeat.DaCapoAlCoda()

AlSegno

Inherits from: RepeatExpressionCommand, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.AlSegno(text=None)

Jump to the sign. Presumably a forward jump, not a repeat.

>>> from music21 import *
>>> rm = repeat.AlSegno()

DalSegno

Inherits from: RepeatExpressionCommand, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.DalSegno(text=None)

The Dal Segno command, indicating a return to the segno and a continuation to the end. By default, repeatAfterJump is False, indicating that any repeats encountered on the Da Capo repeat not be repeated.

>>> from music21 import *
>>> rm = repeat.DaCapoAlFine()

DalSegnoAlFine

Inherits from: RepeatExpressionCommand, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.DalSegnoAlFine(text=None)

The Dal Segno al Fine command, indicating a return to the segno and a continuation to the Fine object. By default, repeatAfterJump is False, indicating that any repeats encountered on the Dal Segno repeat not be repeated.

>>> from music21 import *
>>> rm = repeat.DaCapoAlFine()

DalSegnoAlCoda

Inherits from: RepeatExpressionCommand, RepeatExpression, RepeatMark, Expression, Music21Object, JSONSerializer

class music21.repeat.DalSegnoAlCoda(text=None)

The Dal Segno al Coda command, indicating a return to the beginning and a continuation to the Coda object. The music resumes at a second Coda object. By default, repeatAfterJump is False, indicating that any repeats encountered on the Da Segno repeat not be repeated.

>>> from music21 import *
>>> rm = repeat.DaCapoAlCoda()

Expander

class music21.repeat.Expander(streamObj=None)

The Expander object can expand a single Part or Part-like Stream with repeats. Nested repeats given with Repeat objects, or repeats and sections designated with RepeatExpression objects, are all expanded.

This class is a utility processor. Direct usage is more commonly from the expandRepeats() method.

To use this object directly, call process() on the score

>>> from music21 import *
>>> s = converter.parse('tinynotation: 3/4 C4 D E')
>>> s.makeMeasures(inPlace = True)
>>> s.measure(1).rightBarline = bar.Repeat(direction='end', times=3)
>>> e = repeat.Expander(s)
>>> s2 = e.process()
>>> s2.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.BassClef>
    {0.0} <music21.meter.TimeSignature 3/4>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note D>
    {2.0} <music21.note.Note E>
    {3.0} <music21.bar.Barline style=double>
{3.0} <music21.stream.Measure 1 offset=3.0>
    {0.0} <music21.clef.BassClef>
    {0.0} <music21.meter.TimeSignature 3/4>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note D>
    {2.0} <music21.note.Note E>
    {3.0} <music21.bar.Barline style=double>
{6.0} <music21.stream.Measure 1 offset=6.0>
    {0.0} <music21.clef.BassClef>
    {0.0} <music21.meter.TimeSignature 3/4>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note D>
    {2.0} <music21.note.Note E>
    {3.0} <music21.bar.Barline style=double>

Expander methods

isExpandable()

Return True or False if this Stream is expandable, that is, if it has balanced repeats or sensible da copo or dal segno indications.

process()

Process all repeats. Note that this processing only happens for Measures contained in the given Stream. Other objects in that Stream are neither processed nor copied.

InsufficientLength

Inherits from: Exception, BaseException

class music21.repeat.InsufficientLength

NoInternalStream

Inherits from: Exception, BaseException

class music21.repeat.NoInternalStream

RepeatFinder

class music21.repeat.RepeatFinder(inpStream=None, defaultMeasureHashFunction=<function translateStreamToString at 0x032BA6F0>)

An object for finding and simplifying repeated sections of music.

Can be passed a stream which contains measures, or which contains parts which contain measures.

the defaultMeasureHashFunction is a function that hashes a measure to search for similar passages. By default it’s search.translateStreamToString.

RepeatFinder attributes

defaultHash

takes a stream of notesAndRests only and returns a string for searching on.

>>> from music21 import *
>>> s = converter.parse("c4 d8 r16 FF8. a'8 b-2.", "3/4")
>>> sn = s.flat.notesAndRests
>>> streamString = search.translateStreamToString(sn)
>>> print streamString
<P>F<)KQFF_
>>> len(streamString)
12

Attributes without Documentation: mList, s, mGroups

RepeatFinder methods

deleteMeasures(toDelete, inPlace=False)

Given a stream s and a list of number toDelete, removes each measure with a number corresponding to a number in toDelete. Renumbers the remaining measures in the stream.

Can take as input either a stream of measures, or a stream of parts containing measures.

>>> from music21 import *
>>> from copy import deepcopy
>>> chorale1 = corpus.parse(corpus.getBachChorales()[1])
>>> s = deepcopy(chorale1)
>>> repeat.RepeatFinder(s).deleteMeasures([6, 3, 4], inPlace=True)
>>> m2 = search.translateStreamToString( chorale1.parts[1].measure(2).notesAndRests)
>>> resm2 = search.translateStreamToString( s.parts[1].measure(2).notesAndRests)
>>> m2 == resm2
True
>>> m5 = search.translateStreamToString( chorale1.parts[1].measure(5).notesAndRests)
>>> resm3 = search.translateStreamToString( s.parts[1].measure(3).notesAndRests)
>>> m5 == resm3
True
>>> m7 = search.translateStreamToString( chorale1.parts[1].measure(7).notesAndRests)
>>> resm4 = search.translateStreamToString( s.parts[1].measure(4).notesAndRests)
>>> m7 == resm4
True
>>> lenS = len(s.parts[0].getElementsByClass(stream.Measure))
>>> lenChorale1 = len(chorale1.parts[0].getElementsByClass(stream.Measure))
>>> lenS + 3 == lenChorale1
True
>>> chorale2 = corpus.parse(corpus.getBachChorales()[2])
>>> s = deepcopy(chorale2)
>>> repeat.RepeatFinder(s).deleteMeasures([3, 4, 5], True)
>>> m2 = search.translateStreamToString( chorale2.parts[0].measure(2).notesAndRests)
>>> resm2 = search.translateStreamToString( s.parts[0].measure(2).notesAndRests)
>>> m3 = search.translateStreamToString( chorale2.parts[0].measure(3).notesAndRests)
>>> m6 = search.translateStreamToString( chorale2.parts[0].measure(6).notesAndRests)
>>> resm3 = search.translateStreamToString( s.parts[0].measure(3).notesAndRests)
>>> m2 == resm2
True
>>> resm3 == m3
False
>>> resm3 == m6
True
>>> chorale3 = corpus.parse(corpus.getBachChorales()[3])
>>> s = repeat.RepeatFinder(chorale3).deleteMeasures([2, 3])
>>> len(s.parts[2].getElementsByClass(stream.Measure)) == len(chorale3.parts[2].getElementsByClass(stream.Measure)) - 2
True
>>> s = repeat.RepeatFinder(chorale3).deleteMeasures([2, 3])
>>> len(s.parts[2].getElementsByClass(stream.Measure)) == len(chorale3.parts[2].getElementsByClass(stream.Measure)) - 2
True
>>> s = repeat.RepeatFinder(chorale3).deleteMeasures([999, 1001001])
>>> len(s.parts[2]) == len(chorale3.parts[2])
True
getMeasureSimilarityList(measureHashFunction=None)

Returns a list mList = [ [2, 3], [3], ... ] such that measures i and j are the same, with i < j, if and only if mList[i] contains j. NOTE: this function refers to the very first measure as measure 0 _regardless_ of whether s starts with measure 0 or 1 (i.e. treats a pickup bar as an entire measure).

hashFunction is a function which takes a stream and hashes it to some type that can be compared with the ‘==’ operator. Measures m and n are considered the same if the hashFunction(m) == hashFunction(n)

>>> from music21 import *
>>> chorales = corpus.getBachChorales()
>>> chorale49 = corpus.parse(chorales[49])
>>> repeat.RepeatFinder(chorale49).getMeasureSimilarityList()
[[4], [5], [6], [7, 15], [], [], [], [15], [], [], [], [], [], [], [], []]
>>> repeat.RepeatFinder(chorale49.parts[0]).getMeasureSimilarityList()
[[4, 12], [5], [6], [7, 15], [12], [], [], [15], [], [], [], [], [], [], [], []]
>>> chorale47 = corpus.parse(chorales[47])
>>> repeat.RepeatFinder(chorale47).getMeasureSimilarityList()    #chorale47 has a pickup
[[], [5], [6], [7], [], [], [], [], [], [], [], [], [], [], [], [], []]
getQuarterLengthOfPickupMeasure()

Returns the number of quarterlengths of the pickup bar. If there is no pickup bar, returns 0.0

>>> from music21 import *
>>> chorales = corpus.getBachChorales()
>>> noPickup = corpus.parse(chorales[1])
>>> repeat.RepeatFinder(noPickup).getQuarterLengthOfPickupMeasure()
0.0
>>> hasPickup = corpus.parse(chorales[2])
>>> repeat.RepeatFinder(hasPickup).getQuarterLengthOfPickupMeasure()
1.0
hasPickup()

Returns True if and only if the internal stream has a pickup bar.

>>> from music21 import *
>>> chorales = corpus.getBachChorales()
>>> noPickup = corpus.parse(chorales[1])
>>> repeat.RepeatFinder(noPickup).hasPickup()
False
>>> hasPickup = corpus.parse(chorales[2])
>>> repeat.RepeatFinder(hasPickup).hasPickup()
True
insertRepeat(barStart, barEnd, inPlace=False)

Given a stream s, inserts a start-repeat at the beginning of the bar specified by barStart and inserts an end-repeat at the bar specified by barEnd. Works on either a stream with measures, or a stream with parts containing measures.

>>> from music21 import *
>>> from copy import deepcopy
>>> chorale1 = corpus.parse(corpus.getBachChorales()[1])
>>> s = repeat.RepeatFinder(chorale1).insertRepeat(3, 6, inPlace=False)
>>> m4 = search.translateStreamToString( chorale1.parts[1].measure(4).notesAndRests)
>>> resm4 = search.translateStreamToString( s.parts[1].measure(4).notesAndRests)
>>> m6 = search.translateStreamToString( chorale1.parts[1].measure(4).notesAndRests)
>>> resm6 = search.translateStreamToString( s.parts[1].measure(4).notesAndRests)
>>> m7 = search.translateStreamToString( chorale1.parts[1].measure(4).notesAndRests)
>>> resm7 = search.translateStreamToString( s.parts[1].measure(4).notesAndRests)
>>> m4 == resm4
True
>>> m6 == resm6
True
>>> m7 == resm7
True
>>> len(s.parts[0].flat.getElementsByClass(bar.Repeat))
2
>>> len(s.flat.getElementsByClass(bar.Repeat))
8
>>> s.parts[0].measure(3).leftBarline.direction
'start'
>>> s.parts[0].measure(6).rightBarline.direction
'end'
simplify(repeatThreshold=4, repeatEndingThreshold=3, inPlace=False)

No documentation.

RepeatMark

class music21.repeat.RepeatMark

Base class of all repeat objects, including RepeatExpression objects and Repeat (Barline) objects.

This object is used to for multiple-inheritance of such objects and to filter by class in order to get all things that mark repeats.

The RepeatMark is not itself a Music21Object so you should use multiple inheritance to put these things in Streams.

The following demonstration shows how a user might see if a Stream has any repeats in it.

>>> from music21 import *
>>> class PartialRepeat(repeat.RepeatMark, base.Music21Object):
...    def __init__(self):
...        base.Music21Object.__init__(self)

>>> s = stream.Stream()
>>> s.append(note.Note())
>>> s.append(PartialRepeat())
>>> repeats = s.getElementsByClass(repeat.RepeatMark)
>>> if len(repeats) > 0:
...    print "Stream has %d repeat(s) in it" % (len(repeats))
Stream has 1 repeat(s) in it