Removing objects
Important
Please read Getting objects and Processing objects articles before reading this one.
This article describes ways to remove different objects (like timed events or notes) from MIDI files and track chunks.
RemoveTimedEvents
RemoveTimedEvents methods is what you need to remove MIDI events by the specified conditions. For example, we have such a file:
var midiFile = new MidiFile(
new TrackChunk(
new TextEvent("A") { DeltaTime = 10 },
new NoteOnEvent(),
new NoteOffEvent()),
new TrackChunk(
new TextEvent("B")));
And we want to remove all Text events:
midiFile.RemoveTimedEvents(
e => e.Event.EventType == MidiEventType.Text);
After this instruction executed, the file will be equal to this one:
var midiFile = new MidiFile(
new TrackChunk(
new NoteOnEvent() { DeltaTime = 10 },
new NoteOffEvent()),
new TrackChunk());
So e => e.Event.EventType == MidiEventType.Text
defines what objects to remove. Of course, absolute times of all events will be preserved as you can see. We can also use the overload without predicate to remove all timed events:
midiFile.RemoveTimedEvents();
Please examine the TimedEventsManagingUtilities class to see other RemoveTimedEvents
overloads.
As you've learned from the Processing objects: ProcessTimedEvents article, you can instantiate timed events as custom classes derived from TimedEvent and use those instances within a predicate to select objects. This article won't repeat information from there so please read it.
All RemoveTimedEvents methods return count of removed objects.
RemoveNotes
Use RemoveNotes methods to remove notes. The approach is the same as for timed events removing (see above): you just work with notes instead of timed events.
Please read Processing objects: ProcessNotes article to learn about how to customize objects selection.
RemoveChords
Obviously you can remove chords too – RemoveChords is what you need. It's redundant to describe how to use these methods since usage is the same as for timed events and notes. Also please read Processing objects: ProcessChords article.
RemoveObjects
All methods we saw before remove objects of the same type. So you can remove only either notes or chords or timed events. But there is a way to remove objects of different types at the same time – RemoveObjects. For example, if you want to remove notes that are not a part of some chord:
midiFile.RemoveObjects(
ObjectType.Note | ObjectType.Chord,
obj => obj is Note);
Or we can remove objects based on the type of an object:
midiFile.RemoveObjects(
ObjectType.Note | ObjectType.Chord,
obj => obj is Note note
? note.Velocity > 10
: ((Chord)obj).Velocity > 20);
If you need to remove objects of several types simultaneously, RemoveObjects will be much faster than consecutive calls of methods for specific object types. I.e. this instruction
midiFile.RemoveObjects(
ObjectType.Note | ObjectType.Chord,
/*...*/);
will execute faster than
midiFile.RemoveChords(/*...*/);
midiFile.RemoveNotes(/*...*/);
More than that, some cases can be covered with RemoveObjects only like the example described above – remove notes that are not a part of some chord. Of course you can always use objects managers to remove objects with any desired logic, but that way is much slower.