Пример #1
0
    def componentIndexAtQtrPosition(self, quarterPosition):
        '''returns the index number of the duration component sounding at
        the given quarter position.
        
        e.g. given d1, d2, d3 as 3 quarter notes and
        self.components = [d1, d2, d3]
        then
        self.getComponentIndexAtQtrPosition(1.5) == d2
        self.getComponentIndexAtQtrPosition(2.0) == d3
        self.getComponentIndexAtQtrPosition(2.5) == d3
        '''
        
        if self.components == []:
            raise DurationException("Need components to run getComponentIndexAtQtrPosition")
        elif quarterPosition > self.quarterLength:
            raise DurationException("quarterPosition is after the end of the duration")
        elif quarterPosition < 0:
            raise DurationException("Okay, now you're just being silly, with negative positions")
        
        if common.almostEquals(quarterPosition, 0):
            return self.components[0]
        elif common.almostEquals(quarterPosition, self.quarterLength):
            return self.components[-1]
        
        currentPosition = 0.0

        for i in range(len(self.components)):
            currentPosition += self.components[i].quarterLength
            if currentPosition > quarterPosition and not common.almostEquals(currentPosition, quarterPosition):
                return i
        
        raise DurationException("Okay, so how did this happen?")
Пример #2
0
def tupletTest():
    note1 = Note()
    note1.duration.type = "quarter"

    ### create a tuplet with 5 dotted eighths in the place of 3 double-dotted eighths
    dur1 = Duration()
    dur1.type = "eighth"
    dur1.dots = 1

    dur2 = Duration()
    dur2.type = "eighth"
    dur2.dots = 2

    tup1 = Tuplet()
    tup1.tupletActual = [5, dur1]
    tup1.tupletNormal = [3, dur2]

    print "For 5 dotted eighths in the place of 3 double-dotted eighths"
    print "Total tuplet length is",
    print tup1.totalTupletLength(),
    assert common.almostEquals(tup1.totalTupletLength(), 2.625)

    print "quarter notes.\nEach note is",
    print tup1.tupletMultiplier(),
    assert common.almostEquals(tup1.tupletMultiplier(), 0.7)
    print "times as long as it would normally be."

    ### create a new dotted quarter and apply the tuplet to it
    dur3 = Duration()
    dur3.type = "quarter"
    dur3.dots = 1
    dur3.tuplets = [tup1]
    print "So a tuplet-dotted-quarter's length is",
    print dur3.getQuarterLength(),
    assert common.almostEquals(dur3.getQuarterLength(), 1.05)
    print "quarter notes"

    ### create a tuplet with 3 sixteenths in the place of 2 sixteenths
    tup2 = Tuplet()
    dur4 = Duration()
    dur4.type = "16th"
    tup2.tupletActual = [3, dur4]
    tup2.tupletNormal = [2, dur4]

    print "\nTup2 (3 sixteenths in the place of 2 16ths):\nTotal tuplet length is",
    print tup2.totalTupletLength(),
    assert common.almostEquals(tup2.totalTupletLength(), 0.5)
    print "quarter notes.\nEach note is",
    print tup2.tupletMultiplier(),
    assert common.almostEquals(tup2.tupletMultiplier(), 0.66666666667)

    print "times as long as it would normally be."

    dur3.tuplets = [tup1, tup2]
    print "So a tuplet-dotted-quarter's length under both tuplets is",
    print dur3.getQuarterLength(),
    assert common.almostEquals(dur3.getQuarterLength(), 0.7)
    print "quarter notes"
Пример #3
0
def tupletTest():
    note1 = Note()
    note1.duration.type = "quarter"

    ### create a tuplet with 5 dotted eighths in the place of 3 double-dotted eighths
    dur1 = Duration()
    dur1.type = "eighth"
    dur1.dots = 1

    dur2 = Duration()
    dur2.type = "eighth"
    dur2.dots = 2

    tup1 = Tuplet()
    tup1.tupletActual = [5,dur1]
    tup1.tupletNormal = [3,dur2]

    print "For 5 dotted eighths in the place of 3 double-dotted eighths"
    print "Total tuplet length is",
    print tup1.totalTupletLength(),
    assert common.almostEquals(tup1.totalTupletLength(), 2.625)

    print "quarter notes.\nEach note is",
    print tup1.tupletMultiplier(),
    assert common.almostEquals(tup1.tupletMultiplier(), 0.7)
    print "times as long as it would normally be."
    
    ### create a new dotted quarter and apply the tuplet to it
    dur3 = Duration()
    dur3.type = "quarter"
    dur3.dots = 1
    dur3.tuplets = [tup1]
    print "So a tuplet-dotted-quarter's length is",
    print dur3.getQuarterLength(),
    assert common.almostEquals(dur3.getQuarterLength(), 1.05)
    print "quarter notes"

    ### create a tuplet with 3 sixteenths in the place of 2 sixteenths
    tup2 = Tuplet()
    dur4 = Duration()
    dur4.type = "16th"
    tup2.tupletActual = [3, dur4]
    tup2.tupletNormal = [2, dur4]

    print "\nTup2 (3 sixteenths in the place of 2 16ths):\nTotal tuplet length is",
    print tup2.totalTupletLength(),
    assert common.almostEquals(tup2.totalTupletLength(), 0.5)
    print "quarter notes.\nEach note is",
    print tup2.tupletMultiplier(),
    assert common.almostEquals(tup2.tupletMultiplier(), 0.66666666667)

    print "times as long as it would normally be."

    dur3.tuplets = [tup1,tup2]
    print "So a tuplet-dotted-quarter's length under both tuplets is",
    print dur3.getQuarterLength(),
    assert common.almostEquals(dur3.getQuarterLength(), 0.7)
    print "quarter notes"
Пример #4
0
    def componentIndexAtQtrPosition(self, quarterPosition):
        '''returns the index number of the duration component sounding at
        the given quarter position.
        
        e.g. given d1, d2, d3 as 3 quarter notes and
        self.components = [d1, d2, d3]
        then
        self.getComponentIndexAtQtrPosition(1.5) == d2
        self.getComponentIndexAtQtrPosition(2.0) == d3
        self.getComponentIndexAtQtrPosition(2.5) == d3
        '''

        if self.components == []:
            raise DurationException(
                "Need components to run getComponentIndexAtQtrPosition")
        elif quarterPosition > self.quarterLength:
            raise DurationException(
                "quarterPosition is after the end of the duration")
        elif quarterPosition < 0:
            raise DurationException(
                "Okay, now you're just being silly, with negative positions")

        if common.almostEquals(quarterPosition, 0):
            return self.components[0]
        elif common.almostEquals(quarterPosition, self.quarterLength):
            return self.components[-1]

        currentPosition = 0.0

        for i in range(len(self.components)):
            currentPosition += self.components[i].quarterLength
            if currentPosition > quarterPosition and not common.almostEquals(
                    currentPosition, quarterPosition):
                return i

        raise DurationException("Okay, so how did this happen?")
Пример #5
0
def complexTest():
    note1 = ComplexNote()
    d1 = Duration()
    d1.type = "whole"
    d2 = Duration()
    d2.type = "quarter"
    note1.appendDuration(d1)
    note1.appendDuration(d2)
    assert common.almostEquals(note1.duration.quarterLength, 5.0)
    assert note1.duration.componentIndexAtQtrPosition(2) == 0
    assert note1.duration.componentIndexAtQtrPosition(4) == 1
    assert note1.duration.componentIndexAtQtrPosition(4.5) == 1
    note1.duration.sliceComponentAtPosition(1.0)
    print note1.lilyName
    for thisNote in (note1.splitAtDurations()):
        print thisNote.lilyName
Пример #6
0
def complexTest():
    note1 = ComplexNote()
    d1 = Duration()
    d1.type = "whole"
    d2 = Duration()
    d2.type = "quarter"
    note1.appendDuration(d1)
    note1.appendDuration(d2)
    assert common.almostEquals(note1.duration.quarterLength, 5.0)
    assert note1.duration.componentIndexAtQtrPosition(2) == 0    
    assert note1.duration.componentIndexAtQtrPosition(4) == 1    
    assert note1.duration.componentIndexAtQtrPosition(4.5) == 1    
    note1.duration.sliceComponentAtPosition(1.0)
    print note1.lilyName
    for thisNote in (note1.splitAtDurations()):
        print thisNote.lilyName
Пример #7
0
def test():
    #    note1 = Note("c#1")
    #    assert note1.duration.quarterLength == 4
    #    note1.duration.dots = 1
    #    assert note1.duration.quarterLength == 6
    #    note1.duration.type = "eighth"
    #    assert note1.duration.quarterLength == 0.75
    #    assert note1.octave == 4
    #    assert note1.step == "C"
    note2 = Rest()
    assert note2.isRest is True
    note3 = Note()
    note3.name = "B-"
    assert note3.accidental is not None
    assert note3.accidental.name == "flat"
    assert note3.pitchClass == 10

    a5 = Note()
    a5.name = "A"
    a5.octave = 5
    assert common.almostEquals(a5.freq440, 880.0) is True
    assert a5.pitchClass == 9
Пример #8
0
def test():
#    note1 = Note("c#1")
#    assert note1.duration.quarterLength == 4
#    note1.duration.dots = 1
#    assert note1.duration.quarterLength == 6
#    note1.duration.type = "eighth"
#    assert note1.duration.quarterLength == 0.75
#    assert note1.octave == 4
#    assert note1.step == "C"
    note2 = Rest()
    assert note2.isRest is True
    note3 = Note()
    note3.name = "B-"
    assert note3.accidental is not None
    assert note3.accidental.name == "flat"
    assert note3.pitchClass == 10

    a5 = Note()
    a5.name = "A"
    a5.octave = 5
    assert common.almostEquals(a5.freq440, 880.0) is True
    assert a5.pitchClass == 9
Пример #9
0
 def getTypeFromQtrLength(self, qL):
     if common.almostEquals(qL, 64.0): return 'duplex-maxima'
     if common.almostEquals(qL, 32.0): return 'maxima'
     if common.almostEquals(qL, 16.0): return 'longa'
     if common.almostEquals(qL, 8.0): return 'breve'
     if common.almostEquals(qL, 4.0): return 'whole'
     if common.almostEquals(qL, 2.0): return 'half'
     if common.almostEquals(qL, 1.0): return 'quarter'
     if common.almostEquals(qL, 0.5): return 'eighth'
     if common.almostEquals(qL, 0.25): return '16th'
     if common.almostEquals(qL, 0.125): return '32nd'
     if common.almostEquals(qL, 0.0625): return '64th'
     if common.almostEquals(qL, 0.03125): return '128th'
     if common.almostEquals(qL, 0.015625): return '256th'
Пример #10
0
 def getTypeFromQtrLength(self, qL):
     if common.almostEquals(qL, 64.0): return 'duplex-maxima'
     if common.almostEquals(qL, 32.0): return 'maxima'
     if common.almostEquals(qL, 16.0): return 'longa'
     if common.almostEquals(qL, 8.0):  return 'breve'
     if common.almostEquals(qL, 4.0):  return 'whole'
     if common.almostEquals(qL, 2.0):  return 'half'
     if common.almostEquals(qL, 1.0):  return 'quarter'
     if common.almostEquals(qL, 0.5):  return 'eighth'
     if common.almostEquals(qL, 0.25): return '16th'
     if common.almostEquals(qL, 0.125): return '32nd'
     if common.almostEquals(qL, 0.0625): return '64th'
     if common.almostEquals(qL, 0.03125): return '128th'
     if common.almostEquals(qL, 0.015625): return '256th'