示例#1
0
    def load_track(self, miditrack):
        """
        The main method for converting the track data into sound events

        Loops sequentially through each event and does the following:
        1. Update the current time from the event
        2. If the event is a note on event, then create a new note and add it to the track
        3. If the event is a note off event, then compute its duration
        """
        present_time = 0
        on_notes = {}
        track = InstrumentTrack()
        for event_index in range(0, len(miditrack), 1):
            event = miditrack[event_index]
            present_time += event.tick
            if MidiUtils.is_new_note(event):
                note = Note(start_time=present_time, pitch=event.pitch, volume=event.velocity)
                on_notes[note.pitch] = note
                track.add_note(note)
            if MidiUtils.has_note_ended(event):
                note = on_notes[event.pitch]
                note.duration = present_time - note.start_time
        instrument = MidiUtils.get_instrument(miditrack)
        if instrument is None:
            instrument = instruments.PIANO
        self.transcript.add_track(instrument, track)