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

I cannot Find any Functions to delete MIDI Events #46

Closed
FFjet opened this issue Nov 26, 2017 · 2 comments
Closed

I cannot Find any Functions to delete MIDI Events #46

FFjet opened this issue Nov 26, 2017 · 2 comments

Comments

@FFjet
Copy link

FFjet commented Nov 26, 2017

I want to delete some events in a track!But I don't know how to do it lol.
Please Help! ;D

@john-davies
Copy link

I needed this as well so I've created pull request #52 which gives options for adding or deleting events from a list.

@craigsapp
Copy link
Owner

There was no way of deleting MIDI events(!), but finally I have done something about it...

The example program https://github.com/craigsapp/midifile/blob/f1d049169ddf974c9f5a230d1885e626dea979c3/src-programs/removenote.cpp demonstrates how to use the new deleting system. In the example program, here is the code which goes through the MidiEvents and applies array<uchar>::clear() to the MidiMessages that should be deleted:

// Delete any MIDI message which is a note message on key 63:
int removekey = 63;
for (int i=0; i<midifile[0].getSize(); i++) {
if (!midifile[0][i].isNote()) {
continue;
}
if (midifile[0][i].getP1() == removekey) {
midifile[0][i].clear();
}
}
// calling MidiFile::removeEmpties() is optional, since
// writing the MidiFile will automatically skip over any
// MIDI events with no message content:
midifile.removeEmpties();
cout << midifile;

In the example program I write notes on MIDI keys 60, 61, 62, 63, 64, 65, and then delete the ones on key 63. The output from the program should have the 63 note messages removed:

"MThd"			; MIDI header chunk marker
4'6			; bytes to follow in header chunk
2'0			; file format: Type-0 (single track)
2'1			; number of tracks
2'120			; ticks per quarter note

;;; TRACK 0 ----------------------------------
"MTrk"			; MIDI track chunk marker
4'44			; bytes to follow in track chunk
v1	90 '60 '64	; note-on C4
v1	90 '60 '0	; note-off C4
v1	90 '61 '64	; note-on C#4
v1	90 '61 '0	; note-off C#4
v1	90 '62 '64	; note-on D4
v1	90 '62 '0	; note-off D4
v1	90 '64 '64	; note-on E4
v1	90 '64 '0	; note-off E4
v1	90 '65 '64	; note-on F4
v1	90 '65 '0	; note-off F4
v0	ff 2f v0	; end-of-track

When the MidiFile is written at line 51, any MidiEvents that have no byte content (according to vector<uchar>::empty() will be ignored. So the empty MidiEvents do not have to actually be deleted from the MidiEventList in order for them to be ignored.

As done on line 49, you can optionally remove the empty MidiMessages from the MidiFile/MidiEventList by calling MidiFile::removeEmpties() (or MidiEventList::removeEmpties() to remove empty message in a specific track).

This system is implemented in commit f1d0491

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