def get_time_in_ms(self): event_list = self.score.tempo_sequence.sequence_list score_len = self.score.length() fine_tempo_sequence = TempoEventSequence() for event in event_list: if isinstance(event, TempoEvent): fine_tempo_sequence.add(TempoEvent(event.object, event.time)) elif isinstance(event, TempoFunctionEvent): t1 = event.time beat_duration = event.beat_duration if event.beat_duration is None else \ ScoreToVstMidiConverter.DEFAULT_BEAT_DURATION next_event = self.score.tempo_sequence.successor(event) t2 = next_event.time if next_event is not None else Position( score_len.duration) while t1 < t2: tempo = int( event.tempo( t1, next_event.time if next_event is not None else Position(score_len))) delta_wnt = (tempo * ScoreToVstMidiConverter.TEMPO_EVENT_DURATION_MS * beat_duration.duration) / \ (60.0 * 1000.0) fine_tempo_sequence.add( TempoEvent(Tempo(tempo, beat_duration), t1)) t1 += delta_wnt conversion = TimeConversion(fine_tempo_sequence, self.score.time_signature_sequence, Position(score_len)) actual_time = conversion.position_to_actual_time( Position(self.score.duration)) return actual_time
def test_time_conversion_simple(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(1, 1)) actual_time = conversion.position_to_actual_time(Position(3, 4)) print(actual_time) self.assertTrue(actual_time == 3000, 'actual time = {0} should be 3000'.format(actual_time)) position = conversion.actual_time_to_position(3000) print(position) self.assertTrue(position, Position(3, 4))
def test_position_to_actual_time(self): 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(5, 4)) ]) conversion = TimeConversion(tempo_line, ts_line, Position(2, 1)) actual_time = conversion.position_to_actual_time(Position(6, 4)) print(actual_time) # 4 quarter notes @ 60 with 1/4 beat = 4000 # 1 quarter note @ 20 with 1/4 beat = 3000 # 2 eighth notes @ 20 (effective 40) with 1/8 beat = 3000 self.assertTrue( actual_time == 10000, 'actual time = {0} should be 10000'.format(actual_time)) position = conversion.actual_time_to_position(10000) print(position) self.assertEquals(position, Position(6, 4))
def real_duration(self): duration = self.duration conversion = TimeConversion(self.tempo_sequence, self.time_signature_sequence, Position(self.duration.duration)) return conversion.position_to_actual_time(Position(duration.duration))
def real_time_duration(self): interval = Interval(0, self.duration) conversion = TimeConversion(self.tempo_sequence, self.time_signature_sequence, Position(self.duration.duration)) return conversion.position_to_actual_time(interval.upper)