Beispiel #1
0
 def beat_position(self, position):
     """
     Get the beat position corresponding to given position.
     :param position:
     :return: BeatPosition
     """
     conversion = TimeConversion(self.tempo_sequence,
                                 self.time_signature_sequence,
                                 Position(self.duration.duration))
     return conversion.position_to_bp(Position(position.position))
Beispiel #2
0
    def verify(self, pdi):
        """
        Verify that the beat constraint holds.
        :param pdi: PositionDeltaInfo.
        :return: True/False on meeting constraint.
        """
        conversion = TimeConversion(pdi.tempo_event_sequence,
                                    pdi.ts_event_sequence,
                                    Position(pdi.line_duration()))
        new_position = pdi.correct_position(self.actor.get_absolute_position())
        beat_position = conversion.position_to_bp(new_position)
        if beat_position.beat_fraction > 0:
            return False

        beat = beat_position.beat
        ts = pdi.ts_event_sequence.floor_event(new_position).object
        beat_list = self.beat_ids if self.beat_ids is not None else ts.beats_matching(
            self.beat_type)
        return beat in beat_list
Beispiel #3
0
 def beat_duration(self):
     duration = self.duration
     conversion = TimeConversion(self.tempo_sequence,
                                 self.time_signature_sequence,
                                 Position(self.duration.duration))
     return conversion.position_to_bp(Position(duration.duration))
Beispiel #4
0
    def test_position_to_bp(self):
        tempo_line = EventSequence(TempoEvent(Tempo(60), Position(0)))
        ts_line = EventSequence(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))
        conversion = TimeConversion(tempo_line, ts_line, Position(2, 1))

        bp = conversion.position_to_bp(Position(1, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(0, 2),
                        'bp is {0}, not BP[0, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(1))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 1),
                        'bp is {0}, not BP[1 ,1]'.format(bp))

        tempo_line = EventSequence([
            TempoEvent(Tempo(60), Position(0)),
            TempoEvent(Tempo(20), Position(4, 4))
        ])
        ts_line = EventSequence([
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)),
            TimeSignatureEvent(TimeSignature(2, Duration(1, 8)),
                               Position(6, 4))
        ])
        conversion = TimeConversion(tempo_line, ts_line, Position(2, 1))

        bp = conversion.position_to_bp(Position(5, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 2),
                        'bp is {0}, not BP[1, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(13, 8))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 1),
                        'bp is {0}, not BP[2, 1]'.format(bp))

        tempo_line = EventSequence([
            TempoEvent(Tempo(60), Position(0)),
            TempoEvent(Tempo(20), Position(4, 4))
        ])
        ts_line = EventSequence([
            TimeSignatureEvent(TimeSignature(4, Duration(1, 4)), Position(0)),
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)),
                               Position(5, 2))
        ])
        conversion = TimeConversion(tempo_line, ts_line, Position(4),
                                    Duration(1, 2))

        # pickup
        bp = conversion.position_to_bp(Position(0))
        print(bp)
        self.assertTrue(bp == BeatPosition(0, 2),
                        'bp is {0}, not BP[0, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(1, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(0, 3),
                        'bp is {0}, not BP[0, 3]'.format(bp))

        # measure 1
        bp = conversion.position_to_bp(Position(1, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 0),
                        'bp is {0}, not BP[1, 0]'.format(bp))

        bp = conversion.position_to_bp(Position(3, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 1),
                        'bp is {0}, not BP[1, 1]'.format(bp))

        bp = conversion.position_to_bp(Position(1, 1))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 2),
                        'bp is {0}, not BP[1, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(5, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 3),
                        'bp is {0}, not BP[1, 3]'.format(bp))

        # measure 2
        bp = conversion.position_to_bp(Position(3, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 0),
                        'bp is {0}, not BP[2, 0]'.format(bp))

        bp = conversion.position_to_bp(Position(7, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 1),
                        'bp is {0}, not BP[2, 1]'.format(bp))

        bp = conversion.position_to_bp(Position(2, 1))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 2),
                        'bp is {0}, not BP[2, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(9, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 3),
                        'bp is {0}, not BP[2, 3]'.format(bp))

        # measure 3
        bp = conversion.position_to_bp(Position(5, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(3, 0),
                        'bp is {0}, not TS[3, 0]'.format(bp))

        bp = conversion.position_to_bp(Position(11, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(3, 1),
                        'bp is {0}, not TS[3, 1]'.format(bp))

        bp = conversion.position_to_bp(Position(3, 1))
        print(bp)
        self.assertTrue(bp == BeatPosition(3, 2),
                        'bp is {0}, not TS[3, 2]'.format(bp))

        # measure 4
        bp = conversion.position_to_bp(Position(13, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(4, 0),
                        'bp is {0}, not BP[4, 0]'.format(bp))

        bp = conversion.position_to_bp(Position(7, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(4, 1),
                        'bp is {0}, not BP[4, 1]'.format(bp))

        bp = conversion.position_to_bp(Position(15, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(4, 2),
                        'bp is {0}, not BP[4, 2]'.format(bp))