How to use the music21.duration.Duration 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 / noteworthy / translate.py View on Github external
def setDurationForObject(self, generalNote, durationInfo):
        '''
        generalNote could be a Note, Chord, or Rest

        DurationInfo is a string like:

            Whole,Dotted,Slur

        '''
        from music21 import noteworthy
        dictionaries = noteworthy.dictionaries

        parts = durationInfo.split(',')
        lengthnote = parts[0]
        thisNoteIsSlurred = False
        durationObject = duration.Duration(dictionaries["dictionaryNoteLength"][lengthnote])

        for kk in parts:
            if kk == "Grace":
                #print("GRACE NOTE")
                # Now it doesn't work, the function for grace notes have to be added here
                environLocal.warn('skipping grace note')
                return
            elif kk == "Slur":
                #print("SLUR")
                if self.withinSlur is False:
                    self.beginningSlurNote = generalNote
                thisNoteIsSlurred = True
            elif kk == "Dotted":
                durationObject.dots = 1
            elif kk == "DblDotted":
                durationObject.dots = 2
github cuthbertLab / music21 / music21 / stream.py View on Github external
def testStreamSortRecursion(self):
        srcStream = Stream()
        for x in range(4):
            n = note.Note('G#')
            n.duration = duration.Duration('quarter')
            e = Element(n)
            e.offset = x * 2
            srcStream.append(e)  # 0, 2, 4, 6

        midStream = Stream()
        for x in range(4):
            srcNew = srcStream.copy()
            srcNew.offset = x * 5 
            midStream.append(srcNew) # 0, 5, 10, 15

        farStream = Stream()
        for x in range(4):
            midNew = midStream.copy()
            midNew.offset = x * 13
            farStream.append(midNew)  # 0, 13, 26, 39 
github cuthbertLab / music21 / music21 / harmony.py View on Github external
def writeAsChord(self, val):
        self._writeAsChord = val
        # try:
        #     self._updatePitches()
        # except exceptions21.Music21Exception:
        #     pass
        if val and self.duration.quarterLength == 0:
            self.duration = duration.Duration(1)
github cuthbertLab / music21 / music21 / alpha / trecento / notation.py View on Github external
dur = lenList[i]*mDur

            if (rem - dur) > -0.0001: #Fits w/i measure up to rounding error
                n.duration = duration.Duration(dur)
                m.append(n)
                rem -= dur
            else: #Syncopated across barline
                n.duration = duration.Duration(rem)
                n.tie = tie.Tie('start')
                m.append(n)
                measureList.append(m)
                measureNumOffset += 1

                m = stream.Measure(number=brevisLength.number + measureNumOffset)
                n_tied = note.Note(mList[i].pitch)
                n_tied.duration = duration.Duration(dur - rem)
                n_tied.tie = tie.Tie('end')
                m.append(n_tied)
                rem = div.barDuration.quarterLength - dur + rem
        measureList.append(m)

    return measureList
github cuthbertLab / music21 / music21 / alpha / analysis / fixer.py View on Github external
self.assertListEqual([n1, n2, n3], list(result.notes), 'all notes fill up partial duration')

        result = getNotesWithinDuration(n1, duration.Duration(1.5))
        self.assertListEqual([n1, n2], list(result.notes), 'some notes fill up full duration')

        result = getNotesWithinDuration(n1, duration.Duration(1.75))
        self.assertListEqual([n1, n2], list(result.notes), 'some notes fill up partial duration')

        # set active site from m2 to m1 (which runs out of notes to fill up)
        result = getNotesWithinDuration(n1, duration.Duration('half'), referenceStream=m1)
        self.assertListEqual([n1], list(result.notes), 'partial fill up from reference stream m1')

        m3 = stream.Measure()
        m3.id = 'm3'
        r1 = note.Rest()
        r1.duration = duration.Duration('quarter')
        m3.append([n1, r1])  # n1 active site now with m2
        result = getNotesWithinDuration(n1, duration.Duration('half'))
        msg = 'note and rest fill up full duration'
        self.assertListEqual([n1, r1], list(result.notesAndRests), msg)

        # set active site from m3 to m2
        result = getNotesWithinDuration(n1, duration.Duration('half'), referenceStream=m2)
        self.assertListEqual([n1, n2, n3], list(result.notes), 'fill up from reference stream m2')
github ahankinson / pymei / pymei / Import / xmltomusic21.py View on Github external
# grab the pitch name
            d[af.ns_to_prefix(k)] = v
        
        lg.debug("{0}".format(d))
        p = d['pname']
        #octave
        o = d['oct']
        if 'accid.ges' in d:
            a = _convert_accidentals(d['accid.ges'])
        else:
            a = ""
        nt = "{0}{1}{2}".format(p,o,a)
        
        n = note.Note(nt)        
        notelen = int(d['dur']) # this will break if it encounters "long" or "breve!"
        n.duration = duration.Duration(notelen)
        
        # lg.debug("{0}".format(dir(n)))
        
        return n
    
    # add any children.
    c = list(el)
    if len(c) > 0:
        # loopdy-loopdy! This calls itself for any children components found.
        m = map(_xml_to_music21, c)
github mcleavey / musical-neural-net / utils.py View on Github external
has_end=True
                    break
                if score[i+j][-3:]=="eoc":
                    duration+=1

            if not has_end:
                duration=12

            add_wait = 0
            if score[i][-3:]=="eoc":
                score[i]=score[i][:-3]
                add_wait = 1

            try: 
                new_note=music21.note.Note(int(score[i][1:])+note_offset)    
                new_note.duration = music21.duration.Duration(duration*speed)
                new_note.offset=time_offset*speed
                if score[i][0]=="v":
                    violin_notes.append(new_note)
                else:
                    piano_notes.append(new_note)                
            except:
                print("Unknown note: " + score[i])

            

            
            time_offset+=add_wait
                
    violin=music21.instrument.fromString("Violin")
    piano=music21.instrument.fromString("Piano")
    violin_notes.insert(0, violin)
github cuthbertLab / music21 / music21 / musicxml / translate.py View on Github external
>>> a.setDefaults()
    >>> m = musicxml.Measure()
    >>> m.setDefaults()
    >>> a.external['measure'] = m # assign measure for divisions ref
    >>> a.external['divisions'] = m.external['divisions']
    >>> c = duration.Duration()
    >>> musicxml.translate.mxToDuration(a, c)
    
    >>> c.quarterLength
    1.0

    '''
    from music21 import duration

    if inputM21 == None:
        d = duration.Duration
    else:
        d = inputM21

    if mxNote.external['measure'] == None:
        raise TranslateException(
        "cannont determine MusicXML duration without a reference to a measure (%s)" % mxNote)

    mxDivisions = mxNote.external['divisions']
    if mxNote.duration != None: 
        if mxNote.get('type') != None:
            type = duration.musicXMLTypeToType(mxNote.get('type'))
            forceRaw = False
        else: # some rests do not define type, and only define duration
            type = None # no type to get, must use raw
            forceRaw = True
github cuthbertLab / music21 / music21 / alpha / analysis / fixer.py View on Github external
result = getNotesWithinDuration(n1, duration.Duration('quarter'))
        self.assertIsInstance(result, stream.Stream)
        self.assertListEqual([n1], list(result.notes), 'starting note occupies full duration')

        result = getNotesWithinDuration(n1, duration.Duration('half'))
        self.assertListEqual([n1], list(result.notes), 'starting note occupies partial duration')

        result = getNotesWithinDuration(n1, duration.Duration('eighth'))
        self.assertListEqual([], list(result.notes), 'starting note too long')

        m2 = stream.Measure()
        n2 = note.Note('D')
        n2.duration = duration.Duration('eighth')
        n3 = note.Note('E')
        n3.duration = duration.Duration('eighth')
        m2.append([n1, n2, n3])

        result = getNotesWithinDuration(n1, duration.Duration('quarter'))
        self.assertListEqual([n1], list(result.notes), 'starting note occupies full duration')

        result = getNotesWithinDuration(n1, duration.Duration('half'))
        self.assertListEqual([n1, n2, n3], list(result.notes), 'all notes fill up full duration')

        result = getNotesWithinDuration(n1, duration.Duration('whole'))
        self.assertListEqual([n1, n2, n3], list(result.notes), 'all notes fill up partial duration')

        result = getNotesWithinDuration(n1, duration.Duration(1.5))
        self.assertListEqual([n1, n2], list(result.notes), 'some notes fill up full duration')

        result = getNotesWithinDuration(n1, duration.Duration(1.75))
        self.assertListEqual([n1, n2], list(result.notes), 'some notes fill up partial duration')
github cuthbertLab / music21 / music21 / meter.py View on Github external
def testMeterBeam(self):
        from music21 import stream, note
        ts = music21.meter.TimeSignature('6/8', 2)
        b = [duration.Duration('16th')] * 12
        s = stream.Stream()
        s.insert(0, ts)
        for x in b:
            n = note.Note()
            n.duration = x
            s.append(n)
        s.show()