How to use the music21.stream.Measure function in music21

To help you get started, we’ve selected a few music21 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cuthbertLab / music21 / music21 / variant.py View on Github external
def testVariantClassA(self):
        m1 = stream.Measure()
        v1 = Variant()
        v1.append(m1)

        self.assertIn('Variant', v1.classes)

        self.assertFalse(v1.hasElementOfClass('Variant'))
        self.assertTrue(v1.hasElementOfClass('Measure'))
github cuthbertLab / music21 / music21 / musicxml / fromMxObjects.py View on Github external
def mxToMeasure(mxMeasure, spannerBundle=None, inputM21=None):
    '''Translate an mxMeasure (a MusicXML :class:`~music21.musicxml.Measure` object)
    into a music21 :class:`~music21.stream.Measure`.

    If an `inputM21` object reference is provided, this object will be
    configured and returned; otherwise, a new :class:`~music21.stream.Measure` object is created.

    The `spannerBundle` that is passed in is used to accumulate any created Spanners.
    This Spanners are not inserted into the Stream here.

    '''
    if inputM21 == None:
        m = stream.Measure()
    else:
        m = inputM21

    # staff assignments: can create a dictionary with components in each
    # staff; this dictionary will then be used to copy this measure and 
    # split components between two parts of more than one staff is defined
    staffReference = {}

    # doing this will create an instance, but will not be passed
    # out of this method, and thus is only for testing
    if spannerBundle is None:
        #environLocal.printDebug(['mxToMeasure()', 'creating SpannerBundle'])
        spannerBundle = spanner.SpannerBundle()

    mNumRaw = mxMeasure.get('number')
    if mNumRaw is None:
github cuthbertLab / music21 / music21 / repeat.py View on Github external
def testExpandRepeatExpressionH(self):        
        # test one back repeat at end of a measure
        from music21 import stream, bar, note

        # simple da capo alone
        m1 = stream.Measure()
        m1.repeatAppend(note.Note('c4', type='half'), 2)

        m2 = stream.Measure()
        m2.repeatAppend(note.Note('e4', type='half'), 2)

        m3 = stream.Measure()
        m3.leftBarline = bar.Repeat(direction='start')
        m3.repeatAppend(note.Note('g4', type='half'), 2)
        m3.rightBarline = bar.Repeat(direction='end')

        m4 = stream.Measure()
        m4.repeatAppend(note.Note('a4', type='half'), 2)
        dcHandle = DaCapo('D.C.')
        m4.append(dcHandle)

        m5 = stream.Measure()
github cuthbertLab / music21 / music21 / repeat.py View on Github external
def testRepeatCoherenceB(self):
        from music21 import stream, bar, repeat, note

        # a nested repeat; acceptable
        s = stream.Part()
        m1 = stream.Measure()
        m1.leftBarline = bar.Repeat(direction='start')
        #m1.rightBarline = bar.Repeat(direction='end', times=2)
        m1.repeatAppend(note.Note('g3', quarterLength=1), 4)

        m2 = stream.Measure()
        m2.leftBarline = bar.Repeat(direction='start')
        #m2.rightBarline = bar.Repeat(direction='end', times=2)
        m2.repeatAppend(note.Note('b3', quarterLength=1), 4)

        m3 = stream.Measure()
        #m3.leftBarline = bar.Repeat(direction='start')
        m3.rightBarline = bar.Repeat(direction='end', times=2)
        m3.repeatAppend(note.Note('d4', quarterLength=1), 4)

        m4 = stream.Measure()
        #m4.leftBarline = bar.Repeat(direction='start')
github cuthbertLab / music21 / music21 / alpha / analysis / fixer.py View on Github external
'midi': measure stream,
                'omr': measure stream,
                'expected': measure stream,
            }
            '''
            omrMeasure = stream.Measure()
            omrNote = note.Note('F')
            omrNote.duration = duration.Duration('whole')
            omrMeasure.append(omrNote)

            expectedFixedOmrMeasure = stream.Stream()
            expectedOmrNote = deepcopy(omrNote)
            expectedOmrNote.expressions.append(expressions.Turn())
            expectedFixedOmrMeasure.append(expectedOmrNote)

            midiMeasure = stream.Measure()
            turn = [note.Note('G'), note.Note('F'), note.Note('E'), note.Note('F')]
            midiMeasure.append(turn)

            returnDict = {
                'name': 'Single Turn Measure',
                'midi': midiMeasure,
                'omr': omrMeasure,
                'expected': expectedFixedOmrMeasure,
            }
            return returnDict
github cuthbertLab / music21 / music21 / stream.py View on Github external
raise StreamException('mismatch between oMax and highestTime (%s, %s)' % (oMax, streamObj.highestTime))
    #environLocal.printDebug(['oMin, oMax', oMin, oMax])

    # if a ref stream is provided, get highst time from there
    # only if it is greater thant the highest time yet encountered
    if refStream != None:
        if refStream.highestTime > oMax:
            oMax = refStream.highestTime

    # create a stream of measures to contain the offsets range defined
    # create as many measures as needed to fit in oMax
    post = Stream()
    o = 0 # initial position of first measure is assumed to be zero
    measureCount = 0
    while True:    
        m = Measure()
        # get active time signature at this offset
        m.timeSignature = meterStream.getElementAtOrBefore(o)
        m.clef = clefObj

        #environLocal.printDebug([measureCount, o, oMax, m.timeSignature,
        #                        m.timeSignature.barDuration.quarterLength])
        m.measureNumber = measureCount + 1
        # avoid an infinite loop
        if m.timeSignature.barDuration.quarterLength == 0:
            raise StreamException('time signature has no duration')    
        post.insertAtOffset(m, o) # insert measure
        o += m.timeSignature.barDuration.quarterLength # increment by meter length
        if o >= oMax: # may be zero
            break # if length of this measure exceedes last offset
        else:
            measureCount += 1
github cuthbertLab / music21 / music21 / repeat.py View on Github external
def testRepeatEndingsJ(self):
        '''Two sets of two endings (1,2, then 3) without a start repeat
        '''
        from music21 import stream, note, spanner, bar

        p = stream.Part()
        m1 = stream.Measure(number=1)
        m1.append(note.Note('c4', type='whole'))
        m2 = stream.Measure(number=2)
        m2.append(note.Note('d4', type='whole'))
        m3 = stream.Measure(number=3)
        m3.append(note.Note('e4', type='whole'))
        m4 = stream.Measure(number=4)
        m4.append(note.Note('f4', type='whole'))
        m5 = stream.Measure(number=5)
        m5.append(note.Note('g4', type='whole'))
        m6 = stream.Measure(number=6)
        n1 = note.Note('a4', type='half')
        n2 = note.Note('a4', type='half')
        m6.append([n1, n2])
        m7 = stream.Measure(number=7)
        m7.append(note.Note('b4', type='whole'))
        m8 = stream.Measure(number=8)
        m8.append(note.Note('c5', type='whole'))
github cuthbertLab / music21 / music21 / stream / iterator.py View on Github external
def testAddingFiltersMidRecursiveIteration(self):
        from music21 import note, stream
        from music21.stream.iterator import RecursiveIterator as ImportedRecursiveIterator
        m = stream.Measure()
        r = note.Rest()
        n = note.Note()
        m.append([r, n])
        p = stream.Part()
        p.append(m)

        sc = stream.Score()
        sc.append(p)

        sIter = sc.recurse()
        p0 = next(sIter)
        self.assertIs(p0, p)

        child = sIter.childRecursiveIterator
        self.assertIsInstance(child, ImportedRecursiveIterator)
github cuthbertLab / music21 / music21 / musicxml / m21ToXML.py View on Github external
def __init__(self, measureObj=None, parent=None):
        super(MeasureExporter, self).__init__()
        if measureObj is not None:
            self.stream = measureObj
        else: # no point, but...
            self.stream = stream.Measure()
        
        self.parent = parent # PartExporter
        self.xmlRoot = Element('measure')
        self.currentDivisions = defaults.divisionsPerQuarter

        # TODO: allow for mid-measure transposition changes.
        self.transpositionInterval = None
        self.mxTranspose = None
        self.measureOffsetStart = 0.0
        self.offsetInMeasure = 0.0
        self.currentVoiceId = None
        
        self.rbSpanners = [] # rightBarline repeat spanners
        
        if parent is None:
            self.spannerBundle = spanner.SpannerBundle()