You could pretty easily write a Tool to output events to a text file (something like CSV or TSV or other format), and then use one of the CSV-to-MIDI converters to create a MIDI file.
These are some examples found on the web, but I haven't tested them:
https://www.fourmilab.ch/webtools/midicsv/
https://pypi.org/project/py-midicsv/
http://abc.sourceforge.net/abcMIDI/
If the reason you want to generate a MIDI file is to do music notation, you could try LilyPond.
Does anyone know of other text-to-midi file conversion programs?