Semyon Kirekov
Semyon Kirekov

Reputation: 1442

Midi instrument index python

I have some midi files. Each midi file has some instruments. I need to know what instruments each midi file consists. I have found some python libs that can parse midi files such as mido. For example, here is the result of parsing one midi file:

I can see how many tracks in this midi file, but I don't know what instruments are playing each track. Here is midi instrument table. Does anybody know the solution?

Upvotes: 1

Views: 488

Answers (1)

Gaslight Deceive Subvert
Gaslight Deceive Subvert

Reputation: 20428

In MIDI instruments are known as programs and each track can contain multiple instruments. Although they may not overlap (unless they use different channels). Here is Python code to display when in a track each program plays. The code assumes that the track is stored in the track variable:

time = 0
for msg in track:
    time += msg.time
    if msg.type == 'program_change':
        program = msg.program
        print(f'Time {time:6}, program {program:3} starts.')

Upvotes: 0

Related Questions