Beispiel #1
0
    def getBeatFloatOrFrac(self):
        '''
        Gets the beat number as a float or fraction. Time signature independent
        
        >>> RTB = romanText.rtObjects.RTBeat
        
        Simple ones:
        
        >>> RTB('b1').getBeatFloatOrFrac()
        1.0
        >>> RTB('b2').getBeatFloatOrFrac()
        2.0
        
        etc.
        
        with easy float:
        
        >>> RTB('b1.5').getBeatFloatOrFrac()
        1.5
        >>> RTB('b1.25').getBeatFloatOrFrac()
        1.25
        
        with harder:
        
        >>> RTB('b1.33').getBeatFloatOrFrac()
        Fraction(4, 3)
        
        >>> RTB('b2.66').getBeatFloatOrFrac()
        Fraction(8, 3)
                
        >>> RTB('b1.2').getBeatFloatOrFrac()
        Fraction(6, 5)


        A third digit of .5 adds 1/2 of 1/DENOM of before.  Here DENOM is 3 (in 5/3) so
        we add 1/6 to 5/3 to get 11/6:
        

        >>> RTB('b1.66').getBeatFloatOrFrac()
        Fraction(5, 3)

        >>> RTB('b1.66.5').getBeatFloatOrFrac()
        Fraction(11, 6)


        Similarly .25 adds 1/4 of 1/DENOM... to get 21/12 or 7/4 or 1.75

        >>> RTB('b1.66.25').getBeatFloatOrFrac()
        1.75

        And .75 adds 3/4 of 1/DENOM to get 23/12
        
        >>> RTB('b1.66.75').getBeatFloatOrFrac()
        Fraction(23, 12)


        A weird way of writing 'b1.5'

        >>> RTB('b1.33.5').getBeatFloatOrFrac()
        1.5
        '''
        beatStr = self.src.replace('b', '')
        # there may be more than one decimal in the number, such as
        # 1.66.5, to show halfway through 2/3rd of a beat
        parts = beatStr.split('.')
        mainBeat = int(parts[0])
        if len(parts) > 1: # 1.66
            fracPart = common.addFloatPrecision('.' + parts[1])
        else:
            fracPart = 0.0
            
        if len(parts) > 2: # 1.66.5 
            fracPartDivisor = float('.' + parts[2]) # 0.5
            if isinstance(fracPart, float):
                fracPart = Fraction.from_float(fracPart)
            denom = fracPart.denominator
            fracBeatFrac = common.opFrac(1./(denom/fracPartDivisor))
        else:
            fracBeatFrac = 0.0

        if len(parts) > 3:
            environLocal.printDebug(['got unexpected beat: %s' % self.src])
            raise RTTokenException('cannot handle specification: %s' %  self.src)
            
            
        beat = common.opFrac(mainBeat + fracPart + fracBeatFrac)
        return beat
Beispiel #2
0
    def getBeatFloatOrFrac(self):
        '''
        Gets the beat number as a float or fraction. Time signature independent

        >>> RTB = romanText.rtObjects.RTBeat

        Simple ones:

        >>> RTB('b1').getBeatFloatOrFrac()
        1.0
        >>> RTB('b2').getBeatFloatOrFrac()
        2.0

        etc.

        with easy float:

        >>> RTB('b1.5').getBeatFloatOrFrac()
        1.5
        >>> RTB('b1.25').getBeatFloatOrFrac()
        1.25

        with harder:

        >>> RTB('b1.33').getBeatFloatOrFrac()
        Fraction(4, 3)

        >>> RTB('b2.66').getBeatFloatOrFrac()
        Fraction(8, 3)

        >>> RTB('b1.2').getBeatFloatOrFrac()
        Fraction(6, 5)


        A third digit of .5 adds 1/2 of 1/DENOM of before.  Here DENOM is 3 (in 5/3) so
        we add 1/6 to 5/3 to get 11/6:


        >>> RTB('b1.66').getBeatFloatOrFrac()
        Fraction(5, 3)

        >>> RTB('b1.66.5').getBeatFloatOrFrac()
        Fraction(11, 6)


        Similarly .25 adds 1/4 of 1/DENOM... to get 21/12 or 7/4 or 1.75

        >>> RTB('b1.66.25').getBeatFloatOrFrac()
        1.75

        And .75 adds 3/4 of 1/DENOM to get 23/12

        >>> RTB('b1.66.75').getBeatFloatOrFrac()
        Fraction(23, 12)


        A weird way of writing 'b1.5'

        >>> RTB('b1.33.5').getBeatFloatOrFrac()
        1.5
        '''
        beatStr = self.src.replace('b', '')
        # there may be more than one decimal in the number, such as
        # 1.66.5, to show halfway through 2/3rd of a beat
        parts = beatStr.split('.')
        mainBeat = int(parts[0])
        if len(parts) > 1:  # 1.66
            fracPart = common.addFloatPrecision('.' + parts[1])
        else:
            fracPart = 0.0

        if len(parts) > 2:  # 1.66.5
            fracPartDivisor = float('.' + parts[2])  # 0.5
            if isinstance(fracPart, float):
                fracPart = fractions.Fraction.from_float(fracPart)
            denom = fracPart.denominator
            fracBeatFrac = common.opFrac(1. / (denom / fracPartDivisor))
        else:
            fracBeatFrac = 0.0

        if len(parts) > 3:
            environLocal.printDebug(['got unexpected beat: %s' % self.src])
            raise RTTokenException('cannot handle specification: %s' %
                                   self.src)

        beat = common.opFrac(mainBeat + fracPart + fracBeatFrac)
        return beat