Switch note-offs between 0x90 w/ 0 velocity and 0x80 styles.
The MIDI protocol allows two types of note-off messages: (1) using the
a command byte starting with the hex digit "8" that always indicates a
note-off message for a given key with a given off-velocity, and (2)
using note-on commands (starting with the hex digit "8"), but setting
the attack velocity to 0.
The following program can convert between these two different styles. The
default behavior is to convert all note-off messages to the "8"
style. If the -0 option is used, the note-offs will be
converted into the "9" style note-off message which use a zero
attack velocity within a note-on message.
#include "MidiFile.h"
#include "Options.h"
#include <iostream>
usingnamespacestd;intmain(intargc,char**argv){Optionsoptions;options.define("0|O|o|z|9|zero=b","Set note-offs to note-on messages with zero velocity.");options.process(argc,argv);boolzeroQ=options.getBoolean("zero");if(options.getArgCount()!=2){cerr<<"Usage: "<<options.getCommand()<<" input.mid output.mid\n";exit(1);}MidiFilemidifile;midifile.read(options.getArg(1));if(!midifile.status()){cerr<<"Error: could not read file"<<options.getArg(1)<<endl;exit(1);}for(inttrack=0;track<midifile.size();track++){for(intevent=0;event<midifile[track].size();event++){if(!midifile[track][event].isNoteOff()){continue;}if(zeroQ){midifile[track][event].setCommandNibble(0x90);midifile[track][event][2]=0;}else{midifile[track][event].setCommandNibble(0x80);}}}midifile.write(options.getArg(2));return0;}