Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"program_change" event on any channel only effects the current track #219

Open
cyclekiller opened this issue Feb 18, 2023 · 5 comments
Open

Comments

@cyclekiller
Copy link

Hello! This repo is very good, but in https://github.com/craffel/pretty-midi/blob/main/pretty_midi/pretty_midi.py#L317, around line 317 ~ 324, in particular line 324:

        for track_idx, track in enumerate(midi_data.tracks):
            # Keep track of last note on location:
            # key = (instrument, note),
            # value = (note-on tick, velocity)
            last_note_on = collections.defaultdict(list)
            # Keep track of which instrument is playing in each channel
            # initialize to program 0 for all channels
            current_instrument = np.zeros(16, dtype=np.int)

The current_instrument array is reset to zero in each new track. This may cause problem in some MIDIs: If one track sets a program_change event in some channel, another track that shares the same channel (i.e. has some notes in the same channel) is not affected by this program_change.
Window's default midi player and MIDIEditor doesn't have this problem. Some DAWs like Cubase also have this problem but doesn't really matter.
This MIDI file is an (extreme) example demonstrating this issue.

@cyclekiller
Copy link
Author

I know how to fix this "program_change" issue, but should be very careful not to break other parts of the code.

@craffel
Copy link
Owner

craffel commented Feb 23, 2023

Hi there, if you can find in the MIDI file spec that this is the intended behavior, a PR would be welcome. However, to the best of my knowledge, a program change for some channel on track A should not change the program for the same channel on track B.

@cyclekiller
Copy link
Author

Maybe you are right. I can't find any useful information about this issue on the internet. I'm trying to design a rule, for example, when a track has no program change events within it, the program of the last track is assigned to this track. Or a related track that shares the same channel. Something that would be approximately right for many of the abnormal cases.

@cyclekiller
Copy link
Author

So regarding to the issue of tracks without programs, here is some discussion I found. Basically, some rule that I just mentioned above is necessary.

@caenopy
Copy link

caenopy commented Aug 8, 2023

Looks like the MIDI spec is silent on how program changes should be handled between tracks. Tracks are merely an organizational feature of MIDI files, and in General MIDI 1.0, devices have no way of knowing which message corresponds to which track. The assumed behavior should probably be that program changes are global. There are many files in LMD that assume this and program data is currently lost when these are parsed with pretty_midi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants